//Steffen, Jordan - stej4019
//cs201-807
//prog14.cc
//An exciting two player
//tic-tac-toe game that will
//provide hours of fun!
#include <iostream>
using namespace std;
void printMyBoard(string board);
//prints the board whilst displaying current moves
bool move(string &board, char turn, int square);
//tests for valid move and updates board
bool win(string board, char turn);
//tests for win
int main()
{
cout<<"Welcome to Tic-Tac-Toe!"<<endl;
cout<<"Three in a row wins!"<<endl;
cout<<'\n';
cout<<"---------------------------------------"<<endl;
cout<<"To start the game, X goes first."<<endl;
//welcome message telling X to go first
string board = " "; //10 spaces
printMyBoard(board); //prints board
char turn = 'X'; //first player is X
int counter = 1; //counter starts at 1
int square;
while(counter <= 9) //loop runs while counter is 9 or less
{
cout<<"Please enter a square (1-9): "<<endl;
//requests user enters a square to play
cin>>square;
if(move(board, turn, square)) //runs test (see function move)
{
if(win(board, turn)) //runs test (see function win)
{
cout<<"---------------------------------------"<<endl;
cout<<"Thank you, "<<turn<<" has won!"<<endl;
printMyBoard(board);
return 0; //program exits if win found
}
else
{
if(turn == 'X') //switches players
{
turn = 'O';
}
else
{
turn = 'X';
}
cout<<"---------------------------------------"<<endl;
cout<<"Thank you, it is now "<<turn<<"'s turn."<<endl;
printMyBoard(board);
++counter;
//displays next player
//loop runs again with incrementation
}
}
else
{
cout<<'\t'<<"Invalid move. Please try again."<<endl;
//displayed if invalid move attempted
//loop runs again without incrementation
}
}
cout<<"---------------------------------------"<<endl;
cout<<"Thank you, the game is a Stalemate!"<<endl;
printMyBoard(board);
//displayed if stalemate occurs
return 0;
}
/* This function prints the game board whenver
it is called from within main(). The board
is created using the subscript operator to
access the individual chars located in board. */
void printMyBoard(string &board)
{
cout<<'\n';
cout<<board[1]<<"|"<<board[2]<<"|"<<board[3]<<endl; //row one
cout<<"------"<<endl;
cout<<board[4]<<"|"<<board[5]<<"|"<<board[6]<<endl; //row two
cout<<"------"<<endl;
cout<<board[7]<<"|"<<board[8]<<"|"<<board[9]<<endl; //row three
cout<<'\n';
}
/* Tests for valid move based on the square
chosen being between 1 and 9 and also if the
square was already taken in a previous move.
If it's not valid, false is returned.
If it's valid, the player's symbol is placed
inside the requested square, and inserted in
the board string, and true is returned. */
bool move(string &board, char turn, int square)
{
if(square >=1 && square <=9) //tests between 1 and 9
{
if(board[square] == 'X' || 'O') //tests if space already occupied
{
return false;
}
else
{
board[square] = turn; //updates board
return true;
}
}
else
{
return false;
}
}
/* Tests all possible three way combinations
of a win in a 3 x 3 tic-tac-toe game. If one
is found the function exits, returning true,
without running the rest of the tests. If
there is no win found, false is returned. */
bool win(string board, char turn)
{
if(board[1] == turn && board[2] == turn && board[3] == turn)
{
return true;
}
if(board[1] == turn && board[4] == turn && board[7] == turn)
{
return true;
}
if(board[1] == turn && board[5] == turn && board[9] == turn)
{
return true;
}
if(board[2] == turn && board[5] == turn && board[8] == turn)
{
return true;
}
if(board[3] == turn && board[6] == turn && board[9] == turn)
{
return true;
}
if(board[3] == turn && board[5] == turn && board[7] == turn)
{
return true;
}
if(board[4] == turn && board[5] == turn && board[6] == turn)
{
return true;
}
if(board[7] == turn && board[8] == turn && board[9] == turn)
{
return true;
}
return false;
}