I’m writing a webapp for chess which, for a given move and a database of games, shows all the moves that follow from that move. Pretty much what this website has already done: http://www.chessgames.com/perl/explorer
The database will have a table of “moves” which have a link to the previous move in the sequence. Finding the moves following a given move (x) can then be done by searching the table for any “move” that has “previousMove” as x. This is simple enough, but I can’t think of an efficient way to add to the moves table from a given game of chess. My algorithm so far is:
prevMove = null
for move in game.moves:
dbMove = getMoveFromDB(move, prevMove)
if not dbMove
dbMove = createDBMove()
dbMove.prevMove = prevMove
prevMove = dbMove
For an average of 40 moves per chess game, this means that there would be 40+ database reads to import a game. Is this inevitable or is there a better way to import the moves from a game into the database?
6