Create a class TicTacToe that will enable you to write a
complete program to play the
game of Tic-Tac-Toe. The class contains as private data a 3-
by-3 double array of integers. The constructor
should initialize the empty board to all zeros. Allow two
human players. Wherever the first
player moves, place a 1 in the specified square; place a 2
wherever the second player moves. Each
move must be to an empty square. After each move determine
whether the game has been won and
whether the game is a draw. If you feel ambitious, modify
your program so that the computer makes
the moves for one of the players automatically. Also, allow
the player to specify whether he or she
wants to go first or second. If you feel exceptionally
ambitious, develop a program that will play
three-dimensional Tic-Tac-Toe on a 4-by-4-by-4
Answer Posted / sameen javed
public class tictactoe {
private char[][] gameBoard = new char[3][3];
private final char Nothing = ' ';
private final char PLAYER1 = 'X';
private final char PLAYER2 = 'O';
public void start() {
for (int x = 0; x < 3; x++) {
for (int y = 0; y < 3; y++) {
gameBoard[x][y] = Nothing;
}
}
}
public char winr() {
for (int i = 0; i < gameBoard.length; i++) {
/* check horizontally */
if (gameBoard[i][0] == gameBoard[i][1] &&
gameBoard[i][0] == gameBoard[i][2]) {
if (gameBoard[i][0] != Nothing) {
return gameBoard[i][0];
}
}
/* check vertically */
if (gameBoard[0][i] == gameBoard[1][i] &&
gameBoard[0][i] == gameBoard[2][i]) {
if (gameBoard[0][i] != Nothing) {
return gameBoard[0][i];
}
}
}
/* check diagonally */
if (gameBoard[0][0] == gameBoard[1][1] &&
gameBoard[0][0] == gameBoard[2][2]) {
if (gameBoard[0][0] != Nothing) {
return gameBoard[0][0];
}
}
if (gameBoard[0][2] == gameBoard[1][1] &&
gameBoard[0][2] == gameBoard[2][0]) {
for (int i = 0; i < gameBoard.length; i++) {
char[] cs = gameBoard[i];
}
if (gameBoard[0][2] != Nothing) {
}
{
return gameBoard[0][2];
}
}
return Nothing;
}
}
Is This Answer Correct ? | 16 Yes | 16 No |
Post New Answer View All Answers
What is Negative testing?
what is delimiter in sas ?
Write code to read the records from a file and load any array of size 99?make sure that you take care of all the error conditions?
5. How do you round the addition or subtraction of two numbers in assembler?
When we have two versions of the dot net installed how does the compiler know which version of DLL it has to select to an application.
what is best way to create a Thread class & why?
WS-NUM PIC S9(05)V(02) SIGN TRAILING SEPARATE MOVE '0050000+' TO WS-NUM The value stored is 00500,00+ MOVE '0050000-' TO WS-NUM Then what is the value will be stored in WS-NUM? Am getting '-00500,00'.....>>> What should I declare to WS-NUM so that I can get correct values for both + & - signs.
In JCl , we have COND parameter.This holds comparison code and condition.It also has only and even parameters. ex: COND((4,GE),EVEN).what the comma mean here. is that 'and' or 'or'.
what are the advantages of sap on other software ?
I want Ada programming language books. Could anyone post me any link for that?
Plz sent me in .net 2.0 interview Question & answers?
how does database connection using ADO.NET?
When we use Windows authentication mode
Define Form modules, Class modules and Standard modules
Colors specified with the notation