Esempio n. 1
0
    def generate_new_block(self, x=0, y=0):
        #first test if a new block is valid
        test_block = Block.new_block(x, y, False)
        if detect_collisions(self.board, test_block):
            self.game_over()

        else:
            self.last_block = self.block
            self.block = Block.new_block(x, y)        
Esempio n. 2
0
    def get_best_move(self):
        if self.game_running:
            # print_matrix(self.board)
            #we test if this block is valid before using it
            test_block = Block.new_block(0, 0, False)
            moves = potential_moves(self.board, test_block)

            potential_boards = [potential_board for potential_board, _ in moves]
            potential_blocks = [potential_block for _, potential_block in moves]

            index = classifier.return_best_board(potential_boards, self.weights, rows + 1, cols)
            best_block = potential_blocks[index]

            best_block.y = 0
            self.block = best_block

            if not self.headless:   
                self.update_screen()

            if not fast_mode:
                while True:
                    if self.drop():
                        if not self.headless:        
                            time.sleep(interval_between_drop)
                            self.update_screen()

                    else:
                        break
            else:
                while True:
                    if not self.drop():
                        if not self.headless:        
                            self.update_screen()
                        break
Esempio n. 3
0
    def drop(self):
        if self.game_running:
            if drop_helper(self.board, self.block):
                self.block.y += 1
                return True

            #if collide, then add block to board and check if game over
            else:
                if detect_collisions(self.board, self.block):
                    self.game_over()

                self.board = add_block_to_board(self.board, self.block)
                #if not game over, then create new block

                time.sleep(0.5)
                self.clear_rows()
                self.check_level()

                # target_coords = self.generate_best_move()
                # print(target_coords[0][0])
                self.block = Block.new_block()
                # print(self.block.block_type)
                # print("______")
                return False
Esempio n. 4
0
 def init_game(self):
     self.score = 0
     self.level = 1
     self.lines_cleared = 0
     self.board = self.new_board()
     self.block = Block.new_block()