def test_can_win_on_empty_square_board(self):
   """Test whether _can_win function returns that a player can always win on an empty square board."""
   # Create empty tic-tac-toe game board
   board = TicTacToeBoard()
   # A perfect player is not guaranteed to win on an empty board
   self.assertFalse(board._can_win(1))
   self.assertFalse(board._can_win(2))
 def test_can_tie_on_empty_square_board(self):
   """Test whether _can_tie function returns that a player can tie on an empty square board."""
   # Create empty tic-tac-toe game board
   board = TicTacToeBoard()
   # A perfect player is guaranteed to tie on an empty board
   self.assertTrue(board._can_tie(1))
   self.assertTrue(board._can_tie(2))
 def test_get_winner_on_non_empty_player_1_wins_square_board(self):
   """Test whether get_winner function provides player 1 wins for a non-empty square board that player 1 won."""
   # Create non-empty tic-tac-toe game board that player 1 has won
   board = TicTacToeBoard()
   board.matrix[0] = [1, 0, 0]
   board.matrix[1] = [2, 1, 0]
   board.matrix[2] = [0, 2, 1]
   winner = board.get_winner()
   self.assertEqual(winner, 1)
 def test_get_winner_on_non_empty_game_not_over_square_board(self):
   """Test whether get_winner function provides game not over status for a non-empty square board."""
   # Create non-empty game-not-over tic-tac-toe game board
   board = TicTacToeBoard()
   board.matrix[0] = [1, 2, 0]
   board.matrix[1] = [0, 1, 0]
   board.matrix[2] = [0, 2, 0]
   winner = board.get_winner()
   # Make sure that the game is not over
   self.assertEqual(winner, board.GAME_WINNER_GAME_NOT_OVER)
 def test_get_winner_on_non_empty_tied_game_square_board(self):
   """Test whether get_winner function provides game tied status for a non-empty square board."""
   # Create non-empty tied tic-tac-toe game board
   board = TicTacToeBoard()
   board.matrix[0] = [1, 2, 1]
   board.matrix[1] = [1, 2, 1]
   board.matrix[2] = [2, 1, 2]
   winner = board.get_winner()
   self.assertEqual(board.GAME_WINNER_TIED, -1)
   self.assertEqual(winner, board.GAME_WINNER_TIED)
Exemplo n.º 6
0
 def test_players_name_right(self):
     self.my_tictactoe.tictactoeboard = TicTacToeBoard(5, '', '')
     names_ok = self.my_tictactoe.players_name_ok(max_size=10)
     self.assertFalse(names_ok)
     self.my_tictactoe.tictactoeboard = TicTacToeBoard(5, 'Test', 'Test2')
     names_ok = self.my_tictactoe.players_name_ok(max_size=10)
     self.assertTrue(names_ok)
     self.my_tictactoe.tictactoeboard.player2 = 'Test2Test2Test2Test2'
     names_ok = self.my_tictactoe.players_name_ok(max_size=10)
     self.assertFalse(names_ok)
 def test_get_lines_on_empty_square_board(self):
   """Test whether the _get_lines function provides the correct lines for an empty square board."""
   board = TicTacToeBoard()
   lines = board._get_lines()
   # Make sure there are the correct number of lines
   self.assertEqual(len(lines), board.column_count + board.row_count + 2)
   # Make sure all lines are empty
   emptyLine = [board.CELL_NO_PLAYER for i in xrange(board.column_count)]
   for line in lines:
     self.assertEqual(line, emptyLine)
 def test_can_win_on_non_empty_square_board_they_cannot_win(self):
   """Test whether _can_win function returns that a player can win on a non-empty square board they cannot win."""
   # Create non-empty tic-tac-toe game board that player 2 cannot win
   board = TicTacToeBoard()
   board.matrix[0] = [1, 2, 1]
   board.matrix[1] = [2, 1, 0]
   board.matrix[2] = [0, 0, 2]
   self.assertFalse(board._can_win(2))
   # Create non-empty tic-tac-toe game board that player 1 cannot win
   board = TicTacToeBoard()
   board.matrix[0] = [2, 1, 2]
   board.matrix[1] = [1, 2, 0]
   board.matrix[2] = [0, 0, 1]
   self.assertFalse(board._can_win(1))
Exemplo n.º 9
0
 def start(self, row_count=3, column_count=3):
   """Starts the game.
   
   Arguments:
   row_count -- Number of rows in tic-tac-toe board
   column_count -- Number of columns in tic-tac-toe board
   
   Side-Effects:
   Creates a tic-tac-toe game board with the specified number of rows and columns.
   Decides the order in which the players will take turns.
   Displays the game board to standard output and then alternates turns between
   the user and the computer player.
   During the user's turn, it prompts the user to input
   the coordinates with which to mark the game board.
   The user can also input 'q' to quit the game.
   If the game continues until competion, it indicates whether the player won, lost, or tied the game.
   The user should never win.
   
   """
   # Initialize game board
   self.board = TicTacToeBoard(row_count, column_count)
   # Initialize players
   self._initialize_players()
   # Welcome the user to the game
   self._display_welcome()
   # Let the players turns marking the game board
   self._take_turns()    
   # Display the game results
   self._display_results()
