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

Suppose i have all the implementation code required is written in doGet() but my class has doPost() method. i need code implemented in doGet() how can we do that?

1 Answers  


9.Difference between even and odd signals?explain with the diagram?

0 Answers  


Given an array of size n. It contains numbers in the range 1 to n. Find the numbers which aren?t present.

0 Answers   Amazon,


code for connection from windows forms to sql server

0 Answers   ME,


what is web service in java? have u use before.

0 Answers  






Iam using Microsoft Visual Studio to create a system for mobile store I want to know how to calculate mobile price that the customer buy and how to reduce quantity from the data base that we have for mobile .And also how to calculate revnue for each mobile and revnue for the total mobile

0 Answers  


How to print a name "ARCHANA" in any programming language

6 Answers   Value Labs,


How can we alter the data after creating a view

1 Answers  


what is the difference between read the data from table and infotype

0 Answers  


what is the abap/4

1 Answers  


how we define two jobs have same name??is it exist??

0 Answers   CTS,


How can you incorporate a Datawindow to a Oracle8i stored procedure?

0 Answers   IBM,


Categories