You are on page 1of 4

//Project Name: Tic-Tac-Toe

//Project Purpose: Play Tic-Tac-Toe


//Created by: Jared Clark on 3/20/14

#include <iostream>
#include <iomanip>
using namespace std;

void displayRules () {
cout << "Welcome to the Tic-Tac-Toe championship!!!" << endl;
cout << "Player 1 will have the X's, and Player 2 the O's." << endl;
cout << "Please flip a coin to determine who goes first!" << endl;
}//function for displaying game rules

void whosTurn (bool X) {
switch (X) {
case true:
cout << "It's X's turn!" << endl << endl;
break;
case false:
cout << "It's O's turn!" << endl << endl;
break;
}//switch
}//function player's turn

void drawBoard (char G[]) {
cout << endl; //space before board
for (int cnt = 0; cnt < 9; cnt++) {
if (cnt == 3 || cnt == 6) {
cout << endl;
}
cout << G[cnt];
}//for loop
cout << endl << endl;//space after board
}//function to draw game board

int selectIndex () {
int index = 0;
int rowCount;
//variable for "row"
cout << "Please enter the row you would like to select!" << endl;
cin >> rowCount;
rowCount = rowCount - 1; //adjust from user input (1 to 3) to array index
(0 to 2)
while (rowCount > 2 || rowCount < 0) { //input validation
cout << "The valid numbers for the row are 1, 2, or 3." <<
endl;
cout << "Please select one of these values." << endl;
cin >> rowCount;
rowCount = rowCount - 1;
} //while
rowCount = rowCount * 3;

//variable for "collumn"
cout << "Please enter the collumn you would like to select!" << endl;
cin >> index;
index = index - 1; //adjust from user input (1 to 3) to array index (0 to
2)
while (index > 2 || index < 0) { //input validation
cout << "The valid numbers for the collumn are 1, 2, or 3." <<
endl;
cout << "Please select one of these values." << endl;
cin >> index;
index = index - 1;
} //while

//actual index for array
return (index + rowCount);
}//function for calculating array index of chosen spot on the board

int boardCheck (char G[], int I) {
int count = 0;

while (count == 0) {

if (G[I] == '-') {
count++;
} //breaks the loop

else {
cout << "That spot is already taken. Please select again." << endl;
drawBoard(G);
I = selectIndex();
}//calls previous functions in order to select a new index

return I;
}
} //function to check spot availability and call previous functions if unavaiable

char drawLetter(bool X) { //function to return letter to be drawn
switch (X) {
case true: //if it is Player X's turn, return X
return 'X';
break;
case false: //if it is Player O's turn, return O
return 'O';
break;
}
}//function

bool checkWinner (char G[], int c) {//function to check for a winner
if (c >= 5) {//if at least five turns have passed, check for a winner
//check by row
if (G[0] == G[1] && G[1] == G[2] && G[0] != '-')
return true;
if (G[3] == G[4] && G[4] == G[5] && G[3] != '-')
return true;
if (G[6] == G[7] && G[7] == G[8] && G[6] != '-')
return true;
//check by collumn
if (G[0] == G[3] && G[3] == G[6] && G[0] != '-')
return true;
if (G[1] == G[4] && G[4] == G[7] && G[1] != '-')
return true;
if (G[2] == G[5] && G[5] == G[8] && G[2] != '-')
return true;
//check diagonals
if (G[0] == G[4] && G[4] == G[8] && G[0] != '-')
return true;
if (G[2] == G[4] && G[4] == G[6] && G[2] != '-')
return true;
}//first if statement
else return false;
}//function

int displayVictory (char G[], bool X, bool V, int c) {//function for displaying victory
if (V == true) {//test if a player has won
switch (X) {//test for which player
case true:
drawBoard(G);
cout << "Congratulations! Player 1 is the winner!!!" << endl <<
"Please say \"woo!\"";
cin >> V; //honestly, just here so the message will be visable
return 10; //return 10 to cnt to end the for loop
case false:
drawBoard(G);
cout << "Congratulations! Player 2 is the winner!!!" << endl <<
"Please say \"woo!\"";
cin >> V; //honestly, just here so the message will be visable
return 10; //return 10 to cnt to end the for loop
}//switch
}//if
return c; //if winner not yet determined, return cnt's current value
}//function

void displayStalemate(char G[], int c) {//function to display stalemate message
if (c == 9) {
drawBoard(G);
cout << "Stalemate! No winner this round." << endl << "Say \"Awwwwww\"";
cin >> c; //honestly, just here so the message will be visable
}
}//function

bool playerSwap (bool X) {
switch (X) {
case true: //if it is Player X's turn, then it is switched to player O's
turn
X = false;
break;
case false: //if it is Player O's turn, then it is switched to player X's
turn
X = true;
break;
}
return X;
}

int main() {
//initialize variables
char gameBoard[9] = {'-', '-', '-', '-', '-', '-', '-', '-', '-'};
bool playerX = true; //variable for determining player turn.
bool victory = false; //variable for declaring a player the victor.
int index = 0; //index for the game board array

//display the rules
displayRules();

for(int cnt = 1; cnt < 10; cnt++) {
//display player's turn
whosTurn(playerX);

//draw the board
drawBoard(gameBoard);

//player chooses row and column
index = selectIndex();

//compare to board; if it's free, continue; else return to previous two
steps
index = boardCheck(gameBoard, index);

//draw the new space in the gameBoard
gameBoard[index] = drawLetter(playerX);

//after five turns, check for a winner each turn
victory = checkWinner(gameBoard, cnt);

//if a winner is found, display victory and end the for loop
cnt = displayVictory(gameBoard, playerX, victory, cnt);

//after nine turns, if no one has won, game is a draw
displayStalemate(gameBoard, cnt);

//swap players at end of turn
playerX = playerSwap(playerX);

}//for
return 0;
}

You might also like