Exemplo n.º 10
0
 def test_draw_right_player(self):
     self.my_tictactoe.tictactoeboard = TicTacToeBoard(5, 'Test', 'Test2')
     self.my_tictactoe.set_game()
     self.assertEqual(self.my_tictactoe.whose_turn,
                      f"Vuoro: {self.my_tictactoe.tictactoeboard.player1}")
     self.my_tictactoe.tictactoeboard.whose_turn = 2
     self.my_tictactoe.draw_status()
     self.assertEqual(self.my_tictactoe.whose_turn,
                      f"Vuoro: {self.my_tictactoe.tictactoeboard.player2}")
 def test_get_lines_on_board(self):
   """Test whether the _get_lines function provides the correct lines for a non empty square board."""
   # Create non-empty square tic-tac-toe game board    
   board = TicTacToeBoard()
   board.matrix[0] = [1, 2, 0]
   board.matrix[1] = [0, 1, 0]
   board.matrix[2] = [0, 2, 0]
   lines = board._get_lines()
   # Make sure there are the correct number of lines
   self.assertEqual(board.column_count + board.row_count + 2, 8)
   self.assertEqual(len(lines), 8)
   # Make sure the lines are correct
   self.assertEqual(lines.count([0,0,0]), 1)
   self.assertEqual(lines.count([1,0,0]), 1)
   self.assertEqual(lines.count([1,2,0]), 1)
   self.assertEqual(lines.count([0,1,0]), 2)
   self.assertEqual(lines.count([1,1,0]), 1)
   self.assertEqual(lines.count([0,2,0]), 1)
   self.assertEqual(lines.count([2,1,2]), 1)
Exemplo n.º 12
0
    def start_game(self):
        """Call Tkinter Class where the user sets the names of the players and number of squares.
        If one of player names are incorrect, starts again with error message.
        Else calls set_game function
        """

        self.start_menu = StartMenu(self.result_text)

        num_squares, player1, player2 = self.get_game_variables()

        self.tictactoeboard = TicTacToeBoard(num_squares, player1, player2)

        if self.tictactoeboard.num_squares == 0:
            return

        if self.players_name_ok(self.start_menu.name_max_size) is False:
            self.result_text = 'Virheellinen pelaajan nimi'
            self.start_game()
        else:
            self.set_game()
Exemplo n.º 13
0
def main():
    global inputmanager,screen,board
    #init graphics
    init()
    size = (150,150)
    screen = set_mode(size)
    
    #init game data
    nr_of_rectangles = 9
    board = TicTacToeBoard(nr_of_rectangles)
    #board.paint(screen)

    #init input
    inputmanager = InputManager([
    ("Mouse", 1, "Press", (lambda: board.make_turn(Point(get_pos()[0],get_pos()[1])))),
    ("Key", K_UP, "Press", (lambda: print("Hello Keyboard!"))),
    ])


    loop()
Exemplo n.º 14
0
def main():
    global inputmanager, screen, board
    #init graphics
    init()
    size = (150, 150)
    screen = set_mode(size)

    #init game data
    nr_of_rectangles = 9
    board = TicTacToeBoard(nr_of_rectangles)
    #board.paint(screen)

    #init input
    inputmanager = InputManager([
        ("Mouse", 1, "Press",
         (lambda: board.make_turn(Point(get_pos()[0],
                                        get_pos()[1])))),
        ("Key", K_UP, "Press", (lambda: print("Hello Keyboard!"))),
    ])

    loop()
Exemplo n.º 15
0
 def test_who_wins_works(self):
     self.my_tictactoe.tictactoeboard = TicTacToeBoard(5, 'Test', 'Test2')
     self.my_tictactoe.set_game()
     self.my_tictactoe.tictactoeboard.result = Result.FIRST_WIN
     self.my_tictactoe.set_result()
     self.assertEqual(
         self.my_tictactoe.result_text,
         f"{self.my_tictactoe.tictactoeboard.player1} voittaa")
     self.my_tictactoe.tictactoeboard.result = Result.SECOND_WIN
     self.my_tictactoe.set_result()
     self.assertEqual(
         self.my_tictactoe.result_text,
         f"{self.my_tictactoe.tictactoeboard.player2} voittaa")
     self.my_tictactoe.tictactoeboard.result = Result.DRAW
     self.my_tictactoe.set_result()
     self.assertEqual(self.my_tictactoe.result_text, 'Tasapeli')
Exemplo n.º 16
0
 def test_draw_xo(self):
     self.my_tictactoe.tictactoeboard = TicTacToeBoard(5, 'Test', 'Test2')
     self.my_tictactoe.set_game()
     self.assertEqual(self.my_tictactoe.tictactoeboard.result,
                      Result.ONGOING)
     self.assertEqual(self.my_tictactoe.tictactoeboard.whose_turn, 1)
     self.my_tictactoe.set_xo(0, 0)
     self.assertEqual(self.my_tictactoe.tictactoeboard.whose_turn, 2)
     self.assertEqual(self.my_tictactoe.tictactoeboard.result,
                      Result.ONGOING)
     self.my_tictactoe.set_xo(0, 0)
     self.assertEqual(self.my_tictactoe.tictactoeboard.whose_turn, 2)
     self.assertEqual(self.my_tictactoe.tictactoeboard.result,
                      Result.ONGOING)
     self.my_tictactoe.set_xo(self.my_tictactoe.grid_size - 1,
                              self.my_tictactoe.grid_size - 1)
     self.assertEqual(self.my_tictactoe.tictactoeboard.whose_turn, 1)
     self.assertEqual(self.my_tictactoe.tictactoeboard.result,
                      Result.ONGOING)
Exemplo n.º 17
0
 def test_variables_after_set_num_squares(self):
     self.my_tictactoe.tictactoeboard = TicTacToeBoard(0, 'Test', 'Test2')
     self.assertEqual(self.my_tictactoe.tictactoeboard.num_squares, 0)
     self.assertTrue(self.my_tictactoe.screen == None)
     self.assertTrue(self.my_tictactoe.x_image == None)
     self.assertTrue(self.my_tictactoe.o_image == None)
     self.assertTrue(self.my_tictactoe.tictactoeboard != None)
     self.assertTrue(self.my_tictactoe.square_size == 0)
     self.assertTrue(self.my_tictactoe.grid_size == 800)
     self.my_tictactoe.tictactoeboard.num_squares = 5
     self.my_tictactoe.set_game()
     self.assertTrue(self.my_tictactoe.tictactoeboard.num_squares <= 30
                     and self.my_tictactoe.tictactoeboard.num_squares >= 5)
     self.assertTrue(self.my_tictactoe.screen != None)
     self.assertTrue(self.my_tictactoe.x_image != None)
     self.assertTrue(self.my_tictactoe.o_image != None)
     self.assertTrue(self.my_tictactoe.tictactoeboard != None)
     self.assertTrue(self.my_tictactoe.square_size ==
                     int(self.my_tictactoe.grid_size /
                         self.my_tictactoe.tictactoeboard.num_squares))
     self.assertTrue(
         self.my_tictactoe.grid_size == self.my_tictactoe.grid_size -
         (self.my_tictactoe.grid_size %
          self.my_tictactoe.tictactoeboard.num_squares))
 def test_get_other_player_function(self):
   """Test whether the get_other_player_id function provides the id of the other player."""
   board = TicTacToeBoard()
   self.assertEqual(board.get_other_player_id(1), 2)
   self.assertEqual(board.get_other_player_id(2), 1)
 def test_get_winner_on_empty_square_board(self):
   """Test whether get_winner function provides game not over status for an empty square board."""
   board = TicTacToeBoard()
   # Make sure that the game is not over when it begins with an empty board
   winner = board.get_winner()
   self.assertEqual(winner, board.GAME_WINNER_GAME_NOT_OVER)
