Exemple #1
0
def quality(board):
    board = equivalenceClassRepr(board)
    if board in values:
        return values[board]

    tempBoard = GameBoard()
    tempBoard.setBoard(board)
    tempBoard.checkGameOver()

    if tempBoard.gameOver:
        values[board] = tempBoard.winner
        return tempBoard.winner

    turn = turnOf(board)
    defualtQuality = {Moves.X: Moves.O, Moves.O: Moves.X}[turn]
    for r in range(3):
        for c in range(3):
            if board[r][c] != Moves.NA:
                continue
            tempBoard.makeMove(r, c, turn)
            childQuality = quality(tempBoard.getBoardAsTuple())
            if childQuality == turn:
                values[board] = childQuality
                return childQuality
            if childQuality == Moves.NA:
                defualtQuality = Moves.NA
            tempBoard.board[r][c] = Moves.NA
    values[board] = defualtQuality
    return defualtQuality
Exemple #2
0
def play():
    """ Plays the actual game on the web with the learned data. """
    q_table = start_q_table('qtable.pickle')
    board = GameBoard()
    state = board.get_board()

    for _ in range(100):
        # Decide on action
        action = choose_action(q_table, state)
        state, done = board.click(action)

        if done:
            print('Game over!')
            return
Exemple #3
0
 def __init__(self):
     self.settings = GameSettings()
     self.scoreBoard = ScoreBoard(self.settings)
     self.gameBoard = GameBoard(self.settings)
     self.instructions = Instructions()
     self.endScreen = EndScreen()
     self.ballGrid = [[0 for x in range(self.settings.numberBallsH)]
                      for y in range(self.settings.numberBallsV)]
     self.level = 2
     self.score = 0
Exemple #4
0
def read_board():
    file_name = filedialog.askopenfilename()
    if not file_name:
        return

    with open(file_name, "r") as file:
        line = file.readline()
        split_line = line.split(",")
        width = int(split_line[0].strip())
        height = int(split_line[1].strip())
        board = GameBoard((width, height))

        for line in file:
            split_line = line.split(",")
            x = int(split_line[0].strip())
            y = int(split_line[1].strip())
            is_alive = split_line[2].strip() == "True"

            board.set_cell(Point(x, y), is_alive)

    return board
def startGame():
    # create instances of mando'a and initialize the score to zero and what not

    # Creating our character
    jango = Mandalorian()

    os.system('clear')
    boardInstance = GameBoard(jango)

    #for i in range(10):
    while (1):
        gameEnd = gameLoop(boardInstance, jango)
        char = input.get()
        if char == 'q':
            gameEnd += endGame()
        else:
            mandoHandler(boardInstance, jango, char)

        if gameEnd == 1:
            break
Exemple #6
0
 def _clear_game(self):
     self.board = GameBoard(self.grid_size)
Exemple #7
0
    def _make_default_board(self):
        board = GameBoard(self.grid_size)

        # Glider at the top left
        board.set_cell(Point(1, 0), True)
        board.set_cell(Point(2, 1), True)
        board.set_cell(Point(0, 2), True)
        board.set_cell(Point(1, 2), True)
        board.set_cell(Point(2, 2), True)

        # Blinker at the top middle
        board.set_cell(Point(20, 2), True)
        board.set_cell(Point(20, 3), True)
        board.set_cell(Point(20, 4), True)

        return board
Exemple #8
0
class Game:
    def __init__(self, grid_size, display):
        self.grid_size = grid_size
        self.board = self._make_default_board()
        self.button_area_height = 100
        self.cell_size = 25, 25
        self.screen_size = width, height = self.grid_size[0] * self.cell_size[
            0], (self.grid_size[1] *
                 self.cell_size[1]) + self.button_area_height
        self.screen = display.set_mode(self.screen_size)
        self.display = display

        buttons = [
            GameButton(self._toggle_pause, "Pause"),
            GameButton(self._reset_game, "Reset"),
            GameButton(self._clear_game, "Clear"),
            GameButton(self._load_board, "Load"),
            GameButton(self._save_board, "Save"),
        ]

        self.button_bar = ButtonBar(self.screen_size, self.button_area_height,
                                    buttons)
        self.is_paused = False

    def _make_default_board(self):
        board = GameBoard(self.grid_size)

        # Glider at the top left
        board.set_cell(Point(1, 0), True)
        board.set_cell(Point(2, 1), True)
        board.set_cell(Point(0, 2), True)
        board.set_cell(Point(1, 2), True)
        board.set_cell(Point(2, 2), True)

        # Blinker at the top middle
        board.set_cell(Point(20, 2), True)
        board.set_cell(Point(20, 3), True)
        board.set_cell(Point(20, 4), True)

        return board

    def _load_board(self):
        new_board = gameBoardReaderWriter.read_board()
        if new_board:
            self.board = new_board

    def _save_board(self):
        gameBoardReaderWriter.write_board(self.board)

    def _toggle_pause(self):
        self.is_paused = not self.is_paused
        return self.is_paused

    def _reset_game(self):
        self.board = self._make_default_board()

    def _clear_game(self):
        self.board = GameBoard(self.grid_size)

    def run_game_loop(self, getGameEvent, setEventTimer):

        mouse_move_pos = (0, 0)

        MOVE_TO_NEXT_GENERATION = pygame.USEREVENT + 1
        setEventTimer(MOVE_TO_NEXT_GENERATION, 225)

        while True:
            mouse_down_pos = (-1, -1)
            for event in getGameEvent.get():
                if event.type == pygame.QUIT:
                    return
                if event.type == pygame.MOUSEBUTTONDOWN:
                    mouse_down_pos = event.pos
                if event.type == pygame.MOUSEMOTION:
                    mouse_move_pos = event.pos
                if event.type == MOVE_TO_NEXT_GENERATION:
                    if not self.is_paused:
                        self.board = self.board.next_generation()

            self.screen.fill(colors.GAME_BACKGROUND)

            board_render = BoardRender(self.screen, self.board, self.cell_size)
            board_render.mouse_move_test(mouse_move_pos)
            board_render.click_test(mouse_down_pos)
            board_render.draw()

            self.button_bar.mouse_move_test(mouse_move_pos)
            self.button_bar.click_test(mouse_down_pos)
            self.button_bar.draw(self.screen)

            self.display.flip()

            pygame.time.wait(20)
Exemple #9
0
def main():
    newGameBoard = GameBoard()
Exemple #10
0
def main():
    
    pygame.init()
    pygame.display.set_caption("Tetris")
    
    screen = pygame.display.set_mode((400,600), 0, 32)
    screen.fill((255, 255, 255))

    block_length = 20
    height = 25 # min 20
    width = 8 # min 8
    padding_left = 20
    padding_top = 20
    border_width = 5
    
    gameboard = GameBoard(width, height)

    font = pygame.font.SysFont('arail', 64)
    title_surface = font.render('TETRIS', True, (88, 87, 86))
    screen.blit(title_surface, (215, 50))

    small_font = pygame.font.SysFont('arail', 20)
    score_hint_surface = font.render('Score:', True, (0, 0, 0))
    score_surface = font.render('0', True, (0, 0, 0))
    screen.blit(score_hint_surface, (215, 150))
    screen.blit(score_surface, (215, 190))
    
    board_rect = ((padding_left, padding_top), (width * block_length + border_width * 2, height * block_length + border_width * 2))
    grid_origin = ((padding_left + border_width), (padding_top + height * block_length + border_width))
    clock = pygame.time.Clock()
    time_elapsed = 0
    
    
    running = True
    
    while running:
        for event in pygame.event.get():

            if event.type == pygame.QUIT:
                running = False

            if not gameboard.pause:
                if event.type == pygame.KEYDOWN and event.key == pygame.K_UP:
                    gameboard.rotate()
                if event.type == pygame.KEYDOWN and event.key == pygame.K_LEFT:
                    gameboard.move_left()
                if event.type == pygame.KEYDOWN and event.key == pygame.K_RIGHT:
                    gameboard.move_right()
            if event.type == pygame.KEYDOWN and event.key == pygame.K_p:
                gameboard.toggle_pause()

        time_elapsed += clock.tick(30)
        if time_elapsed > 70 * (11 - gameboard.speed):
            time_elapsed = 0
            change = gameboard.next_tick()
            if change['score_change']: 
                screen.fill((255, 255, 255), ((215, 190), (185, 60)))
                score_surface = font.render(str(gameboard.score), True, (0, 0, 0))
                screen.blit(score_surface, (215, 190))
            if change['dead']:
                running = False
                # TODO add game over hint and replay button
        screen.fill((0, 0, 0), board_rect)
        gameboard.draw(screen, grid_origin, block_length)
        pygame.display.flip()
