create a program that will have a player play tic-tac-toe against the computer.
ID: 3626690 • Letter: C
Question
create a program that will have a player play tic-tac-toe against the computer. The interface needs to be basic, using text characters, but you’re welcome to create a graphical user interface if you wish.Program input:
• User selects X or O
• User selects who goes first
• Position where user wants to place X or O in. Position should be something like A1, which means column A row 1.
Program output:
• The board needs to be displayed. Something close to:
| |
| | 1
----------------------------
| |
| | 2
----------------------------
| |
| | 3
A B C
Requirements
• User should be able to start a new game after he’s done.
• User should be able to call a draw or quit a game before it ends
• Once either the computer or the user wins, a message should be displayed stating who won. If it’s a tie, the message should state that the game ended in a tie
Here is a link that demonstrates how to play the game online.
http://boulter.com/ttt/index.cgi
Explanation / Answer
please rate - thanks
import java.util.*;
public class tictactoe
{
public static void main(String[] args)
{
char[][] matrix=new char[3][3]; /* the tic tac toe matrix */
boolean first;
char done,player,computer;
System.out.println("This is the game of Tic Tac Toe.");
System.out.println("You will be playing against the computer.");
System.out.print("do you want to be X or Y? ");
Scanner in=new Scanner(System.in);
player=in.nextLine().charAt(0);
player=Character.toUpperCase(player);
if(player=='X')
computer='Y';
else
{player='Y';
computer='X';
}
done = ' ';
init_matrix(matrix);
System.out.print("Do you want to go first? (Y/N) ");
if(Character.toUpperCase(in.nextLine().charAt(0))!='Y')
{ disp_matrix(matrix);
get_computer_move(matrix,computer);
}
do {
disp_matrix(matrix);
get_player_move(matrix,player);
done = check(matrix); /* see if winner */
if(done!= ' ') break; /* winner!*/
disp_matrix(matrix);
get_computer_move(matrix,computer);
done = check(matrix); /* see if winner */
} while(done== ' ');
if(done=='X') System.out.println("You won!");
else System.out.println("I won!!!!");
disp_matrix(matrix); /* show final positions */
}
/* Initialize the matrix. */
public static void init_matrix(char matrix[][])
{
int i, j;
for(i=0; i<3; i++)
for(j=0; j<3; j++) matrix[i][j] = ' ';
}
/* Get a player's move. */
public static void get_player_move(char matrix[][],char p)
{ Scanner input = new Scanner(System.in);
int x, y;
char a;
String move;
System.out.print("Enter X,Y coordinates for your move: ");
move=input.nextLine();
move=move.substring(0,1).toUpperCase() + move.substring(1);
x=move.charAt(0)-'A';
y=move.charAt(1)-'0'-1;
if(matrix[x][y]!= ' '){
System.out.println("Invalid move, try again.");
get_player_move(matrix,p);
}
else matrix[x][y] = p;
}
/* Get a move from the computer. */
public static void get_computer_move(char matrix[][],char p)
{
int i, j=0,k=0;
for(i=0; i<3; i++){
for(j=0; j<3; j++)
if(matrix[i][j]==' ')
{ matrix[i][j] = p;
System.out.println("Computer Move is "+ (char)(i+'A') +""+(j+1));
k=1;
break;
}
if(k==1)
break;
}
if(i*j==9) {
System.out.println("draw");
System.exit(0);
}
}
/* Display the matrix on the screen. */
public static void disp_matrix(char matrix[][])
{
int t;
for(t=0; t<3; t++) {
System.out.println(" "+matrix[t][0]+" | "+ matrix[t][1]+" | "+ matrix [t][2]+" "+(t+1));
if(t!=2) System.out.println("---|---|---");
}
for(t='A';t<='C';t++)
System.out.print((char)t+" ");
System.out.println();
}
/* See if there is a winner. */
public static char check(char matrix[][])
{
int i;
for(i=0; i<3; i++) /* check rows */
if(matrix[i][0]==matrix[i][1] &&
matrix[i][0]==matrix[i][2]) return matrix[i][0];
for(i=0; i<3; i++) /* check columns */
if(matrix[0][i]==matrix[1][i] &&
matrix[0][i]==matrix[2][i]) return matrix[0][i];
/* test diagonals */
if(matrix[0][0]==matrix[1][1] &&
matrix[1][1]==matrix[2][2])
return matrix[0][0];
if(matrix[0][2]==matrix[1][1] &&
matrix[1][1]==matrix[2][0])
return matrix[0][2];
return ' ';
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.