From cbeb91b0ce2fc7eed39e4f7968d9b2e3708df40e Mon Sep 17 00:00:00 2001 From: Pooneh Shooshtari Date: Tue, 25 Oct 2016 19:57:28 -0600 Subject: [PATCH] implemented test --- test_ttt.py | 100 ++++++++++++++++++++++++++++++++++++++++------------ ttt.py | 41 ++++++++++++++++++++- 2 files changed, 117 insertions(+), 24 deletions(-) diff --git a/test_ttt.py b/test_ttt.py index 4f12211..3e722f2 100644 --- a/test_ttt.py +++ b/test_ttt.py @@ -1,29 +1,83 @@ + import unittest import ttt +GameStates = ttt.GameStates + +x = 'x' +o = 'o' +E = '.' + + class TestTTT( unittest.TestCase ): - def test_1(self): - - self.assertEqual( 0, 0 ) # pass - self.assertEqual( 1, 0 ) # fail - - something = board = None - self.assertEqual( something, ttt.game_state( board ) ) - - # all built in test functions - # from: https://docs.python.org/2/library/unittest.html - - # self.assertEqual(a, b) a == b - # self.assertNotEqual(a, b) a != b - # self.assertTrue(x) bool(x) is True - # self.assertFalse(x) bool(x) is False - # self.assertIs(a, b) a is b 2.7 - # self.assertIsNot(a, b) a is not b 2.7 - # self.assertIsNone(x) x is None 2.7 - # self.assertIsNotNone(x) x is not None 2.7 - # self.assertIn(a, b) a in b 2.7 - # self.assertNotIn(a, b) a not in b 2.7 - # self.assertIsInstance(a, b) isinstance(a, b) 2.7 - # self.assertNotIsInstance(a, b) not isinstance(a, b) 2.7 + def test_unfinished(self): + + empty_board = [E, E, E, + E, E, E, + E, E, E] + + unfinished_board = [x, E, E, + E, E, o, + E, E, E] + + + + + self.assertEqual(GameStates.unfinished, ttt.game_state(empty_board) ) + self.assertEqual(GameStates.unfinished, ttt.game_state(unfinished_board) ) + + + def test_x_win(self): + + x_win_board = [ o, o, x, + x, x, x, + o, o, x ] + self.assertEqual(GameStates.x_wins, ttt.game_state(x_win_board) ) + + def test_o_win(self): + + o_win_board = [ o, o, o, + x, x, E, + x, E, x ] + self.assertEqual(GameStates.o_wins, ttt.game_state(o_win_board) ) + + def test_draw(self): + + draw_board = [ o, x, o, + x, x, o, + x, o, x ] + self.assertEqual(GameStates.draw, ttt.game_state(draw_board) ) + + def test_invalid(self): + + + invalid_board_1 = [ o, o, o, + x, x, o, + o, o, x, o ] + + + + invalid_board_2 = [ o, o, o, + x, x, "p", + o, o, x] + + too_many_x_board = [x, x, E, + E, E, o, + E, x, E] + + too_many_o_board = [o, x, E, + o, E, o, + o, x, E] + + + self.assertEqual(GameStates.invalid, ttt.game_state(invalid_board_1) ) + self.assertEqual(GameStates.invalid, ttt.game_state(invalid_board_2) ) + self.assertEqual(GameStates.invalid, ttt.game_state(too_many_x_board) ) + self.assertEqual(GameStates.invalid, ttt.game_state(too_many_o_board) ) + + +if __name__ == '__main__': + unittest.main() + diff --git a/ttt.py b/ttt.py index 46467ea..9b09132 100644 --- a/ttt.py +++ b/ttt.py @@ -5,6 +5,7 @@ board is a list of 9 elements from ['x','o','.']. '.' means empty spot. """ + import types class GameStates(object): @@ -18,5 +19,43 @@ class GameStates(object): o_wins = 3 draw = 4 + +list_of_possibilities = [(0,1,2),(3,4,5),(6,7,8),(0,3,6),(1,4,7),(2,5,8),(0,4,8),(6,4,2)] + + +def check_win(board, which): + for poss in list_of_possibilities: + if board[poss[0]]== which and board[poss[1]]==which and board[poss[2]]==which: + return True + return False + + + def game_state(board): - return states # you decide what should get returned here + if len(board) != 9: + return GameStates.invalid + + if board.count("x") - board.count("o") > 1 or board.count("x") - board.count("o") < 0: + return GameStates.invalid + + for item in board: + if item not in ["x", "o", "."]: + return GameStates.invalid + + if check_win(board, "x"): + return GameStates.x_wins + + if check_win(board, "o"): + return GameStates.o_wins + + if "." in board: + return GameStates.unfinished + + return GameStates.draw + + + + + + +