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

Answers were Sorted based on User's Feedback



Create a class TicTacToe that will enable you to write a complete program to play the game of Tic..

Answer / 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

Create a class TicTacToe that will enable you to write a complete program to play the game of Tic..

Answer / usman ahmed

#include <iostream>
using namespace std;
const int boardSize = 3;
const int masksCounter = 8;
const bool mask[masksCounter][boardSize][boardSize] = {
{
{1, 1, 1},
{0, 0, 0},
{0, 0, 0}
},
{
{0, 0, 0},
{1, 1, 1},
{0, 0, 0}
},
{
{0, 0, 0},
{0, 0, 0},
{1, 1, 1}
},
{
{1, 0, 0},
{1, 0, 0},
{1, 0, 0}
},
{
{0, 1, 0},
{0, 1, 0},
{0, 1, 0}
},
{
{0, 0, 1},
{0, 0, 1},
{0, 0, 1}
},
{
{1, 0, 0},
{0, 1, 0},
{0, 0, 1}
},
{
{0, 0, 1},
{0, 1, 0},
{1, 0, 0}
}
};

enum {
None = 0,
Player1 = 1,
Player2 = 2,
Draw = 3
};

class TicTacToe {
public:
friend class Player; // Make Player class able to search in board field
TicTacToe();
bool set(int x, int y, int p);
int getStatus();
void displayBoard();
private:
// The class contains as private data a 3-by-3 two-dimensional array
// of integers.
int board[boardSize][boardSize];
}obj;

class Player {
public:
Player(int i, bool a = false) : id(i), artificial(a) {}
void move(TicTacToe & game);
private:
int id;
bool artificial;
};
void main()
{
obj.displayBoard();
system("pause");
}

Is This Answer Correct ?    13 Yes 13 No

Post New Answer

More Programming Languages AllOther Interview Questions

how to convert infix expression to prefix expression?

0 Answers   nvidia,


Write a Pseudo Code to find the angle between two hands of a clock for a given time.

6 Answers   Goldman Sachs, HCL,


What parameters are used to run a JCL JOB on a perticular DAY and TIME and DATE

1 Answers  


how will you code the subfile which is in editing mode (multiple case subfile)?

0 Answers   Bank Of America,


how MATLSB software suitable for electrical branch? which tools are useful??

1 Answers  






how many logical drive we can form to physical device in our computer?

1 Answers   nvidia,


how to stop execution of step 3 in a job mainframe

0 Answers  


what is the difference between java , sap ,.net , orecle apps ?

1 Answers  


what is jackson structured develepment?? and how is it different from OMT??

1 Answers  


why rownum=1 works properly but not rownum=>2,=>3 etc.

0 Answers   Oracle,


Plz sent me in .net 2.0 interview Question & answers?

0 Answers  


Can we write a method in JSP.If so how?

0 Answers   HCL,


Categories