Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

hello i have finally finished my tic tac toe game code and went running it, it keeps giving me an error undefined reference to

hello i have finally finished my tic tac toe game code and went running it, it keeps giving me an error " undefined reference to winmain@16" i am on eclipse program can you see if i have mistake in code

#include

#include

#include

#include

using namespace std;

enum status { WIN, DRAW, CONTINUE };

class ticTacToe

{

public:

void play();

private:

void displayBoard() const;

bool isValidMove(int row, int col) const;

bool getXOMove(char playerSymbol);

status gameStatus();

char board[3][3] = { {' ', ' ', ' '},

{' ', ' ', ' '},

{' ', ' ', ' '} };

int noOfMoves = 0;

};

void ticTacToe::play()

{

srand(time(0));

bool done = false;

char player;

if (rand() % 2 == 0) {

player = 'X';

}

else {

player = 'O';

}

displayBoard();

while (!done) {

done = getXOMove(player);

if (player == 'X') {

player = 'O';

}

else {

player = 'X';

}

}

}

void ticTacToe::displayBoard() const

{

cout << setw(3) << "1" << setw(3) << "2" << setw(3) << "3" << endl;

for (int row = 0; row < 3; row++) {

cout << setw(3) << row + 1;

for (int col = 0; col < 3; col++) {

cout << setw(3) << board[row][col];

}

cout << endl;

}

cout << endl;

}

bool ticTacToe::isValidMove(int row, int col) const

{

if (row < 0 || row > 2 || col < 0 || col > 2) {

return false;

}

if (board[row][col] != ' ') {

return false;

}

return true;

}

bool ticTacToe::getXOMove(char playerSymbol)

{

int row, col;

cout << "Player " << playerSymbol << ", enter your move (row column): ";

cin >> row >> col;

row--;

col--;

if (!isValidMove(row, col)) {

cout << "Invalid move. Please try again." << endl;

return false;

}

board[row][col] = playerSymbol;

noOfMoves++;

displayBoard();

status gameStatus = this->gameStatus();

if (gameStatus == WIN) {

cout << "Player " << playerSymbol << " wins!" << endl;

return true;

}

else if (gameStatus == DRAW) {

cout << "Game is a draw." << endl;

return true;

}

return false;

}

status ticTacToe::gameStatus()

{

for (int row = 0; row < 3; row++) {

if (board[row][0] != ' ' && board[row][0] == board[row][1] && board[row][1] == board[row][2]) {

return WIN;

}

}

for (int col = 0; col < 3; col++) {

if (board[0][col] != ' ' && board[0][col] == board[1][col] && board[1][col] == board[2][col]) {

return WIN;

}

}

if (board[0][0] != ' ' && board[0][0] == board[1][1] && board[1][1] == board[2][2]) {

return WIN;

}

if (board[2][0] != ' ' && board[2][0] == board[1][1] && board[1][1] == board[0][2]) {

return WIN;

}

if (noOfMoves == 9) {

return DRAW;

}

return CONTINUE;

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions