C++ error

JC0133

Senior member
Nov 2, 2010
201
1
76
I am having an issue in my code I can't seem to figure out. I stopped when I realized there was an error. It is in the boolean function below. The bracket is giving me an error and I am not sure what I have double checked and all of my brackets match up so not sure what the problem is.

Code:
 //Tic-Tac-Toe
//Plays the game of tic-tac-toe against a human opponent

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;


//global constant
const char X = 'X';
const char O = 'O';
const char EMPTY = ' ';
const char TIE = 'T';
const char NO_ONE = 'N';

//function prototypes
void instuctions();
char askYesNo(string question);
int askNumber(string question, int high, int low=0);
char humanPiece();
char opponent(char piece);
void displayBoard(const vector<char>& board);
char winner(const vector<char>& board);
bool isLegal(const vector<char>& board, int move);
int humanMove(const vector<char>& board, char human);
int computerMove(vector<char> board, char computer);
void announceWInner(char winner, char computer, char human);


//main function
int main() {
}//end of main

//instructions
void instructions() {
    cout << "Welcome to the ultimate man-machine showdown!!.  Tic-Tac-Toc,"<<endl;
    cout << "--where human brain is pit against silicon processor."<<endl;

    cout <<"\nMake your move known by entering a number, 0 - 8.  The number"<<endl;
    cout <<"corresponds to the desired board position, as illustrated:\n\n";

    cout <<"        0 | 1 | 2"<<endl;
    cout <<"        ---------"<<endl;
    cout <<"        3 | 4 | 5"<<endl;
    cout <<"        ---------"<<endl;
    cout <<"        6 | 7 | 8"<<endl;

    cout <<"Prepare yourself, human, The battle isabout to begin.\n"<<endl;
}//end instructions


char askYesNo(string question) {
    char response;

    do {
        cout << question << "(y/n): ";
        cin >> response;

    }while (response != 'y' && response != 'n');
}//end of askYesNo 

int askNumber(string question, int high, int low) {
    int number;

    do {
        cout << question << " (" << low << " - " <<high <<"): ";
        cin >> number;
    }while (number > high || number < low);

    return number;
}//end of askNumber

char humanPiece() {
    char go_first = askYesNo("Do you require the first move?");

    if(go_first == 'y') {
        cout <<"\nThen take the first move. You will need it.\n";
        return X;
    }
    else {
        cout <<"\nYour bravery will be your undoing... I will go first.\n";
        return O;
    }
}//end of humanPiece

char opponent(char piece) {
    if(piece == X) {
        return O;
    }
    else {
        return X;
    }
}//end of opponent

void displayBoard(const vector<char>& board) {
    cout << "\n\t" << board[0] << " | " << board[1] << " | " <<board[2];
    cout << "\n\t" << "----------";
    cout << "\n\t" << board[3] << " | " << board[4] << " | " <<board[5];
    cout << "\n\t" << "----------";
    cout << "\n]t" << board[6] << " | " << board[7] << " | " <<board[8];
    cout <<"\n\n";
}//end of display board

char winner(const vector<char>& board) {
    //all possible winning rows
    const int WINNING_ROWS[8][3] = { {0, 1, 2},
                                     {3, 4, 5},
                                     {6, 7, 8},
                                     {0, 3, 6},
                                     {1, 4, 7},
                                     {2, 5, 8},
                                     {0, 4, 8},
                                     {2, 4, 6} };
    const int TOTAL_ROWS = 8;

    //if any winning row has three values that are the same (and not EMPTY)
    //then we have a winner

    for (int row = 0; row < TOTAL_ROWS; ++row) {
        if ( (board[WINNING_ROWS[row][0]] != EMPTY) && (board[WINNING_ROWS[row][0]] == board[WINNING_ROWS[row][1]]) && (board[WINNING_ROWS[row][1]] == board[WINNING_ROWS[row][2]]) )
        {
            return board[WINNING_ROWS[row][0]];
    }

        //since nobody has won, check for a tie (no empty squares left)
        if(count(board.begin(), board.end(), EMPTY) == 0) {
            return TIE;
        }
        else {
            return NO_ONE;
        }

}//end of winner

    
    bool isLegal(const vector<char>& board, int move) 
    { //error here with this bracket
        return (board[move] == EMPTY);
    }//end of isLegal function
    
    

    int humanMove(const vector<char>& board, char human) {
        int move = askNumber("Where will you move?", (board.size() - 1));

            return move;
    }//end of human Move
 

iCyborg

Golden Member
Aug 8, 2008
1,330
56
91
I am having an issue in my code I can't seem to figure out. I stopped when I realized there was an error. It is in the boolean function below. The bracket is giving me an error and I am not sure what I have double checked and all of my brackets match up so not sure what the problem is.
You should triple check then, the function above it does not have all the braces matched up. :)
 

postmortemIA

Diamond Member
Jul 11, 2006
7,721
40
91
yep function char winner is not closing where you think it is.
btw empty main function .. how do you think this will work?
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
yep function char winner is not closing where you think it is.
btw empty main function .. how do you think this will work?

As stated above the main method needs to activate some other method
or run code

As to the original issue;
comment out each method until you find the one that is the problem.

Make sure that every if has proper bracketing
Make sure every else has the bracketing
Check the for and do/while loops.

And yes; you are short a bracket.

The bool method is where the compiler fails to try to adjust. It is not where the error is.