def new_game(self): while self.running: self.grid = Grid(config.rows, config.columns, self) self.agent = Agent(self.grid, self) self.run() if self.restart(): self.running = True else: self.running = False
def test_init(): row0 = Grid() assert row0.size == 0 row1 = Grid(None, None, None) assert row1.size == 3 row2 = Grid(None, None) assert row2.size == 2 row3 = Grid(None) assert row3.size == 1 print row2
def test_grid_assignment0(): SIZE = 3 grid = Grid([Column([(x,y) for y in range(SIZE)]) for x in range(SIZE)]) assert grid[0][0] == (0,0) assert grid[1][2] == (1,2) for x in range(SIZE): for y in range(SIZE): assert grid[x][y] == (x, y)
def test_grid_and_squares(): SIZE = 3 grid = Grid([Column([Square(x,y) for y in range(SIZE)]) for x in range(SIZE)]) assert isinstance(grid[0][0], Square) square = grid[0][0] print square assert (square.x, square.y) == (0,0) square0 = grid[1][2] print square0 assert (square0.x, square0.y) == (1,2)
def testCollapseRow(): print("Running tests for collapse_row()") grid = Grid(4) a = [2, 0, 0, 0] b = [2, 0, 2, 0] c = [2, 2, 2, 0] d = [2, 0, 2, 2] e = [8, 8, 16, 8] f = [2, 0, 2, 4] g = [2, 8, 4, 4] h = [2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2] a_sol = ([2, 0, 0, 0], False) b_sol = ([4, 0, 0, 0], True) c_sol = ([4, 2, 0, 0], True) d_sol = ([4, 2, 0, 0], True) e_sol = ([16, 16, 8, 0], True) f_sol = ([4, 4, 0, 0], True) g_sol = ([2, 8, 8, 0], True) h_sol = ([4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], True) assert (grid.collapse_row(a) == a_sol) assert (grid.collapse_row(b) == b_sol) assert (grid.collapse_row(c) == c_sol) assert (grid.collapse_row(d) == d_sol) assert (grid.collapse_row(e) == e_sol) assert (grid.collapse_row(f) == f_sol) assert (grid.collapse_row(g) == g_sol) assert (grid.collapse_row(h) == h_sol)
def main(win): grid = Grid() start = None end = None run = True while run: grid.redraw(win) for event in pygame.event.get(): if event.type == pygame.QUIT: run = False if pygame.mouse.get_pressed()[0]: pos = pygame.mouse.get_pos() node = grid.get_clicked_position(pos) if not start and node != end: start = node start.is_start = True elif not end and node != start and node: end = node end.is_end = True elif node != end and node != start and node: node.make_barrier() if pygame.mouse.get_pressed()[2]: pos = pygame.mouse.get_pos() node = grid.get_clicked_position(pos) if node: node.reset() if node == start: start = None elif node == end: end = None if event.type == pygame.KEYDOWN: if event.key == pygame.K_SPACE and start and end: for row in grid.grid: for node in row: node.update_neighbors(grid.grid, grid.ROWS, grid.ROWS) algorithm((lambda: grid.redraw(win)), start, end) if event.key == pygame.K_r: start = None end = None grid = Grid() pygame.quit()
def test_grids_assignment(): grid = Grid([Column(i, i, i) for i in range(3)]) assert isinstance(grid[0], Column) assert grid[0][0] == 0 print grid[0][1] assert grid[0][1] == 0 print grid [0][2] assert grid[0][2] == 0 print grid[2][1] assert grid[2][1] == 2
def setup(self): """ Setup and start game window """ print("Starting setup") given = 0 if self.difficulty == "easy": given = random.randint(36, 40) if self.difficulty == "medium": given = random.randint(30, 34) if self.difficulty == "hard": given = random.randint(19, 27) print(given) board_array = [ [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], ] print("Generating board") print_board(board_array) board_array = make_board() print_board(board_array) for counter in range(81 - given): row = random.randint(0, 8) col = random.randint(0, 8) while board_array[row][col] == 0: row = random.randint(0, 8) col = random.randint(0, 8) board_array[row][col] = 0 print(given) print("Board finished") data.gridobj = Grid(board_array, 9, 9) self.Dialog = QtWidgets.QDialog() self.ui = UiForm() self.ui.setup_ui(self.Dialog) labelslist = [] rowcounter = 0 itemcounter = 1 data.labels = [[], [], [], [], [], [], [], [], []] for child in self.Dialog.findChildren(QLabel): if child.objectName() != "strikes": labelslist.append(child) labelslist.sort(key=lambda x: int(x.objectName())) for item in labelslist: data.labels[rowcounter].append(item) if itemcounter % 9 == 0: rowcounter += 1 itemcounter = 0 itemcounter += 1 for rowcounter in range(0, 9): for colcounter in range(0, 9): if board_array[rowcounter][colcounter] != 0: data.labels[rowcounter][colcounter].setText( str(board_array[rowcounter][colcounter])) data.labels[rowcounter][colcounter].setStyleSheet( "color: rgb(40, 255, 6); border: " "2px solid white !important") self.Dialog.show()
import os os.environ['SDL_VIDEO_WINDOW_POS'] = "%d,%d" % (400,100) surface = pygame.display.set_mode((1200, 900)) pygame.display.set_caption('Minesweeper') class States(Enum): running = auto() game_over = auto() win = auto() state = States.running player = Player() grid = Grid(player) running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False if event.type == pygame.MOUSEBUTTONDOWN and state == States.running: if pygame.mouse.get_pressed()[0]: # check for the left mouse button pos = pygame.mouse.get_pos() grid.click(pos[0], pos[1]) elif pygame.mouse.get_pressed()[2]: pos = pygame.mouse.get_pos() grid.mark_mine(pos[0]//30, pos[1]//30) if grid.check_if_win():
def main(): grid = Grid(GRID_X, GRID_Y, SCREEN_SIZE) grid.build_grid() a_star = None maze_generator = None step_a_on_update = True pressed_tiles = [] running = True while running: # run the program at most 60 frames per second - makes program controllable on any system clock.tick(FPS) # process inputs for event in pygame.event.get(): if event.type == pygame.QUIT: running = False elif event.type == pygame.MOUSEBUTTONDOWN: if pygame.mouse.get_pressed(3) == (0, 0, 1): # right mouse clicked for tile in grid.tiles: if tile.rect.collidepoint(event.pos): if tile == grid.start_tile: tile.set_color(GRID_COLOR) grid.start_tile = None elif tile == grid.end_tile: tile.set_color(GRID_COLOR) grid.end_tile = None elif not tile.blocked: if not grid.start_tile: tile.set_color(START_COLOR) grid.start_tile = tile elif not grid.end_tile: tile.set_color(END_COLOR) grid.end_tile = tile elif event.type == pygame.MOUSEBUTTONUP: for tile in pressed_tiles: if tile.drag_clicked: tile.drag_clicked = False pressed_tiles.clear() elif event.type == pygame.KEYDOWN: if event.key == pygame.K_s: if grid.start_tile and grid.end_tile: a_star = ASTAR(grid.start_tile, grid.end_tile, grid) elif event.key == pygame.K_n: if a_star: a_star.step_a_star() elif event.key == pygame.K_f: step_a_on_update = not step_a_on_update elif event.key == pygame.K_r: grid = Grid(GRID_X, GRID_Y, SCREEN_SIZE) grid.build_grid() elif event.key == pygame.K_m: maze_generator = MazeGenerator(grid) # moved outside event loop for mouse drag # gets called while left mouse is held if pygame.mouse.get_pressed(3) == (1, 0, 0): for tile in grid.tiles: if tile.rect.collidepoint(pygame.mouse.get_pos()): # if the tile is neither a start or end, and it hasn't been clicked during mouse down if not (tile == grid.start_tile or tile == grid.end_tile) and not tile.drag_clicked: tile.block(not tile.blocked) # while mouse is held, change the tile if not tile.drag_clicked: pressed_tiles.append(tile) tile.drag_clicked = True # update if step_a_on_update and a_star: if not a_star.finished: a_star.step_a_star() if maze_generator and not maze_generator.Finished: maze_generator.step_draw_maze() # render draw_screen(grid) pygame.quit()
def test_setitem(): row = Grid(1, 2, 3) assert (row[0], row[1], row[2]) == (1, 2, 3) row[0] = 4 assert row[0] == 4
def test_grids_init(): column0 = Column(None, None, None) grid = Grid([Column(None,None,None) for i in range(3)]) print type(grid[0]) assert isinstance(grid[0], Column)
def new_board(self): if self.running: self.guessing = False self.drawing = True self.board = Grid()
class Game(): def __init__(self): pygame.init() self.game_display = pygame.display.set_mode( (stg.display_x, stg.display_y)) pygame.display.set_caption(stg.display_title) self.tf_model = tf.keras.models.load_model('num_reader.model') self.running = True self.guessing = False self.drawing = True self.clock = pygame.time.Clock() self.click = False self.prediction_text = None def new_board(self): if self.running: self.guessing = False self.drawing = True self.board = Grid() def new_guess(self): self.info_text1 = gui.TextWindow( stg.text3_x, stg.text3_y, stg.text3_w, stg.text3_h, stg.text3_text_color, stg.text3_text, stg.text3_font) self.info_text2 = gui.TextWindow( stg.text4_x, stg.text4_y, stg.text4_w, stg.text4_h, stg.text4_text_color, stg.text4_text, stg.text4_font) while self.running: self.new_board() self.run() def run(self): self.playing = True while self.playing: self.clock.tick(60) self.events() self.update() self.draw() def events(self): for event in pygame.event.get(): if event.type == pygame.QUIT: self.playing = False self.running = False pygame.quit() sys.exit() if event.type == pygame.MOUSEBUTTONDOWN: self.click = True if event.type == pygame.MOUSEBUTTONUP: self.click = False self.mouse_pos = pygame.mouse.get_pos() self.keys = pygame.key.get_pressed() if self.keys[pygame.K_ESCAPE]: self.playing = False self.running = False pygame.quit() sys.exit() if self.keys[pygame.K_SPACE]: self.drawing = False self.guessing = True if self.keys[pygame.K_c]: self.new_board() def update(self): if self.drawing: if self.click: self.board.update() elif self.guessing: self.guess() def draw(self): self.game_display.fill(stg.BG_COLOR) self.board.draw(self.game_display) if self.drawing: self.info_text1.draw(self.game_display) elif self.prediction_text: self.prediction_text.draw(self.game_display) self.info_text2.draw(self.game_display) pygame.display.update() def guess(self): # self.data = self.overwriting_data() self.data = np.reshape(self.board.grid, (-1, 28, 28)) self.predictions = self.tf_model.predict(self.data) self.prediction = (np.argmax(self.predictions[0])) self.prediction_text = gui.TextWindow(stg.text2_x, stg.text2_y, stg.text2_w, stg.text2_h, stg.text2_text_color, (f'{stg.text2_text}{self.prediction}'), stg.text2_font) self.guessing = False
def main(): print("Welcome to Tic Tac Toe!") p1, p2 = Player(input("Player 1:")), Player(input("Player 2:")) p1.setSymbol('X') p2.setSymbol('O') new_board = Grid() new_board.fillSections() current_player, next_player = p1, p2 current_token, next_token = p1.getSymbol(), p2.getSymbol() move = None while True: print(new_board.drawGrid()) move = int(input("What's your move? ")) while not new_board.checkSpace(move): new_board.updateSection(move, current_token) if new_board.chickenWinner(current_token): print(new_board.drawGrid()) print(f"{current_player} has won the game!") break elif new_board.itsFull(): print(new_board.drawGrid()) print("The Game is a tie!") break current_player, next_player = next_player, current_player current_token, next_token = next_token, current_token
def main(): while (setting.menu_pick): width = 540 height = 650 main_menu = pygame.display.set_mode((width, height)) mode_pick = True run = True while mode_pick: reddraw_main_menu(main_menu) for event in pygame.event.get(): if event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: mode_pick = False run = False setting.menu_pick = False if event.type == pygame.MOUSEBUTTONDOWN: pos = pygame.mouse.get_pos() if (button(220, 100, 100, 40, pos)): setting.number = 5 button_selected_toFalse() setting.picked_5 = True if (button(220, 160, 100, 40, pos)): setting.number = 6 button_selected_toFalse() setting.picked_6 = True if (button(220, 220, 100, 40, pos)): setting.number = 7 button_selected_toFalse() setting.picked_7 = True if (button(420, 580, 100, 40, pos)): mode_pick = False pygame.display.update() win = pygame.display.set_mode((width, height)) pygame.display.set_caption("Hitori") the_board = return_board( setting.number ) # Sets the board to a random board based on what size board the user chose. board = Grid(setting.number, setting.number, 540, 540, the_board) board.mark_all_cubes_type() start = time.time() strikes = 0 clock_running = True while run: if (clock_running): play_time = round(time.time() - start) for event in pygame.event.get(): if event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: run = False setting.menu_pick = False if event.type == pygame.QUIT: run = False if event.type == pygame.MOUSEBUTTONDOWN: pos = pygame.mouse.get_pos() #Checks to if a button is clicked below the board if (button(260, 550, 100, 40, pos)): if (board.path_check()): print( "All non marked squares can reach each other") else: print( "Marked Cubes are blocking off non marked cubes, Wrong" ) if (board.adjacent_marked_check() and board.num_col_row_check() and board.path_check()): print("win") clock_running = False set_fastest_time(play_time) else: print("fail") if (strikes <= 8): strikes += 1 if (button(260, 600, 100, 40, pos)): run = False #checks to see if game board is clicked clicked = board.click(pos) if clicked: board.select(clicked[0], clicked[1]) redraw_window(win, board, play_time, strikes) pygame.display.update()
class Game(): def __init__(self): pygame.init() self.game_display = pygame.display.set_mode( (config.display_w, config.display_h)) pygame.display.set_caption(config.caption) self.running = True self.clock = pygame.time.Clock() self.click = False self.player_win = False self.difficulty = False self.choose = None def new_game(self): while self.running: self.grid = Grid(config.rows, config.columns, self) self.agent = Agent(self.grid, self) self.run() if self.restart(): self.running = True else: self.running = False def run(self): self.turn = config.AI_turn self.playing = True while self.playing: self.check_filled() self.clock.tick(60) self.events() self.update() self.draw() if self.winning(self.grid.grid, config.player_mark): self.player_win = True self.playing = False elif self.winning(self.grid.grid, config.AI_mark): self.playing = False def events(self): self.click = False for event in pygame.event.get(): if event.type == pygame.QUIT: self.playing = False self.running = False pygame.quit() sys.exit() if event.type == pygame.MOUSEBUTTONDOWN: self.click = True self.mouse_pos = pygame.mouse.get_pos() self.keys = pygame.key.get_pressed() if self.keys[pygame.K_ESCAPE]: self.playing = False self.running = False pygame.quit() sys.exit() def update(self): if self.turn == config.AI_turn: self.grid.update(config.AI_mark) self.turn += 1 self.turn = self.turn % 2 elif self.turn == config.player_turn: if self.click: self.grid.update(config.player_mark) self.turn += 1 self.turn = self.turn % 2 def draw(self): self.game_display.fill(config.bg_color) self.grid.draw_grid(self.game_display) pygame.display.update() def check_filled(self): self.filled_spots = 0 for columns in range(self.grid.cols): if self.grid.open_spot(columns) == "XD": self.filled_spots += 1 if self.filled_spots == self.grid.cols: self.playing = False def winning(self, grid, piece): # Horizontal for row in range(self.grid.row): for column in range(self.grid.cols - (config.inarow - 1)): self.window = list(grid[row, column:column + config.inarow]) if self.window.count(piece) == config.inarow: return True # Vertical for row in range(self.grid.row - (config.inarow - 1)): for column in range(self.grid.cols): self.window = list(grid[row:row + config.inarow, column]) if self.window.count(piece) == config.inarow: return True # Positive diagonal for row in range(self.grid.row - (config.inarow - 1)): for column in range(self.grid.cols - (config.inarow - 1)): self.window = list(grid[range(row, row + config.inarow), range(column, column + config.inarow)]) if self.window.count(piece) == config.inarow: return True # Negative diagonal for row in range((config.inarow - 1), self.grid.row): for column in range(self.grid.cols - (config.inarow - 1)): self.window = list(grid[range(row, row - config.inarow, -1), range(column, column + config.inarow)]) if self.window.count(piece) == config.inarow: return True return False def start_screen(self): self.start = False while not self.start: self.events() self.start = gui.start_screen(self.mouse_pos, self.click, self.game_display) def restart(self): self.reset = False if self.player_win: self.winner = config.player_mark else: self.winner = config.AI_mark while not self.reset: self.events() self.choose = gui.restart_screen(self.mouse_pos, self.click, self.game_display, self.winner) if self.choose: return self.choose elif self.choose == False: return self.choose else: pass def game_over(self): pass def difficulty_screen(self): while not self.difficulty: self.events() self.difficulty = gui.difficulty_screen(self.mouse_pos, self.click, self.game_display)
def test_getitem(): row0 = Grid(1, None, None) assert row0[0] == 1
from board import Square, Grid, Column SIZE = 3 grid = Grid([Column([(i, j) for j in range(SIZE)]) for i in range(SIZE)]) print grid
def create_graphic_board(size=SIZE): return Grid([ Column([Tile(DISPLAY_SURF, x, y) for y in range(size)]) for x in range(size) ])