3D Tic-Tac-Toe
Getting Started
Install
Download the files from this repo, and execute them with your Java IDE, or from the console. This program requires JRE 1.8.0+.
Help
Just follow the prompts! The computer will ask you to input the three coordinates of the place where you want to enter the X
or the O
. It will say whose turn it is. You have to keep track of what is being placed where, since if you enter coordinates where an X
or an O
is already placed, your turn will be skipped. The game will display an error and exit if you enter out of bound coordinates.
How it Works
What it is
To know how the program works, we need to first understand what it actually does. This is a program to play a 3d version of tic-tac-toe with two players.
Variables
Note: All these variables are declared and initialised in
main()
itself. The variables used in other parts of the program are for local calculations only.
int x, y, z
: They are used to input and store the 3-d coordinates where the player enters theX
or theO
on his or her turn.int winner
: It stores the winner and is outputted after the game ends.int[][] z0, z1, z2
: These arrays are used to store the positions of all theX
s and theO
s. Basically, all three of them together make up the game board.
Functions
public static void main()
Most of the program code is in the main()
function. It does not return any value. It calls the other functions when and where necessary. All the important variables and arrays are declared in this method, and passed to other methods at the time of calling, if required. This is also the only method which is public
in the entire program, so that abstraction is implemented and the user does not get confused as to which program he has to run.
private void instructions()
Is used to display the rules and instructions for playing the game, at the beginning. It does not return any value.
private void ifinvalid(int x, int y, int z)
Checks if all of the three coordinates that the user has entered are valid or not. If they are invalid, exits the program.
private int[][] update0(int[][] z0, int x, int y, int player)
Updates the array z0[][]
with the coordinates entered by the player, by placing the player number at that coordinate. This only happens if there is no value previously at those coordinates. If that is the case, the player has to skip a turn as punishment. update1()
and update2()
modify arrays z1[][]
and z2[][]
respectively, also checking for the same condition as this method.
private boolean check(int[][] z0, int[][] z1, int[][] z2)
It checks whether the game has ended. This is done by a whole bunch of ifs
, checking for diagonals, rows or columns in 3 dimensions of the same player.