Class Game


  • @ParametersAreNonnullByDefault
    class Game
    extends java.lang.Object
    This class contains logic for the AI of the game and maintains the UI. The AI is unbeatable.
    Author:
    Abhyudaya Sharma 1710110019, Kabir Kanha Arora 1710110165
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private javax.swing.JPanel boxes
      JPanel to display the buttons for the game
      private javax.swing.JButton[][] buttons
      Clickable JButtons for user interactions.
      private java.lang.String compChar
      The character assigned to the computer; either 'X' or 'O'.
      private java.util.Vector<java.lang.Integer> computerMoves
      List of moves made by the computer.
      private javax.swing.JList<java.lang.Integer> computerMovesList
      Displayable list of moves made by the computer.
      private static int computerWins
      The number of wins made by the computer.
      private PlayerType currentPlayer
      The current player.
      private boolean flag  
      private javax.swing.JFrame gameFrame
      The containing the game.
      private java.util.Vector<java.lang.Integer> humanMoves
      List of moves made by the user.
      private javax.swing.JList<java.lang.Integer> humanMovesList
      Displayable list of moves made by the human user.
      private static int humanWins
      The number of wins made by the human player.
      private static java.lang.String leader
      String that stores the name of the current scoreboard leader
      private int[][] magicSquare
      Magic square used by the AI.
      private static int size
      The size of the tic-tac-toe board = 3.
      private java.lang.String userChar
      The character assigned to the user; either 'X' or 'O'.
      private int winningSum
      The sum that gives a user his or her win.
    • Constructor Summary

      Constructors 
      Constructor Description
      Game()  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      private void addOnClickListener​(javax.swing.JButton button, int row, int col)
      Adds listener to the button for user interaction which get fired when the button is clicked.
      private void computerMove()
      Handles the move of the user.
      private int correctCorner​(java.util.List Player)
      Chooses the correct corner to move in to block the double side fork
      private int elementCounter​(java.util.List Player, int[] arr)
      Counts the number of elements common in an array and a List
      private boolean emptyCorner()
      Finds an empty corner in the matrix
      private boolean emptySide()
      Finds an empty side in the matrix
      private boolean fillFirstFound​(int[] temp1, int[] temp2)
      Moves in the square identified by two integer arrays.
      private boolean fillOppositeCorner​(int num)
      Moves in a corner opposite to an an already filled one.
      private void findAndDisable​(int num)
      Finds a button from its magic square number and disables it after it has been clicked.
      private PlayerType findStartPlayer()
      Asks the user for input about who starts first.
      private void gameOver​(java.lang.String winner)
      Finishes the game and asks the user whether he/she wants to play again.
      private PlayerType getNextPlayer()
      Returns the player who has the next turn.
      private boolean humanWin​(java.util.List Player)
      Checks if the win for the human player is possible.
      private boolean oppositeCorner()
      Finds the opposite corner for the move.
      private int possWin​(java.util.List Opponent, java.util.List Player)
      Checks if the winning is possible
      (package private) void start()
      Execution starts here.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • currentPlayer

        private PlayerType currentPlayer
        The current player.
      • userChar

        private java.lang.String userChar
        The character assigned to the user; either 'X' or 'O'.
      • compChar

        private java.lang.String compChar
        The character assigned to the computer; either 'X' or 'O'.
      • size

        private static final int size
        The size of the tic-tac-toe board = 3.
        See Also:
        Constant Field Values
      • gameFrame

        private final javax.swing.JFrame gameFrame
        The containing the game.
      • humanMoves

        private final java.util.Vector<java.lang.Integer> humanMoves
        List of moves made by the user.
      • computerMoves

        private final java.util.Vector<java.lang.Integer> computerMoves
        List of moves made by the computer.
      • humanMovesList

        private final javax.swing.JList<java.lang.Integer> humanMovesList
        Displayable list of moves made by the human user.
      • computerMovesList

        private final javax.swing.JList<java.lang.Integer> computerMovesList
        Displayable list of moves made by the computer.
      • buttons

        private javax.swing.JButton[][] buttons
        Clickable JButtons for user interactions.
      • flag

        private boolean flag
      • humanWins

        private static int humanWins
        The number of wins made by the human player.
      • computerWins

        private static int computerWins
        The number of wins made by the computer.
      • boxes

        private javax.swing.JPanel boxes
        JPanel to display the buttons for the game
      • leader

        private static java.lang.String leader
        String that stores the name of the current scoreboard leader
      • magicSquare

        @Nonnegative
        private final int[][] magicSquare
        Magic square used by the AI.
      • winningSum

        private final int winningSum
        The sum that gives a user his or her win.
    • Constructor Detail

      • Game

        Game()
    • Method Detail

      • start

        void start()
        Execution starts here.
      • addOnClickListener

        private void addOnClickListener​(javax.swing.JButton button,
                                        int row,
                                        int col)
        Adds listener to the button for user interaction which get fired when the button is clicked.
        Parameters:
        button - the button to which the action listener will be added
        row - zero-indexed row position of the button in the grid
        col - zero-indexed column position of the button in the grid
      • computerMove

        private void computerMove()
        Handles the move of the user.
      • gameOver

        private void gameOver​(java.lang.String winner)
        Finishes the game and asks the user whether he/she wants to play again.
        Parameters:
        winner - name of the player who won the game.
      • findStartPlayer

        @CheckForNull
        private PlayerType findStartPlayer()
        Asks the user for input about who starts first.
        Returns:
        the PlayerType corresponding to the user's selected choice.s
      • possWin

        private int possWin​(java.util.List Opponent,
                            java.util.List Player)
        Checks if the winning is possible
        Parameters:
        Opponent - list of computer moves
        Player - list of human moves
        Returns:
        difference or zero
      • findAndDisable

        private void findAndDisable​(int num)
        Finds a button from its magic square number and disables it after it has been clicked.
        Parameters:
        num - the number of the button corresponding to the magic square.
      • getNextPlayer

        @Nonnull
        private PlayerType getNextPlayer()
        Returns the player who has the next turn.
        Returns:
        the player who has the next turn.
      • humanWin

        private boolean humanWin​(java.util.List Player)
        Checks if the win for the human player is possible.
        Parameters:
        Player - List of player moves.
        Returns:
        true if a win is possible
      • oppositeCorner

        private boolean oppositeCorner()
        Finds the opposite corner for the move.
        Returns:
        opposite corner for the move
      • emptyCorner

        private boolean emptyCorner()
        Finds an empty corner in the matrix
        Returns:
        an empty corner in the matrix
      • emptySide

        private boolean emptySide()
        Finds an empty side in the matrix
        Returns:
        an empty side in the matrix
      • fillFirstFound

        private boolean fillFirstFound​(int[] temp1,
                                       int[] temp2)
        Moves in the square identified by two integer arrays. The first corresponding pair location found empty is the move.
        Parameters:
        temp1 - integer array 1
        temp2 - integer array 2
        Returns:
        true if successfully moved
      • fillOppositeCorner

        private boolean fillOppositeCorner​(int num)
        Moves in a corner opposite to an an already filled one.
        Parameters:
        num - An entry existing in the list of human moves.
        Returns:
        true is move is successful
      • elementCounter

        private int elementCounter​(java.util.List Player,
                                   int[] arr)
        Counts the number of elements common in an array and a List
        Parameters:
        Player - the list of player moves
        arr - an integer array
        Returns:
        the number of common elements
      • correctCorner

        private int correctCorner​(java.util.List Player)
        Chooses the correct corner to move in to block the double side fork
        Parameters:
        Player - lise of human moves
        Returns:
        the spot for the correct move. 0 if none.