Hey all, can you guys give me some input on how effecient this code is? It seems rather effecient to me, the main game loop only calls three functions, and those functions are re-used for each player as well as very small within themselves. I also believe there are no unneccesary variables to prevent effeciency.
Can you guys give some input please?
Thanks
// Tac Tac Toe
#include <iostream.h>
// Functions
void DrawBoard( void );
void PlayerTakeTurn( void );
void CheckWinner( void );
// Variables
int Board[3][3]; // Game Board
int Row, Column; // Input Controls
int TurnAmount; // Amount of Turns Taken, used for draws.
int PlayerTurn; // Who's turn is it?
bool ValidTurn; // Valid Turn?
int winner; // What player won?
int x,y; // Misc.
// Main Function - Defines and Main Loop
int main()
{
// Make all objects in the grid = 0;
for(x = 0; x < 3; x++)
{
for(y = 0; y < 3; y++)
{
Board[x][y] = 0;
}
}
winner = 0; // No winne yet
PlayerTurn = 1; // Player 1 starts
TurnAmount = 0; // Max 9 turns before cats game
// Main Loop
while(winner == 0)
{
ValidTurn = false; // Reset turn validity each turn
TurnAmount++; // Another turn closer to cats game
DrawBoard(); // Draw the game board
PlayerTakeTurn(); // Takes the players turn
if (TurnAmount > 8) // Cats game, end the game now.
break;
}
// Who won?
switch (winner)
{
case 1:
cout << "Player 1 emerges victorious!" << endl;
break;
case 2:
cout << "Player 2 emerges victorious!" << endl;
break;
default:
cout << "Draw! No one wins! You both suck." << endl;
break;
// Quit
return 0;
}
// Draws the playing grid, complete with X's and O's
void DrawBoard( void )
{
for(x = 0; x < 3; x++)
{
for(y = 0; y < 3; y++)
{
if (y > 0)
cout << "|";
switch(Board[x][y])
{
case 1:
cout << "X";
break;
case 2:
cout << "O";
break;
default:
cout << " ";
break;
}
}
cout << endl;
if (x < 2)
cout << "-----" << endl;
}
return;
}
// This is where the player takes their turn.
void PlayerTakeTurn( void )
{
// Repeat until a turn is valid.
while(ValidTurn == false)
{
cout << "Player " << PlayerTurn <<" your turn" << endl;
cout << "Row: ";
cin >> Row;
Row--;
cout << "Column: ";
cin >> Column;
Column--;
if (Board[Row][Column] == 0)
{
// Checks for valid turn
Board[Row][Column] = PlayerTurn;
ValidTurn = true;
CheckWinner();
if (PlayerTurn == 1)
{
PlayerTurn = 2;
}
else
{
PlayerTurn = 1;
}
}
else
{
cout << "Invalid Turn, try again." << endl;
ValidTurn = false;
}
}
return;
}
// Checks for a winner of the game.
void CheckWinner( void )
{
y = 0;
x = 0;
// Is this the best way to do this? I can't think of a better one.
// Top Row
if (Board[0][0] == PlayerTurn && Board[0][1] == PlayerTurn && Board[0][2] == PlayerTurn)
winner = PlayerTurn;
// Middle Row
else if (Board[1][0] == PlayerTurn && Board[1][1] == PlayerTurn && Board[1][2] == PlayerTurn)
winner = PlayerTurn;
// Bottom Row
else if (Board[2][0] == PlayerTurn && Board[2][1] == PlayerTurn && Board[2][2] == PlayerTurn)
winner = PlayerTurn;
// Left Column
else if (Board[0][0] == PlayerTurn && Board[1][0] == PlayerTurn && Board[2][0] == PlayerTurn)
winner = PlayerTurn;
// Middle Column
else if (Board[0][1] == PlayerTurn && Board[1][1] == PlayerTurn && Board[2][1] == PlayerTurn)
winner = PlayerTurn;
// Right Column
else if (Board[0][2] == PlayerTurn && Board[1][2] == PlayerTurn && Board[2][2] == PlayerTurn)
winner = PlayerTurn;
// Diagonal, Top Left - Bottom Right
else if (Board[0][0] == PlayerTurn && Board[1][1] == PlayerTurn && Board[2][2] == PlayerTurn)
winner = PlayerTurn;
// Diagonal, Top Right - Bottom Left
else if (Board[0][2] == PlayerTurn && Board[1][1] == PlayerTurn && Board[2][0] == PlayerTurn)
winner = PlayerTurn;
return;
}