def test_move(state, movestring): """ Test placing a simple tile move """ coord, word = movestring.split(u' ') rowid = Board.ROWIDS row, col = 0, 0 xd, yd = 0, 0 horiz = True if coord[0] in rowid: row = rowid.index(coord[0]) col = int(coord[1:]) - 1 yd = 1 else: row = rowid.index(coord[-1]) col = int(coord[0:-1]) - 1 xd = 1 horiz = False move = Move(word, row, col, horiz) next_is_blank = False for c in word: if c == u'?': next_is_blank = True continue if not state.board().is_covered(row, col): move.add_cover(row, col, u'?' if next_is_blank else c, c) next_is_blank = False row += xd col += yd legal = state.check_legality(move) if legal != Error.LEGAL: print(u"Play is not legal, code {0}".format(Error.errortext(legal))) return False print(u"Play {0} is legal and scores {1} points".format(move, state.score(move))) state.apply_move(move) print(state.__str__()) return True
def test_game(players, silent): """ Go through a whole game by pitting two AutoPlayers against each other """ # The players parameter is a list of tuples: (playername, constructorfunc) # where constructorfunc accepts a State parameter and returns a freshly # created AutoPlayer (or subclass thereof) that will generate moves # on behalf of the player. # Initial, empty game state state = State(tileset = NewTileSet, drawtiles = True) print(u"After initial draw, bag contains {0} tiles".format(state.bag().num_tiles())) print(u"Bag contents are:\n{0}".format(state.bag().contents())) print(u"Rack 0 is {0}".format(state.rack(0))) print(u"Rack 1 is {0}".format(state.rack(1))) # Set player names for ix in range(2): state.set_player_name(ix, players[ix][0]) if not silent: print(state.__str__()) # This works in Python 2 and 3 # Generate a sequence of moves, switching player sides automatically t0 = time.time() while not state.is_game_over(): # Call the appropriate player creation function apl = players[state.player_to_move()][1](state) g0 = time.time() move = apl.generate_move() g1 = time.time() legal = state.check_legality(move) if legal != Error.LEGAL: # Oops: the autoplayer generated an illegal move print(u"Play is not legal, code {0}".format(Error.errortext(legal))) return if not silent: print(u"Play {0} scores {1} points ({2:.2f} seconds)".format(move, state.score(move), g1 - g0)) # Apply the move to the state and switch players state.apply_move(move) if not silent: print(state.__str__()) # Tally the tiles left and calculate the final score state.finalize_score() p0, p1 = state.scores() t1 = time.time() if not silent: print(u"Game over, final score {4} {0} : {5} {1} after {2} moves ({3:.2f} seconds)".format(p0, p1, state.num_moves(), t1 - t0, state.player_name(0), state.player_name(1))) return state.scores()
def test_exchange(state, numtiles): """ Test exchange move """ exch = state.player_rack().contents()[0:numtiles] move = ExchangeMove(exch) legal = state.check_legality(move) if legal != Error.LEGAL: print(u"Play is not legal, code {0}".format(Error.errortext(legal))) return False print(u"Play {0} is legal and scores {1} points".format(move, state.score(move))) state.apply_move(move) print(state.__str__()) return True
def test_challenge(state): """ Test challenge move """ move = ChallengeMove() legal = state.check_legality(move) msg = "" if isinstance(legal, tuple): legal, msg = legal if legal != Error.LEGAL: print(u"Play is not legal, code {0} {1}".format(Error.errortext(legal), msg)) return False print(u"Play {0} is legal and scores {1} points".format(move, state.score(move))) state.apply_move(move) print(state.__str__()) return True
def test_exchange(state, numtiles): """ Test exchange move """ exch = state.player_rack().contents()[0:numtiles] move = ExchangeMove(exch) legal = state.check_legality(move) msg = "" if isinstance(legal, tuple): legal, msg = legal if legal != Error.LEGAL: print(u"Play is not legal, code {0} {1}".format(Error.errortext(legal), msg)) return False print(u"Play {0} is legal and scores {1} points".format(move, state.score(move))) state.apply_move(move) print(state.__str__()) return True
def test_challenge(state): """ Test challenge move """ move = ChallengeMove() legal = state.check_legality(move) msg = "" if isinstance(legal, tuple): legal, msg = legal if legal != Error.LEGAL: print(u"Play is not legal, code {0} {1}".format( Error.errortext(legal), msg)) return False print(u"Play {0} is legal and scores {1} points".format( move, state.score(move))) state.apply_move(move) print(state.__str__()) return True
def test_move(state, movestring): """ Test placing a simple tile move """ coord, word = movestring.split(u' ') rowid = Board.ROWIDS xd, yd = 0, 0 horiz = True if coord[0] in rowid: row = rowid.index(coord[0]) col = int(coord[1:]) - 1 yd = 1 else: row = rowid.index(coord[-1]) col = int(coord[0:-1]) - 1 xd = 1 horiz = False move = Move(word, row, col, horiz) next_is_blank = False for c in word: if c == u'?': next_is_blank = True continue if not state.board().is_covered(row, col): move.add_cover(row, col, u'?' if next_is_blank else c, c) next_is_blank = False row += xd col += yd legal = state.check_legality(move) msg = "" if isinstance(legal, tuple): legal, msg = legal if legal != Error.LEGAL: print(u"Play is not legal, code {0} {1}".format( Error.errortext(legal), msg)) return False print(u"Play {0} is legal and scores {1} points".format( move, state.score(move))) state.apply_move(move) print(state.__str__()) return True
def test_game(players, silent): """ Go through a whole game by pitting two AutoPlayers against each other """ # The players parameter is a list of tuples: (playername, constructorfunc) # where constructorfunc accepts a State parameter and returns a freshly # created AutoPlayer (or subclass thereof) that will generate moves # on behalf of the player. # Initial, empty game state state = State(tileset=NewTileSet, drawtiles=True) print(u"After initial draw, bag contains {0} tiles".format( state.bag().num_tiles())) print(u"Bag contents are:\n{0}".format(state.bag().contents())) print(u"Rack 0 is {0}".format(state.rack(0))) print(u"Rack 1 is {0}".format(state.rack(1))) # Set player names for ix in range(2): state.set_player_name(ix, players[ix][0]) if not silent: print(state.__str__()) # This works in Python 2 and 3 # Generate a sequence of moves, switching player sides automatically t0 = time.time() while not state.is_game_over(): # Call the appropriate player creation function apl = players[state.player_to_move()][1](state) g0 = time.time() move = apl.generate_move() g1 = time.time() legal = state.check_legality(move) if legal != Error.LEGAL: # Oops: the autoplayer generated an illegal move print(u"Play is not legal, code {0}".format( Error.errortext(legal))) return if not silent: print(u"Play {0} scores {1} points ({2:.2f} seconds)".format( move, state.score(move), g1 - g0)) # Apply the move to the state and switch players state.apply_move(move) if not silent: print(state.__str__()) # Tally the tiles left and calculate the final score state.finalize_score() p0, p1 = state.scores() t1 = time.time() if not silent: print( u"Game over, final score {4} {0} : {5} {1} after {2} moves ({3:.2f} seconds)" .format(p0, p1, state.num_moves(), t1 - t0, state.player_name(0), state.player_name(1))) return state.scores()