def test_player_2_crosses(self): game = Kalah(6, 4) game.play('e') game.play('E') game.play('a') game.play('A') self.assertEqual(game.status(), (5, 1, 6, 6, 5, 0, 1, 5, 1, 6, 6, 5, 0, 1))
def test_capture_player_2(self): game = Kalah(6, 4) game.set_status( {'f': 4, 'e': 4, 'd': 4, 'c': 8, 'b': 8, 'a': 8, 'bank_player_one': 1, 'F': 0, 'E': 0, 'D': 0, 'C': 0, 'B': 0, 'A': 9, 'bank_player_tow': 2}) game.turn = [0, 1] game.play('A') self.assertEqual(game.status(), (5, 5, 5, 9, 0, 9, 1, 1, 0, 0, 0, 0, 0, 13))
def test_non_capture(self): game = Kalah(6, 4) game.turn = [0, 1] game.set_status( {'f': 4, 'e': 4, 'd': 4, 'c': 8, 'b': 0, 'a': 8, 'bank_player_one': 1, 'F': 1, 'E': 0, 'D': 0, 'C': 0, 'B': 0, 'A': 5, 'bank_player_tow': 13}) game.play('F') self.assertEqual(game.status(), (4, 4, 4, 8, 0, 8, 1, 0, 1, 0, 0, 0, 5, 13))
def test_crossing_other_bank(self): game = Kalah(6, 4) game.play('e') game.play('E') game.play('d') game.play('D') game.play('c') game.play('C') game.play('b') game.play('B') game.play('a') game.play('A') self.assertEqual(game.status(), (9, 4, 3, 2, 2, 1, 4, 9, 4, 3, 2, 1, 0, 4))
def test_capture_player_1(self): game = Kalah(6, 4) game.play('f') game.play('F') game.play('b') game.play('F') game.play('c') game.play('F') game.play('d') game.play('F') game.play('b') game.play('a') self.assertEqual(game.status(), (0, 5, 0, 1, 0, 0, 11, 1, 11, 7, 6, 6, 0, 0))
def test_end_tie(self): game = Kalah(6, 4) game.set_status( {'f': 2, 'e': 2, 'd': 0, 'c': 0, 'b': 8, 'a': 8, 'bank_player_one': 2, 'F': 0, 'E': 0, 'D': 0, 'C': 2, 'B': 0, 'A': 0, 'bank_player_tow': 24}) self.assertEqual(game.play('f'), 'tie')
def test_end_game_player_2_win(self): game = Kalah(6, 4) game.turn = [0, 1] game.set_status( {'f': 0, 'e': 0, 'd': 0, 'c': 2, 'b': 0, 'a': 0, 'bank_player_one': 10, 'F': 2, 'E': 3, 'D': 0, 'C': 0, 'B': 8, 'A': 8, 'bank_player_tow': 15}) self.assertEqual(game.play('F'), 'Player 2 wins')
def simulate_game(holes, seeds, steps): result = [] game = Kalah(holes, seeds) for step in steps: result.append((game.play(step), game.status())) return result
def render_game(holes, seeds, steps): game = Kalah(holes, seeds) for i in range(0, len(steps)): if steps[i] > holes - 1: steps[i] -= holes print(game.play(steps[i])) print(game)
def simulate_game(holes, seeds, steps): game = Kalah(holes, seeds) for i in range(0, len(steps)): if steps[i] > holes - 1: steps[i] -= holes msg = game.play(steps[i]) status = game.status() l = {msg: status} return l
class KalahTestCase(unittest.TestCase): def setUp(self): self.game = Kalah(6, 4) def test_initial_status(self): self.assertEqual(self.game.status(), (4, 4, 4, 4, 4, 4, 0, 4, 4, 4, 4, 4, 4, 0)) def test_illegal_hole(self): self.assertRaises(IndexError, self.game.play, -1) self.assertRaises(IndexError, self.game.play, 15) def test_simple_move(self): self.game.play(0) self.assertEqual(self.game.status(), (0, 5, 5, 5, 5, 4, 0, 4, 4, 4, 4, 4, 4, 0)) def test_crossing_move(self): self.game.play(4) self.assertEqual(self.game.status(), (4, 4, 4, 4, 0, 5, 1, 5, 5, 4, 4, 4, 4, 0)) def test_two_simple_moves(self): self.game.play(0) self.assertEqual(self.game.status(), (0, 5, 5, 5, 5, 4, 0, 4, 4, 4, 4, 4, 4, 0)) self.game.play(8) self.assertEqual(self.game.status(), (0, 5, 5, 5, 5, 4, 0, 4, 0, 5, 5, 5, 5, 0)) def test_player_two_crosses(self): self.game.play(0) self.assertEqual(self.game.status(), (0, 5, 5, 5, 5, 4, 0, 4, 4, 4, 4, 4, 4, 0)) self.game.play(8) self.assertEqual(self.game.status(), (0, 5, 5, 5, 5, 4, 0, 4, 0, 5, 5, 5, 5, 0)) self.game.play(2) self.assertEqual(self.game.status(), (0, 5, 0, 6, 6, 5, 1, 5, 0, 5, 5, 5, 5, 0)) self.game.play(10) self.assertEqual(self.game.status(), (1, 6, 0, 6, 6, 5, 1, 5, 0, 5, 0, 6, 6, 1)) def test_crossing_other_bank(self): self.game.set_status([1, 6, 0, 6, 2, 9, 1, 5, 0, 5, 0, 6, 6, 1]) self.game.play(5) self.assertEqual(self.game.status(), (2, 7, 0, 6, 2, 0, 2, 6, 1, 6, 1, 7, 7, 1)) def test_empty_hole(self): self.game.set_status([2, 7, 0, 6, 2, 0, 2, 6, 1, 6, 1, 7, 7, 1]) self.assertRaises(ValueError, self.game.play, 2) def test_bonus_move_player_one(self): self.assertEqual(self.game.play(2), "Player 1 plays next") self.assertEqual(self.game.status(), (4, 4, 0, 5, 5, 5, 1, 4, 4, 4, 4, 4, 4, 0)) def test_bonus_move_player_two(self): self.game.set_player(1) self.assertEqual(self.game.play(9), "Player 2 plays next") def test_capture_player_one(self): self.game.set_status([2, 7, 0, 6, 2, 0, 2, 6, 1, 6, 1, 7, 7, 1]) self.game.play(0) self.assertEqual(self.game.status(), (0, 8, 0, 6, 2, 0, 4, 6, 1, 6, 0, 7, 7, 1)) def test_capture_player_two(self): self.game.set_player(1) self.game.set_status([0, 7, 1, 6, 2, 0, 4, 5, 2, 6, 0, 7, 7, 1]) self.game.play(8) self.assertEqual(self.game.status(), (0, 7, 0, 6, 2, 0, 4, 5, 0, 7, 0, 7, 7, 3)) def test_non_capture(self): self.game.set_player(1) self.game.set_status([0, 8, 0, 6, 2, 0, 4, 5, 2, 6, 0, 7, 7, 1]) self.game.play(8) self.assertEqual(self.game.status(), (0, 8, 0, 6, 2, 0, 4, 5, 0, 7, 1, 7, 7, 1)) def test_end_game(self): self.game.set_status([0, 0, 0, 0, 0, 0, 25, 4, 2, 6, 0, 7, 3, 1]) self.assertEqual(self.game.play(5), "Player 1 wins.") self.game.set_status([2, 2, 6, 0, 7, 3, 3, 0, 0, 0, 0, 0, 0, 25]) self.game.set_player(1) self.assertEqual(self.game.play(8), "Player 2 wins.") self.game.set_status([0, 0, 0, 0, 0, 0, 24, 3, 2, 6, 0, 7, 4, 2]) self.assertEqual(self.game.play(5), "Tie") def test_4_holes_4_seeds(self): print(self.game.render()) self.game = Kalah(4, 4) self.game.set_status([0, 0, 0, 0, 17, 4, 2, 6, 2, 1]) self.assertEqual(self.game.play(2), "Player 1 wins.") self.game.set_status([1, 2, 6, 2, 4, 0, 0, 0, 0, 17]) self.game.set_player(1) self.assertEqual(self.game.play(8), "Player 2 wins.") self.game.set_status([0, 0, 0, 0, 16, 3, 2, 6, 3, 2]) self.assertEqual(self.game.play(2), "Tie") def test_rep(self): assert repr(Kalah(6, 4)) == "Kalah(4, 6, status=(4, 4, 4, 4, 4, 4, 0, 4, 4, 4, 4, 4, 4, 0), player=0)" def test_render(self): #self.game = Kalah(6, 6) x = """ P L A Y E R 2 ____________________________________________________________ |★★★★ ____ ____ ____ ____ ____ ____ ★★★★| |★ ★ [_4__] [_4__] [_4__] [_4__] [_4__] [_4__] ★ ★| |★ 0 ★ ____ ____ ____ ____ ____ ____ ★ 0 ★| |★★★★ [_4__] [_4__] [_4__] [_4__] [_4__] [_4__] ★★★★| ____________________________________________________________ P L A Y E R 1""" self.assertEqual(self.game.render(), x)
def test_crossing_move(self): game = Kalah(6, 4) game.play('a') self.assertEqual(game.status(), (4, 4, 4, 4, 4, 0, 1, 5, 5, 5, 4, 4, 4, 0))
def test_bonus_move_player_2(self): game = Kalah(6, 4) game.play('f') self.assertEqual(game.play('D'), 'Player 2 plays next')
class KalahTestCase(unittest.TestCase): def setUp(self): self.game = Kalah(6, 4) self.game1 = Kalah(5, 5) self.game2 = Kalah(4, 6) def tearDown(self): pass def test_init(self): self.assertEqual(self.game.kalah_board, [4, 4, 4, 4, 4, 4, 0, 4, 4, 4, 4, 4, 4, 0]) self.assertEqual(self.game1.kalah_board, [5, 5, 5, 5, 5, 0, 5, 5, 5, 5, 5, 0]) def test_illegal_hole(self): self.assertEqual(self.game.kalah_board, [4, 4, 4, 4, 4, 4, 0, 4, 4, 4, 4, 4, 4, 0]) self.game.play(6) self.assertEqual(self.game.kalah_board, [4, 4, 4, 4, 4, 4, 0, 4, 4, 4, 4, 4, 4, 0]) self.assertEqual(self.game1.kalah_board, [5, 5, 5, 5, 5, 0, 5, 5, 5, 5, 5, 0]) self.game.play(-5) self.assertEqual(self.game1.kalah_board, [5, 5, 5, 5, 5, 0, 5, 5, 5, 5, 5, 0]) def test_simple_move(self): self.assertEqual(self.game.kalah_board, [4, 4, 4, 4, 4, 4, 0, 4, 4, 4, 4, 4, 4, 0]) self.game.play(0) self.assertEqual(self.game.kalah_board, [0, 5, 5, 5, 5, 4, 0, 4, 4, 4, 4, 4, 4, 0]) def test_cross_move(self): self.assertEqual(self.game.player_turn, True) self.assertEqual(self.game.kalah_board, [4, 4, 4, 4, 4, 4, 0, 4, 4, 4, 4, 4, 4, 0]) self.game.play(2) self.assertEqual(self.game.kalah_board, [4, 4, 0, 5, 5, 5, 1, 4, 4, 4, 4, 4, 4, 0]) self.assertEqual(self.game.player_turn, True) self.game.play(5) self.assertEqual(self.game.kalah_board, [4, 4, 0, 5, 5, 0, 2, 5, 5, 5, 5, 4, 4, 0]) def test_two_simple_moves(self): self.assertEqual(self.game.player_turn, True) self.assertEqual(self.game.kalah_board, [4, 4, 4, 4, 4, 4, 0, 4, 4, 4, 4, 4, 4, 0]) self.game.play(1) self.assertEqual(self.game.kalah_board, [4, 0, 5, 5, 5, 5, 0, 4, 4, 4, 4, 4, 4, 0]) self.assertEqual(self.game.player_turn, False) self.game.play(1) self.assertEqual(self.game.kalah_board, [4, 0, 5, 5, 5, 5, 0, 4, 0, 5, 5, 5, 5, 0]) def test_player_2_crosses(self): self.test_two_simple_moves() self.assertEqual(self.game.player_turn, True) self.assertEqual(self.game.kalah_board, [4, 0, 5, 5, 5, 5, 0, 4, 0, 5, 5, 5, 5, 0]) self.game.play(2) self.assertEqual(self.game.kalah_board, [4, 0, 0, 6, 6, 6, 1, 5, 0, 5, 5, 5, 5, 0]) self.assertEqual(self.game.player_turn, False) self.game.play(2) self.assertEqual(self.game.kalah_board, [5, 0, 0, 6, 6, 6, 1, 5, 0, 0, 6, 6, 6, 1]) self.assertEqual(self.game.player_turn, True) def test_cross_other_bank(self): self.assertEqual(self.game2.player_turn, True) self.assertEqual(self.game2.kalah_board, [6, 6, 6, 6, 0, 6, 6, 6, 6, 0]) self.game2.play(3) self.assertEqual(self.game2.kalah_board, [7, 6, 6, 0, 1, 7, 7, 7, 7, 0]) self.assertEqual(self.game2.player_turn, False) def test_empty_hole(self): self.assertEqual(self.game.player_turn, True) self.assertEqual(self.game.kalah_board, [4, 4, 4, 4, 4, 4, 0, 4, 4, 4, 4, 4, 4, 0]) self.game.play(2) self.assertEqual(self.game.kalah_board, [4, 4, 0, 5, 5, 5, 1, 4, 4, 4, 4, 4, 4, 0]) self.assertEqual(self.game.player_turn, True) self.game.play(2) self.assertEqual(self.game.kalah_board, [4, 4, 0, 5, 5, 5, 1, 4, 4, 4, 4, 4, 4, 0]) self.assertEqual(self.game.player_turn, True) def test_bonus_move_player_1(self): self.assertEqual(self.game.player_turn, True) self.assertEqual(self.game.kalah_board, [4, 4, 4, 4, 4, 4, 0, 4, 4, 4, 4, 4, 4, 0]) self.game.play(2) self.assertEqual(self.game.kalah_board, [4, 4, 0, 5, 5, 5, 1, 4, 4, 4, 4, 4, 4, 0]) self.assertEqual(self.game.player_turn, True) def test_bonus_move_player_2(self): self.assertEqual(self.game.player_turn, True) self.assertEqual(self.game.kalah_board, [4, 4, 4, 4, 4, 4, 0, 4, 4, 4, 4, 4, 4, 0]) self.game.play(0) self.assertEqual(self.game.kalah_board, [0, 5, 5, 5, 5, 4, 0, 4, 4, 4, 4, 4, 4, 0]) self.assertEqual(self.game.player_turn, False) self.game.play(2) self.assertEqual(self.game.kalah_board, [0, 5, 5, 5, 5, 4, 0, 4, 4, 0, 5, 5, 5, 1]) self.assertEqual(self.game.player_turn, False) def test_capture_player_1(self): self.game.kalah_board = [ 1, 0, 3, 4, 5, 6, 7, 1, 2, 3, 4, 5, 6, 7, ] self.assertEqual(self.game.player_turn, True) self.assertEqual(self.game.kalah_board, [ 1, 0, 3, 4, 5, 6, 7, 1, 2, 3, 4, 5, 6, 7, ]) self.game.play(0) self.assertEqual(self.game.kalah_board, [ 0, 0, 3, 4, 5, 6, 10, 1, 0, 3, 4, 5, 6, 7, ]) self.assertEqual(self.game.player_turn, True) def test_illegal_capture(self): self.assertEqual(self.game.player_turn, True) self.game.kalah_board = [ 1, 2, 3, 1, 5, 3, 7, 0, 0, 1, 0, 5, 6, 24, ] self.game.play(5) self.game.kalah_board = [ 1, 2, 3, 1, 5, 0, 8, 1, 1, 1, 0, 5, 6, 24, ] self.assertEqual(self.game.player_turn, False) def test_no_capture_player(self): self.game.kalah_board = [ 1, 0, 3, 4, 5, 6, 7, 1, 0, 3, 4, 5, 6, 7, ] self.assertEqual(self.game.player_turn, True) self.assertEqual(self.game.kalah_board, [ 1, 0, 3, 4, 5, 6, 7, 1, 0, 3, 4, 5, 6, 7, ]) self.game.play(0) self.assertEqual(self.game.kalah_board, [ 0, 1, 3, 4, 5, 6, 7, 1, 0, 3, 4, 5, 6, 7, ]) self.assertEqual(self.game.player_turn, False) def test_game_ends_and_player_1_capture(self): self.assertEqual(self.game.player_turn, True) self.game.kalah_board = [ 0, 0, 1, 0, 0, 0, 21, 1, 2, 3, 1, 5, 6, 8, ] self.assertEqual(self.game.new_game_is_needed, False) self.game.play(2) self.assertEqual(self.game.kalah_board, [ 0, 0, 0, 0, 0, 0, 23, 0, 0, 0, 0, 0, 0, 25, ]) self.assertEqual(self.game.player_2, 1) self.assertEqual(self.game.player_1, 0) self.assertEqual(self.game.ties, 0) self.assertEqual(self.game.new_game_is_needed, True) def test_game_ends_and_player_2_capture(self): self.assertEqual(self.game.player_turn, True) self.game.kalah_board = [ 1, 2, 3, 1, 5, 6, 8, 0, 0, 1, 0, 0, 0, 21, ] self.assertEqual(self.game.new_game_is_needed, False) self.game.player_turn = not self.game.player_turn self.game.play(2) self.assertEqual(self.game.kalah_board, [ 0, 0, 0, 0, 0, 0, 25, 0, 0, 0, 0, 0, 0, 23, ]) self.assertEqual(self.game.player_2, 0) self.assertEqual(self.game.player_1, 1) self.assertEqual(self.game.ties, 0) self.assertEqual(self.game.new_game_is_needed, True) def test_game_ends_and_tie(self): self.assertEqual(self.game.player_turn, True) self.game.kalah_board = [ 0, 1, 0, 0, 0, 0, 22, 0, 0, 1, 0, 0, 0, 24, ] self.assertEqual(self.game.new_game_is_needed, False) self.game.play(1) self.game.kalah_board = [ 0, 0, 0, 0, 0, 0, 24, 0, 0, 0, 0, 0, 0, 24, ] self.assertEqual(self.game.player_2, 0) self.assertEqual(self.game.player_1, 0) self.assertEqual(self.game.ties, 1) self.assertEqual(self.game.new_game_is_needed, True) def test_game_ends_player_1_capture_more_than_half(self): self.assertEqual(self.game.player_turn, True) self.game.kalah_board = [ 0, 0, 1, 0, 5, 6, 24, 1, 2, 3, 1, 5, 6, 7, ] self.assertEqual(self.game.new_game_is_needed, False) self.game.play(2) self.game.kalah_board = [ 0, 0, 0, 0, 5, 6, 26, 1, 2, 3, 0, 5, 6, 7, ] self.assertEqual(self.game.player_2, 0) self.assertEqual(self.game.player_1, 1) self.assertEqual(self.game.ties, 0) self.assertEqual(self.game.new_game_is_needed, True) def test_game_ends_player_2_capture_more_than_half(self): self.assertEqual(self.game.player_turn, True) self.game.kalah_board = [ 1, 2, 3, 1, 5, 6, 7, 0, 0, 1, 0, 5, 6, 24, ] self.game.player_turn = not self.game.player_turn self.assertEqual(self.game.player_turn, False) self.game.play(2) self.game.kalah_board = [ 1, 2, 3, 0, 5, 6, 7, 0, 0, 0, 0, 5, 6, 26, ] self.assertEqual(self.game.player_2, 1) self.assertEqual(self.game.player_1, 0) self.assertEqual(self.game.ties, 0) self.assertEqual(self.game.new_game_is_needed, True) def test_repr(self): self.assertEqual( repr(self.game), "Kalah(4, 6, status=(4, 4, 4, 4, 4, 4, 0, 4, 4, 4, 4, 4, 4, 0), player = 1)" ) self.game.player_turn = not self.game.player_turn self.assertEqual( repr(self.game), "Kalah(4, 6, status=(4, 4, 4, 4, 4, 4, 0, 4, 4, 4, 4, 4, 4, 0), player = 2)" ) def test_str(self): self.assertEqual( self.game.__str__(), "Kalah[4, 4, 4, 4, 4, 4, 0, 4, 4, 4, 4, 4, 4, 0], player = 1") self.game.player_turn = not self.game.player_turn self.assertEqual( self.game.__str__(), "Kalah[4, 4, 4, 4, 4, 4, 0, 4, 4, 4, 4, 4, 4, 0], player = 2")
def test_bonus_move_player_1(self): game = Kalah(6, 4) self.assertEqual(game.play('d'), 'Player 1 plays next')
def test_empty_hole(self): game = Kalah(6, 4) game.play('e') game.play('A') game.play('f') self.assertRaises(ValueError, game.play, 'A')
class KalahTestCase(unittest.TestCase): def setUp(self): self.game = Kalah(6, 4) def test_init_status(self): assert self.game.status() == (4, 4, 4, 4, 4, 4, 0, 4, 4, 4, 4, 4, 4, 0) def test_init_play(self): assert self.game.play(1) == "Player 2 plays next" assert self.game.play(1) == "Player 1 plays next" self.assertRaises(ValueError, self.game.play, -2) self.assertRaises(ValueError, self.game.play, 7) self.assertRaises(ValueError, self.game.play, self.game.holes) def test_init_done(self): assert self.game.game_over == False def test_init_score(self): assert self.game.score() == (0, 0) def test_simple_move(self): self.game.play(2) assert self.game.status() == (4, 4, 0, 5, 5, 5, 1, 4, 4, 4, 4, 4, 4, 0) def test_few_moves(self): self.game.play(2) assert self.game.status() == (4, 4, 0, 5, 5, 5, 1, 4, 4, 4, 4, 4, 4, 0) self.game.play(3) assert self.game.status() == (4, 4, 0, 0, 6, 6, 2, 5, 5, 4, 4, 4, 4, 0) self.game.play(4) assert self.game.status() == (5, 5, 0, 0, 6, 6, 2, 5, 5, 4, 4, 0, 5, 1) self.game.play(0) assert self.game.status() == (0, 6, 1, 1, 7, 7, 2, 5, 5, 4, 4, 0, 5, 1) self.assertRaises(ValueError, self.game.play, 4) self.game.play(0) assert self.game.status() == (0, 6, 1, 1, 7, 7, 2, 0, 6, 5, 5, 1, 6, 1) def test_captures(self): self.game.set_board([0, 0, 4, 4, 4, 9, 5, 5, 5, 4, 4, 4]) self.game.play(5) assert self.game.status() == (1, 0, 4, 4, 4, 0, 7, 6, 6, 6, 5, 0, 5, 0) self.game.set_board([4, 4, 4, 4, 4, 4, 0, 4, 4, 4, 4, 8]) self.game.play(5) assert self.game.status() == (5, 5, 5, 5, 5, 0, 7, 0, 4, 4, 4, 4, 0, 7) self.game.set_board([1, 0, 4, 5, 4, 5, 7, 7, 6, 6, 0, 3]) self.game.set_bank([0, 0]) self.game.play(0) assert self.game.status() == (0, 1, 4, 5, 4, 5, 0, 7, 7, 6, 6, 0, 3, 0) def test_f_play_win(self): self.game.set_board([0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1]) self.game.set_bank([5, 1]) assert self.game.play(5) == "Player 1 wins" def test_s_play_win(self): self.game.set_board([0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]) self.game.set_bank([0, 1]) self.game.play(1) assert self.game.play(5) == "Player 2 wins" def test_tie(self): self.game.set_board([0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]) self.game.set_bank([0, 0]) self.game.play(1) assert self.game.play(5) == "Tie" def test_repr(self): assert repr( Kalah(6, 4) ) == "Kalah(4, 6, status=(4, 4, 4, 4, 4, 4, 0, 4, 4, 4, 4, 4, 4, 0), player=0)"
def test_simple_move(self): game = Kalah(6, 4) game.play('f') self.assertEqual(game.status(), (0, 5, 5, 5, 5, 4, 0, 4, 4, 4, 4, 4, 4, 0))
def test_two_simple_moves(self): game = Kalah(6, 4) game.play('e') game.play('E') self.assertEqual(game.status(), (4, 0, 5, 5, 5, 5, 0, 4, 0, 5, 5, 5, 5, 0))