예제 #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()
예제 #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
예제 #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)
예제 #4
0
 def Create_Squares(self):
     self.squares = []
     i = 0
     while i < 40:
         self.squares.append(Square(i))
         i += 1
예제 #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
예제 #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))
예제 #7
0
 def Test_Create_Lot(self):
     lot = Square(1)
     unittest.assertTrue(type(lot.Lot) == Lot)
예제 #8
0
 def Test_Create_Prison(self):
     prison = Square(30)
     unittest.assertTrue(prison.Prison == True)
예제 #9
0
 def Test_Create_tax(self):
     tax = Square(4)
     unittest.assertTrue(tax.Tax == 200)
예제 #10
0
 def Test_Create_Commu(self):
     commu = Square(2)
     unittest.assertTrue(commu.Event == 'Caisse de Communauté')
예제 #11
0
 def Test_Create_Chance(self):
     chance = Square(7)
     unittest.assertTrue(chance.Event == 'Carte Chance')