Question

There is a problem with thecode. I get the error messages " 'board' was not declared...

There is a problem with thecode. I get the error messages

" 'board' was not declared in this scope",

\src\TicTacToe.cpp|7|error: expected unqualified-id before ')' token|

\src\TicTacToe.cpp|100|error: expected declaration before '}' token|

Here is the code

#ifndef TICTACTOE_H
#define TICTACTOE_H
#include

class TicTacToe {
private: char board [3][3];
public:
TicTacToe () {}
void printBoard();
void computerTurn(char ch);
bool playerTurn (char ch);
char winVerify();

};

************************************

TicTacToe.cpp

#endif // TICTACTOE_H

#include "TicTacToe.h"

#include

#include

using namespace std;

TicTacToe() {
for(int i = 0; i < 3; i++)
for (int j = 1; j < 3; j++)
board[i][j] = '0' + i*3 + j + 1;
}

void printBoard() {
for(int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++)
if (j != 2)
cout << board[i][j] << "|";
else
cout << board[i][j];
cout << endl;
for (int j = 0; j < 5 && i != 2; j++)
cout << "-";
cout << endl;
}
}

void computerTurn(char ch) {
if (ch != 'X' && ch != 'O') {
cout << "INVALID SYMBOL" << endl;
exit(1);
}
int i,j;
do {
i = rand() % 3;
j = rand() % 3;
} while (board[i][j] == 'O' || board[i][j] == 'X' );

board[i][j] = ch;
}

void playerTurn(char ch) {
if (ch != 'X' && ch != 'O') {
cout << "INVALID SYMBOL" << endl;
exit(1);
}
cout << "Enter a position: ";
int pos;
cin >> pos;
int i = pos/3;
int j = pos%3;
while (i < 0 || i > 2) {
cout << "Invalid position\nEnter a valid position: ";
cin >> pos;
i = pos/3;
j = pos%3;
}
board[i][j] = ch;
}

char winVerify() {
int i, j;

for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++)
if (board[i][j] != 'X')
break;
if (j == 3)
return 'X';

for (j = 0; j < 3; j++)
if (board[i][j] != 'O')
break;
if (j == 3)
return 'O';


for (j = 0; j < 3; j++)
if (board[j][i] != 'X')
break;
if (j == 3)
return 'X';

for (j = 0; j < 3; j++)
if (board[j][i] != 'O')
break;
if (j == 3)
return 'O';

}

if (board[0][0] == board[1][1] == board[2][2])
return board[1][1];


if (board[0][2] == board[1][1] == board[2][0])
return board[1][1];

return 0;
};
};

**************************Main.cpp

#include "TicTacToe.h"
#include
#include


using namespace std;

int main(int argc, char const *argv[])
{
int choice;
int count;
char winner = 0;
TicTacToe board1;
do {
cout << "0. Computer plays first" << endl;
cout << "1. User plays first" << endl;
cout << "Enter choice: " ;
cin >> choice;
} while(choice < 0 || choice > 1);

char comp;
char user;
if (choice == 0) {
comp = 'X';
user = 'O';
}
else {
comp = 'O';
user = 'X';
}

for (count = 0; count < 9 && winner == 0; count ++) {
board1.printBoard();
if (count % 2 == choice)
board1.computerTurn(comp);
else
board1.playerTurn(user);

winner = board1.winVerify();
}

if (winner == 0) {
cout << "DRAW" << endl;
}
else if (winner == comp) {
cout << "Computer wins" << endl;
}
else {
cout << "YOU won" << endl;
}
return 0;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Main.cpp:-
---------------------------------
#include "TicTacToe.h"
#include "TicTacToe.cpp"
#include<iostream>
using namespace std;
int main(int argc, char const *argv[])
{
int choice;
int count;
char winner = 0;
TicTacToe board1;
do
{
cout << "0. Computer plays first" << endl;
cout << "1. User plays first" << endl;
cout << "Enter choice: " ;
cin >> choice;
} while(choice < 0 || choice > 1);
char comp;
char user;
if (choice == 0)
{
comp = 'X';
user = 'O';
}
else
{
comp = 'O';
user = 'X';
}
for (count = 0; count < 9 && winner == 0; count ++)
{
board1.printBoard();
if (count % 2 == choice)
board1.computerTurn(comp);
else
board1.playerTurn(user);
winner = board1.winVerify();
}
if (winner == 0)
{
cout << "DRAW" << endl;
}
else if (winner == comp)
{
cout << "Computer wins" << endl;
}
else
{
cout << "YOU won" << endl;
}
return 0;
}

TicTacToe.cpp:-
--------------------------------
#include "TicTacToe.h"
#include<iostream>
#include<stdlib.h>
using namespace std;
TicTacToe::TicTacToe()
{
for(int i = 0; i < 3; i++)
for (int j = 1; j < 3; j++)
board[i][j] = '0' + i*3 + j + 1;
}
void TicTacToe::printBoard()
{
for(int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
if (j != 2)
cout << board[i][j] << "|";
else
cout << board[i][j];
cout << endl;
for (int j = 0; j < 5 && i != 2; j++)
cout << "-";
cout << endl;
}
}
void TicTacToe::computerTurn(char ch)
{
if (ch != 'X' && ch != 'O')
{
cout << "INVALID SYMBOL" << endl;
exit(1);
}
int i,j;
do
{
i = rand() % 3;
j = rand() % 3;
} while (board[i][j] == 'O' || board[i][j] == 'X' );
board[i][j] = ch;
}
bool TicTacToe::playerTurn(char ch)
{
if (ch != 'X' && ch != 'O')
{
cout << "INVALID SYMBOL" << endl;
exit(1);
}
cout << "Enter a position: ";
int pos;
cin >> pos;
int i = pos/3;
int j = pos%3;
while (i < 0 || i > 2)
{
cout << "Invalid position\nEnter a valid position: ";
cin >> pos;
i = pos/3;
j = pos%3;
}
board[i][j] = ch;
}
char TicTacToe::winVerify()
{
int i, j;
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
if (board[i][j] != 'X')
break;
if (j == 3)
return 'X';
for (j = 0; j < 3; j++)
if (board[i][j] != 'O')
break;
if (j == 3)
return 'O';
for (j = 0; j < 3; j++)
if (board[j][i] != 'X')
break;
if (j == 3)
return 'X';
for (j = 0; j < 3; j++)
if (board[j][i] != 'O')
break;
if (j == 3)
return 'O';
}
if (board[0][0] == board[1][1] == board[2][2])
return board[1][1];
if (board[0][2] == board[1][1] == board[2][0])
return board[1][1];
return 0;
}