Exemple #11
0
 def build(self):
     #init game view
     self.appData = GameBoard()
     return self.appData;
Exemple #12
0
 def newRound(self):
     self.board = GameBoard()
     self.turn = Moves.X
     if rnd.randint(0, 1) == 0:
         self.swapPlayers()
Exemple #13
0
 def __init__(self):
     self.XScore, self.OScore, self.ties = 0, 0, 0
     self.observers = []
     self.board = GameBoard()
     self.turn = Moves.X
Exemple #14
0
class Game:
    """This class represents a simple game class that notifies observers of changes. Unlike GameBoard which stored only the state of the board, this class
	stores information about the players and notifies them of any changes."""
    def __init__(self):
        self.XScore, self.OScore, self.ties = 0, 0, 0
        self.observers = []
        self.board = GameBoard()
        self.turn = Moves.X

    def registerPlayers(self, XPlayer, OPlayer):
        self.XPlayer = XPlayer
        self.OPlayer = OPlayer
        XPlayer.setMark(Moves.X)
        OPlayer.setMark(Moves.O)
        self.observers.extend([XPlayer, OPlayer])

    def register(self, *observers):
        self.observers.extend(observers)

    def notify(self, e):
        for observer in self.observers:
            observer.gameUpdate(e)

    def newRound(self):
        self.board = GameBoard()
        self.turn = Moves.X
        if rnd.randint(0, 1) == 0:
            self.swapPlayers()

    def swapPlayers(self):
        temp = self.XPlayer
        self.XPlayer = self.OPlayer
        self.OPlayer = temp
        temp = self.XScore
        self.XScore = self.OScore
        self.OScore = temp
        self.XPlayer.setMark(Moves.X)
        self.OPlayer.setMark(Moves.O)

    def askForMove(self):
        self.PlayerToPlay = {
            Moves.X: self.XPlayer,
            Moves.O: self.OPlayer
        }[self.turn]
        self.PlayerToPlay.makeMove()

    def play(self, player, row, col):
        if self.PlayerToPlay != player:
            return
        self.board.makeMove(row, col, self.turn)
        self.advanceTurn()
        self.notify(PlayerMoveEvent(player.getMark(), row, col))

    def advanceTurn(self):
        self.turn = {Moves.X: Moves.O, Moves.O: Moves.X}[self.turn]
        self.PlayerToPlay = {
            Moves.X: self.XPlayer,
            Moves.O: self.OPlayer
        }[self.turn]

    def run(self):
        if self.board.gameOver:
            if self.board.winner == Moves.X:
                self.XScore += 1
            elif self.board.winner == Moves.O:
                self.OScore += 1
            else:
                self.ties += 1
            self.notify(GameOverEvent(self.board.winner))
        else:
            self.askForMove()

    def isValid(self, row, col):
        return self.board.board[row][col] == Moves.NA

    def isGameOver(self):
        return self.board.gameOver

    def mainLoop(self, numberOfGames=100):
        while not isGameOver and numberOfGames > 0:
            self.run()
            numberOfGames -= 1
Exemple #15
0
    def test_blinker_oscillator(self):
        # https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life#Examples_of_patterns
        # Blinker is an oscillator with period 2

        board = GameBoard((10, 10))

        # Make a blinker starting vertical
        # -X-
        # -X-
        # -X-

        board.set_cell(Point(4, 1), True)
        board.set_cell(Point(4, 2), True)
        board.set_cell(Point(4, 3), True)

        # Move to next generation
        board = board.next_generation()

        # The blinker should switch to horizontal
        # ---
        # XXX
        # ---
        self.assertFalse(board.is_cell_alive(Point(4, 1)))
        self.assertFalse(board.is_cell_alive(Point(4, 3)))

        self.assertTrue(board.is_cell_alive(Point(3, 2)))
        self.assertTrue(board.is_cell_alive(Point(4, 2)))
        self.assertTrue(board.is_cell_alive(Point(5, 2)))

        # Move to next generation again
        board = board.next_generation()

        # The blinker should switch back to vertical
        # -X-
        # -X-
        # -X-
        self.assertTrue(board.is_cell_alive(Point(4, 1)))
        self.assertTrue(board.is_cell_alive(Point(4, 2)))
        self.assertTrue(board.is_cell_alive(Point(4, 3)))

        self.assertFalse(board.is_cell_alive(Point(3, 2)))
        self.assertFalse(board.is_cell_alive(Point(5, 2)))