def test_win_blanks6(self): self.test_has_win() b = board_class.Board() b.board = ['N', 'N', 'O', 'X', 'O', 'N', 'O', 'X', 'O'] actual = b.win() self.assertTrue( actual, msg="if end diagonal down is 3 Os, win should return true")
def test_win_blanks2(self): self.test_has_win() b = board_class.Board() b.board = ['O', 'N', 'X', 'X', 'N', 'O', 'O', 'N', 'N'] actual = b.win() self.assertFalse( actual, msg="win should return False if there are no winners yet.")
def test_has_Board_constructor(self): self.test_has_Board_class() b = board_class.Board() self.assertTrue( hasattr(b, '__init__'), msg='Could not find constructor ("__init__") method in Board class' )
def test_win_col3(self): self.test_has_win() b = board_class.Board() b.board = ['N', 'N', 'X', 'N', 'N', 'X', 'N', 'O', 'X'] actual = b.win() self.assertTrue( actual, msg="if last column row is 3 Xs, win should return true")
def test_full_has_zero_param(self): self.test_full_exists() b = board_class.Board() self.assertTrue( len(signature(b.full).parameters) == 0, msg='The full method in Board should not have any user parameters.' )
def test_choose_spot_last_after_one_del(self): self.test_Board_choose_one_param() b = board_class.Board() b.take_spot(3) expected = 9 actual = b.choose_spot_at(7) self.assertEqual(expected, actual)
def test_full_empty(self): self.test_full_has_zero_param() b = board_class.Board() actual = b.full() self.assertFalse( actual, msg="When the board has no Xs or Os, full should return False")
def test_Board_take_spot3_list(self): self.test_Board_take_one_param() b = board_class.Board() b.take_spot(3) actual = b.spots_left expected = [1, 2, 4, 5, 6, 7, 8, 9] self.assertEqual(expected, actual)
def test_win_row_2(self): self.test_has_win() b = board_class.Board() b.board = ['X', 'N', 'N', 'O', 'O', 'O', 'N', 'N', 'N'] actual = b.win() self.assertTrue(actual, msg="if middle row is 3 Os, win should return true")
def test_getwin_has_zero_param(self): self.test_has_get_winner() b = board_class.Board() self.assertTrue( len(signature(b.get_winner).parameters) == 0, msg= 'The get_winner method in Board should not have any user parameters.' )
def test_setO_3(self): self.test_setX_one_param() b = board_class.Board() b.board = ['O', 'X', 'N', 'N', 'N', 'O', 'X', 'N', 'N'] b.setO(3) expected = ['O', 'X', 'O', 'N', 'N', 'O', 'X', 'N', 'N'] actual = b.board self.assertEqual(expected, actual)
def test_setO_one_param(self): self.test_setO_exists() b = board_class.Board() self.assertTrue( len(signature(b.setO).parameters) == 1, msg= 'The setOmethod in Board should have a self and an integer parameter. It does not.' )
def test_has_SIZE_attr(self): self.test_has_Board_constructor() b = board_class.Board() self.assertTrue( hasattr(b, 'SIZE'), msg= 'You should have an attribute named SIZE in the Board class. It should have the value 9.' )
def test_unfilled_none(self): self.test_unfilled_no_params() b = board_class.Board() for i in range(1, b.SIZE + 1): b.take_spot(i) actual = b.nbr_of_unfilled_slots() expected = 0 self.assertEqual(expected, actual)
def test_Board_take_one_param(self): self.test_Board_has_take_spot() b = board_class.Board() self.assertTrue( len(signature(b.take_spot).parameters) == 1, msg= 'The take_spot method in Board should have a self and an integer parameter. It does not.' )
def test_unfilled_no_params(self): self.test_has_unfilled_slots() b = board_class.Board() self.assertTrue( len(signature(b.nbr_of_unfilled_slots).parameters) == 0, msg= 'The nbr_of_unfilled_slots method in Board should have a self parameter only' )
def test_get_winner_blanks(self): self.test_has_get_winner() b = board_class.Board() expected = b.BLANK actual = b.get_winner() self.assertEqual( expected, actual, msg="get_winner should return Blank before anybody has played.")
def test_get_winner_row1(self): self.test_has_get_winner() b = board_class.Board() b.board = ['O', 'O', 'O', 'X', 'N', 'N', 'N', 'N', 'N'] expected = 'O' actual = b.get_winner() self.assertEqual(expected, actual, msg="if top row is 3 Os, O should be a winner")
def test_get_winner_row3(self): self.test_has_get_winner() b = board_class.Board() b.board = ['X', 'N', 'N', 'N', 'N', 'N', 'X', 'X', 'X'] expected = 'X' actual = b.get_winner() self.assertEqual(expected, actual, msg="if bottow row is 3 Xs, X should be a winner")
def test_get_winner_diag_0(self): self.test_has_get_winner() b = board_class.Board() b.board = ['O', 'X', 'N', 'N', 'O', 'N', 'O', 'X', 'O'] expected = 'O' actual = b.get_winner() self.assertEqual(expected, actual, msg="if diagonal down is 3 Os, O should be a winner")
def test_get_winner_cat(self): self.test_has_get_winner() b = board_class.Board() b.board = ['X', 'O', 'X', 'O', 'O', 'X', 'X', 'X', 'O'] expected = b.BLANK actual = b.get_winner() self.assertEqual(expected, actual, msg="In no winner games, get_winner should return N ")
def test_full_full(self): self.test_full_has_zero_param() b = board_class.Board() for i in range(1, b.SIZE + 1): b.setO(i) actual = b.full() self.assertTrue( actual, msg="When the board is all Xs or Os, full should return True")
def test_full_half_full(self): self.test_full_has_zero_param() b = board_class.Board() for i in range(1, b.SIZE + 1, 2): b.setX(i) actual = b.full() self.assertFalse( actual, msg= "When the board has only some Xs and Os, full should return False")
def test_get_winner_blanks2(self): self.test_has_get_winner() b = board_class.Board() b.board = ['O', 'N', 'X', 'X', 'N', 'O', 'O', 'N', 'N'] expected = b.BLANK actual = b.get_winner() self.assertEqual( expected, actual, msg="get_winner should return Blank if there are no winners yet.")
def test_get_winner_col_2(self): self.test_has_get_winner() b = board_class.Board() b.board = ['N', 'X', 'N', 'N', 'X', 'N', 'O', 'X', 'X'] expected = 'X' actual = b.get_winner() self.assertEqual( expected, actual, msg="if middle column row is 3 Xs, X should be a winner")
def test_setO_1(self): self.test_setO_one_param() b = board_class.Board() b.setO(1) expected = ['O', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N'] actual = b.board self.assertEqual( expected, actual, msg= "After setting the first spot to be O, board[0] should be O, and all others BLANK" )
def test_choose_spot_first_after_two_del(self): self.test_Board_choose_one_param() b = board_class.Board() b.take_spot(3) b.take_spot(6) expected = 1 actual = b.choose_spot_at(0) self.assertEqual( expected, actual, msg= "After removing 3 and 6, the list should be 1, 2, 4, 5, 7, 8, 9. Index 0 holds 1." )
def test_unfilled_some(self): self.test_unfilled_no_params() b = board_class.Board() b.take_spot(2) b.take_spot(9) b.take_spot(8) b.take_spot(7) actual = b.nbr_of_unfilled_slots() expected = b.SIZE - 4 self.assertEqual( expected, actual, msg= 'The take_spot method in Board should have a self and an integer parameter. It does not.' )
def run_suite(): """Run informal tests on code.""" suite = test.TestSuite() # testing the __init__(), __str__() and change_board() methods game1 = board.Board(6, 7) print(game1) # testing the change_board() method test_board1 = np.array([[0, 0, 1, 0], [0, 0, 0, 0], [1, 0, 0, -1], [0, 0, 0, 0], [0, -1, 0, 0]]) game1.change_board(test_board1) print(game1) print(game1._height, "x", game1._width) # testing the is_empty() method suite.run_test(game1.is_empty((0, 0)), True, "Test #1.1") suite.run_test(game1.is_empty((4, 1)), False, "Test #1.2") suite.run_test(game1.is_empty((2, 3)), False, "Test #1.3") suite.run_test(game1.is_empty((2, 1)), True, "Test #1.4") # testing the set_full() and get_cell_content() methods game1.set_full((4, 0), board.PLAYER1) suite.run_test(game1.get_cell_content((4, 0)), board.PLAYER1, "Test #2.1") game1.set_full((4, 2), board.PLAYER1) suite.run_test(game1.get_cell_content((4, 2)), board.PLAYER1, "Test #2.2") game1.set_full((0, 0), board.PLAYER2) suite.run_test(game1.get_cell_content((0, 0)), board.PLAYER2, "Test #2.3") # testing the clear_board() method game1.clear_board() print(game1) # reporting the results of the test suite.report_results()
def main(): pygame.init() board = board_class.Board() screen = pygame.display.set_mode(board.size) pygame.display.set_caption("Tetris") clock = pygame.time.Clock() blocks_moved = False while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() if event.type == pygame.KEYDOWN: # Active piece is most recent piece added and the one of which # the player is in control active_piece = board.pieces[-1] if event.key == pygame.K_LEFT: if blank_left(active_piece, board): active_piece.move_left() elif event.key == pygame.K_RIGHT: if blank_right(active_piece, board): active_piece.move_right() board.update_cells() if blocks_moved == False: generate_new_block(board) blocks_moved = move_blocks_down(board) graphics.draw_screen(screen, board) pygame.display.update() clock.tick(10)