TicTacToe.h:-
---------------------------
#ifndef TICTACTOE_H
#define TICTACTOE_H
#include<iostream>
class TicTacToe
{
private: char board[3][3];
public:
TicTacToe();
void printBoard();
void computerTurn(char ch);
bool playerTurn (char ch);
char winVerify();
};
#endif // TICTACTOE_H


Sample Output:-
-------------------------------
0. Computer plays first
1. User plays first
Enter choice: 16
0. Computer plays first
1. User plays first
Enter choice: 1
3|2|3
-----
|5|6
-----
|8|9
Enter a position: 15
Invalid position
Enter a valid position: 6
3|2|3
-----
|5|6
-----
X|8|9
3|2|3
-----
|5|6
-----
X|8|O
Enter a position: 1
3|X|3
-----
|5|6
-----
X|8|O
3|X|3
-----
|O|6
-----
X|8|O
Enter a position: 3
3|X|3
-----
X|O|6
-----
X|8|O
3|X|3
-----
X|O|6
-----
X|O|O
Enter a position: 2
3|X|X
-----
X|O|6
-----
X|O|O
O|X|X
-----
X|O|6
-----
X|O|O
Enter a position: 4
DRAW
--------------------------------
Process exited after 60.05 seconds with return value 0
Press any key to continue . . .

0. Computer plays first 1. User plays first Enter choice: 16 e. Computer plays first 1. User plays first Enter choice: 1 312x1ol6 x1810 3 X 3 x1ol6 xlolo Enter a position: 2 x1ol6 xlolo x1ol6 xlolo Enter a position: 4 DRAW Process exited after 60.05

