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...

  • 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)<<"...

  • C++ getline errors I am getting getline is undefined error messages. I would like the variables...

    C++ getline errors I am getting getline is undefined error messages. I would like the variables to remain as strings. Below is my code. #include <iostream> #include<string.h> using namespace std; int index = 0; // variable to hold how many customers are entered struct Address //Structure for the address. { int street; int city; int state; int zipcode; }; // Customer structure struct Customer { string firstNm, lastNm; Address busAddr, homeAddr; }; // Functions int displayMenu(); Customer getCustomer(); void showCustomer(Customer);...

  • 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 need to update this C++ code according to these instructions. The team name should be...

    I need to update this C++ code according to these instructions. The team name should be "Scooterbacks". I appreciate any help! Here is my code: #include <iostream> #include <string> #include <fstream> using namespace std; void menu(); int loadFile(string file,string names[],int jNo[],string pos[],int scores[]); string lowestScorer(string names[],int scores[],int size); string highestScorer(string names[],int scores[],int size); void searchByName(string names[],int jNo[],string pos[],int scores[],int size); int totalPoints(int scores[],int size); void sortByName(string names[],int jNo[],string pos[],int scores[],int size); void displayToScreen(string names[],int jNo[],string pos[],int scores[],int size); void writeToFile(string...

  • This is my code for a TicTacToe game. However, I don't know how to write JUnit...

    This is my code for a TicTacToe game. However, I don't know how to write JUnit tests. Please help me with my JUnit Tests. I will post below what I have so far. package cs145TicTacToe; import java.util.Scanner; /** * A multiplayer Tic Tac Toe game */ public class TicTacToe {    private char currentPlayer;    private char[][] board;    private char winner;       /**    * Default constructor    * Initializes the board to be 3 by 3 and...

  • C++ problem where should I do overflow part? in this code do not write a new...

    C++ problem where should I do overflow part? in this code do not write a new code for me please /////////////////// // this program read two number from the user // and display the sum of the number #include <iostream> #include <string> using namespace std; const int MAX_DIGITS = 10; //10 digits void input_number(char num[MAX_DIGITS]); void output_number(char num[MAX_DIGITS]); void add(char num1[MAX_DIGITS], char num2[MAX_DIGITS], char result[MAX_DIGITS], int &base); int main() { // declare the array = {'0'} char num1[MAX_DIGITS] ={'0'}; char...

  • 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...

  •    moviestruct.cpp #include <iostream> #include <fstream> #include <cstdlib> #include <ostream> #include <fstream> #include <cstdlib> #include <cstring>...

       moviestruct.cpp #include <iostream> #include <fstream> #include <cstdlib> #include <ostream> #include <fstream> #include <cstdlib> #include <cstring> using namespace std; typedef struct{ int id; char title[250]; int year; char rating[6]; int totalCopies; int rentedCopies; }movie; int loadData(ifstream &infile, movie movies[]); void printAll(movie movies[], int count); void printRated(movie movies[], int count); void printTitled(movie movies[], int count); void addMovie(movie movies[],int &count); void returnMovie(movie movies[],int count); void rentMovie(movie movies[],int count); void saveToFile(movie movies[], int count, char *filename); void printMovie(movie &m); int find(movie movies[], int...

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
ADVERTISEMENT