def main(): initialize_database() game = Game() while game.running: game.curr_menu.display_menu() game.run()
def _handle_events_in_menu(self, event): if event.type == pygame.MOUSEBUTTONDOWN: if event.button == 1: if event.pos[0] > 273 and event.pos[0] < 383 and event.pos[ 1] > 106 and event.pos[1] < 122: self.view = "game" self.game = Game() elif event.pos[0] > 270 and event.pos[0] < 325 and event.pos[ 1] > 155 and event.pos[1] < 173: self.view = "stats"
def initialize(self, *args): self.human_players = args[0] self.game = Game(self.human_players) self.piece_repository = PieceRepository(settings.BOARD_RECT) self.board = Board(self.game, self.piece_repository, settings.BOARD_RECT) self.inventory = Inventory(self.game, self.piece_repository, settings.INVENTORY_RECT) self.initialized = True
def test_open_cell_and_3bvs(self): game = Game(5, 5, 1) game.mine_map = np.array([ [1, 1, 0, 0, 1], [0, 0, 0, 1, 0], [0, 0, 0, 0, 0], [1, 0, 0, 0, 1], [0, 0, 0, 0, 0], ]) game.fill_numbers() game.open_cell((4, 2)) self.assertEqual(game.get_3bvs(), 10)
def test_exceptions(): # Too few mines self.assertRaises(InvalidGame, lambda: Game(10, 10, 0)) self.assertRaises(InvalidGame, lambda: Game(10, 10, -5)) # Too many mines self.assertRaises(InvalidGame, lambda: Game(10, 10, 100)) self.assertRaises(InvalidGame, lambda: Game(10, 10, 150)) # Init mine outside range self.assertRaises(InvalidGame, lambda: Game(10, 10, 50, (-5, 5))) self.assertRaises(InvalidGame, lambda: Game(10, 10, 50, (5, -5))) self.assertRaises(InvalidGame, lambda: Game(10, 10, 50, (5, 15))) self.assertRaises(InvalidGame, lambda: Game(10, 10, 50, (15, 5)))
def game_start(top): global game, name_label, window window = top for widget in top.winfo_children(): widget.destroy() game = Game(["AHP", "PKMS"]) pieces = list(game.get_usable_piece_codes()) piece_buttons = [] for i in range(len(pieces)): piece = pieces[i] piece_button = Button( top, text=piece, command=lambda i=i: set_piece(pieces[i], piece_buttons[i])) piece_buttons += [piece_button] piece_button.grid(row=0, column=i) grid = game.cells grid_buttons = [] for i in range(4): button_row = [] for j in range(4): cell = grid[i][j] cell_text = ' ' if cell is not None: cell_text = cell.code grid_button = Button( top, text=cell_text, command=lambda i=i, j=j: put_piece(i, j, grid_buttons[i][j])) grid_button.grid(row=i + 3, column=j + 6) button_row += [grid_button] grid_buttons += [button_row] name = game.current_name() name_label = Label(top, text=name) name_label.grid(row=1, column=5)
def high_start(top): scores = Game.read_high_score() for widget in top.winfo_children(): widget.destroy() button = Button(top, text="back", command=m_m.MainMenu.start) for i in range(len(scores)): label = Label(top, text=str(scores[i])) label.grid(row=i + 1) button.grid(row=i + 2)
def test_bfs_number_cell(self): game = Game(5, 5, 1) game.mine_map = np.array([ [1, 1, 0, 0, 1], [0, 0, 0, 1, 0], [0, 0, 0, 0, 0], [1, 0, 0, 0, 1], [0, 0, 0, 0, 0], ]) game.fill_numbers() expected = {(3, 2), (3, 3), (3, 1), (2, 1), (2, 3), (4, 3), (2, 2), (4, 2), (4, 1)} actual1 = game.bfs((4, 2)) actual2 = game.bfs((3, 2)) self.assertEqual(actual1, actual2) self.assertEqual(actual1, expected)
class TestGame(unittest.TestCase): def setUp(self): self.game = Game() self.game_with_full_board = Game(225, 15) self.game_with_empty_board = Game(0, 15) def test_board_is_correct_width(self): self.assertEqual(self.game.size, len(self.game.board[0])) def test_board_is_correct_hight(self): self.assertEqual(self.game.size, len(self.game.board)) def test_board_has_correct_amount_of_bombs(self): bomb_count = 0 for row in self.game.board: for block in row: if block.bomb: bomb_count += 1 self.assertEqual(self.game.bombs, bomb_count) def test_handles_rightclick(self): self.game.handle_rightclick_on_board(4, 6) self.assertEqual(True, self.game.board[6][4].flag) def test_handles_leftclick_on_board_if_bomb(self): self.assertEqual( self.game_with_full_board.handle_leftclick_on_board(0, 6), False) def test_count_bombs(self): self.game_with_full_board._count_bombs_around_block(0, 0) self.assertEqual(self.game_with_full_board.board[0][0].bombs_around, 3) self.game_with_full_board._count_bombs_around_block(14, 0) self.assertEqual(self.game_with_full_board.board[0][14].bombs_around, 3) self.game_with_full_board._count_bombs_around_block(0, 14) self.assertEqual(self.game_with_full_board.board[14][0].bombs_around, 3) self.game_with_full_board._count_bombs_around_block(14, 14) self.assertEqual(self.game_with_full_board.board[14][14].bombs_around, 3) def test_handles_leftclick_on_board_if_not_bomb(self): self.assertEqual( self.game_with_empty_board.handle_leftclick_on_board(5, 7), True) def test_cant_open_flagged_block(self): self.game.handle_rightclick_on_board(4, 6) self.game.handle_leftclick_on_board(4, 6) self.assertEqual(self.game.board[6][4].open, False) def test_wont_open_neighbours_if_bomb_neighbour(self): self.game.board[6][5].bomb = True self.game.handle_leftclick_on_board(4, 6) self.assertEqual(self.game.board[6][3].open, False) def test_count_bomb_as_flagged_if_rightclikked(self): self.game_with_full_board.handle_rightclick_on_board(4, 6) self.assertEqual(self.game_with_full_board.bombs_without_flag, 224) def test_count_bomb_as_not_flagged_if_rightclikked(self): self.game_with_full_board.handle_rightclick_on_board(4, 6) self.game_with_full_board.handle_rightclick_on_board(4, 6) self.assertEqual(self.game_with_full_board.bombs_without_flag, 225)
options = [1, 2, 3] def printBoard(board): print() print('-' * 13) print('| ' + board[0] + ' | ' + board[1] + ' | ' + board[2] + ' |') print('-' * 13) print('| ' + board[3] + ' | ' + board[4] + ' | ' + board[5] + ' |') print('-' * 13) print('| ' + board[6] + ' | ' + board[7] + ' | ' + board[8] + ' |') print('-' * 13) print() game = Game() index = 0 print('-' * 20) print('Available opions:') print('1 - Simple') print('2 - Impossible') print('3 - Play against a friend') print('-' * 20) print() playingOption = -1 while (not (playingOption in options)): if (not (playingOption in options) and playingOption != -1): print('Invalid input.') playingOption = int(input('Select an option: [1,2,3]: '))
from datetime import datetime import numpy as np from logic.engine.satsolver import SatSolverEngine from logic.game import Game from logic.player import Player # logging.basicConfig(level=logging.DEBUG) np.set_printoptions(linewidth=160) count = 0 with open("foo.csv", 'w') as fw: while True: count += 1 game = Game(24, 30, 217, (0, 0)) solver = SatSolverEngine() player = Player(game, solver) start = datetime.now() play = player.play() if play == 1: print("Done in {} tries".format(count)) count = 0 elapsed = (datetime.now() - start).total_seconds() print("Clear {}% in {}".format(100 * play, elapsed)) fw.write("{},{}\n".format(play, elapsed))
maps.map1.world_size = (32, 32) maps.map1.chunk = (1322, 43944) maps.map1.max_number_of_roads = 5 #5 maps.map1.max_number_of_rivers = 5 #5 maps.map1.village_homogeneity = 0.1 maps.map1.seed_static_objects = 15 maps.map1.zoom_cell_sizes = [32] ##for map_initializer in [maps.map1, maps.map0, maps.map2, maps.map3]: ## me = map_initializer.configure_map_editor(FPS) #me = "Map Editor" ## app.get_screen().fill((0,0,0)) #### me.show_hmap() map_initializer = maps.map1 me = map_initializer.configure_map_editor(FPS) game = Game(me) humans = Race("Green team", "human", LUNAR, me, "green", team=1) #LUNAR, STELLAR or SOLAR ##humans.base_material_cost["grass"] = 2 ##humans.base_material_cost["forest"] = 5 humans.dist_factor = 10 ##humans.base_terrain_attack["grass"] = 2. ##humans["infantry"].material_cost["sand"] = 4 ##humans["infantry"].terrain_attack["snow"] = 0.8 humans.finalize() #always call this function to finish initialize a race !!! humans2 = Race("Red team", "human", SOLAR, me, "red", team=2) ##humans2.base_material_cost["forest"] = 10 ##humans2.base_terrain_attack["grass"] = 0.8 humans2.dist_factor = 10
pygame.display.flip() e = thorpy.make_button("Generate another map", refresh) me, img, mi = generate_map() ei = thorpy.Clickable(elements=[thorpy.Image(img)]) ei.fit_children() ei.user_func = thorpy.functions.quit_menu_func els = [e, ei] me.screen.fill((255, 255, 255)) pygame.display.flip() thorpy.store("screen", els) m = thorpy.Menu(els) m.play() game = Game(me) humans = Race("Coco", "human", LUNAR, me, "green", team=1) #LUNAR, STELLAR or SOLAR humans.dist_factor = 10 humans.finalize() #always call this function to finish initialize a race !!! humans2 = Race("Turtudur Buldur", "human", LUNAR, me, "blue", team=2) humans2.dist_factor = 10 humans2.finalize() players = [Player(1, humans.name, humans), Player(2, humans2.name, humans2)] game.set_players(players) #<fast> : quality a bit lower if true, loading time a bit faster. #<use_beach_tiler>: quality much better if true, loading much slower. Req. Numpy!
import os # tylko, jesli skrypt zostal uruchomiony if __name__ == '__main__': import os import sys from path import root_path from logic.game import Game game = Game() game.run()
def test_init_cell(): for i in range(3): for j in range(6): for _ in range(1, 18): game = Game(3, 6, _, (i, j)) self.assertEqual(game.mine_map[i, j], 0)
def test_no_init_cell(): for i in range(1, 16): game = Game(3, 6, i) self.assertEqual(game.mine_map.sum(), i)
''' import sys from random import shuffle from numpy import mean from logic.game import Game from utils.constants import Modes mode = sys.argv[1] if mode == Modes.PLAY: cardRange = int(sys.argv[2]) numLives = int(sys.argv[3]) powerTries = int(sys.argv[4]) names = sys.argv[5:] game = Game(names, cardRange, numLives, powerTries) game.playGame() elif mode == Modes.TRIAL: numTrials = int(sys.argv[2]) writeStep = int(sys.argv[3]) writeFile = sys.argv[4] cardRange = int(sys.argv[5]) numLives = int(sys.argv[6]) powerTries = int(sys.argv[7]) names = sys.argv[8:] wins = {name: 0 for name in names} finishes = {name: [] for name in names} for i in range(numTrials): if i != 0 and i % writeStep == 0: with open("count.txt", "a") as f: f.write(
def setUp(self): self.game = Game()
def setUp(self): self.game = Game() self.game_with_full_board = Game(225, 15) self.game_with_empty_board = Game(0, 15)
maps.map1.chunk = (1322, 43944) maps.map1.max_number_of_roads = 5 #5 maps.map1.max_number_of_rivers = 5 #5 maps.map1.village_homogeneity = 0.1 maps.map1.seed_static_objects = 15 maps.map1.zoom_cell_sizes = [32] ##for map_initializer in [maps.map1, maps.map0, maps.map2, maps.map3]: ## me = map_initializer.configure_map_editor(FPS) #me = "Map Editor" ## app.get_screen().fill((0,0,0)) #### me.show_hmap() map_initializer = maps.map0 me = map_initializer.configure_map_editor(FPS) game = Game(me) humans = Race("Green team", "human", LUNAR, me, "green", team=1) #LUNAR, STELLAR or SOLAR ##humans.base_material_cost["grass"] = 2 ##humans.base_material_cost["forest"] = 5 humans.dist_factor = 10 ##humans.base_terrain_attack["grass"] = 2. ##humans["infantry"].material_cost["sand"] = 4 ##humans["infantry"].terrain_attack["snow"] = 0.8 humans.finalize() #always call this function to finish initialize a race !!! humans2 = Race("Red team", "human", SOLAR, me, "red", team=2) ##humans2.base_material_cost["forest"] = 10 ##humans2.base_terrain_attack["grass"] = 0.8 humans2.dist_factor = 10
class Ui: def __init__(self): pygame.init() self.font = pygame.font.SysFont("Arial", 24) self.font2 = pygame.font.SysFont("Arial", 19) self.screen = pygame.display.set_mode((640, 480)) pygame.display.set_caption("Minesweeper") self.view = "menu" self.game = None self.block_size = 20 self.game_won = None self.repo = game_repo self._run() def _run(self): while True: self._draw_view() self._handle_events() def _handle_events(self): for event in pygame.event.get(): if event.type == pygame.QUIT: exit() if self.view == "menu": self._handle_events_in_menu(event) elif self.view == "game": self._handle_events_in_game(event) elif self.view == "stats": self._handle_events_in_stats(event) elif self.view == "game_over": self._handle_events_in_game_over(event) def _handle_events_in_menu(self, event): if event.type == pygame.MOUSEBUTTONDOWN: if event.button == 1: if event.pos[0] > 273 and event.pos[0] < 383 and event.pos[ 1] > 106 and event.pos[1] < 122: self.view = "game" self.game = Game() elif event.pos[0] > 270 and event.pos[0] < 325 and event.pos[ 1] > 155 and event.pos[1] < 173: self.view = "stats" def _handle_events_in_game(self, event): if event.type == pygame.MOUSEBUTTONDOWN: if event.button == 1: if event.pos[0] > 5 and event.pos[0] < 55 and event.pos[ 1] > 10 and event.pos[1] < 33: self.view = "menu" elif event.pos[0] > 2 * self.block_size and event.pos[ 0] < 300 + (2 * self.block_size) and event.pos[ 1] > 4 * self.block_size and event.pos[1] < ( 4 * self.block_size) + 300: x = event.pos[0] - 20 - self.block_size y = event.pos[1] - 60 - self.block_size x = x // 20 y = y // 20 if not self.game.handle_leftclick_on_board(x, y): self.game_won = False self.view = "game_over" elif event.button == 3: if event.pos[0] > 2 * self.block_size and event.pos[ 0] < 300 + (2 * self.block_size) and event.pos[ 1] > 4 * self.block_size and event.pos[1] < ( 4 * self.block_size) + 300: x = event.pos[0] - 20 - self.block_size y = event.pos[1] - 60 - self.block_size x = x // 20 y = y // 20 self.game.handle_rightclick_on_board(x, y) if self.game.bombs_without_flag == 0: self.game_won = True self.view = "game_over" def _draw_view(self): self.screen.fill((255, 255, 255)) if self.view == "menu": self._draw_menu() elif self.view == "game": self._draw_game() elif self.view == "stats": self._draw_stats() elif self.view == "game_over": self._draw_game_over() pygame.display.flip() def _draw_game(self): self._draw_grid() self._draw_board() text_minesweeper = self.font.render("Minesweeper", True, (0, 0, 0)) self.screen.blit(text_minesweeper, (280, 10)) text_back = self.font.render("Back", True, (0, 0, 0)) self.screen.blit(text_back, (5, 10)) def _draw_grid(self): for x in range(2 * self.block_size, 300 + (2 * self.block_size), self.block_size): for y in range(4 * self.block_size, (4 * self.block_size) + 300, self.block_size): rect = pygame.Rect(x, y, self.block_size, self.block_size) pygame.draw.rect(self.screen, (127, 127, 127), rect, 1) def _draw_board(self): for i in range(self.game.size): for j in range(self.game.size): if self.game.board[i][j].flag: flag = self.font2.render("F", True, (0, 0, 0)) self.screen.blit( flag, (20 + self.block_size + (j * self.block_size), 60 + self.block_size + (i * self.block_size))) elif self.game.board[i][j].open: num = self.font2.render( str(self.game.board[i][j].bombs_around), True, (0, 0, 0)) self.screen.blit( num, (20 + self.block_size + (j * self.block_size), 60 + self.block_size + (i * self.block_size))) def _draw_menu(self): text_new_game = self.font.render("New game", True, (0, 0, 0)) self.screen.blit(text_new_game, (270, 100)) text_stats = self.font.render("Stats", True, (0, 0, 0)) self.screen.blit(text_stats, (270, 150)) def _draw_stats(self): text_back = self.font.render("Back", True, (0, 0, 0)) self.screen.blit(text_back, (5, 10)) wins = self.repo.get_win_amound() loses = self.repo.get_lost_amound() text_wins_loses = self.font.render("won/lost", True, (0, 0, 0)) self.screen.blit(text_wins_loses, (270, 100)) color = (0, 0, 0) # white if wins > loses: color = (0, 255, 0) # green elif wins < loses: color = (255, 0, 0) # red text_wins_loses_amount = self.font.render(f"{wins}/{loses}", True, color) self.screen.blit(text_wins_loses_amount, (270, 150)) def _handle_events_in_stats(self, event): if event.type == pygame.MOUSEBUTTONDOWN: if event.button == 1: if event.pos[0] > 5 and event.pos[0] < 55 and event.pos[ 1] > 10 and event.pos[1] < 33: self.view = "menu" def _draw_game_over(self): if self.game_won: text_game_over = self.font.render("You won", True, (0, 0, 0)) elif not self.game_won: text_game_over = self.font.render("You lost", True, (0, 0, 0)) self.screen.blit(text_game_over, (270, 100)) def _handle_events_in_game_over(self, event): timestamp = datetime.now() if self.game_won: win_or_loss = "Win" else: win_or_loss = "Loss" self.repo.add_game(timestamp, win_or_loss) sleep(4) self.view = "menu"
def start_server(): game = Game() threading._start_new_thread(start_listener, (game,))
for button_genmap in buttons: button_genmap.blit() pygame.display.flip() button_genmap = thorpy.make_button("Generate another map", refresh) me, img, mi = generate_map() button_img_map = thorpy.Clickable(elements=[thorpy.Image(img)]) button_img_map.fit_children() button_img_map.user_func = thorpy.functions.quit_menu_func buttons = [button_genmap,button_img_map] thorpy.store("screen", buttons) me.screen.fill((255,255,255)) pygame.display.flip() m = thorpy.Menu(buttons) m.play() game = Game(me) #<fast> : quality a bit lower if true, loading time a bit faster. #<use_beach_tiler>: quality much better if true, loading much slower. #<load_tilers> : Very slow but needed if you don't have Numpy but still want HQ. game.build_map(mi, fast=False, use_beach_tiler=True, load_tilers=False) me.set_zoom(level=0) #adding an object that must be drawn before the others on the same cell: game.set_fire((10,10)) #adding a smoke: game.add_smoke("large", (10,10)) print(mi.seed_static_objects) m = thorpy.Menu(me.e_box,fps=me.fps) m.play()
FPS = 80 pygame.init() screen = pygame.display.set_mode((W, H)) mi = MapInitializer("First demo map") mi.chunk = (random.randint(0, 1000), random.randint(0, 1000)) wm, hm = 32, 16 mi.world_size = (wm, hm) mi.set_terrain_type(terrain_medium, colorscale_normal) mi.max_number_of_roads = random.randint(0, 6) mi.max_number_of_rivers = random.randint(0, 6) mi.zoom_cell_sizes = [32, 16] mi.seed_static_objects = random.randint(0, 1000) me = mi.configure_map_editor(FPS) #me = "Map Editor" img = me.get_hmap_img((wm * 10, hm * 10)) game = Game(me) #<fast> : quality a bit lower if true, loading time a bit faster. #<use_beach_tiler>: quality much better if true, loading much slower. #<load_tilers> : Very slow but needed if you don't have Numpy but still want HQ. game.build_map(mi, fast=False, use_beach_tiler=True, load_tilers=False) me.set_zoom(level=0) ##game.set_fire((3,7)) ##game.add_smoke("large",(4,4)) clock = pygame.time.Clock() done = False while not done: clock.tick(FPS) screen.fill((0, 0, 0))
from logic.ai import AI from logic.ai_v2 import AIV2 from logic.game_essentials import LINES from logic.ai_v2 import CPU_NAME boardSize = 4 # bestParams = [1, 4, 20, 10**10] # bestUserParams = [1, 4, 20, 10**10] bestParams = [10, 31, 80, 10**10] bestUserParams = [10, 31, 80, 10**10] user = User() cpu = AI(boardSize, CPU_NAME, user.getName(), bestParams, bestUserParams, LINES) game = Game(user, cpu) game.play() # Playing the machines against each other # while True: # # bar = random.randint(100, 1000000) # champ = AI(boardSize, 'Champ', 'Challenger', bestParams, bestUserParams, LINES) # challengerParams = [] # lastNum = 1 # first_param = random.randint(1, 10) # second_param = random.randint(first_param, 100) # third_param = random.randint(second_param, 1000) # challengerUserParams = [first_param, second_param, third_param, 10 ** 10] # # print(challengerUserParams)