I am a first semester progamming student having a bit of trouble with this parti
ID: 3625672 • Letter: I
Question
I am a first semester progamming student having a bit of trouble with this particular problem... the specs are below. thanks!* Write a program that allows the user to play a game of "tic-tac-toe"
against the computer.
* For ease, you can assume that the user will make the first move.
* For ease, you can assume that the user will be the "X" in the game.
* When it is the user's turn to move, the program should output the
game board, and should ask the user which square he/she wants to move into.
The user will enter a number from 1 to 9, representing
one of the nine squares. (The top row is 1, 2, 3,
the next row is 4, 5, 6, the bottom row is 7, 8, 9.)
* If the user chooses a square that was already taken, the program
should again ask the user to choose a square.
* When the computer makes a move, it must (of course) only move into
an empty square. Do not let the computer move into a square that is
already taken.
* For ease, you can assume that when the computer moves, it moves randomly.
* As soon as somebody (user or computer) wins, by putting 3 marks
in a row or column or diagonal, the program should announce the winner
(user or computer), and should output the game board, and
should immediately stop.
* If all 9 squares get filled, but nobody has won (i.e., a cat's game),
the program should announce a "cat's game", output the game board, and
should immediately stop.
* Right before the user is asked to make a move, the program should
output the current game board. It should use X's and O's.
The board MUST have this game board output pattern (of course, the X's and O's
should get based on the current player/computer game, this is sample X's O's):
X | O |
---+---+---
O | X | O
---+---+---
| | O
---------------------------------------------------------------------
HINTS:
======
* You could use String or int variables or array, to keep track of the moves.
* If you use String comparisons, recall that Java Strings compare with
.equals instead of the usual ==.
Example:
if (s.equals("X"))
instead of
if (s=="X")
* For functions, "return ____;" sets the function's return (i.e. answer) value,
and also immediately ends the function.
* For subroutines (including "main"), "return;" immediately ends
the subroutine.
* Code to generate a random integer from 1 to 9, including 1 and 9:
int r = (int) (Math.random() * 9) + 1;
* Code to generate a random integer from 0 to 8, including 0 and 8:
int r = (int) (Math.random() * 9);
Explanation / Answer
please rate - thanks
import java.util.*;
public class tictactoerandom
{
public static void main(String[] args)
{Random r=new Random();
char[][] matrix=new char[3][3]; /* the tic tac toe matrix */
char done;
System.out.println("This is the game of Tic Tac Toe.");
System.out.println("You will be playing against the computer.");
done = ' ';
init_matrix(matrix);
do {
disp_matrix(matrix);
get_player_move(matrix);
done = check(matrix); /* see if winner */
if(done!= ' ') break; /* winner!*/
disp_matrix(matrix);
get_computer_move(matrix,r);
done = check(matrix); /* see if winner */
} while(done== ' ');
if(done=='X') System.out.println("I won!");
else if(done=='Y')System.out.println("You won!!!!");
else System.out.println("We Tied!!!!");
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[][])
{ Scanner input = new Scanner(System.in);
int x, y,n;
char comma;
System.out.print("Enter a number between 1 and 9 for your move: ");
n=input.nextInt();
x=(n-1)/3;
y=(n+2)%3;
if(n<1||n>9||matrix[x][y]!= ' '){
System.out.println("Invalid move, try again.");
get_player_move(matrix);
}
else matrix[x][y] = 'X';
}
/* Get a move from the computer. */
public static void get_computer_move(char matrix[][],Random r)
{
int i, j=0,k=0;
int n=r.nextInt(9)+1;
i=(n-1)/3;
j=(n+2)%3;
while(matrix[i][j]!=' ')
{n=r.nextInt(9)+1;
i=(n-1)/3;
j=(n+2)%3;
}
matrix[i][j] = 'O';
System.out.println("Computer Move is "+ n );
}
/* 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]);
if(t!=2) System.out.println("---|---|---");
}
System.out.println();
}
/* See if there is a winner. */
public static char check(char matrix[][])
{
int i,j;
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];
for(i=0;i<3;i++)
for(j=0;j<3;j++)
if(matrix[i][j]==' ')
return ' ';
return 'T';
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.