Exemplo n.º 20
0
class TicTacToe:
    """Ui class for TicTacToe game.

    Attributes:
        start_menu: Starting menu for setting game parameters
        tictactoeboard: Tictactoe board data structure
        screen: Pygame window
        x_image: X image for player 1
        o_image: O image for player 2
        square_size: How big is one square in pixels (grid size / number of squares).
        grid_size: Pygame window size in pixels
        result_text: Result of the game
        whose_turn: Whose turn is it (text message)
        background_color: Background color of the board and the status window
        grid color: Color of grid lines
        bottom_height: How long is the window below the grid (Status window and buttons)
        buttom_width: How long is buttons width
        running: Is game still running or not
    """
    def __init__(self):
        """Class constructor, which initializes the variables
        and final values are set later, because they depend on user input
        """

        self.start_menu = None
        self.tictactoeboard = None

        self.screen = None
        self.x_image = None
        self.o_image = None
        self.square_size = 0
        self.grid_size = 800
        self.result_text = ''
        self.whose_turn = ''
        self.background_color = (210, 210, 210)
        self.grid_color = (0, 0, 0)
        self.bottom_height = 100
        self.button_width = self.grid_size / 3
        self.running = True

    def run(self):
        """Function where game runs in infinite loop
        until game ends or player press quit
        """

        self.start_game()

        if self.tictactoeboard.num_squares == 0:
            return

        while self.running and self.tictactoeboard.num_squares > 0:
            self.play_game()

            if self.tictactoeboard.result == Result.ONGOING and self.running:
                pygame.display.flip()

        pygame.quit()  # pylint: disable=no-member

    def play_game(self):
        """Infinite loop for reading user mouse events. Break after running is set to False"""

        for event in pygame.event.get():

            if event.type == pygame.MOUSEBUTTONDOWN:  # pylint: disable=no-member
                self.check_button_pressed(event.pos[0], event.pos[1])
                if self.running is False:
                    break
                if event.pos[0] > self.grid_size or event.pos[
                        1] > self.grid_size:
                    continue
                self.set_xo(event.pos[0], event.pos[1])

            if self.tictactoeboard.result != Result.ONGOING:

                self.set_result()
                pygame.quit()  # pylint: disable=no-member

                self.start_game()

                if self.tictactoeboard.num_squares == 0:
                    self.running = False
                    break

    def start_game(self):
        """Call Tkinter Class where the user sets the names of the players and number of squares.
        If one of player names are incorrect, starts again with error message.
        Else calls set_game function
        """

        self.start_menu = StartMenu(self.result_text)

        num_squares, player1, player2 = self.get_game_variables()

        self.tictactoeboard = TicTacToeBoard(num_squares, player1, player2)

        if self.tictactoeboard.num_squares == 0:
            return

        if self.players_name_ok(self.start_menu.name_max_size) is False:
            self.result_text = 'Virheellinen pelaajan nimi'
            self.start_game()
        else:
            self.set_game()

    def get_game_variables(self):
        """Show Tkinter menu and set num_squares, player1 and player2 to what user gave it in
        Tkinter window

        Returns:
            Number of squares in board, player 1 name and player 2 name
        """

        self.start_menu.show()
        return self.start_menu.num_squares, self.start_menu.player1, self.start_menu.player2

    def players_name_ok(self, max_size):
        """Check if the player names are the correct size

        Args:
            max_size: what is players name max length

        Returns:
            True, if names length are correct (1-max_size). Otherwise returns False
        """

        player1_len = len(self.tictactoeboard.player1)
        player2_len = len(self.tictactoeboard.player2)
        names_ok = True
        if player1_len > max_size or player1_len < 1:
            names_ok = False
        if player2_len > max_size or player2_len < 1:
            names_ok = False
        return names_ok

    def set_game(self):
        """Sets game variables right with right num_squares and then calls:
        - draw_grid function which draws board.
        - draw_status function which draws whose turn is it (first time player 1)
        - draw_buttons function which draws save, load and quit buttons
        """

        self.square_size = int(self.grid_size /
                               self.tictactoeboard.num_squares)
        self.grid_size = self.grid_size - (self.grid_size %
                                           self.tictactoeboard.num_squares)
        self.button_width = math.floor(self.grid_size / 3)

        self.x_image = pygame.transform.scale(pygame.image.load\
        ("src/images_xo/x.png"), (self.square_size, self.square_size))
        self.o_image = pygame.transform.scale(pygame.image.load\
        ("src/images_xo/o.png"), (self.square_size, self.square_size))

        os.environ['SDL_VIDEO_WINDOW_POS'] = "center"
        pygame.init()  # pylint: disable=no-member
        window_size = [self.grid_size, self.grid_size + self.bottom_height]
        self.screen = pygame.display.set_mode(window_size, pygame.NOFRAME,
                                              pygame.SHOWN)  # pylint: disable=no-member

        self.draw_grid()
        self.draw_status()
        self.draw_buttons()

    def set_xo(self, mouse_x, mouse_y):
        """Add x or o in tictactoeboard data structure to the square that user clicked

        Args:
            mouse_x: X coordinate which position user click with mouse
            mouse_y: Y coordinate which position user click with mouse
        """

        x_square = math.floor(mouse_x / self.square_size)
        y_square = math.floor(mouse_y / self.square_size)

        if self.tictactoeboard.whose_turn == 1 and not \
                                         self.tictactoeboard.is_taken(y_square, x_square):
            self.tictactoeboard.add_x(x_square, y_square)
        elif self.tictactoeboard.whose_turn == 2 and not \
                                            self.tictactoeboard.is_taken(y_square, x_square):
            self.tictactoeboard.add_o(x_square, y_square)

        self.update_board()
        self.draw_status()

    def update_board(self):
        """Draw empty grid and then draw all x and o to the grid.
        Function gets x and o positions from the tictactoeboard data structure"""

        self.draw_grid()

        for x_square in range(0, self.tictactoeboard.num_squares):
            for y_square in range(0, self.tictactoeboard.num_squares):
                x_coordinate = self.square_size * x_square
                y_coordinate = self.square_size * y_square
                if self.tictactoeboard.board[y_square][x_square] == 'x':
                    self.screen.blit(self.x_image,
                                     (x_coordinate, y_coordinate))
                elif self.tictactoeboard.board[y_square][x_square] == 'o':
                    self.screen.blit(self.o_image,
                                     (x_coordinate, y_coordinate))

    def draw_grid(self):
        """Draws empty grid for the game"""

        self.screen.fill((self.background_color),
                         (0, 0, self.grid_size, self.grid_size))

        for x_int in range(0, self.grid_size, self.square_size):
            for y_int in range(0, self.grid_size, self.square_size):
                rect = pygame.Rect(x_int, y_int, self.square_size,
                                   self.square_size)
                pygame.draw.rect(self.screen, self.grid_color, rect, 1)

    def draw_status(self):
        """Draws a status of the game (whose turn is it) and also calls function draw_buttons
        which draws save, load and quit buttons
        """

        my_font = 'arial'
        status_font_size = 45
        font_color = (0, 0, 0)
        status_coordinates = (0, self.grid_size, self.grid_size,
                              self.bottom_height / 2)
        status_text_center = (self.grid_size / 2,
                              self.grid_size + int(self.bottom_height / 4))

        if self.tictactoeboard.whose_turn == 1:
            self.whose_turn = f"Vuoro: {self.tictactoeboard.player1}"
        else:
            self.whose_turn = f"Vuoro: {self.tictactoeboard.player2}"

        font = pygame.font.SysFont(my_font, status_font_size)
        text = font.render(self.whose_turn, 1, font_color)
        self.screen.fill((self.background_color), status_coordinates)
        text_rect = text.get_rect(center=status_text_center)
        self.screen.blit(text, text_rect)

        pygame.display.update()

    def draw_buttons(self):
        """draws save, load and quit buttons to bottom of window"""

        save_button_coordinates = (0, self.grid_size + self.bottom_height/2, \
                                   self.button_width, self.bottom_height/2)
        save_button_center = (0 + self.button_width/2, \
                            self.grid_size + int(self.bottom_height * 0.75))
        self.draw_one_button('Tallenna peli', save_button_coordinates,
                             save_button_center)


        download_button_coordinates = (self.button_width, self.grid_size + self.bottom_height/2,\
                                       self.button_width, self.bottom_height/2)
        download_button_center = (self.grid_size / 2,\
                                self.grid_size + int(self.bottom_height * 0.75))
        self.draw_one_button('Lataa peli', download_button_coordinates,
                             download_button_center)


        quit_button_coordinates = (self.button_width * 2, self.grid_size + self.bottom_height/2,\
                                       self.grid_size - 2 * self.button_width, self.bottom_height/2)
        quit_button_center = (self.grid_size - self.button_width/2,\
                                self.grid_size + int(self.bottom_height * 0.75))
        self.draw_one_button('Lopeta peli', quit_button_coordinates,
                             quit_button_center)

        pygame.display.update()

    def draw_one_button(self, button_text, button_coordinates, button_center):
        """Auxiliary function for drawing buttons

        Args:
            button_text: Button text
            button_coordinates: Where to draw button and what are buttons width and height
            button_center: Where is center of button
        """

        button_font = 'arial'
        button_font_size = 24
        button_color = (150, 150, 150)
        font_color = (50, 50, 50)
        border_color = (120, 120, 120)

        font = pygame.font.SysFont(button_font, button_font_size)
        text = font.render(button_text, 1, font_color)
        pygame.draw.rect(self.screen, (button_color), button_coordinates)

        # Draws button borders and text
        pygame.draw.rect(self.screen, border_color,
                         pygame.Rect(button_coordinates), 4, 0)
        text_rect = text.get_rect(center=button_center)
        self.screen.blit(text, text_rect)

    def check_button_pressed(self, mouse_x, mouse_y):
        """Check if user press one of buttons

        Args:
            mouse_x: Check x coordinate which position user clicked with mouse
            mouse_y: Check y coordinate which position user clicked with mouse
        """

        if 0 < mouse_x < self.button_width and\
           self.grid_size + self.bottom_height/2 < mouse_y < self.grid_size + self.bottom_height:
            self.save_game()

        elif self.button_width < mouse_x < self.button_width * 2 and\
             self.grid_size + self.bottom_height/2 < mouse_y < self.grid_size + self.bottom_height:
            self.load_game()

        elif self.button_width * 2 < mouse_x < self.grid_size and\
             self.grid_size + self.bottom_height/2 < mouse_y < self.grid_size + self.bottom_height:
            self.running = False

    def save_game(self):
        """Function for saving game and printing result message"""

        message = 'Tallennus onnistui'
        window_title = 'Tallennus'
        try:
            save_menu = tk.Tk()
            save_menu.withdraw()
            save_filename = tkinter.filedialog.asksaveasfilename(title= "Tallenna tiedosto"\
                            ,filetypes=[("Tictactoe tiedostot", "*.ttt")])
            save_menu.destroy()
            with open(save_filename, "wb") as save_file:
                pickle.dump(self.tictactoeboard, save_file)
        except (IOError, TypeError, AttributeError, pickle.PicklingError):
            message = 'Tallennus epäonnistui'
        finally:
            self.print_message(message, window_title)

    def load_game(self):
        """Function for loading game and printing result message"""

        message = 'Lataus onnistui'
        window_title = 'Lataus'
        try:
            load_menu = tk.Tk()
            load_menu.withdraw()
            load_filename = tkinter.filedialog.askopenfilename(parent=load_menu, \
                            title= "Lataa tiedosto", \
                            filetypes=[("Tictactoe tiedostot", "*.ttt")])
            load_menu.destroy()
            with open(load_filename, "rb") as load_file:
                self.tictactoeboard = pickle.load(load_file)
        except (IOError, TypeError, AttributeError, pickle.UnpicklingError):
            message = 'Lataus epäonnistui'
        else:
            self.set_game()
            self.update_board()
        finally:
            self.print_message(message, window_title)

    def print_message(self, message, window_title):
        """Function that print message if needed. Uses Tkinter popup window

        Args:
            message: Message that is shown to user
            window_title: Window title
        """

        errorwindow = tk.Tk()
        errorwindow.overrideredirect(1)
        errorwindow.withdraw()
        tkinter.messagebox.showinfo(window_title, message)
        errorwindow.destroy()

    def set_result(self):
        """Set result text who wins or is it draw"""

        if self.tictactoeboard.result == Result.FIRST_WIN:
            self.result_text = f"{self.tictactoeboard.player1} voittaa"
        elif self.tictactoeboard.result == Result.SECOND_WIN:
            self.result_text = f"{self.tictactoeboard.player2} voittaa"
        elif self.tictactoeboard.result == Result.DRAW:
            self.result_text = 'Tasapeli'
 def test_get_next_move_boards_on_non_empty_game_not_over_square_board(self):
   """Test whether _get_next_move_boards function provides all next move boards for a non-empty game-not-over square board."""
   # Create non-empty game-not-over tic-tac-toe game board
   board = TicTacToeBoard()
   board.matrix[0] = [2, 0, 0]
   board.matrix[1] = [1, 2, 0]
   board.matrix[2] = [1, 1, 2]
   next_boards = board._get_next_move_boards(1)
   # Initialize valid next boards
   valid_next_boards = []
   # Add valid next board 1 to list
   valid_next_board = TicTacToeBoard()
   valid_next_board.matrix = copy.deepcopy(board.matrix)
   valid_next_board.matrix[0][1] = 1
   valid_next_boards.append(valid_next_board)
   # Add valid next board 2 to list
   valid_next_board = TicTacToeBoard()
   valid_next_board.matrix = copy.deepcopy(board.matrix)
   valid_next_board.matrix[0][2] = 1
   valid_next_boards.append(valid_next_board)
   # Add valid next board 3 to list
   valid_next_board = TicTacToeBoard()
   valid_next_board.matrix = copy.deepcopy(board.matrix)
   valid_next_board.matrix[1][2] = 1
   valid_next_boards.append(valid_next_board)
   # Make sure there are the correct number of next boards for player 1
   next_board_matrices = [next_board.matrix for next_board in next_boards]
   self.assertEqual(len(next_board_matrices), 3)
   # Make sure that each valid next board for player 1 was found 
   for valid_next_board in valid_next_boards:
     self.assertTrue(valid_next_board.matrix in next_board_matrices)
 def test_can_win_on_non_empty_square_board_that_you_won(self):
   """Test whether _can_win function returns that a player can win a non-empty square board that they have won."""
   # Create non-empty tic-tac-toe game board that player 1 has won
   board = TicTacToeBoard()
   board.matrix[0] = [1, 0, 0]
   board.matrix[1] = [2, 1, 0]
   board.matrix[2] = [0, 2, 1]
   self.assertTrue(board._can_win(1))
   # Test another similar board
   board = TicTacToeBoard()
   board.matrix[0] = [1, 0, 0]
   board.matrix[1] = [1, 2, 0]
   board.matrix[2] = [1, 2, 0]
   self.assertTrue(board._can_win(1))
   # Test another similar board
   board = TicTacToeBoard()
   board.matrix[0] = [1, 1, 1]
   board.matrix[1] = [0, 2, 0]
   board.matrix[2] = [0, 2, 0]
   self.assertTrue(board._can_win(1))
   # Test another similar board
   board = TicTacToeBoard()
   board.matrix[0] = [0, 2, 1]
   board.matrix[1] = [0, 1, 0]
   board.matrix[2] = [1, 2, 0]
   self.assertTrue(board._can_win(1))    
   # Create non-empty tic-tac-toe game board that player 2 has won
   board = TicTacToeBoard()
   board.matrix[0] = [2, 0, 0]
   board.matrix[1] = [1, 2, 0]
   board.matrix[2] = [0, 1, 2]
   self.assertTrue(board._can_win(2))
   # Test another similar board
   board = TicTacToeBoard()
   board.matrix[0] = [2, 0, 0]
   board.matrix[1] = [2, 1, 0]
   board.matrix[2] = [2, 1, 0]
   self.assertTrue(board._can_win(2))
   # Test another similar board
   board = TicTacToeBoard()
   board.matrix[0] = [2, 2, 2]
   board.matrix[1] = [0, 1, 0]
   board.matrix[2] = [0, 1, 0]
   self.assertTrue(board._can_win(2))
   # Test another similar board
   board = TicTacToeBoard()
   board.matrix[0] = [0, 1, 2]
   board.matrix[1] = [0, 2, 0]
   board.matrix[2] = [2, 1, 0]
   self.assertTrue(board._can_win(2))
