Game Sample: Tic Tac Toe

Below is my attempt at a 2 player Tic Tac Toe game. It’s fairly simple, but very neatly done. I’m currently working on an AI to incorporate into the program as an option for single players. I will be sure to post when finished! Heres some output:

```//
//  main.cpp
//  Tic Tac Toe
//
//  Created by Aidan Takami on 3/8/18.
//

#include
using namespace std;

//Array to represent the spots on the board
char spot[10] = {'o','1','2','3','4','5','6','7','8','9'};

int ifWon();
void board();
void clearBoard();

//Main method
int main()
{

cout << "\n\n\tTic Tac Toe\n\n";
start:

int player = 1,i = -1,choice;

char side, playOn;

//Game loop, which will continue while no player has won
while(i==-1){

//Draws the board
board();

//Deciphers which players turn it is, using the modulus operator
player=(player%2)?1:2;

//Intro text & takes players move
cout << "Player " << player << ", enter a number:  ";         cin >> choice;

//Decides the team that the current player is on (X's or O's)
side=(player == 1) ? 'X' : 'O';

//Will take the player's move and mark the correct box (if unoccupied) with the correct side
if (choice == 1 && spot[1] == '1')

spot[1] = side;
else if (choice == 2 && spot[2] == '2')

spot[2] = side;
else if (choice == 3 && spot[3] == '3')

spot[3] = side;
else if (choice == 4 && spot[4] == '4')

spot[4] = side;
else if (choice == 5 && spot[5] == '5')

spot[5] = side;
else if (choice == 6 && spot[6] == '6')

spot[6] = side;
else if (choice == 7 && spot[7] == '7')

spot[7] = side;
else if (choice == 8 && spot[8] == '8')

spot[8] = side;
else if (choice == 9 && spot[9] == '9')

spot[9] = side;

//If selected box is occupied
else
{
cout<<"Invalid move ";

//gives the same player another turn
player--;
cin.ignore();
cin.get();
}

//determines if the board contains a win or draw.
i=ifWon();

//Continues the play process
player++;
}

//If a player won, draws the board and announces who won
board();
if(i==1)

cout<<"Player "<<--player<<" wins! ";

//If draw, announces a draw
else
cout<<"==>\aGame draw";

//Gives players an option to play again
cout << "Would you like to play again? (y/n)" << endl;     cin >> playOn;

if(playOn == 'y' || playOn == 'Y' ){
clearBoard();
goto start;
}

else
return 0;
}

//function that returns the result of the game

int ifWon()
{

//Game was won
if (spot[1] == spot[2] && spot[2] == spot[3])
return 1;
else if (spot[4] == spot[5] && spot[5] == spot[6])

return 1;
else if (spot[7] == spot[8] && spot[8] == spot[9])

return 1;
else if (spot[1] == spot[4] && spot[4] == spot[7])

return 1;
else if (spot[2] == spot[5] && spot[5] == spot[8])

return 1;
else if (spot[3] == spot[6] && spot[6] == spot[9])

return 1;
else if (spot[1] == spot[5] && spot[5] == spot[9])

return 1;
else if (spot[3] == spot[5] && spot[5] == spot[7])

return 1;

//Game was a draw
else if (spot[1] != '1' && spot[2] != '2' && spot[3] != '3'
&& spot[4] != '4' && spot[5] != '5' && spot[6] != '6'
&& spot[7] != '7' && spot[8] != '8' && spot[9] != '9')

return 0;

//Game still in progress
else
return -1;
}

//Function that draws the board
void board()
{

cout << endl << endl << endl;

//Prints each box of the board with the contents of the array properly alligned
cout << "Player 1 (X)  -  Player 2 (O)" << endl << endl << endl;
cout << "     |     |     " << endl;
cout << "  " << spot[1] << "  |  " << spot[2] << "  |  " << spot[3] << endl;
cout << "_____|_____|_____" << endl;
cout << "     |     |     " << endl;
cout << "  " << spot[4] << "  |  " << spot[5] << "  |  " << spot[6] << endl;
cout << "_____|_____|_____" << endl;
cout << "     |     |     " << endl;
cout << "  " << spot[7] << "  |  " << spot[8] << "  |  " << spot[9] << endl;
cout << "     |     |     " << endl << endl;
}

//Method to clear the board & reset all values
void clearBoard(){

spot[1] = '1';
spot[2] = '2';
spot[3] = '3';
spot[4] = '4';
spot[5] = '5';
spot[6] = '6';
spot[7] = '7';
spot[8] = '8';
spot[9] = '9';

}
```