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


Please Help Members By Posting Answers For Below Questions

EXPLAIN UNARY OPEARATORS

1792


What is the Difference between in memory database and physical database

2070


why we use abstract in awt?

1550


diffrence between oracle apps , .NET , SAP

1593


Is the IT field raise again? What is the position of IT after 4 years?

1669






Given an array of size n, containing every element from 1 to n+1, except one. Find the missing element.

638


what is c sharp dotnet

1731


any drawback are there in mantis?

1636


how do find the user exit for selected feild whatis the process and can u plz explain it

1715


write algo for cobol program whichuse three flat file to extract some specific information 8 marks mainframe

1554


I need source code for Enrollment System using Visual Basic 6.0/2008 database MS Access 2007 for my school thesis project...please help me..kindly send in my email jpinedamcp@gmail.com

1576


One boy has to climb steps. He can climb 1 or 2 steps at a time. Write a function that will returns number of way a boy can climb the steps. Int WaytoSteps(int n) (eg:- suppose number of steps is n=4 ,the function will return 5 (one-one-one-one ,one-one-two, one-two-one-,two-one-one, two-two)

2985


when will triggars the at new event in abap and web dybn pro?

1481


what is the purpose of checked Menu options

1970


1.Mutating table

1366