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
EXPLAIN UNARY OPEARATORS
What is the Difference between in memory database and physical database
why we use abstract in awt?
diffrence between oracle apps , .NET , SAP
Is the IT field raise again? What is the position of IT after 4 years?
Given an array of size n, containing every element from 1 to n+1, except one. Find the missing element.
what is c sharp dotnet
any drawback are there in mantis?
how do find the user exit for selected feild whatis the process and can u plz explain it
write algo for cobol program whichuse three flat file to extract some specific information 8 marks mainframe
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
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)
when will triggars the at new event in abap and web dybn pro?
what is the purpose of checked Menu options
1.Mutating table