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

3.Give the formula for determine the range of the data type?

1 Answers  


What are events in smartforms?

0 Answers   Accenture,


This is the portal and portlet interview question API: is JSR 168 API Server: is ibm web sphere portal server6.0 IDE: is RAD 7.0 Question:In normal struts what is the actionservlet? Ans:org.apache.struts.action.ActionServlet. But What is the actionservlet in strutsportlet?

1 Answers   CTS, IBM,


what is programmable BIST in today ic design

0 Answers   IIT,


1. How many jobs can we write in a single file? 2. How many maximum members can exist in a single partition data set(pds) in jcl?

0 Answers   Steria,






what will be the model paper for the post of assistant programmer in delhi high court

2 Answers  


What is the difference between in-proc and out-of-proc?

2 Answers   Oracle,


Describe the following: (i) DNS (ii) Name Resolution (iii) Subnet Masking (iv) Urgent Pointer

1 Answers  


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

0 Answers   Oracle,


what does mean and stack mean in genral programming?

0 Answers  


Write a shell program where you enter a number which corresponds to K.M. Find out the corresponding values in m, cm, inches, and feet. Hints:- 1 k.m= 1000 m 1 m= 100 cm 1 inches= 2.54 cm. 1 feet= 12 inches

0 Answers  


hii I inntrested abt scjp certification but i dont knw abt scjp plz send details abt scjp exam and syllabus.

0 Answers  


Categories