- Get link
- X
- Other Apps
How to make Simple Weather Application in Android Studio
- Get link
- X
- Other Apps
Introduction
Tic-tac-toe is a very popular paper and pencil game, programmers often
make this game as a beginner for practice. As a starter in programming,
we feel very happy after completing our first tic-tac-toe game and we
enjoy a lot by playing our own made game with our friends and family
members.
In this blog, we will learn how to create tic-tac-toe game in
java.
Tic-Tac_Toe
First, we need to understand the logic of the game. In this game, we need
a 3 x 3 grid pattern like fig 1 and in the console, it will look like fig
2, the player who places three of their choices in vertical, horizontal,
or diagonal will be the winner of the game.
(fig 1) (fig 2)
Following are some steps we need to do, in order to create a
tic-toc-toe game.
1) Display Pattern
First, we need to display a pattern to the user. For this, we will create a class and then create a static method "display" in it. This method will display a pattern for us.
import java.util.Scanner;
public class Tictoc {
char tictoc[][]=new char[3][3];
//Display Fuction
public static void display(char tictoc[][]) {
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
System.out.print(tictoc[i][j]+" ");
}
System.out.println();
}
}
}
In order to show patter. We will first create "Tictoc" class object in "main" method and call the static display method to display patter.
Tictoc game=new Tictoc();
int counter=0;
for(int i=0 ; i<3; i++)
{
for(int j=0 ; j<3 ; j++)
{
game.tictoc[i][j]=Character.forDigit(++counter,10);
}
}
//Display Board
display(game.tictoc);
2) Take marker input from the user
In the above section, we successfully display pattern to the user (like fig a). Now it's time to take user choice. First, user one will enter his/her choice, the user can pick between 0 and 8, if user select 1 then in "replace" method we will change second index of tictoc[][] array to user one symbol "X" and with "display" method we will show updated pattern like fig b.
char input;
for(int i=0;i<4;i++)
{
System.out.print(user1+" Turn: ");
input=in.next().charAt(0);
replace(game.tictoc,input,user1mark);
display(game.tictoc);
System.out.print(user2+" Turn: ");
input=in.next().charAt(0);
replace(game.tictoc,input,user2mark);
display(game.tictoc);
}
System.out.print("User "+user1+": ");
input=in.next().charAt(0);
replace(game.tictoc,input,user1mark);
display(game.tictoc);
3) Final step is to check the winner
Now, the last step remaining is to check the winner. Here we will check, does any player place three of their choices in vertical, horizontal, or diagonal. If yes then we will declare this player winner, otherwise, we will display draw message to the user.
if(game.checkForWin())
System.out.print("We have a Winner");
else
System.out.print("Match is Draw");
That's it. Following is the complete source code of the game. Enjoy
Source Code
import java.util.Scanner;
public class Tictoc {
char tictoc[][]=new char[3][3];
//Display Fuction
public static void display(char tictoc[][]) {
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
System.out.print(tictoc[i][j]+" ");
}
System.out.println();
}
}
//Replace Function
static void replace(char arr[][], char find, char replace) {
for (int i = 0; i < 3 ; i++)
{
for(int j=0 ; j < 3 ; j++)
{
if (arr[i][j] == find)
{
arr[i][j] = replace;
return;
}
}
}
}
//check for win function
public boolean checkForWin() {
return (checkForRow() || checkForColomn() || checkForDiagnol());
}
//This function check row, colomn, diagnol for checkForRow, checForDiagnol, checkForColomn function
public boolean check(char c1,char c2, char c3)
{
return((c1==c2) && (c2==c3));
}
//check for row function
public boolean checkForRow() {
for(int i=0; i<3; i++)
{
if(check(tictoc[i][0],tictoc[i][1],tictoc[i][2])==true)
return true;
}
return false;
}
//check for colomn function
public boolean checkForColomn() {
for(int i=0; i<3; i++)
{
if(check(tictoc[0][i],tictoc[1][i],tictoc[2][0])==true)
return true;
}
return false;
}
//check for Diagnol function
public boolean checkForDiagnol()
{
return((check(tictoc[0][0],tictoc[1][1],tictoc[2][2])==true) || (check(tictoc[0][2],tictoc[1][1],tictoc[2][0])==true));
}
//main Function
public static void main(String[] args) {
Tictoc game=new Tictoc();
Scanner in=new Scanner(System.in);
//Select Player
String user1,user2;
char user1mark,user2mark;
System.out.print("Enter Player one Name: ");
user1=in.nextLine();
System.out.print("Enter Player two Name: ");
user2=in.nextLine();
//Mark Selection
System.out.println(user1+" Select Your Marker (O or X): ");
//System.out.println(name2+" Select Your Marker (O or X): ");
user1mark=in.next().charAt(0);
while(user1mark != 'X' && user1mark != 'x' && user1mark != 'O' && user1mark != 'o')
{
System.out.print("Invalid Input (Select O or X): ");
user1mark=in.next().charAt(0);
}
if(user1mark=='X' || user1mark=='x')
user2mark='O';
else
user2mark='X';
//initialize board
int counter=0;
for(int i=0 ; i<3; i++)
{
for(int j=0 ; j<3 ; j++)
{
game.tictoc[i][j]=Character.forDigit(++counter,10);
}
}
//Display Board
display(game.tictoc);
//Play
char input;
for(int i=0;i<4;i++)
{
System.out.print(user1+" Turn: ");
input=in.next().charAt(0);
replace(game.tictoc,input,user1mark);
display(game.tictoc);
System.out.print(user2+" Turn: ");
input=in.next().charAt(0);
replace(game.tictoc,input,user2mark);
display(game.tictoc);
}
System.out.print("User "+user1+": ");
input=in.next().charAt(0);
replace(game.tictoc,input,user1mark);
display(game.tictoc);
//Check For Win or Tie
if(game.checkForWin())
System.out.print("We have a Winner");
else
System.out.print("Match is Draw");
}
}
- Get link
- X
- Other Apps
Comments
very very useful
ReplyDeleteg
ReplyDeletemy is not working
ReplyDelete