Hey guys, I wrote a TicTacToe program at my friends house in like 10 minutes, so I am sure I can improve some things. Can someone please give me some idea's on either stuff to add or stuff I can improve on (make more efficient).
#include <iostream>
using namespace std;
void InitializeBoard(char board[][3]);
void PrintBoard(char board[][3]);
void TakeInput(char board[][3], char marker);
char ChangeMarker(char marker);
bool CheckWin(char board[][3]);
bool CheckTie(char board[][3]);
int main()
{
char board[3][3];
char currentMarker = 'X';
InitializeBoard(board);
while (true)
{
PrintBoard(board);
TakeInput(board, currentMarker);
if (CheckWin(board))
break;
if (CheckTie(board))
break;
currentMarker = ChangeMarker(currentMarker);
}
PrintBoard(board);
if (CheckTie(board))
cout << "You tied!" << endl;
else
cout << "Player " << currentMarker << " has won!" << endl;
cin.ignore();
cin.get();
return 0;
}
void InitializeBoard(char board[][3])
{
board[0][0] = '1';
board[0][1] = '2';
board[0][2] = '3';
board[1][0] = '4';
board[1][1] = '5';
board[1][2] = '6';
board[2][0] = '7';
board[2][1] = '8';
board[2][2] = '9';
}
void PrintBoard(char board[][3])
{
cout << board[0][0] << " | " << board[0][1] << " | " << board[0][2] << endl;
cout << "---------" << endl;
cout << board[1][0] << " | " << board[1][1] << " | " << board[1][2] << endl;
cout << "---------" << endl;
cout << board[2][0] << " | " << board[2][1] << " | " << board[2][2] << endl;
cout << "---------" << endl;
}
void TakeInput(char board[][3], char marker)
{
char number;
cout << "Please enter what number to want: ";
cin >> number;
for (int row = 0; row < 3; row++)
for (int col = 0; col < 3; col++)
if (board[row][col] == number)
board[row][col] = marker;
}
char ChangeMarker(char marker)
{
if (marker == 'X')
return 'O';
else
return 'X';
}
bool CheckWin(char board[][3])
{
for (int i = 0; i < 3; i++)
{
if((board[i][0] == 'X' && board[i][1] == 'X' && board[i][2] == 'X') || (board[i][0] == 'O' && board[i][1] == 'O' && board[i][2] == 'O'))
return true;
if((board[0][i] == 'X' && board[1][i] == 'X' && board[2][i] == 'X') || (board[0][i] == 'O' && board[1][i] == 'O' && board[2][i] == 'O'))
return true;
}
if ((board[0][0] == 'X' && board[1][1] == 'X' && board[2][2] == 'X') || (board[0][0] == 'O' && board[1][1] == 'O' && board[2][2] == 'O'))
return true;
if ((board[0][2] == 'X' && board[1][1] == 'X' && board[2][0] == 'X') || (board[0][2] == 'O' && board[1][1] == 'O' && board[2][0] == 'O'))
return true;
return false;
}
bool CheckTie(char board[][3])
{
for (int x = 0; x < 3; x++)
for (int y = 0; y < 3; y++)
if (board[x][y] != 'X' && board[x][y] != 'O')
return false;
return true;
}
Thanks for any feedback.