Tic Tac Toe in Java
Now let's review, in detail, each class, method and idea we use to create the game.
Notice how this class and the method is small and clean.
It is good practice to do this: just use the main() to start your application, try your best to not fill it of variables, methods, and other things.
Well, this class just started the game, creating an object 'game' Class "Game".
To learn more, go to class "Game"!
Board.java ClassThis class is a very simple and useful because it will be in charge of creating the board, draw it on the screen as well as carefully checking to see if a line, column or diagonal was filled by one of the players.
The class starts by creating a 3x3 matrix of integers, the matrix 'board' which is our board.
Then the default constructor invokes the method "clearBoard()", which as the name says, makes all matrix 'board' integers receive the 0 value.
The following code will be invoked several times during the game, is the method that displays the array on the screen, 'showBoard()'.
He goes through all the elements of the screen, if it find -1, it prints a 'X', if it finds 1 it prints the 'O', and if it finds the number 0 does not print anything (empty field on the board).
If no queue is full, or if no one has won, the method gained () returns 0 and we can continue in our method "Play ()".
The following methods, getPosition() and setPosition() are self-explanatory. The getter returns which number (-1, 0, 1) is in that position, and the setter changes a location of the board to -1 or 1 depending on the player (if is X or O). Positions are always represented by a vector called 'attempt', where 'attempt ' represents the line and 'attempt ' represents the column.
The following methods are checking all lines (checkLines()), columns (checkColumns()) and the two diagonals (checkDiagonals()).
If any line, column or diagonal is filled and marked with 'X', then the sum of the elements in this queue will be -3 (-1 + -1 + -1 = -3), and the method returns -1, signaling that the player 1 won.
If any line, column or diagonal is filled and labeled with 'O', then the sum of the elements in this queue is 3 (1 + 1 + 1 = 3), and the method returns 1, indicating that player 2 won.
The latter method, the 'fullBoard()' checks all array elements, if you find a number 0, returns false. This indicates that the matrix is not fully populated.
Should you not find even a number 0, is because all are 1 or -1. That is, the board is complete, we return true and the game has a draw.
Game.java ClassThis class will create, through variables:
private Board board;
Numbers that will control the number of round and who will play each turn:
private int turn=1, who=1;
private Player player1;
private Player player2;
And an object of Scanner class to receive data from human players.
Whenever we create an object of a class, its default constructor runs. In case is the "Game()".
It will start the board:
board = new Board();
(Go to class board and read how it works. Then go back here)
Then the players will start, to see who will be first, second and who will be human or computer (choice, for now, humans).
Next, we'll run the game from the command:
The method "Play()" is going to control the whole game. It returns a boolean.
If the game is not finished, it returns 'true', then the while loop will always run.
When the game ends, win or result a draw, the method "Play()" returns 'false', while it will no longer continue and the constructor finishes, ending object creation, and consequently the game.
Remember, our main() only creates an object. As the object was created, the application ended.
Player.java ClassIn this class, we apply our knowledge of abstraction and polymorphism.
There are two types of players, the human and the computer.
Therefore, their methods of play are different. So it will be abstract, since it has abstract methods 'play()' and 'Try()', as the strategy is different for each type of player, and the computer will not make attempts that can not.
In this class we define the array of integers 'attempt ' where attempt will hold the line and attempt to position the column where the player attempts to mark their X or O.
Method 'play()' get the current board, as well as the method 'Try()' which also receives the position (line, column) that the player will try to mark their X or O.
Method 'checkTry()' will make sure that the position that the player will score on the board is really empty, or if that position is actually the number 0.
Soon, we will create the player Computer, in an easy level, which will play simply checking the board randomly, so we use the method 'checkTry()' to the computer too (why is not abstract, vai to serve both as to Human Computer ).
Human.java ClassIt inherits the abstract class "Player".
Each object created receives a number, 1 or 2, that will characterize as player 1 or player 2, for people to know who's to turn.
Next, we'll do the Override Method 'play()', customized for human players.
First, call the method 'Try()', which will ask the line and column, will check if these numbers are valid (they are 1, 2 or 3) and will make the change matrix we see, for real matrix of the game.
Because as a player, we see the line and columns 1, 2 and 3. However, to create the matrix board  , we work with lines and columns of numbers 0, 1 and 2.
So if the player types line 2 and column 3, subtract one unit of each and send line 1 and column 2 to Java.
When we implement a computer game, it is not necessary, we use straight lines and columns 0, 1 and 2.
These changes are only for humans to read.
So when the human player put a correct data, mark the 'X' or 'O' on the board through the method 'setPosition()', object 'board' of class "Board".
This information (board and attempt) are required method pro 'checkTry()' whether the desired position no longer exists any X or 0, and make sure that the human user will not put a number greater than 3 or less than 1.
Computer.java ClassIt inherits the abstract class "Player", and in this tutorial it does nothing.
We will implement it in an upcoming tutorial, where you will learn about artificial intelligence and we can finally play with the computer through an application that you yourself did.