Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
5 views2 pages

Java

The document contains a Java program for a Tic Tac Toe game that allows two players to input their names and take turns entering their moves. The game checks for a winner after each move and announces the winner or declares a draw if all positions are filled without a winner. The program uses a 3x3 array to represent the game board and tracks player moves using strings.

Uploaded by

swativerma11146
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views2 pages

Java

The document contains a Java program for a Tic Tac Toe game that allows two players to input their names and take turns entering their moves. The game checks for a winner after each move and announces the winner or declares a draw if all positions are filled without a winner. The program uses a 3x3 array to represent the game board and tracks player moves using strings.

Uploaded by

swativerma11146
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

import java.util.

*;
class tictac
{

public static void main()


{
Scanner sc=new Scanner(System.in);
String arr[][]=new String[3][3];
int i,j,m,n,c=0;
String s0="",s1="",s2="",sp="";
String name1,name2;
String c0="",c1="",c2="";
System.out.println("Enter name of two players=");
name1=sc.next();
name2=sc.next();
for(i=1;i<10;i++)
{

if(i%2==0)
{
System.out.println("SECOND PERSON CHNACE TO ENTER");
System.out.println("enter the position=");
m=sc.nextInt();
n=sc.nextInt();
System.out.println("Enter the symbol=");
arr[m][n]=sc.next();
if((m==0&n==0)||(n==1&&m==0)||(n==2&&m==0))
s0=s0+arr[m][n];
if((m==1&&n==0)||(n==1&&m==1)||(n==2&&m==1))
s1=s1+arr[m][n];
if((m==2&&n==0)||(n==1&&m==2)||(n==2&&m==2))
s2=s2+arr[m][n];
if((n==0&&m==0)||(n==0&&m==1)||(n==0&&m==2))
c0=c0+arr[m][n];
if((n==1&&m==0)||(n==1&&m==1)||(n==1&&m==2))
c1=c1+arr[m][n];
if((n==2&&m==0)||(n==2&&m==1)||(n==2&&m==2))
c2=c2+arr[m][n];
if(m==n)
sp=sp+arr[m][n];
}
else
{
System.out.println("FIRST PERSON CHANCE TO ENTER");
System.out.println("enter the position=");
m=sc.nextInt();
n=sc.nextInt();
System.out.println("enter the symbol");
arr[m][n]=sc.next();
if((m==0&n==0)||(n==1&&m==0)||(n==2&&m==0))
s0=s0+arr[m][n];
if((m==1&&n==0)||(n==1&&m==1)||(n==2&&m==1))
s1=s1+arr[m][n];
if((m==2&&n==0)||(n==1&&m==2)||(n==2&&m==2))
s2=s2+arr[m][n];
if((n==0&&m==0)||(n==0&&m==1)||(n==0&&m==2))
c0=c0+arr[m][n];
if((n==1&&m==0)||(n==1&&m==1)||(n==1&&m==2))
c1=c1+arr[m][n];
if((n==2&&m==0)||(n==2&&m==1)||(n==2&&m==2))
c2=c2+arr[m][n];
if(m==n)
sp=sp+arr[m][n];
}
c++;
if(sp.equalsIgnoreCase("xxx")==true||s0.equalsIgnoreCase("xxx")==true||
s0.equalsIgnoreCase("xxx")==true||s1.equalsIgnoreCase("xxx")==true||
s2.equalsIgnoreCase("xxx")==true||c0.equalsIgnoreCase("xxx")==true||
c1.equalsIgnoreCase("xxx")==true||c2.equalsIgnoreCase("xxx")==true)
{
System.out.println("player 1 "+name1+" wins");
break;
}
if(sp.equalsIgnoreCase("xxx")==true||s0.equalsIgnoreCase("ooo")==true||
s0.equalsIgnoreCase("ooo")==true||s1.equalsIgnoreCase("ooo")==true||
s2.equalsIgnoreCase("ooo")==true||c0.equalsIgnoreCase("ooo")==true||
c1.equalsIgnoreCase("ooo")==true||c2.equalsIgnoreCase("ooo")==true)
{
System.out.println("player 2 "+name2+" wins");
break;
}
}
if(c==9)
System.out.println("nobody wins=");
}
}

You might also like