def __init__(self, player_x, player_o, players_interface): self.grid = Grid() self.player_x = player_x self.player_o = player_o self.pi = players_interface self.current_player = None self.current_player = player_x
def test_serialize(self): g = Grid(4, 3) level_dict = g.serialize_level() print(level_dict) level_json = json.dumps(level_dict) print(level_dict[1][1])
class Game: def __init__(self, player_x, player_o, players_interface): self.grid = Grid() self.player_x = player_x self.player_o = player_o self.pi = players_interface self.current_player = None self.current_player = player_x def get_switched_player(self): return self.player_o if self.current_player == self.player_x else self.player_x def play(self): (row, col) = self.pi.choose_cell(self.current_player, self.player_x, self.player_o, self.grid) if self.grid.is_empty(row, col): self.grid.fill( player=self.current_player, row=row, col=col ) else: raise BadCellError(self.current_player, row, col) self.current_player = self.get_switched_player() def get_winner(self): return self.grid.get_winner() def is_won(self): return True if self.grid.get_winner() is not None else False
def test_neighbours002(self): g = Grid(4, 3) cell_00 = g.get_cell(0, 0) n = cell_00.neighbours(g) self.assertCountEqual([n.pos for n in cell_00.neighbours_cells(g)], [(1, 0), (0, 1)])
def test_move32_blank(self): g = Grid(4, 3) cell_32 = g.get_cell(3, 2) c = Cellgame() print(g) print(cell_32.neighbours(g)) c.move(cell_32, g) print(g) self.assertCountEqual( [1, 1, 1], [g.get_cell(r[0], r[1]).state for r in [(2, 2), (3, 2), (3, 1)]])
def test_3_vertical_fill_on_last_col_is_a_winning_pattern(self): player = Player('test') grid = Grid() self.assertIsNone(grid.get_winner()) self.draw_winning_pattern(player, grid, row=0, col=1, vertical=True, horizontal=False) self.assertEqual(player, grid.get_winner())
def test_right_left_diagonal_is_a_winning_pattern(self): player = Player('test') grid = Grid() self.assertIsNone(grid.get_winner()) self.draw_winning_pattern(player, grid, row=0, col=2, vertical=True, horizontal=True) self.assertEqual(player, grid.get_winner())
def test_move00_blank(self): g = Grid(4, 3) cell_00 = g.get_cell(0, 0) c = Cellgame() print(g) print(cell_00.neighbours(g)) c.move(cell_00, g) print(g) print(repr(g)) self.assertCountEqual( [1, 1, 1], [g.get_cell(r[0], r[1]).state for r in [(0, 0), (0, 1), (1, 0)]])
def test_move32(self): g = Grid(4, 3) cell_21 = g.get_cell(3, 2) cell_21.state = 1 c = Cellgame() print(g) print(cell_21.neighbours(g)) c.move(cell_21, g) print(repr(g)) print(g) self.assertCountEqual( [1, 1], [g.get_cell(r[0], r[1]).state for r in [(2, 2), (3, 1)]])
def test_move23_blank_square(self): g = Grid(4, 4) cell_23 = g.get_cell(2, 3) c = Cellgame() print(g) print(cell_23.neighbours(g)) c.move(cell_23, g) print(g) self.assertCountEqual([1, 1, 1, 1], [ g.get_cell(r[0], r[1]).state for r in [(1, 3), (2, 2), (3, 3), (2, 3)] ])
def test_move01(self): g = Grid(4, 3) c = Cellgame() cell_01 = g.get_cell(0, 1) cell_01.state = 1 print(g) print(cell_01.neighbours_cells(g)) c.move(cell_01, g) print(g) self.assertCountEqual( [1, 1, 1], [g.get_cell(r[0], r[1]).state for r in [(0, 2), (0, 0), (1, 1)]])
def test_player_interface_can_ask_for_player_to_fill_a_cell(self): pi = PlayersInterface() x = Player('test') o = Player('test') g = Grid() with self.assertRaises(NotImplementedError): pi.choose_cell(x, x, o, g)
def test_player_interface_can_display_winner(self): pi = PlayersInterface() x = Player('test') o = Player('test') g = Grid() with self.assertRaises(NotImplementedError): pi.display_winner(x, x, o, g)
def test_that_a_grid_contain_the_right_cell_matrix(self): grid = Grid() self.assertEqual(3, len(grid.cells)) self.assertEqual(3, len(grid.cells[0])) self.assertEqual(3, len(grid.cells[1])) self.assertEqual(3, len(grid.cells[2])) self.assertTrue(grid.cells[0][0].is_empty()) self.assertTrue(grid.cells[2][2].is_empty())
class Game: def __init__(self, height, width): self.height = height self.width = width self.status = "running" self.grid = Grid(height, width) self.rules = Cellgame() def event_grid(self, x, y): self.rules.move(self.grid.get_cell(x, y), self.grid)
class TestGrid(unittest.TestCase): def setUp(self): self.grid = Grid(1, 1) def test_grid_to_string(self): self.assertEqual(str(self.grid), """[["{\'neighbors\': \'[]\'}"]]""") def test_grid_to_json(self): self.assertEqual(self.grid._json_repr(), { "width": 1, "height": 1, "cells": [ [ { "neighbors": [], "filled": False } ] ] })
def __init__(self, height, width): self.height = height self.width = width self.status = "running" self.grid = Grid(height, width) self.rules = Cellgame()
def main(): win = pygame.display.set_mode((540, 600)) pygame.display.set_caption("Sudoku") board = Grid(9, 9, 540, 540, win) key = None run = True start = time.time() strikes = 0 while run: play_time = round(time.time() - start) for event in pygame.event.get(): if event.type == pygame.QUIT: run = False if event.type == pygame.KEYDOWN: if event.key == pygame.K_1: key = 1 if event.key == pygame.K_2: key = 2 if event.key == pygame.K_3: key = 3 if event.key == pygame.K_4: key = 4 if event.key == pygame.K_5: key = 5 if event.key == pygame.K_6: key = 6 if event.key == pygame.K_7: key = 7 if event.key == pygame.K_8: key = 8 if event.key == pygame.K_9: key = 9 if event.key == pygame.K_DELETE: board.clear() key = None if event.key == pygame.K_SPACE: board.solve_gui() if event.key == pygame.K_RETURN: i, j = board.selected if board.boxes[i][j].temp != 0: if board.place(board.boxes[i][j].temp): print("Success") else: print("Wrong") strikes += 1 key = None if board.is_finished(): print("Game over") if event.type == pygame.MOUSEBUTTONDOWN: pos = pygame.mouse.get_pos() clicked = board.click(pos) if clicked: board.select(clicked[0], clicked[1]) key = None if board.selected and key != None: board.sketch(key) redraw_window(win, board, play_time, strikes) pygame.display.update()
def test_cells_can_be_filled(self): grid = Grid() player = Player('test') grid.fill(player, 1, 1) self.assertFalse(grid.is_empty()) self.assertEqual(player, grid.cells[1][1].player)
def test_is_empty_return_grids_emptiness(self): grid = Grid() self.assertTrue(grid.is_empty())
def test_neighbours12(self): g = Grid(4, 3) cell_12 = g.get_cell(1, 2) print(cell_12.neighbours(g)) self.assertCountEqual(cell_12.neighbours(g), [(0, 2), (2, 2), (1, 1)])
def test_is_empty_can_take_args_for_cell_emptiness(self): player = Player('test') grid = Grid() grid.fill(player, 0, 2) self.assertTrue(grid.is_empty(0, 1)) self.assertFalse(grid.is_empty(0, 2))
def setUp(self): self.grid = Grid() self.mr = MarsRover(self.grid)
def setUp(self): self.grid = Grid(1, 1)
def test_init(self): c = Cell(0, (1, 1), Grid(4, 3)) self.assertEqual(c.pos, (1, 1))
def test_grid_can_exist(self): grid = Grid() self.assertIsNotNone(grid)
def test_import_level(self): g = Grid(4, 3) level_dict = g.serialize_level() level = g.import_level(level_dict) print(g)
def test_neighbours00(self): g = Grid(4, 3) cell_00 = g.get_cell(0, 0) print(cell_00.neighbours(g)) self.assertCountEqual(cell_00.neighbours(g), [(1, 0), (0, 1)])
def test_init(self): g = Grid(4, 3) cell_12 = g.get_cell(1, 2) print(cell_12.pos) print(g) self.assertEqual(cell_12.pos, (1, 2))
class MarsRoverTester(unittest.TestCase): def setUp(self): self.grid = Grid() self.mr = MarsRover(self.grid) def test_initial_position(self): self.assertEqual(self.mr.getPosition(), (0,0)) def test_initial_orientation(self): self.assertEqual(self.mr.getOrientation(), "N") def test_face_N(self): self.assertEqual(self.mr.getOrientation(), "N") def test_face_E(self): self.mr.turnRight() self.assertEqual(self.mr.getOrientation(), "E") def test_face_W(self): self.mr.turnLeft() self.assertEqual(self.mr.getOrientation(), "W") def test_face_S_turning_left(self): self.mr.turnLeft() self.mr.turnLeft() self.assertEqual(self.mr.getOrientation(), "S") def test_face_S_turning_right(self): self.mr.turnRight() self.mr.turnRight() self.assertEqual(self.mr.getOrientation(), "S") def test_face_N_turning_left(self): self.mr.turnLeft() self.mr.turnLeft() self.mr.turnLeft() self.mr.turnLeft() self.assertEqual(self.mr.getOrientation(), "N") def test_face_N_turning_right(self): self.mr.turnRight() self.mr.turnRight() self.mr.turnRight() self.mr.turnRight() self.assertEqual(self.mr.getOrientation(), "N") def test_move_F(self): self.mr.moveF() self.assertEqual(self.mr.getPosition(), (0,1)) def test_size_of_grid(self): self.assertEqual(self.grid.getSize(), (100,100)) def test_wrap_the_grid_N(self): initial_position = self.mr.getPosition() for i in range(initial_position[1], self.mr.getGrid().getSize()[1]): self.mr.moveF() self.assertEquals(self.mr.getPosition(), initial_position) def test_sequence_FFRFF(self): self.mr.moveF() self.mr.moveF() self.mr.turnRight() self.mr.moveF() self.mr.moveF() self.assertEqual(self.mr.getPosition(), (2, 2)) self.assertEqual(self.mr.getOrientation(), "E") def tearDown(self): pass