Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop p

Tic tac toe game in java - download source code

Hello, guys welcome to this programming blog :). Through this post, I'll show you how to implement a tic tac toe game in java. And I'll also provide you with the source code.

Tic tac toe game in java langugae

Tic toe game in java

As we know Tic tac toe game has 9 spots to put an 'X' and an 'O'. So to save the state of these spots, we can use an array having the length 9. We know that if there are three 'X' connected in a line then the player who turned his last move with 'X', is the winner.

After each turn, we have to output the state of the game. For this, we can show a board as shown below or we can make a GUI. I will make a cmd (command line) type game. I'll show you a GUI version in another post.

            |   |
-------------
| |
-------------
| |

We'll implement a two-player game. The player having the 'X' will turn his move first. along with each move, we will perform some checks to determine a win or draw. I have previously written a post on tic tac toe win detection. You should read from that to understand it better.

download the source code from here: Tic tac toe java source code.

In the src folder, we have two files Game.java and TTT.java. These two define the two classes Game and TTT respectively. TTT class behaves like a controller while the Game class structures the state of the game.

Methods declared in TTT class are-

  run()
showMSG()
processUserCMD()
processUserCMD(String cmd)
rename()
start()
restart()

and one static main() to
instantiate TTT object and call the
run()
And methods inside Game class
  processGame(String cmd)
drawBoard()
update()
check()
isFinshed()
setName()

TTT class takes commands in an infinite loop from the command-line interface. For this, Two overloaded methods are responsible for taking and processing the command as shown below.


   private void processUserCMD() {
while (true) {
String cmd = scanner.nextLine();
// remove leading and trailing spaces, if any exists.
cmd = cmd.trim();
processUserCMD(cmd);
}
}


private void processUserCMD(String cmd) {
switch (cmd) {
case "start":
start(); break;
case "restart":
restart(); break;
case "rename":
rename(); break;
case "exit":
System.out.println("Exited from the game!");
System.exit(0);
default:
if (game != null) {
game.processGame(cmd);
if (game.isFinished()) {
game = null;
}
}
}
}

In the main method, an object of TTT is instantiated and we call the run() method. run() method further calls the showMSG() and processUserCMD(). showMSG() print out all the available command to the command-line interface. processUserCMD() method takes and process the command in a loop.

Available commands are rename (to rename the name of players), start (To start a new game), restart( to restart the game) and exit ( to exit from the program).

Commands are processed in a switch case block and each command calls its corresponding method. In the default case, we check if the game is live if it is then we process the game.

No comments:

Post a Comment