Jump to content
  • Advertisement
Sign in to follow this  
kevtimc

My first tiectactoe.cpp, need help debugging

This topic is 4424 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

const int BOARD_MAX = 8;
const string EMPTY = " ";
const char X = 'X';
const char O = 'O';
const char NO_ONE = 'N';
const char TIE = 'T';

void instructions();
void DisplayBoard(const vector<string>& board);
char AskQuestion(string question);
char WhoGoesFirst();
char Opponent(char Humanturn);
char winner(const vector<string>& board);
bool IsLegal(const vector<string>& board, int move);
int HumanMove(const vector<string>& board);
int AskNumber(string question, int high, int low);
int ComputerMove(vector<string>& board, char Computerturn);
char winner(const vector<string>& board);
void AnnounceWinner(char winner, char Humanturn, char ComputerTurn);

int main()
{
    int move;
    instructions();
    vector<string>board(BOARD_MAX, EMPTY);
    char Humanturn = WhoGoesFirst();
    char Computerturn = Opponent(Humanturn);
    char turn = 'X';
    DisplayBoard(board);
    
    // main game loop
    while(winner(board) == NO_ONE)
    {
                 if(Humanturn == turn)
                 {
                              move = HumanMove(board);
                              board[move] = Humanturn;
                              }
                              else
                              {
                                  move = ComputerMove(board, Computerturn);
                                  board[move] = Computerturn;
                                  }
                                  DisplayBoard(board);
                                  turn = Opponent(turn);
                                  }
                                  
                                  AnnounceWinner(winner(board), Humanturn, Computerturn);
    system("PAUSE");          
    return 0;
}

inline bool IsLegal(int move, const vector<string>& board)
{
       return(board[move] == EMPTY);
}
void instructions()
{
     cout << "\tThis is Tic-Tac-Toe." << endl;
     cout << "You will be pit against the most intelligent machine known to man, the computer.";
     cout << "The instructions are sipmle, pick a number that corresponds that with the grid\nto place your move there." << endl;
     cout << "\tPrepared?";
}

char AskQuestion(string question)
{
char answer;
     do {
     cout << endl << question;
     cin >> answer;
     }
     while(answer != 'y' && answer != 'n');
     return answer;
}

char WhoGoesFirst()
{
     char First = AskQuestion("Do you want to go first human?");
     if(First == 'y')
     {
              cout << endl << "Alright, your move...";
              return X;
              }
              else
              {
                  cout << endl << "Hmm, my turn...";
                  return O;
                  }
                  }
                  
char Opponent(char Humanturn)
{
     if(Humanturn == X)
     {
                  return O;
                  }
                  else
                  {
                      return X;
                      }
                      }
                      
int AskNumber(string question, int high, int low)
{
    int move;
    do{
                     cout << question << " (" << low << " - " << high << " ).";
                     cin >> move;
                     }
                     while(move < low || move > high);
                     return move;
} 

void DisplayBoard(const vector<string>& board)
{
     cout << "\n| " << board[0] << " | " << board[1] << " | " board[2] " |" << endl;
     cout << "- - - - - - - - -";
     cout << "\n| " << board[3] << " | " << board[4] << " | " board[5] " |" << endl;
     cout << "- - - - - - - - -";
     cout << "\n| " << board[6] << " | " << board[7] << " | " board[8] " |" << endl;
}

int HumanMove(const vector<string>& board)
{
    int move = AskNumber("\n Where do you want to move, human?", 0, 8);
    if(IsLegal(move, board))
    {
                     return move;
                     }
                     else
                     {
                         int move = AskNumber("\nWhere do you want to move, human?", 0, 8);
                         }
                         }
                         
int ComputerMove(const vector<string>& board, char Computerturn)
{
    cout << "\nIt is my turn now...\n";
    
    //lets see if the computer can win on the next move with a for loop
    for(int move = 0; move < board.size(); ++move)
    {
            board[move] = Computerturn;
            if(IsLegal(board, move))
                              if(winner(board) == computer)
                              {
                                               return move;
                                               }
                                               board[move] = EMPTY;
                                               }
                                               }
    //lets tell this function's scope that the human's piece is equal to the opposite of the computer's piece
    char human = Opponent(Computerturn)
    //lets see if the human can win on the next turn with a for loop
    for(int move = 0; move < board.size(); ++move)
    {
             if(IsLegal(move, board))
             {
                              board[move] = human;
                              if(winner(board) == human)
                              {
                                               return move;
                                               }
                                               board[move] = EMPTY;
                                               }
                                               }
    
    vector<int> AllMoves;
    AllMoves.push_back(0);
    AllMoves.push_back(1);
    AllMoves.push_back(2);
    AllMoves.push_back(3);
    AllMoves.push_back(4);
    AllMoves.push_back(5);
    AllMoves.push_back(6);
    AllMoves.push_back(7);
    AllMoves.push_back(8);
    srand(time(0));
    random_shuffle(AllMoves.begin(), AllMoves.end());
    
    //lets take this vector and decide which random move the computer makes
    for(int i = 0; i < board.size(); ++i)
    {
            int move = AllMoves;
            if(IsLegal(move, board))
            {
                             return move;
                             }
                             }
                             
                             
