0% found this document useful (0 votes)
388 views3 pages

TicTacToe in BlueJ

This document contains the code for a Tic Tac Toe game in Java. It defines classes and methods to: 1) Represent the game board as a 2D array and print it out 2) Check for a win by looking at rows, columns and diagonals 3) Allow either single player vs computer mode or multiplayer mode where players take turns 4) Generate random moves for the computer and check for wins after each turn

Uploaded by

shivam Gautam
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)
388 views3 pages

TicTacToe in BlueJ

This document contains the code for a Tic Tac Toe game in Java. It defines classes and methods to: 1) Represent the game board as a 2D array and print it out 2) Check for a win by looking at rows, columns and diagonals 3) Allow either single player vs computer mode or multiplayer mode where players take turns 4) Generate random moves for the computer and check for wins after each turn

Uploaded by

shivam Gautam
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/ 3

import java.io.

*;
public class TicTacToe
{
char a[][]=new char[3][3];
int p=0;char nn;
public void Board()
{
for(int i=0;i<3;i++)
{
System.out.println(" | | ");
System.out.println(" "+a[i][0]+" | "+a[i][1]+" | "+a[i][2]);
if(i!=2)
System.out.println("___|___|___");
else System.out.println(" | | ");
}
}

public boolean check(int ch,int n)


{
char a1[]=new char[4];
int g[]=new int[4];
int i,j,k;
for(i=0;i<4;i++)
g[i]=0;
for(i=0;i<3;i++)
{g[0]=0;g[1]=0;
for(j=0;j<3;j++)
{
if(ch==a[i][j])
++g[0];else a1[0]=a[i][j];
if(ch==a[j][i])
++g[1];else a1[1]=a[j][i];
if(i==j)
{if(ch==a[i][j])
++g[2];else a1[2]=a[i][j];}
if((i+j)==2)
{ if(ch==a[i][j])
++g[3];else a1[3]=a[i][j];}
}
for(k=0;k<4;k++)
if(g[k]==n)
{if(Character.isDigit(a1[k])&&n==2)
{nn=a1[k];return(true);}
else if(n==3)
return(true);
}}return(false);
}

boolean Checkwin(char ch)


{return(check(ch,3));}

void com()
{char x;
if(check('O',2))
assign(nn,'O');
else if(check('X',2))
assign(nn,'O');
else
do
{
x=(char)((int)(Math.random()*9)+48);
}while(assign(x,'O')==false);
}

boolean assign(char x, char ch)


{
int m,n;
for(m=0;m<3;m++)
for(n=0;n<3;n++)
if(a[m][n]==x)
{
a[m][n]=ch;++p;Board();return(true);
}
return(false);
}

void ttt()throws IOException


{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int i,j,k,ch,d=0;p=0;
char x='0';
boolean flag=false;
String nm[]=new String[2];
char v[]={'X','O'};
for(i=0;i<3;i++)
for(j=0;j<3;j++)
a[i][j]=++x;
System.out.println("Let's play,Tic Tac Toe");
System.out.println("Single Player=1 Multiplayer=2");
ch=Integer.parseInt(br.readLine());
if(ch==1)
{System.out.println("Enter Your Name");
nm[0]=br.readLine();}
if(ch==2)
{System.out.println("Enter Your Names, One by One");
nm[0]=br.readLine();
nm[1]=br.readLine();
System.out.println("Let's Decide whose turn will be the first one");
d=(int)(Math.random()*2);
System.out.println(nm[d]+" will play first");}
Board();
while(flag==false&&p<=9)
{
if(ch==1)
{
System.out.println(nm[0]+", Enter your no.");
x=br.readLine().charAt(0);
while(assign(x,'X')==false)
{System.out.println("Sorry ,entered no. not valid.Please Try
Again");
x=br.readLine().charAt(0);}
if(Checkwin('X'))
{System.out.println("Congratulations "+nm[0]+" You Won The
Match!!");flag=true;}
else if(p<9)
{
System.out.println("Computer's Turn");
com();
if(Checkwin('O'))
{System.out.println("Computer Won The Match. Better Luck Next
Time "+nm[0]);flag=true;}
}
}
if(ch==2)
{
for(k=d;k<=1;k++)
{ if(p>=9)
k=3;
System.out.println(nm[k]+" Enter your no.");
x=br.readLine().charAt(0);
while(assign(x,v[k])==false)
{System.out.println("Sorry ,entered number is not valid.Please
Try Again");
x=br.readLine().charAt(0);}
if(Checkwin(v[k]))
{System.out.println("Congratulations "+nm[k]+" You Won The
Match!!");flag=true;k=3;}
}d=0;
}
if(p==9&&flag==false)
{System.out.println("Match Draw");flag=true;}
}}}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy