Making a Multiplayer Tic Tac Toe game using JAVA
Hey guys, Welcome to my blog one more time and today, I am going to share the code for our very famous and time-pass GAME which is Tic Tac Toe.
It took me almost 3 hours to create the code which is bug free. It is a console application so you can just copy the code and run it on any java editor or you may import this project from the link given below.
Source package
import java.util.*;
public class play {
int move;
int count = 0;
int coun = 0;
Scanner sc = new Scanner(System.in);
String p1;
String p2;
char[][] arr = { {'1','2','3'}, //that is the main interface of the game
{'4','5','6'},
{'7','8','9'}
};
public play() {
welcome(); //welcome screen
getNames(); //getting names of the players
beginGame(p1,p2); //begins the game
}
public void welcome() {
System.out.println("Hey this is Akash's GAME");
System.out.println("this is a two player game so lets get started enter the move number where you want to locate your symbol ");
System.out.println("below is the interface of the program");
}
public void getNames() {
System.out.println("Player 1 please enter your name");
p1 = sc.next();
System.out.println("Player 2 please enter your name");
p2 = sc.next();
}
public void beginGame(String p1,String p2) {
interfaceOfGame(arr);
while(true) {
boolean ans = false;
do {
System.out.println(p1 + ", Its your turn");
ans = move(p1,'X');
}while(!ans);
interfaceOfGame(arr);
if(checkWin(p1)) //check winning conditions and get out of loop when one of either wins
break;
do {
System.out.println(p2 + ", Its your turn");
ans = move(p2,'O');
}while(!ans);
interfaceOfGame(arr);
if(checkWin(p2)) //check winning conditions and get out of loop when one of either wins
break;
}
}
public boolean checkWin(String p) {
int b = 0;
for(int i = 0; i< 3; i++) {
if((arr[i][0] == arr[i][1] && arr[i][1] == arr[i][2]) ||
((arr[0][i] == arr[1][i] && arr[1][i] == arr[2][i])) ||
((arr[0][0] == arr[1][1] && arr[1][1] == arr[2][2])) ||
((arr[0][2] == arr[1][1] && arr[1][1] == arr[2][0]))
) {
b = 1;
}
}
if(b == 1) {
System.out.println("Congrats!!! " + p + " wins");
return true;
}
else if(coun == 9) {
System.out.println("Match draws!! thanks for playing");
return true;
}
else
return false;
}
public boolean move(String p,char sym) {
int n = sc.nextInt();
char temp = sym;
n--;
if(arr[n/3][n%3] == 'O' || arr[n/3][n%3] == 'X' || n > 9 || n < 0 ) { //the program will not execute further until a valid move
System.out.println("Invalid Move");
return false;
}
else {
if(p == p1) {
coun++;
arr[n/3][n%3] = sym;
}
if(p == p2) {
coun++;
arr[n/3][n%3] = sym;
}
}
return true;
}
public void interfaceOfGame(char[][] arr) {
for(int i = 0; i < 3; i++) {
int j;
for(j = 0; j < 3; j++) {
System.out.printf("| %c |",arr[i][j]);
count++;
}
System.out.println(" ");
if(count == 3 || count == 6) {
System.out.println("---------------");
}
}
}
public static void main(String[] args) {
play playTheGame = new play(); //creating the play object and running the program through constructor
}
}
/////******Code Ends*****/////
It took me almost 3 hours to create the code which is bug free. It is a console application so you can just copy the code and run it on any java editor or you may import this project from the link given below.
Source package
import java.util.*;
public class play {
int move;
int count = 0;
int coun = 0;
Scanner sc = new Scanner(System.in);
String p1;
String p2;
char[][] arr = { {'1','2','3'}, //that is the main interface of the game
{'4','5','6'},
{'7','8','9'}
};
public play() {
welcome(); //welcome screen
getNames(); //getting names of the players
beginGame(p1,p2); //begins the game
}
public void welcome() {
System.out.println("Hey this is Akash's GAME");
System.out.println("this is a two player game so lets get started enter the move number where you want to locate your symbol ");
System.out.println("below is the interface of the program");
}
public void getNames() {
System.out.println("Player 1 please enter your name");
p1 = sc.next();
System.out.println("Player 2 please enter your name");
p2 = sc.next();
}
public void beginGame(String p1,String p2) {
interfaceOfGame(arr);
while(true) {
boolean ans = false;
do {
System.out.println(p1 + ", Its your turn");
ans = move(p1,'X');
}while(!ans);
interfaceOfGame(arr);
if(checkWin(p1)) //check winning conditions and get out of loop when one of either wins
break;
do {
System.out.println(p2 + ", Its your turn");
ans = move(p2,'O');
}while(!ans);
interfaceOfGame(arr);
if(checkWin(p2)) //check winning conditions and get out of loop when one of either wins
break;
}
}
public boolean checkWin(String p) {
int b = 0;
for(int i = 0; i< 3; i++) {
if((arr[i][0] == arr[i][1] && arr[i][1] == arr[i][2]) ||
((arr[0][i] == arr[1][i] && arr[1][i] == arr[2][i])) ||
((arr[0][0] == arr[1][1] && arr[1][1] == arr[2][2])) ||
((arr[0][2] == arr[1][1] && arr[1][1] == arr[2][0]))
) {
b = 1;
}
}
if(b == 1) {
System.out.println("Congrats!!! " + p + " wins");
return true;
}
else if(coun == 9) {
System.out.println("Match draws!! thanks for playing");
return true;
}
else
return false;
}
public boolean move(String p,char sym) {
int n = sc.nextInt();
char temp = sym;
n--;
if(arr[n/3][n%3] == 'O' || arr[n/3][n%3] == 'X' || n > 9 || n < 0 ) { //the program will not execute further until a valid move
System.out.println("Invalid Move");
return false;
}
else {
if(p == p1) {
coun++;
arr[n/3][n%3] = sym;
}
if(p == p2) {
coun++;
arr[n/3][n%3] = sym;
}
}
return true;
}
public void interfaceOfGame(char[][] arr) {
for(int i = 0; i < 3; i++) {
int j;
for(j = 0; j < 3; j++) {
System.out.printf("| %c |",arr[i][j]);
count++;
}
System.out.println(" ");
if(count == 3 || count == 6) {
System.out.println("---------------");
}
}
}
public static void main(String[] args) {
play playTheGame = new play(); //creating the play object and running the program through constructor
}
}
/////******Code Ends*****/////
The code above you can run it using any java editor or cmd.
thanks..and Good Luck..!!
Comments
Post a Comment