char winner(const vector<string>& board)
{
     int WinningMoves[] = { {0, 1, 2},
                            {3, 4, 5},
                            {6, 6, 8},
                            {0, 3, 6},
                            {1, 4, 7},
                            {2, 5, 8},
                            {2, 4, 6},
                            {0, 4, 8} };
                           
     //lets see if someone has a winning combination
     for(int i = 0; i < board.end(); ++i)
     {
             if( (board[WinningMoves[0]] != EMPTY) && (board[WinningMoves[0]] == board[WinningMoves[0]]) &&
                 (board[WinningMoves[1]] == board[WinningMoves[2]]) )
                 {
                                            return board[WinningMoves[0]];
                                            }
                                            
             if(count(board.begin(), board.end(), EMPTY) == 0)
             {
                                     return TIE;
                                     }
                                     
             return NO_ONE;
             }
             
void AnnounceWinner(char winner, char Humanturn, char ComputerTurn)
{
     if(winner == Humanturn)
     {
               cout << "\nYou have won human...I will accept defeat.\n";
               }
     
     if(winner == Computerturn)
     {
               cout << "\nI have crushed you human!  Proof that we computers, are smarter then\nyou!\n";
               }
               
     if(winner == TIE)
     {
               cout << "\nNo one has won, and the race between humans and computers still ensues.\n";
               }
               
               }


Looks like i've finished my first TicTac Toe, but I'm getting numerous compile errors that I can't quite figure out. Any help is appreciated, thanks.
Compile Log
-------------------------------
Compiler: Default compiler
Executing  g++.exe...
g++.exe "E:\Documents and Settings\Kev\Desktop\Book Exercizes\MyFirstTicTacToe.cpp" -o "E:\Documents and Settings\Kev\Desktop\Book Exercizes\MyFirstTicTacToe.exe"   -g3  -I"D:\Dev-Cpp\lib\gcc\mingw32\3.4.2\include"  -I"D:\Dev-Cpp\include\c++\3.4.2\backward"  -I"D:\Dev-Cpp\include\c++\3.4.2\mingw32"  -I"D:\Dev-Cpp\include\c++\3.4.2"  -I"D:\Dev-Cpp\include"   -L"D:\Dev-Cpp\lib" -g3 
E:\Documents and Settings\Kev\Desktop\Book Exercizes\MyFirstTicTacToe.cpp: In function `void DisplayBoard(const std::vector&lt;std::string, std::allocator&lt;std::string&gt; &gt;&)':
E:\Documents and Settings\Kev\Desktop\Book Exercizes\MyFirstTicTacToe.cpp:122: error: expected `;' before "board"
E:\Documents and Settings\Kev\Desktop\Book Exercizes\MyFirstTicTacToe.cpp:124: error: expected `;' before "board"
E:\Documents and Settings\Kev\Desktop\Book Exercizes\MyFirstTicTacToe.cpp:126: error: expected `;' before "board"

E:\Documents and Settings\Kev\Desktop\Book Exercizes\MyFirstTicTacToe.cpp: In function `int ComputerMove(const std::vector&lt;std::string, std::allocator&lt;std::string&gt; &gt;&, char)':
E:\Documents and Settings\Kev\Desktop\Book Exercizes\MyFirstTicTacToe.cpp:149: error: passing `const std::string' as `this' argument of `std::basic_string&lt;_CharT, _Traits, _Alloc&gt;& std::basic_string&lt;_CharT, _Traits, _Alloc&gt;::operator=(_CharT) [with _CharT = char, _Traits = std::char_traits&lt;char&gt;, _Alloc = std::allocator&lt;char&gt;]' discards qualifiers

E:\Documents and Settings\Kev\Desktop\Book Exercizes\MyFirstTicTacToe.cpp:151: error: `computer' undeclared (first use this function)
E:\Documents and Settings\Kev\Desktop\Book Exercizes\MyFirstTicTacToe.cpp:151: error: (Each undeclared identifier is reported only once for each function it appears in.)
E:\Documents and Settings\Kev\Desktop\Book Exercizes\MyFirstTicTacToe.cpp:155: error: passing `const std::string' as `this' argument of `std::basic_string&lt;_CharT, _Traits, _Alloc&gt;& std::basic_string&lt;_CharT, _Traits, _Alloc&gt;::operator=(const std::basic_string&lt;_CharT, _Traits, _Alloc&gt;&) [with _CharT = char, _Traits = std::char_traits&lt;char&gt;, _Alloc = std::allocator&lt;char&gt;]' discards qualifiers

E:\Documents and Settings\Kev\Desktop\Book Exercizes\MyFirstTicTacToe.cpp: At global scope:
E:\Documents and Settings\Kev\Desktop\Book Exercizes\MyFirstTicTacToe.cpp:159: error: `Computerturn' was not declared in this scope
E:\Documents and Settings\Kev\Desktop\Book Exercizes\MyFirstTicTacToe.cpp:161: error: expected `,' or `;' before "for"
E:\Documents and Settings\Kev\Desktop\Book Exercizes\MyFirstTicTacToe.cpp:161: error: expected constructor, destructor, or type conversion before '&lt;' token
E:\Documents and Settings\Kev\Desktop\Book Exercizes\MyFirstTicTacToe.cpp:161: error: expected `,' or `;' before '&lt;' token
E:\Documents and Settings\Kev\Desktop\Book Exercizes\MyFirstTicTacToe.cpp:161: error: expected unqualified-id before '++' token
E:\Documents and Settings\Kev\Desktop\Book Exercizes\MyFirstTicTacToe.cpp:161: error: expected `,' or `;' before '++' token
E:\Documents and Settings\Kev\Desktop\Book Exercizes\MyFirstTicTacToe.cpp:175: error: expected constructor, destructor, or type conversion before '.' token
E:\Documents and Settings\Kev\Desktop\Book Exercizes\MyFirstTicTacToe.cpp:175: error: expected `,' or `;' before '.' token
E:\Documents and Settings\Kev\Desktop\Book Exercizes\MyFirstTicTacToe.cpp:176: error: expected constructor, destructor, or type conversion before '.' token
E:\Documents and Settings\Kev\Desktop\Book Exercizes\MyFirstTicTacToe.cpp:176: error: expected `,' or `;' before '.' token
E:\Documents and Settings\Kev\Desktop\Book Exercizes\MyFirstTicTacToe.cpp:177: error: expected constructor, destructor, or type conversion before '.' token

E:\Documents and Settings\Kev\Desktop\Book Exercizes\MyFirstTicTacToe.cpp:177: error: expected `,' or `;' before '.' token
E:\Documents and Settings\Kev\Desktop\Book Exercizes\MyFirstTicTacToe.cpp:178: error: expected constructor, destructor, or type conversion before '.' token
E:\Documents and Settings\Kev\Desktop\Book Exercizes\MyFirstTicTacToe.cpp:178: error: expected `,' or `;' before '.' token
E:\Documents and Settings\Kev\Desktop\Book Exercizes\MyFirstTicTacToe.cpp:179: error: expected constructor, destructor, or type conversion before '.' token
E:\Documents and Settings\Kev\Desktop\Book Exercizes\MyFirstTicTacToe.cpp:179: error: expected `,' or `;' before '.' token
E:\Documents and Settings\Kev\Desktop\Book Exercizes\MyFirstTicTacToe.cpp:180: error: expected constructor, destructor, or type conversion before '.' token
E:\Documents and Settings\Kev\Desktop\Book Exercizes\MyFirstTicTacToe.cpp:180: error: expected `,' or `;' before '.' token
E:\Documents and Settings\Kev\Desktop\Book Exercizes\MyFirstTicTacToe.cpp:181: error: expected constructor, destructor, or type conversion before '.' token
E:\Documents and Settings\Kev\Desktop\Book Exercizes\MyFirstTicTacToe.cpp:181: error: expected `,' or `;' before '.' token

E:\Documents and Settings\Kev\Desktop\Book Exercizes\MyFirstTicTacToe.cpp:182: error: expected constructor, destructor, or type conversion before '.' token
E:\Documents and Settings\Kev\Desktop\Book Exercizes\MyFirstTicTacToe.cpp:182: error: expected `,' or `;' before '.' token
E:\Documents and Settings\Kev\Desktop\Book Exercizes\MyFirstTicTacToe.cpp:183: error: expected constructor, destructor, or type conversion before '.' token
E:\Documents and Settings\Kev\Desktop\Book Exercizes\MyFirstTicTacToe.cpp:183: error: expected `,' or `;' before '.' token
E:\Documents and Settings\Kev\Desktop\Book Exercizes\MyFirstTicTacToe.cpp:184: error: expected constructor, destructor, or type conversion before '(' token
E:\Documents and Settings\Kev\Desktop\Book Exercizes\MyFirstTicTacToe.cpp:184: error: expected `,' or `;' before '(' token
E:\Documents and Settings\Kev\Desktop\Book Exercizes\MyFirstTicTacToe.cpp:185: error: expected constructor, destructor, or type conversion before '(' token
E:\Documents and Settings\Kev\Desktop\Book Exercizes\MyFirstTicTacToe.cpp:185: error: expected `,' or `;' before '(' token
E:\Documents and Settings\Kev\Desktop\Book Exercizes\MyFirstTicTacToe.cpp:188: error: expected unqualified-id before "for"

E:\Documents and Settings\Kev\Desktop\Book Exercizes\MyFirstTicTacToe.cpp:188: error: expected `,' or `;' before "for"

E:\Documents and Settings\Kev\Desktop\Book Exercizes\MyFirstTicTacToe.cpp:188: error: expected constructor, destructor, or type conversion before '&lt;' token
E:\Documents and Settings\Kev\Desktop\Book Exercizes\MyFirstTicTacToe.cpp:188: error: expected `,' or `;' before '&lt;' token
E:\Documents and Settings\Kev\Desktop\Book Exercizes\MyFirstTicTacToe.cpp:188: error: expected unqualified-id before '++' token
E:\Documents and Settings\Kev\Desktop\Book Exercizes\MyFirstTicTacToe.cpp:188: error: expected `,' or `;' before '++' token
E:\Documents and Settings\Kev\Desktop\Book Exercizes\MyFirstTicTacToe.cpp: In function `char winner(const std::vector&lt;std::string, std::allocator&lt;std::string&gt; &gt;&)':
E:\Documents and Settings\Kev\Desktop\Book Exercizes\MyFirstTicTacToe.cpp:207: error: brace-enclosed initializer used to initialize `int'
E:\Documents and Settings\Kev\Desktop\Book Exercizes\MyFirstTicTacToe.cpp:207: error: brace-enclosed initializer used to initialize `int'
E:\Documents and Settings\Kev\Desktop\Book Exercizes\MyFirstTicTacToe.cpp:207: error: brace-enclosed initializer used to initialize `int'
E:\Documents and Settings\Kev\Desktop\Book Exercizes\MyFirstTicTacToe.cpp:207: error: brace-enclosed initializer used to initialize `int'
E:\Documents and Settings\Kev\Desktop\Book Exercizes\MyFirstTicTacToe.cpp:207: error: brace-enclosed initializer used to initialize `int'
E:\Documents and Settings\Kev\Desktop\Book Exercizes\MyFirstTicTacToe.cpp:207: error: brace-enclosed initializer used to initialize `int'
E:\Documents and Settings\Kev\Desktop\Book Exercizes\MyFirstTicTacToe.cpp:207: error: brace-enclosed initializer used to initialize `int'
E:\Documents and Settings\Kev\Desktop\Book Exercizes\MyFirstTicTacToe.cpp:207: error: brace-enclosed initializer used to initialize `int'
E:\Documents and Settings\Kev\Desktop\Book Exercizes\MyFirstTicTacToe.cpp:210: error: no match for 'operator&lt;' in 'i &lt; (+board)-&gt;std::vector&lt;_Tp, _Alloc&gt;::end [with _Tp = std::string, _Alloc = std::allocator&lt;std::string&gt;]()'
E:\Documents and Settings\Kev\Desktop\Book Exercizes\MyFirstTicTacToe.cpp:212: error: invalid types `int[int]' for array subscript
E:\Documents and Settings\Kev\Desktop\Book Exercizes\MyFirstTicTacToe.cpp:212: error: invalid types `int[int]' for array subscript
E:\Documents and Settings\Kev\Desktop\Book Exercizes\MyFirstTicTacToe.cpp:212: error: invalid types `int[int]' for array subscript
E:\Documents and Settings\Kev\Desktop\Book Exercizes\MyFirstTicTacToe.cpp:213: error: invalid types `int[int]' for array subscript
E:\Documents and Settings\Kev\Desktop\Book Exercizes\MyFirstTicTacToe.cpp:213: error: invalid types `int[int]' for array subscript
E:\Documents and Settings\Kev\Desktop\Book Exercizes\MyFirstTicTacToe.cpp:215: error: invalid types `int[int]' for array subscript
E:\Documents and Settings\Kev\Desktop\Book Exercizes\MyFirstTicTacToe.cpp:227: error: a function-definition is not allowed here before '{' token
E:\Documents and Settings\Kev\Desktop\Book Exercizes\MyFirstTicTacToe.cpp:227: error: expected `,' or `;' before '{' token
E:\Documents and Settings\Kev\Desktop\Book Exercizes\MyFirstTicTacToe.cpp:243: error: expected `}' at end of input

Execution terminated

Share this post


Link to post
Share on other sites
Advertisement
for starters, in DisplayBoard:

void DisplayBoard(const vector<string>& board)
{
cout << "\n| " << board[0] << " | " << board[1] << " | " board[2] " |" << endl;
cout << "- - - - - - - - -";
cout << "\n| " << board[3] << " | " << board[4] << " | " board[5] " |" << endl;
cout << "- - - - - - - - -";
cout << "\n| " << board[6] << " | " << board[7] << " | " board[8] " |" << endl;
}






you are missing "<<" operators before amd after board[] 2, 5, and 8:


void DisplayBoard(const vector<string>& board)
{
cout << "\n| " << board[0] << " | " << board[1] << " | " << board[2] << " |" << endl;
cout << "- - - - - - - - -";
cout << "\n| " << board[3] << " | " << board[4] << " | " << board[5] << " |" << endl;
cout << "- - - - - - - - -";
cout << "\n| " << board[6] << " | " << board[7] << " | " << board[8] << " |" << endl;
}






also, keep in mind that the errors will give you the exact line the problem is on, and in most IDEs, double clicking on the error will take you right to that line of code, to help with error fixing.

Share this post


Link to post
Share on other sites
Wow thats a lot of errors. I'm not about to debug the whole thing...but I'll tell you a few things that cought my eye first.

look in your DisplayBoard(const vector<string>& board). You are missing the << marks towards the end of 3 of the statements.

Line 151..you put if(winner(board) == computer) when you never even made a computer variable.

you are trying to convert things that cannot be converted such as Line 149 board[move] = Computerturn;

you forgot lots of ; such as Line 159.

on Line 200 you are trying to define a 1D array as a 2D array. int WinningMoves[] should be changed to int WinningMoves[][]

you also try to return arrays of something when the function is a char function...such as your winner(const vector<string>& board) function.

Theres loads more...but I'll leave it up to you to find the rest :) Just know you are trying to do a lot of illegal conversions. lol this will probably not be fun for you. good luck!

Share this post


Link to post
Share on other sites
Ok, well at my AllMoves vector, what could POSSIBLY be the problem, I'm just pushing back numbers...

Share this post


Link to post
Share on other sites
Ok, I've knocked a few errors out, but now on line 160, its saying that Computerturn wasn't defined in this scope...COMPUTERTURN IS WHAT I'M PASSING TO THE FUNCTION! HOW CAN IT NOT BE IN THE FUNCTION'S SCOPE?!?! :]

Share this post


Link to post
Share on other sites
correct me if I'm wrong...but Allmoves is not an object of anything. What you are doing is trying to call something from an object named Allmoves, which does not exist.

Share this post


Link to post
Share on other sites
::Confusion::

AllMoves is my vector of ints, I'm simply trying to push back the numbers 1-8 into the vector's containers.

Share this post


Link to post
Share on other sites
woops sorry bout that..

I havn't really used vectors in the way you are using them quite that much. In fact, your whole project looks too over complicated. Not trying to be rude or anything..I just think a lot of the things you did, could have been done in a much more simple way.

With that in mind...it's kind of hard to debug somebody elses code, because you aren't familiar with the intentions that the coder had. Basically..the person who made the code is the best person to debug the code. Just keep playing with things going wrong and eventually the answer will hit you. Sometimes..all it takes is a little trial and error.

best of luck!

Share this post


Link to post
Share on other sites
Quote:
Original post by BenDrummin58
woops sorry bout that..

I havn't really used vectors in the way you are using them quite that much. In fact, your whole project looks too over complicated. Not trying to be rude or anything..I just think a lot of the things you did, could have been done in a much more simple way.

With that in mind...it's kind of hard to debug somebody elses code, because you aren't familiar with the intentions that the coder had. Basically..the person who made the code is the best person to debug the code. Just keep playing with things going wrong and eventually the answer will hit you. Sometimes..all it takes is a little trial and error.

best of luck!


I understand, a lot of it is probably becuase it is broken up into so many functions, and includes AI. push_back just adds whatever is in the arguments to the vector's contaniners (I bet you already knew that, oh well). I'm just perplexed at why I'm getting the error, it seems fine to me.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!