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_manual_game(): """ Manual game test """ # Initial, empty game state state = State(tileset=NewTileSet, manual_wordcheck=True, drawtiles=True) print("Manual game") print("After initial draw, bag contains {0} tiles".format( state.bag().num_tiles())) print("Bag contents are:\n{0}".format(state.bag().contents())) print("Rack 0 is {0}".format(state.rack(0))) print("Rack 1 is {0}".format(state.rack(1))) # Set player names for ix in range(2): state.set_player_name(ix, "Player " + ("A", "B")[ix]) # print(state.__str__()) # This works in Python 2 and 3 state.player_rack().set_tiles("stuðinn") test_move(state, "H4 stuði") state.player_rack().set_tiles("dettsfj") test_move(state, "5E detts") test_exchange(state, 3) state.player_rack().set_tiles("dýsturi") test_move(state, "I3 dýs") state.player_rack().set_tiles("?xalmen") # The question mark indicates a blank tile for the subsequent cover test_move(state, "6E ?óx") state.player_rack().set_tiles("eiðarps") test_move(state, "9F eipar") test_challenge(state) test_response(state) state.player_rack().set_tiles("sóbetis") test_move(state, "J3 ós") test_move(state, "9F eiðar") test_challenge(state) test_response(state) # Tally the tiles left and calculate the final score state.finalize_score() p0, p1 = state.scores() print( "Manual game over, final score {3} {0} : {4} {1} after {2} moves" .format( p0, p1, state.num_moves(), state.player_name(0), state.player_name(1) ) )
def test_manual_game(): """ Manual game test """ # Initial, empty game state state = State(tileset = NewTileSet, manual_wordcheck = True, drawtiles = True) print(u"Manual game") 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, "Player " + ("A", "B")[ix]) # print(state.__str__()) # This works in Python 2 and 3 state.player_rack().set_tiles(u"stuðinn") test_move(state, u"H4 stuði") state.player_rack().set_tiles(u"dettsfj") test_move(state, u"5E detts") test_exchange(state, 3) state.player_rack().set_tiles(u"dýsturi") test_move(state, u"I3 dýs") state.player_rack().set_tiles(u"?xalmen") test_move(state, u"6E ?óx") # The question mark indicates a blank tile for the subsequent cover state.player_rack().set_tiles(u"eiðarps") test_move(state, u"9F eipar") test_challenge(state) test_response(state) state.player_rack().set_tiles(u"sóbetis") test_move(state, u"J3 ós") test_move(state, u"9F eiðar") test_challenge(state) test_response(state) # Tally the tiles left and calculate the final score state.finalize_score() p0, p1 = state.scores() print(u"Manual game over, final score {3} {0} : {4} {1} after {2} moves".format(p0, p1, state.num_moves(), state.player_name(0), state.player_name(1)))
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_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(drawtiles=True) # 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 # test_move(state, u"H4 stuði") # test_move(state, u"5E detts") # test_exchange(state, 3) # test_move(state, u"I3 dýs") # test_move(state, u"6E ?óx") # The question mark indicates a blank tile for the subsequent cover # state.player_rack().set_tiles(u"ðhknnmn") # 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()