#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<std::string, std::allocator<std::string> >&)':
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<std::string, std::allocator<std::string> >&, char)':
E:\Documents and Settings\Kev\Desktop\Book Exercizes\MyFirstTicTacToe.cpp:149: error: passing `const std::string' as `this' argument of `std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::operator=(_CharT) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]' 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<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::operator=(const std::basic_string<_CharT, _Traits, _Alloc>&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]' 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 '<' 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: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 '<' 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: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<std::string, std::allocator<std::string> >&)':
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<' in 'i < (+board)->std::vector<_Tp, _Alloc>::end [with _Tp = std::string, _Alloc = std::allocator<std::string>]()'
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