def __init__(self, width, height): self.width = width self.height = height self.cells = dict() # type: Dict[Coord, Cell] for y in range(height): for x in range(width): coord = Coord(x, y) cell = Cell() cell.celltype = Cell.CellType.FLOOR self.cells[coord] = cell
def generateDiamondGrid(self, size): grid = [] prevRow = [] for i in range(size): row = [] currentRow = [] for j in range(size): cell = Cell(pos=(i, j), isOccupied=(i, j) not in self.openCells) if (i > 0): cell.addNeighbor((0, -1), prevRow[j]) # Add top neighbor if (i > 0 and j < size - 1): cell.addNeighbor((1, -1), prevRow[j + 1]) # Add top-right neighbor if (j > 0): cell.addNeighbor((-1, 0), currentRow[-1]) # Add left neighbor row.append(cell) currentRow.append(cell) prevRow = currentRow currentRow = [] grid.append(row) return grid
def __init__(self, player_map): self.win = QMainWindow() self.game_ui.setupUi(self.win) self.win.setWindowTitle("Battleship Game") # Set player map self.player_map = player_map # Generate AI Map and add cells to the grid pane for row in range(10): ai_map_row = [] for column in range(10): # Create AI cell aiButton = Cell(row, column, self) ai_map_row.append(aiButton) self.game_ui.aiGrid.addWidget(aiButton, row, column) # Get player cell from player map playerButton = player_map[row][column] playerButton.setNeutral() self.game_ui.playerGrid.addWidget(playerButton, row, column) self.ai_map.append(ai_map_row) # Init game self.game = BattleShipGame(self.player_map, self.ai_map) # Init AI self.ai = BattleshipAI(self.game) self.ai.placeShips()
def test_cell_constructor_parameters(self): my_cell_default = Cell(True, State.DEFAULT) my_cell_flagged = Cell(False, State.FLAGGED) my_cell_open = Cell(True, State.OPEN) self.assertEqual(my_cell_default.state, State.DEFAULT, "Cell should have state of State.DEFAULT.") self.assertEqual(my_cell_flagged.state, State.FLAGGED, "Cell should have state of State.FLAGGED.") self.assertEqual(my_cell_open.state, State.OPEN, "Cell should have state of State.OPEN.") self.assertEqual(my_cell_default.is_bomb, True, "Cell did not keep value set for is_bomb.") self.assertEqual(my_cell_flagged.is_bomb, False, "Cell did not keep value set for is_bomb.")
def create_cell_grid(grid_size: Coordinates2D) -> T.List[T.List[Cell]]: cells: T.List[T.List[Cell]] = [[Cell() for _ in range(grid_size.x)] for _ in range(grid_size.y)] for y in range(grid_size.y): for x in range(grid_size.x): cells[y][x].set_neighbor( Direction2D.NORTH, cells[y - 1][x] ) cells[y][x].set_neighbor( Direction2D.NORTH_WEST, cells[y - 1][x - 1] ) cells[y][x].set_neighbor( Direction2D.WEST, cells[y][x - 1] ) cells[y][x].set_neighbor( Direction2D.SOUTH_WEST, cells[y + 1 if y < grid_size.y - 1 else 0][x - 1] ) cells[y][x].set_neighbor( Direction2D.SOUTH, cells[y + 1 if y < grid_size.y - 1 else 0][x] ) cells[y][x].set_neighbor( Direction2D.SOUTH_EAST, cells[y + 1 if y < grid_size.y - 1 else 0][x + 1 if x < grid_size.x - 1 else 0] ) cells[y][x].set_neighbor( Direction2D.EAST, cells[y][x + 1 if x < grid_size.x - 1 else 0] ) cells[y][x].set_neighbor( Direction2D.NORTH_EAST, cells[y - 1][x + 1 if x < grid_size.x - 1 else 0] ) return cells
def test_cell_clear(self): my_cell = Cell() my_cell.is_bomb = True my_cell.state = State.FLAGGED # Before clear should not be equivalen to a new cell. self.assertNotEqual(my_cell, Cell()) my_cell.clear() # After clear should be equivalent to a new cell. self.assertEqual(my_cell, Cell())
def __init__(self, width, height): """Intialize the grid and set it up for the desired width and height""" self._width = width self._height = height self._cells = [[Cell() for i in range(self._width)] for j in range(self._height)] self._state = GameState.PLAYING self.clear()
def __init__(self, n, m, points): self.rows = n self.columns = m self.board = [] for i in range(self.rows): board_rows = [Cell() for i in range(self.columns)] self.board.append(board_rows) for point in points: self.set_status_of_cell_at_position(point, Status.ALIVE)
def __init__(self, root): self.cellarray = [] self.touched = False self.touched_location = [] #self.algo = AlphaBeta(self) self.algo = MiniMax(self) self.is_white_move = True flag = True for i in range(8): self.cellarray.append([]) for j in range(8): b = Cell(root) if(flag == True): b.config(location=[i , j], color="white", command = self.cellcallback) b.grid(row=i, column=j) else: b.config(location=[i , j], color="black", command = self.cellcallback) b.grid(row=i, column=j) self.cellarray[i].append(b) flag = not flag flag = not flag
def parseCells(self, setup=True, **kwargs): if not setup: grid = kwargs.get('grid', self.grid) for y, row in enumerate(grid): for x, cell in enumerate(row): cell.parseState() else: for y, row in enumerate(self.grid): self.grid[y] = [] for x, state in enumerate(row[0]): cell = Cell(x, y, state, columns=self.columns, rows=self.rows) self.grid[y].append(cell)
def __init__(self, name, size_x, size_y, step, max_population): metadata = WorldData() self._name = metadata.name = name self._size_x = metadata.size_x = size_x self._size_y = metadata.size_y = size_y self._step = metadata.world_step = step self._max_population = metadata.max_population = max_population self.temp = [] self._board = [] for i in range(size_x * size_y): self._board.append(Cell(i)) self._free_ids = range(1, 9) self._users = {} self._raw_metadata = metadata.encode_self()
def generateBoard(self, size): grid, prevRow = ([], []) for i in range(size): row, currentRow = ([], []) for j in range(size): cell = Cell(pos=(i, j), value=(0)) if (i > 0): cell.addNeighbor((0, -1), prevRow[j]) # Add top neighbor if (i > 0 and j < size-1): cell.addNeighbor((1, -1), prevRow[j+1]) # Add top-right neighbor if (j > 0): cell.addNeighbor((-1, 0), currentRow[-1]) # Add left neighbor row.append(cell) currentRow.append(cell) prevRow = currentRow currentRow = [] grid.append(row) return grid
def __init__(self): self.win = QMainWindow() self.setup_ui.setupUi(self.win) self.win.setWindowTitle("Battleship Game") self.setup_ui.readyButton.clicked.connect(self.actionReadyButton) self.setup_ui.carrierButton.clicked.connect(self.actionCarrierButton) self.setup_ui.battleshipButton.clicked.connect( self.actionBattleshipButton) self.setup_ui.cruiserButton.clicked.connect(self.actionCruiserButton) self.setup_ui.submarineButton.clicked.connect( self.actionSubmarineButton) self.setup_ui.destroyerButton.clicked.connect( self.actionDestroyerButton) self.setup_ui.rotateButton.clicked.connect(self.actionRotateButton) # Fill setup grid with cells for row in range(10): cells_row = [] for column in range(10): setup_cell = Cell(row, column, self) cells_row.append(setup_cell) self.setup_ui.setupGrid.addWidget(setup_cell, row, column) self.player_map.append(cells_row)
def test_initialized_cell_is_dead(self): new_cell = Cell() self.assertEqual(Status.DEAD, new_cell.get_state())
def test_setting_up_cell_state_as_false(self): new_cell = Cell() new_cell.set_state(False) self.assertFalse(new_cell.get_state())
def test_cell_default_values(self): my_cell = Cell() self.assertEqual(my_cell.state, State.DEFAULT, "Cell should have State.Default for default state.") self.assertEqual(my_cell.is_bomb, False, "Cell should have is_bomb set to False by default.")
def test_cell_toggle_flag(self): my_cell = Cell() # should not change. my_cell.state = State.OPEN my_cell.toggle_flag() self.assertEqual(my_cell.state, State.OPEN) # should change to default. my_cell.state = State.FLAGGED my_cell.toggle_flag() self.assertEqual(my_cell.state, State.DEFAULT) # should change to flagged. my_cell.state = State.DEFAULT my_cell.toggle_flag() self.assertEqual(my_cell.state, State.FLAGGED)
def test_cell_can_be_created(self): my_cell = Cell() self.assertEqual(type(my_cell), Cell, 'Could not create variable of type Cell.')
def fill_cells(self, cell_count): for i in range(cell_count): self.add_cell(Cell.random_cell(self.bounds))
def test_setting_up_cell_state_as_alive(self): new_cell = Cell() new_cell.set_state(Status.ALIVE) self.assertEqual(Status.ALIVE, new_cell.get_state())