def test_corners_tie(): # start top left, then bottom right board = TicTacToeBoard(3) board.player = 'X' sys.stdin = io.StringIO("1a\n3c\n2c\n3a\n1b\nn\n") play_game(board) assert board.user_score == 0 assert board.comp_score == 0 # start top left, then top right board = TicTacToeBoard(3) board.player = 'X' sys.stdin = io.StringIO("1a\n1c\n3b\n2a\n3c\nn\n") play_game(board) assert board.user_score == 0 assert board.comp_score == 0 # start bottomw left, then bottom right board = TicTacToeBoard(3) board.player = 'X' sys.stdin = io.StringIO("3a\n3c\n1b\n2a\n2c\nn\n") play_game(board) assert board.user_score == 0 assert board.comp_score == 0
def trying_tie(): board = TicTacToeBoard(3) board.player = 'O' sys.stdin = io.StringIO("3c\n3b\n1c\n2a\nn\n") play_game(board) assert board.user_score == 0 assert board.comp_score == 0 board = TicTacToeBoard(3) board.player = 'O' sys.stdin = io.StringIO("1c\n3c\n2a\n3b\nn\n") play_game(board) assert board.user_score == 0 assert board.comp_score == 0
def trying_win(): """Test if user tries to beat the computer""" board = TicTacToeBoard(3) board.player = 'O' sys.stdin = io.StringIO("2a\n3b\n1c\nn\n") play_game(board) assert board.user_score == 0 assert board.comp_score == 1 board = TicTacToeBoard(3) board.player = 'O' sys.stdin = io.StringIO("2c\n3b\n3c\nn\n") play_game(board) assert board.user_score == 0 assert board.comp_score == 1
def opposite_middles(): """Test if user takes opposite middle positions""" # vertical two board = TicTacToeBoard(3) board.player = 'O' sys.stdin = io.StringIO("1b\n3b\nn\n") play_game(board) assert board.user_score == 0 assert board.comp_score == 1 # horizontal two board = TicTacToeBoard(3) board.player = 'O' sys.stdin = io.StringIO("2a\n2c\nn\n") play_game(board) assert board.user_score == 0 assert board.comp_score == 1
def opposite_corners(): """Test if user takes opposite corners""" # topleft, bottom right board = TicTacToeBoard(3) board.player = 'O' sys.stdin = io.StringIO("1a\n3c\nn\n") play_game(board) assert board.user_score == 0 assert board.comp_score == 1 # top right, bottom left board = TicTacToeBoard(3) board.player = 'O' sys.stdin = io.StringIO("1c\n3a\nn\n") play_game(board) assert board.user_score == 0 assert board.comp_score == 1
def complex_tie(): # start top middle, then top right board = TicTacToeBoard(3) board.player = 'X' sys.stdin = io.StringIO("1b\n1c\n3c\n2a\n3b\nn\n") play_game(board) assert board.user_score == 0 assert board.comp_score == 0 # start top left, then bottom middle board = TicTacToeBoard(3) board.player = 'X' sys.stdin = io.StringIO("1a\n3b\n1c\n2c\n2a\nn\n") play_game(board) assert board.user_score == 0 assert board.comp_score == 0
def test_middle_tie(): # start middle, then left board = TicTacToeBoard(3) board.player = 'X' sys.stdin = io.StringIO("2b\n2a\n1c\n1b\n3c\nn\n") play_game(board) assert board.user_score == 0 assert board.comp_score == 0 # start middle, then bottom right board = TicTacToeBoard(3) board.player = 'X' sys.stdin = io.StringIO("2b\n3c\n1b\n2a\n3a\nn\n") play_game(board) assert board.user_score == 0 assert board.comp_score == 0
def complex_lose(): # start middle, then 3 consecutive board = TicTacToeBoard(3) board.player = 'X' sys.stdin = io.StringIO("2b\n2c\n3a\n3b\nn\n") play_game(board) assert board.user_score == 0 assert board.comp_score == 1 # start middle, then almost win board = TicTacToeBoard(3) board.player = 'X' sys.stdin = io.StringIO("2b\n1b\n1c\n2c\nn\n") play_game(board) assert board.user_score == 0 assert board.comp_score == 1
def adjacent_corners(): """Test if user takes adjacent corners""" # top two board = TicTacToeBoard(3) board.player = 'O' sys.stdin = io.StringIO("1a\n1c\nn\n") play_game(board) assert board.user_score == 0 assert board.comp_score == 1 # right two board = TicTacToeBoard(3) board.player = 'O' sys.stdin = io.StringIO("1c\n3c\n3b\nn\n") play_game(board) assert board.user_score == 0 assert board.comp_score == 1 # bottom two board = TicTacToeBoard(3) board.player = 'O' sys.stdin = io.StringIO("3a\n3c\n2c\nn\n") play_game(board) assert board.user_score == 0 assert board.comp_score == 1 # left two board = TicTacToeBoard(3) board.player = 'O' sys.stdin = io.StringIO("1a\n3a\nn\n") play_game(board) assert board.user_score == 0 assert board.comp_score == 1
def test_wc(): board_1 = TicTacToeBoard(1) win_combos1 = [] for combo in board_1.combo_queue: win_combos1.append(list(combo.indices)) assert (win_combos1 == [[0], [0]]) board_3 = TicTacToeBoard(3) win_combos3 = [] for combo in board_3.combo_queue: win_combos3.append(list(combo.indices)) assert (win_combos3 == [ [0, 1, 2], [3, 4, 5], [6, 7, 8], # rows [0, 3, 6], [1, 4, 7], [2, 5, 8], # columns [0, 4, 8], [2, 4, 6] ]) # diagonals board_5 = TicTacToeBoard(5) win_combos5 = [] for combo in board_5.combo_queue: win_combos5.append(list(combo.indices)) assert (win_combos5 == [ [0, 1, 2, 3, 4], [5, 6, 7, 8, 9], [10, 11, 12, 13, 14], # rows [15, 16, 17, 18, 19], [20, 21, 22, 23, 24], # rows [0, 5, 10, 15, 20], [1, 6, 11, 16, 21], [2, 7, 12, 17, 22], # columns [3, 8, 13, 18, 23], [4, 9, 14, 19, 24], # columns [0, 6, 12, 18, 24], [4, 8, 12, 16, 20] ]) # diagonals board_7 = TicTacToeBoard(7) win_combos7 = [] for combo in board_7.combo_queue: win_combos7.append(list(combo.indices)) assert (win_combos7 == [ [0, 1, 2, 3, 4, 5, 6], [7, 8, 9, 10, 11, 12, 13], [14, 15, 16, 17, 18, 19, 20], [21, 22, 23, 24, 25, 26, 27], [28, 29, 30, 31, 32, 33, 34], [35, 36, 37, 38, 39, 40, 41], [42, 43, 44, 45, 46, 47, 48], # rows [0, 7, 14, 21, 28, 35, 42], [1, 8, 15, 22, 29, 36, 43], [2, 9, 16, 23, 30, 37, 44], [3, 10, 17, 24, 31, 38, 45], [4, 11, 18, 25, 32, 39, 46], [5, 12, 19, 26, 33, 40, 47], [6, 13, 20, 27, 34, 41, 48], # columns [0, 8, 16, 24, 32, 40, 48], [6, 12, 18, 24, 30, 36, 42] ]) # diags board_9 = TicTacToeBoard(9) win_combos9 = [] for combo in board_9.combo_queue: win_combos9.append(list(combo.indices)) assert (win_combos9 == [ [0, 1, 2, 3, 4, 5, 6, 7, 8], [9, 10, 11, 12, 13, 14, 15, 16, 17], [18, 19, 20, 21, 22, 23, 24, 25, 26], [27, 28, 29, 30, 31, 32, 33, 34, 35], [36, 37, 38, 39, 40, 41, 42, 43, 44], [45, 46, 47, 48, 49, 50, 51, 52, 53], [54, 55, 56, 57, 58, 59, 60, 61, 62], [63, 64, 65, 66, 67, 68, 69, 70, 71], [72, 73, 74, 75, 76, 77, 78, 79, 80], # rows [0, 9, 18, 27, 36, 45, 54, 63, 72], [1, 10, 19, 28, 37, 46, 55, 64, 73], [2, 11, 20, 29, 38, 47, 56, 65, 74], [3, 12, 21, 30, 39, 48, 57, 66, 75], [4, 13, 22, 31, 40, 49, 58, 67, 76], [5, 14, 23, 32, 41, 50, 59, 68, 77], [6, 15, 24, 33, 42, 51, 60, 69, 78], [7, 16, 25, 34, 43, 52, 61, 70, 79], [8, 17, 26, 35, 44, 53, 62, 71, 80], # columns [0, 10, 20, 30, 40, 50, 60, 70, 80], [8, 16, 24, 32, 40, 48, 56, 64, 72] ]) # diags
def corners_lose(): # top right three corners board = TicTacToeBoard(3) board.player = 'X' sys.stdin = io.StringIO("1a\n1c\n3c\nn\n") play_game(board) assert board.user_score == 0 assert board.comp_score == 1 # bottom right three corners board = TicTacToeBoard(3) board.player = 'X' sys.stdin = io.StringIO("1c\n3c\n3a\nn\n") play_game(board) assert board.user_score == 0 assert board.comp_score == 1 # bottom left three corners board = TicTacToeBoard(3) board.player = 'X' sys.stdin = io.StringIO("3c\n3a\n1a\nn\n") play_game(board) assert board.user_score == 0 assert board.comp_score == 1 # top left three corners board = TicTacToeBoard(3) board.player = 'X' sys.stdin = io.StringIO("3a\n1a\n1c\nn\n") play_game(board) assert board.user_score == 0 assert board.comp_score == 1
def medium_lose(): # top left triangle board = TicTacToeBoard(3) board.player = 'X' sys.stdin = io.StringIO("1a\n1b\n2a\nn\n") play_game(board) assert board.user_score == 0 assert board.comp_score == 1 # top right, bottom left board = TicTacToeBoard(3) board.player = 'X' sys.stdin = io.StringIO("1c\n2c\n3a\nn\n") play_game(board) assert board.user_score == 0 assert board.comp_score == 1 # start middle, bottom middle triangle board = TicTacToeBoard(3) board.player = 'X' sys.stdin = io.StringIO("2b\n3b\n3a\nn\n") play_game(board) assert board.user_score == 0 assert board.comp_score == 1 # start middle, top right, bottom right board = TicTacToeBoard(3) board.player = 'X' sys.stdin = io.StringIO("2b\n1c\n3c\nn\n") play_game(board) assert board.user_score == 0 assert board.comp_score == 1
def test_user_starts_comp_wins(): # top left triangle board = TicTacToeBoard(3) board.player = 'X' sys.stdin = io.StringIO("1a\n1b\n2a\nn\n") play_game(board) assert board.user_score == 0 assert board.comp_score == 1 # top right, bottom left board = TicTacToeBoard(3) board.player = 'X' sys.stdin = io.StringIO("1c\n2c\n3a\nn\n") play_game(board) assert board.user_score == 0 assert board.comp_score == 1 # start middle, bottom middle triangle board = TicTacToeBoard(3) board.player = 'X' sys.stdin = io.StringIO("2b\n3b\n3a\nn\n") play_game(board) assert board.user_score == 0 assert board.comp_score == 1 # start middle, top right, bottom right board = TicTacToeBoard(3) board.player = 'X' sys.stdin = io.StringIO("2b\n1c\n3c\nn\n") play_game(board) assert board.user_score == 0 assert board.comp_score == 1 # start middle, then 3 consecutive board = TicTacToeBoard(3) board.player = 'X' sys.stdin = io.StringIO("2b\n2c\n3a\n3b\nn\n") play_game(board) assert board.user_score == 0 assert board.comp_score == 1 # start middle, then almost win board = TicTacToeBoard(3) board.player = 'X' sys.stdin = io.StringIO("2b\n1b\n1c\n2c\nn\n") play_game(board) assert board.user_score == 0 assert board.comp_score == 1