Add a comment
Know the answer?
Add Answer to:
There is a problem with thecode. I get the error messages " 'board' was not declared...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • The Code is C++ // tic tac toe game #include <iostream> using namespace std; const int SIZE = 9; int check(char *); void displayBoard(char *); void initBoard(char *); int main() {    char board[...

    The Code is C++ // tic tac toe game #include <iostream> using namespace std; const int SIZE = 9; int check(char *); void displayBoard(char *); void initBoard(char *); int main() {    char board[SIZE];    int player, choice, win, count;    char mark;    count = 0; // number of boxes marked till now    initBoard(board);       // start the game    player = 1; // default player    mark = 'X'; // default mark    do {        displayBoard(board);        cout << "Player " << player << "(" << mark...

  • I've created a functioning program but I can't figure out how to implement class structure into...

    I've created a functioning program but I can't figure out how to implement class structure into it. I'm currently studying classes and I'm wondering if you could help me recreate my program with classes so I can have a better understanding of how classes work. Thank you. #pragma once #include <iostream> using namespace std;    bool DeclareResults(char ch, char gameboard[][3]);    bool FilledBoard(char gameboard[][3]);    void printGameBoard(char gameboard[][3]); #include "fnt.h"; #include <iostream> using namespace std; int main() {    //declare...

  • In the C++ programming language write a program capable of playing 3D Tic-Tac-Toe against the user....

    In the C++ programming language write a program capable of playing 3D Tic-Tac-Toe against the user. Your program should use OOP concepts in its design. Use Inheritance to create a derived class from your Lab #9 Tic-Tac-Toe class. You can use ASCII art to generate and display the 3x3x3 playing board. The program should randomly decide who goes first computer or user. Your program should know and inform the user if an illegal move was made (cell already occupied). The...

  • how to randomly generate moves until the whole board is filled?. If the board size is...

    how to randomly generate moves until the whole board is filled?. If the board size is c*r, you can only call the random function rand() c*r times (excluding the random function rand() that have been used in the provided code). Below is the code I need to convert. It is manual right now (user inputs numbers until whole board filled). I also have a piece of algorithmn below..but am unsure of how I can use it in the code: //Algorithm...

  • Am I getting this error because i declared 'n' as an int, and then asking it...

    Am I getting this error because i declared 'n' as an int, and then asking it to make it a double? This is the coude: #include "stdafx.h" #include <iostream> #include <fstream> #include <string> #include <algorithm> #include <vector> using namespace std; void sort(double grades[], int size); char calGrade(double); int main() {    int n;    double avg, sum = 0;;    string in_file, out_file;    cout << "Please enter the name of the input file: ";    cin >> in_file;   ...

  • I just need a help in replacing player 2 and to make it unbeatable here is my code: #include<i...

    I just need a help in replacing player 2 and to make it unbeatable here is my code: #include<iostream> using namespace std; const int ROWS=3; const int COLS=3; void fillBoard(char [][3]); void showBoard(char [][3]); void getChoice(char [][3],bool); bool gameOver(char [][3]); int main() { char board[ROWS][COLS]; bool playerToggle=false; fillBoard(board); showBoard(board); while(!gameOver(board)) { getChoice(board,playerToggle); showBoard(board); playerToggle=!playerToggle; } return 1; } void fillBoard(char board[][3]) { for(int i=0;i<ROWS;i++) for(int j=0;j<COLS;j++) board[i][j]='*'; } void showBoard(char board[][3]) { cout<<" 1 2 3"<<endl; for(int i=0;i<ROWS;i++) { cout<<(i+1)<<"...

  • Can somebody help me with this coding the program allow 2 players play tic Tac Toe....

    Can somebody help me with this coding the program allow 2 players play tic Tac Toe. however, mine does not take a turn. after player 1 input the tow and column the program eliminated. I want this program run until find a winner. also can somebody add function 1 player vs computer mode as well? Thanks! >>>>>>>>>>>>>Main program >>>>>>>>>>>>>>>>>>>>>>> #include "myheader.h" int main() { const int NUM_ROWS = 3; const int NUM_COLS = 3; // Variables bool again; bool won;...

  • I wrote program that have to block all moves for player 1, for game TicTacTOE AI,...

    I wrote program that have to block all moves for player 1, for game TicTacTOE AI, but it is not working. Can someone check what I have not eliminated? #include using namespace std; const int ROWS = 3; const int COLS = 3; int counter = 0; void aITurn(char b[][COLS]); void fillBoard(char board[ROWS][COLS]); void getChoice(char b[][COLS], bool playerToggle); bool gameOver(char b[][COLS]); void showBoard(char board[ROWS][COLS]); int main() {    ///main is complete, nothing to do here    char board[ROWS][COLS];    bool playerToggle =...

  • Similar to the Yahtzee project you completed earlier, in order to really understand the artificial intelligence...

    Similar to the Yahtzee project you completed earlier, in order to really understand the artificial intelligence and how to improve it; you must first have a good grasp on the game, its concepts, and its rules. For your first assignment, download the linked file below. This is a .cpp file of the game Tic-Tac-Toe. Tic-Tac-Toe Unzip the file, and run the game. Play a few games and begin to analyze the artificial intelligence that is currently programmed. Then, review the...

  • I need help solving this in c++ using visual Studio. For this assignment, you will be filling in missing pieces of code within a program, follow the comments in the code. These comments will describe...

    I need help solving this in c++ using visual Studio. For this assignment, you will be filling in missing pieces of code within a program, follow the comments in the code. These comments will describe the missing portion. As you write in your code, be sure to use appropriate comments to describe your work. After you have finished, test the code by compiling it and running the program, then turn in your finished source code. // TicTacToe.cpp: Follow along with...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT