Esempio n. 1
0
 def init_board(self):
     bomb_counter = 0
     bombs_coordinate = []
     for _ in range(0, self.nb_square):
         self.board.append([Square() for _ in range(0, self.nb_square)])
     while not bomb_counter == self.nb_bomb:
         r1 = randint(0, self.nb_square - 1)
         r2 = randint(0, self.nb_square - 1)
         if not self.board[r1][r2].is_bomb():
             self.board[r1][r2].set_bomb()
             bombs_coordinate.append((r1, r2))
             bomb_counter += 1
     for bomb in bombs_coordinate:
         if bomb[0] != 0:
             self.board[bomb[0] - 1][bomb[1]].increment_content()
             if bomb[1] != 0:
                 self.board[bomb[0] - 1][bomb[1] - 1].increment_content()
             if bomb[1] != (self.nb_square - 1):
                 self.board[bomb[0] - 1][bomb[1] + 1].increment_content()
         if bomb[0] != (self.nb_square - 1):
             self.board[bomb[0] + 1][bomb[1]].increment_content()
             if bomb[1] != 0:
                 self.board[bomb[0] + 1][bomb[1] - 1].increment_content()
             if bomb[1] != (self.nb_square - 1):
                 self.board[bomb[0] + 1][bomb[1] + 1].increment_content()
         if bomb[1] != 0:
             self.board[bomb[0]][bomb[1] - 1].increment_content()
         if bomb[1] != (self.nb_square - 1):
             self.board[bomb[0]][bomb[1] + 1].increment_content()
Esempio n. 2
0
 def init_grid(self):
     range_grid_size = range(0, 9)
     must_generate = True
     while must_generate:
         must_generate = False
         ts = time.time()
         # make sure to reset if grid was stuck and needed to be generated again
         self.grid = []
         # grid full of 0
         for _ in range_grid_size:
             row = [Square(0) for _ in range_grid_size]
             self.grid.append(row)
         # Replacing all the 0 by good values
         for r in range_grid_size:
             should_restart = True
             while should_restart:
                 should_restart = False
                 if time.time() > ts + 2:
                     print("Grid is stuck, generating a new one...")
                     must_generate = True
                     break
                 for c in range_grid_size:
                     self.grid[r][c].value = 0
                     int_available = [i for i in range(1, 10)]
                     while self.grid[r][c].value == 0:
                         random_int = random.choice(int_available)
                         coordinates = (r, c)
                         is_not_in_col = self.check_column(coordinates, random_int)
                         is_not_in_square_fam = self.check_square_family(coordinates, random_int)
                         if random_int not in [val.value for val in self.grid[r]] and is_not_in_col and is_not_in_square_fam:
                             self.grid[r][c] = Square(random_int)
                         else:
                             int_available.remove(random_int)
                         if len(int_available) == 0:
                             should_restart = True
                             break
                     if should_restart:
                         break
             if must_generate:
                 break
Esempio n. 3
0
    def reset_game(self):
        self.score = 0
        self.finished = False
        self.count = 0

        self.resample = 20
        self.resample_limit = 50
        self.sample = [-1, -1]
        self.sample_repeat = 0

        snake_x, snake_y = randrange(3, self.cols - 3), randrange(
            3, self.rows - 3)
        snack_x, snack_y = snake_x, snake_y

        while [snack_x, snack_y] == [snake_x, snake_y]:
            [snack_x, snack_y] = [randrange(self.cols), randrange(self.rows)]

        if self.snake:
            del self.snake
        if self.snack:
            del self.snack

        init_dir_x = choice([0, choice([1, -1])])

        if init_dir_x != 0:
            init_dir_y = 0
        else:
            init_dir_y = choice([1, -1])

        self.snake = Snake(snake_x, snake_y, init_dir_x, init_dir_y, GREEN,
                           self.squareHeight, self.squareWidth)

        self.snack = Square(snack_x, snack_y, 0, 0, RED, self.squareHeight,
                            self.squareWidth)

        self.snake_snack_dist = math.sqrt((snake_x - snack_x)**2 +
                                          (snake_y - snack_y)**2)
Esempio n. 4
0
 def Create_Squares(self):
     self.squares = []
     i = 0
     while i < 40:
         self.squares.append(Square(i))
         i += 1
Esempio n. 5
0
 def __init__(self, x, y, dir_x, dir_y, colour, height, width):
     self.head = Square(x, y, dir_x, dir_y, colour, height, width)
     self.body = [self.head]
     self.turns = {}
     self.square_height = height
     self.square_width = width
Esempio n. 6
0
 def addSquare(self):
     prev_square = self.body[-1]
     self.body.append(Square(prev_square.x - prev_square.dir_x, prev_square.y - prev_square.dir_y,
                             prev_square.dir_x, prev_square.dir_y, GREEN, self.square_height, self.square_width))
Esempio n. 7
0
class Board(object):
    snake = None
    snack = None

    def __init__(self,
                 surface,
                 width,
                 height,
                 rows,
                 cols,
                 colour,
                 human_playing=True,
                 timeout=200):
        self.surface = surface
        self.colour = colour
        self.human_playing = human_playing
        self.timeout = timeout

        self.width = width
        self.height = height
        self.rows = rows
        self.cols = cols
        self.squareWidth = self.width // self.cols
        self.squareHeight = self.height // self.rows

        self.reset_game()

    def update_network(self, network):
        self.network = network

    def reset_game(self):
        self.score = 0
        self.finished = False
        self.count = 0

        self.resample = 20
        self.resample_limit = 50
        self.sample = [-1, -1]
        self.sample_repeat = 0

        snake_x, snake_y = randrange(3, self.cols - 3), randrange(
            3, self.rows - 3)
        snack_x, snack_y = snake_x, snake_y

        while [snack_x, snack_y] == [snake_x, snake_y]:
            [snack_x, snack_y] = [randrange(self.cols), randrange(self.rows)]

        if self.snake:
            del self.snake
        if self.snack:
            del self.snack

        init_dir_x = choice([0, choice([1, -1])])

        if init_dir_x != 0:
            init_dir_y = 0
        else:
            init_dir_y = choice([1, -1])

        self.snake = Snake(snake_x, snake_y, init_dir_x, init_dir_y, GREEN,
                           self.squareHeight, self.squareWidth)

        self.snack = Square(snack_x, snack_y, 0, 0, RED, self.squareHeight,
                            self.squareWidth)

        self.snake_snack_dist = math.sqrt((snake_x - snack_x)**2 +
                                          (snake_y - snack_y)**2)

    def redraw_surface(self):
        self.surface.fill((0, 0, 0))
        self.draw_grid()

        if self.human_playing:
            self.snake.update_dir_human()
            self.move_snake_human()
        else:
            self.snake.update_dir_ai(self.network, self.snack.x, self.snack.y,
                                     self.cols, self.rows)
            self.move_snake_ai()

        self.draw_snake()
        self.draw_snack()
        pygame.display.update()

    def draw_grid(self):
        for x in range(1, self.rows):
            pygame.draw.line(self.surface, WHITE, (0, x * self.squareHeight),
                             (self.width, x * self.squareHeight))

        for y in range(1, self.cols):
            pygame.draw.line(self.surface, WHITE, (y * self.squareWidth, 0),
                             (y * self.squareWidth, self.height))

    def move_snake_ai(self):
        self.snake.move()

        if self.count > self.timeout:
            self.finished = True

        elif not self.snake.valid() or not self.snake_inbounds():
            self.finished = True

        elif [self.snake.head.x,
              self.snake.head.y] == [self.snack.x, self.snack.y]:
            self.score += 100
            self.count = 0
            self.snake.addSquare()
            self.random_snack()

        else:
            new_dist = math.sqrt((self.snake.head.x - self.snack.x)**2 +
                                 (self.snake.head.y - self.snack.y)**2)

            if new_dist > self.snake_snack_dist:
                self.score -= 3
            else:
                self.score += 2

            self.snake_snack_dist = new_dist

        if [self.snake.head.x, self.snake.head.y] == self.sample:
            self.resample = 0
            self.sample_repeat += 1

        elif self.resample == self.resample_limit:
            self.resample = 0
            self.sample_repeat = 0
            self.sample = [self.snake.head.x, self.snake.head.y]

        else:
            self.resample += 1

        if self.sample_repeat >= 3:
            self.score -= 50
            self.finished = True

        self.count += 1

    def move_snake_human(self):
        self.snake.move()

        if not self.snake.valid() or not self.snake_inbounds():
            message_box(
                "You Died!",
                "Your final score was: {}, Start Over?".format(self.score))
            self.reset_game()

        elif [self.snake.head.x,
              self.snake.head.y] == [self.snack.x, self.snack.y]:
            self.score += 100
            self.snake.addSquare()
            self.random_snack()

    def snake_inbounds(self):
        if self.snake.head.x < 0 or self.snake.head.x >= self.cols or self.snake.head.y < 0 or self.snake.head.y >= self.rows:
            return False

        return True

    def draw_snake(self):
        self.snake.draw(self.surface)

    def draw_snack(self):
        self.snack.draw(self.surface)

    def random_snack(self):
        new_x = self.snake.head.x
        new_y = self.snake.head.y

        while [new_x, new_y] in map(lambda square: [square.x, square.y],
                                    self.snake.body):
            new_x = randrange(self.cols)
            new_y = randrange(self.rows)

        self.snack.x = new_x
        self.snack.y = new_y
Esempio n. 8
0
 def Test_Create_Lot(self):
     lot = Square(1)
     unittest.assertTrue(type(lot.Lot) == Lot)
Esempio n. 9
0
 def Test_Create_Prison(self):
     prison = Square(30)
     unittest.assertTrue(prison.Prison == True)
Esempio n. 10
0
 def Test_Create_tax(self):
     tax = Square(4)
     unittest.assertTrue(tax.Tax == 200)
Esempio n. 11
0
 def Test_Create_Commu(self):
     commu = Square(2)
     unittest.assertTrue(commu.Event == 'Caisse de Communauté')
Esempio n. 12
0
 def Test_Create_Chance(self):
     chance = Square(7)
     unittest.assertTrue(chance.Event == 'Carte Chance')