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
I am taking the bmc control m/enterprise manager 7.0 scheduling test and just wanted to see what kind of questions they would ask or if anyone has taken the test and how long it is for how many questions?
what is the diff bw sql direct and jdbc update can't we do select and updating operation in sql direct
what are the activities you enjoy most and How do you see these Developing in the Future with Reference to in your work life and in your personal life
Which method protects back button to retrieve old value from previous page in Struts.
Can any one give an example (Source Code) on virtual function implemetation in Java?
What for decision coverage and modified condition decision coverage are used? Wat is the difference between them?
Describe the difference between Interface-oriented, Object-oriented and Aspect-oriented programming
please any one pass file aid,xpeditor and endeavor tools
Wrtite a JCL for sorting a file with start from 36 postion lenth 9 excluding a num eq to 98768. for 3 marks mainframe
what is d main diff between the java and .net framework
what are resources in case of Threads
What is Negative testing?
what is session state?
What is BASIS
How to change the color of a cell or a row in a datagrid on mouse hover using javascript/.net