Exemplo n.º 23
0
class TicTacToeGame:  
  """A tic-tac-toe game class that allows a single user to play 
  against a perfect computer opponent that always wins or ties.
  
  Public functions:
  start -- Starts the game.
  
  """
  def start(self, row_count=3, column_count=3):
    """Starts the game.
    
    Arguments:
    row_count -- Number of rows in tic-tac-toe board
    column_count -- Number of columns in tic-tac-toe board
    
    Side-Effects:
    Creates a tic-tac-toe game board with the specified number of rows and columns.
    Decides the order in which the players will take turns.
    Displays the game board to standard output and then alternates turns between
    the user and the computer player.
    During the user's turn, it prompts the user to input
    the coordinates with which to mark the game board.
    The user can also input 'q' to quit the game.
    If the game continues until competion, it indicates whether the player won, lost, or tied the game.
    The user should never win.
    
    """
    # Initialize game board
    self.board = TicTacToeBoard(row_count, column_count)
    # Initialize players
    self._initialize_players()
    # Welcome the user to the game
    self._display_welcome()
    # Let the players turns marking the game board
    self._take_turns()    
    # Display the game results
    self._display_results()

  def _take_turns(self):
    """Alternate between players, allowing each player to mark the game board until the game ends, or the user quits.
    
    During the user's turn, prompt the user for coordinates with which to mark the board.
    Allow the user to quit the game by entering 'q'
    """
    # Beginning with the first player,
    # alternate turns between players until the game ends
    self.current_player_id = 1 # the id of the current player
    user_command = '' # the command entered by the user
    while(self.board.is_game_over() is False):
      if self.current_player_id == self.computer_player_id:      
        self.board.take_best_move(self.computer_player_id)
        # End turn and allow the user to take a turn
        self.current_player_id = self.user_player_id
      else:
        # Display the board
        self.board.display()
        # Remind the user whether they are X's or O's
        if self.user_player_id == 1:
          print "You are X's"
        else:
          print "You are O's"
        # Ask user to input the coordinates of her mark, or to press q to quit
        user_command = raw_input('<enter "{rowNum}, {columnNum}" or "q" to quit>: ')
        print ""
        # Process the user command
        if user_command.lower().strip() == 'q':
          # End the game
          break
        else:
          # Mark the board for the user
          self._mark_board_for_user(user_command)
    # Display final board  
    self.board.display()
    # Determine winner
    self.winner_id = self.board.get_winner()   

  def _mark_board_for_user(self, user_command):
    """Mark the board according to the user's command.
    
    Arguments:
    user_command -- a user command that specifies where to mark the board.
    
    Side-Effects:
    If the user command provides valid coordinates for where to mark the board,
    the board is marked at those coordinates by the user.
    
    """
    # Make sure the user has entered valid coordinates for her mark
    # and if so, mark the board for the user
    user_command_parts = user_command.split(',')
    if len(user_command_parts) == 2:
      row = int(user_command_parts[0].strip()) - 1
      col = int(user_command_parts[1].strip()) - 1
      valid_row_range = xrange(self.board.row_count)
      valid_col_range = xrange(self.board.column_count)
      if row in valid_row_range and col in valid_col_range:
        # Make sure a mark does not already exist at the coordinates 
        if  self.board.matrix[row][col] == self.board.CELL_NO_PLAYER:
          # Mark the board at the coordinate for the player
          self.board.matrix[row][col] = self.user_player_id
          # End turn and allow the computer player to take a turn
          self.current_player_id = self.computer_player_id

  def _display_welcome(self):
    """ Display text that welcomes the user to the game."""
    print ""
    print "Welcome to Tic-Tac-Toe"
    print ""
    
  def _display_results(self):
    """Display final game results"""
    print ""
    if self.winner_id == self.user_player_id:
      print "You won!"
    elif self.winner_id == self.computer_player_id:
      print "You lost!"
    elif self.winner_id == self.board.GAME_WINNER_TIED:
      print "You tied!"
    print ""
    
  def _initialize_players(self):
    """Randomly pick whether the user will be the first or second player."""
    self.user_player_id = random.choice([1,2])
    self.computer_player_id = self.board.get_other_player_id(self.user_player_id)
Exemplo n.º 24
0
 def setUp(self):
     self.my_tictactoe_board = TicTacToeBoard(5, '', '')
 def test_can_tie_on_non_empty_square_board_you_lost(self):
   """Test whether _can_tie function returns that a player cannot tie a non-empty square board that they lost."""
   # Create non-empty tic-tac-toe game board that player 1 has won
   board = TicTacToeBoard()
   board.matrix[0] = [1, 0, 0]
   board.matrix[1] = [2, 1, 0]
   board.matrix[2] = [0, 2, 1]
   self.assertFalse(board._can_tie(2))
   # Test another similar board
   board = TicTacToeBoard()
   board.matrix[0] = [1, 0, 0]
   board.matrix[1] = [1, 2, 0]
   board.matrix[2] = [1, 2, 0]
   self.assertFalse(board._can_tie(2))
   # Test another similar board
   board = TicTacToeBoard()
   board.matrix[0] = [1, 1, 1]
   board.matrix[1] = [0, 2, 0]
   board.matrix[2] = [0, 2, 0]
   self.assertFalse(board._can_tie(2))
   # Test another similar board
   board = TicTacToeBoard()
   board.matrix[0] = [0, 2, 1]
   board.matrix[1] = [0, 1, 0]
   board.matrix[2] = [1, 2, 0]
   self.assertFalse(board._can_tie(2))
   # Create non-empty tic-tac-toe game board that player 2 has won
   board = TicTacToeBoard()
   board.matrix[0] = [2, 0, 0]
   board.matrix[1] = [1, 2, 0]
   board.matrix[2] = [0, 1, 2]
   self.assertFalse(board._can_tie(1))
   # Test another similar board
   board = TicTacToeBoard()
   board.matrix[0] = [2, 0, 0]
   board.matrix[1] = [2, 1, 0]
   board.matrix[2] = [2, 1, 0]
   self.assertFalse(board._can_tie(1))
   # Test another similar board
   board = TicTacToeBoard()
   board.matrix[0] = [2, 2, 2]
   board.matrix[1] = [0, 1, 0]
   board.matrix[2] = [0, 1, 0]
   self.assertFalse(board._can_tie(1))
   # Test another similar board
   board = TicTacToeBoard()
   board.matrix[0] = [0, 1, 2]
   board.matrix[1] = [0, 2, 0]
   board.matrix[2] = [2, 1, 0]
   self.assertFalse(board._can_tie(1))
Exemplo n.º 26
0
class TestTicTacToeBoard(unittest.TestCase):
    def setUp(self):
        self.my_tictactoe_board = TicTacToeBoard(5, '', '')

    def test_constructor_is_working(self):
        self.assertEqual(self.my_tictactoe_board.num_squares, 5)
        boolean = False
        if all('-' in x for x in self.my_tictactoe_board.board):
            boolean = True
        self.assertEqual(boolean, True)
        self.assertEqual(self.my_tictactoe_board.whose_turn, 1)
        self.assertEqual(self.my_tictactoe_board.result, Result.ONGOING)

    def test_adding_x_and_o(self):

        # Add first x to empty square
        self.my_tictactoe_board.add_x(0, 0)
        self.assertEqual(
            sum(x.count("x") for x in self.my_tictactoe_board.board), 1)
        self.assertEqual(
            sum(x.count("-") for x in self.my_tictactoe_board.board),
            (self.my_tictactoe_board.num_squares**2) - 1)

        # Test to add x into occupied square
        self.my_tictactoe_board.add_x(0, 0)
        self.assertEqual(
            sum(x.count("x") for x in self.my_tictactoe_board.board), 1)
        self.assertEqual(
            sum(x.count("-") for x in self.my_tictactoe_board.board),
            (self.my_tictactoe_board.num_squares**2) - 1)

        # Add o to an empty square
        self.my_tictactoe_board.add_o(0, 1)
        self.assertEqual(
            sum(x.count("o") for x in self.my_tictactoe_board.board), 1)
        self.assertEqual(
            sum(x.count("-") for x in self.my_tictactoe_board.board),
            (self.my_tictactoe_board.num_squares**2) - 2)

        # Add o to same square than x
        self.my_tictactoe_board.add_o(0, 0)
        self.assertEqual(
            sum(x.count("o") for x in self.my_tictactoe_board.board), 1)
        self.assertEqual(
            sum(x.count("-") for x in self.my_tictactoe_board.board),
            (self.my_tictactoe_board.num_squares**2) - 2)

    def test_if_taken(self):
        self.my_tictactoe_board.add_x(0, 0)
        self.assertTrue(self.my_tictactoe_board.is_taken(0, 0))

    def test_set_winner_right(self):
        self.my_tictactoe_board.set_winner('x')
        self.assertEqual(self.my_tictactoe_board.result, Result.FIRST_WIN)
        self.my_tictactoe_board.set_winner('o')
        self.assertEqual(self.my_tictactoe_board.result, Result.SECOND_WIN)

    def test_check_draw(self):
        self.my_tictactoe_board.add_x(0, 0)
        self.my_tictactoe_board.add_o(0, 1)
        self.my_tictactoe_board.add_o(0, 2)
        self.my_tictactoe_board.add_o(0, 3)
        self.my_tictactoe_board.add_x(0, 4)
        self.my_tictactoe_board.add_x(1, 0)
        self.my_tictactoe_board.add_o(1, 1)
        self.my_tictactoe_board.add_x(1, 2)
        self.my_tictactoe_board.add_x(1, 3)
        self.my_tictactoe_board.add_o(1, 4)
        self.my_tictactoe_board.add_o(2, 0)
        self.my_tictactoe_board.add_x(2, 1)
        self.my_tictactoe_board.add_o(2, 2)
        self.my_tictactoe_board.add_x(2, 3)
        self.my_tictactoe_board.add_o(2, 4)
        self.my_tictactoe_board.add_x(3, 0)
        self.my_tictactoe_board.add_x(3, 1)
        self.my_tictactoe_board.add_o(3, 2)
        self.my_tictactoe_board.add_o(3, 3)
        self.my_tictactoe_board.add_x(3, 4)
        self.my_tictactoe_board.add_x(4, 0)
        self.my_tictactoe_board.add_o(4, 1)
        self.my_tictactoe_board.add_o(4, 2)
        self.my_tictactoe_board.add_x(4, 3)
        self.my_tictactoe_board.add_x(4, 4)
        self.my_tictactoe_board.check_draw()
        self.assertEqual(self.my_tictactoe_board.result, Result.DRAW)

    def test_check_situation_row(self):
        self.my_tictactoe_board.add_x(0, 0)
        self.my_tictactoe_board.check_situation()
        self.assertEqual(self.my_tictactoe_board.result, Result.ONGOING)
        self.my_tictactoe_board.add_o(1, 0)
        self.my_tictactoe_board.check_situation()
        self.assertEqual(self.my_tictactoe_board.result, Result.ONGOING)
        self.my_tictactoe_board.add_x(0, 1)
        self.my_tictactoe_board.check_situation()
        self.assertEqual(self.my_tictactoe_board.result, Result.ONGOING)
        self.my_tictactoe_board.add_o(1, 1)
        self.my_tictactoe_board.check_situation()
        self.assertEqual(self.my_tictactoe_board.result, Result.ONGOING)
        self.my_tictactoe_board.add_x(0, 2)
        self.my_tictactoe_board.check_situation()
        self.assertEqual(self.my_tictactoe_board.result, Result.ONGOING)
        self.my_tictactoe_board.add_o(1, 2)
        self.my_tictactoe_board.check_situation()
        self.assertEqual(self.my_tictactoe_board.result, Result.ONGOING)
        self.my_tictactoe_board.add_x(0, 3)
        self.my_tictactoe_board.check_situation()
        self.assertEqual(self.my_tictactoe_board.result, Result.FIRST_WIN)

    def test_check_situation_col(self):
        self.my_tictactoe_board.add_x(0, 0)
        self.my_tictactoe_board.check_situation()
        self.assertEqual(self.my_tictactoe_board.result, Result.ONGOING)
        self.my_tictactoe_board.add_o(0, 1)
        self.my_tictactoe_board.check_situation()
        self.assertEqual(self.my_tictactoe_board.result, Result.ONGOING)
        self.my_tictactoe_board.add_x(1, 0)
        self.my_tictactoe_board.check_situation()
        self.assertEqual(self.my_tictactoe_board.result, Result.ONGOING)
        self.my_tictactoe_board.add_o(1, 1)
        self.my_tictactoe_board.check_situation()
        self.assertEqual(self.my_tictactoe_board.result, Result.ONGOING)
        self.my_tictactoe_board.add_x(2, 0)
        self.my_tictactoe_board.check_situation()
        self.assertEqual(self.my_tictactoe_board.result, Result.ONGOING)
        self.my_tictactoe_board.add_o(2, 1)
        self.my_tictactoe_board.check_situation()
        self.assertEqual(self.my_tictactoe_board.result, Result.ONGOING)
        self.my_tictactoe_board.add_x(3, 0)
        self.my_tictactoe_board.check_situation()
        self.assertEqual(self.my_tictactoe_board.result, Result.FIRST_WIN)

    def test_check_situation_diagonal(self):
        self.my_tictactoe_board.add_x(0, 0)
        self.my_tictactoe_board.check_situation()
        self.assertEqual(self.my_tictactoe_board.result, Result.ONGOING)
        self.my_tictactoe_board.add_o(0, 1)
        self.my_tictactoe_board.check_situation()
        self.assertEqual(self.my_tictactoe_board.result, Result.ONGOING)
        self.my_tictactoe_board.add_x(1, 1)
        self.my_tictactoe_board.check_situation()
        self.assertEqual(self.my_tictactoe_board.result, Result.ONGOING)
        self.my_tictactoe_board.add_o(1, 1)
        self.my_tictactoe_board.check_situation()
        self.assertEqual(self.my_tictactoe_board.result, Result.ONGOING)
        self.my_tictactoe_board.add_x(2, 2)
        self.my_tictactoe_board.check_situation()
        self.assertEqual(self.my_tictactoe_board.result, Result.ONGOING)
        self.my_tictactoe_board.add_o(2, 1)
        self.my_tictactoe_board.check_situation()
        self.assertEqual(self.my_tictactoe_board.result, Result.ONGOING)
        self.my_tictactoe_board.add_x(3, 3)
        self.my_tictactoe_board.check_situation()
        self.assertEqual(self.my_tictactoe_board.result, Result.FIRST_WIN)

    def test_check_situation_diagonal_other_way(self):
        self.my_tictactoe_board.add_x(0, 4)
        self.my_tictactoe_board.check_situation()
        self.assertEqual(self.my_tictactoe_board.result, Result.ONGOING)
        self.my_tictactoe_board.add_o(0, 1)
        self.my_tictactoe_board.check_situation()
        self.assertEqual(self.my_tictactoe_board.result, Result.ONGOING)
        self.my_tictactoe_board.add_x(1, 3)
        self.my_tictactoe_board.check_situation()
        self.assertEqual(self.my_tictactoe_board.result, Result.ONGOING)
        self.my_tictactoe_board.add_o(1, 1)
        self.my_tictactoe_board.check_situation()
        self.assertEqual(self.my_tictactoe_board.result, Result.ONGOING)
        self.my_tictactoe_board.add_x(2, 2)
        self.my_tictactoe_board.check_situation()
        self.assertEqual(self.my_tictactoe_board.result, Result.ONGOING)
        self.my_tictactoe_board.add_o(2, 1)
        self.my_tictactoe_board.check_situation()
        self.assertEqual(self.my_tictactoe_board.result, Result.ONGOING)
        self.my_tictactoe_board.add_x(3, 1)
        self.my_tictactoe_board.check_situation()
        self.assertEqual(self.my_tictactoe_board.result, Result.FIRST_WIN)