Hi Java question for Tic Tac Toe Here is what I need done: 1-Keep score for both
ID: 3733344 • Letter: H
Question
Hi
Java question for Tic Tac Toe
Here is what I need done:
1-Keep score for both the human and the computer. You will need to do some additional programming to determine that the game has ended when the human wins,
or the computer wins or that it is a cat (tie) game.
2-Disable buttons as they are played. When game is done, only the New Game button will be enabled. (I think this one is done, although I don't mind a better way?)
3-Add logic so the computer makes smart (intelligent) moves. (Not sure how to do this one? get rid of the rand?) Thank you,
Explanation / Answer
Solution:
code:
package chegg;
import java.util.Scanner;
public class TicTacToe {
int col,row; //declared two variable for row and column input from user
int count = 1; //counter for nine places of tic-tac-toe
char a[][] = new char[3][3]; //2D array for nine places
//main function
public static void main(String[] args) {
//create an object 't' of class TicTacToe
TicTacToe t = new TicTacToe();
t.BlankDisplay(); //call the method to show a blank tic-tac-toe with underscore
t.InsertMarker(); //call the method to insert markers
}
//displays the blank tic-tac-toe with underscore
public void BlankDisplay()
{
for(int i = 0;i<3;i++){
for(int j=0;j<3;j++)
{
a[i][j]='_';
System.out.print(" "+a[i][j]);
}
System.out.println();
}
}
//method to store value X or 0, given by the user one by one
public void InsertMarker()
{
while(count<=9){ //counter for nine values
if(count%2!=0){
Scanner sc = new Scanner(System.in);
System.out.println("Player X's turn!"); //input for X
System.out.println("Enter the row");
row = sc.nextInt();
row--;
System.out.println("Enter the column");
col = sc.nextInt();
col--;
for(int i = 0;i<3;i++){
for(int j=0;j<3;j++)
{//insertion of X at given location of user
if((row==i)&&(col==j)){
if(a[i][j]=='_'){
a[row][col]='X';
System.out.print(" "+a[row][col]);
}
else{//check for already filled position
System.out.println("Already filled, Choose another position");
count--;
}
}
else
System.out.print(" "+a[i][j]);
}
System.out.println();
}
count++;
}
else{
Scanner sc = new Scanner(System.in); //input for 0
System.out.println("Player 0's turn!");
System.out.println("Enter the row");
row = sc.nextInt();
row--;
System.out.println("Enter the column");
col = sc.nextInt();
col--;
for(int i = 0;i<3;i++){
for(int j=0;j<3;j++)
{//insertion of 0 at given location of user
if((row==i)&&(col==j)){
if(a[i][j]=='_'){
a[row][col]='0';
System.out.print(" "+a[row][col]);
}
else{//check for already filled position
System.out.println("Already filled, Choose another position");
count--;
}
}
else
System.out.print(" "+a[i][j]);
}
System.out.println();
}
count++;
}
if(count>5)
checkStatus();//call method to check who wins or draw
}
}
//method to check status of win or draw
public void checkStatus()
{
if((a[0][0]==a[0][1])&&(a[0][1]==a[0][2])){
System.out.println("First Row Complete. Player "+a[0][0]+" wins");
System.exit(0);
}
else if((a[1][0]==a[1][1])&&(a[1][1]==a[1][2])){
System.out.println("Second Row Complete. Player "+a[1][0]+" wins");
System.exit(0);
}
else if((a[2][0]==a[2][1])&&(a[2][1]==a[2][2])){
System.out.println("Third Row Complete. Player "+a[2][0]+" wins");
System.exit(0);
}
else if((a[0][0]==a[1][0])&&(a[1][0]==a[2][0])){
System.out.println("First Column Complete. Player "+a[0][0]+" wins");
System.exit(0);
}
else if((a[0][1]==a[1][1])&&(a[1][1]==a[2][1])){
System.out.println("Second Column Complete. Player "+a[0][1]+" wins");
System.exit(0);
}
else if((a[0][2]==a[1][2])&&(a[1][2]==a[2][2])){
System.out.println("Third Column Complete. Player "+a[0][2]+" wins");
System.exit(0);
}
else if((a[0][0]==a[1][1])&&(a[1][1]==a[2][2])){
System.out.println("First diagonal Complete. Player "+a[0][0]+" wins");
System.exit(0);
}
else if((a[0][2]==a[1][1])&&(a[1][1]==a[2][0])){
System.out.println("Second diagonal Complete. Player "+a[0][1]+" wins");
System.exit(0);
}
else if(count==9){
System.out.println("Its a draw");
System.exit(0);
}
}
}
Output:
This is a tic-tac-toe game
You are playing against the computer!
Enter 1-9 to indicate your move
| |
-----
| |
-----
| |
Your move
Please enter your move(0-9):
1
|X|
-----
| |
-----
| |
Computer move: 8
|X|
-----
| |
-----
| |O
Your move
Please enter your move(0-9):
3
|X|
-----
X| |
-----
| |O
Computer move: 5
|X|
-----
X| |O
-----
| |O
Your move
Please enter your move(0-9):
2
|X|X
-----
X| |O
-----
| |O
Computer move: 7
|X|X
-----
X| |O
-----
|O|O
Your move
Please enter your move(0-9):
6
|X|X
-----
X| |O
-----
X|O|O
Computer move: 0
O|X|X
-----
X| |O
-----
X|O|O
Your move
Please enter your move(0-9):
I hope this helps if you find any problem. Please comment below. Don't forget to give a thumbs up if you liked it. :)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.