//Matthew Isnor 8 September 2010
#include <iostream>
#include <windows.h>
using namespace std;
char board[9] = {' ',' ',' ',' ',' ',' ',' ',' ',' '};
void showBoard(){
cout << " " << board[0] << " | " << board[1] << " | " << board[2] << endl;
cout << " _ " << " _ " << " _ " << endl;
cout << " " << board[3] << " | " << board[4] << " | " << board[5] << endl;
cout << " _ " << " _ " << " _ " << endl;
cout << " " << board[6] << " | " << board[7] << " | " << board[8] << endl;
}
void move(char player){
int choice;
bool input = 0;
while (input == 0)
{
cout << "Please make a move selection 0-8: " << endl;
cin >> choice;
if (choice > 8 || choice < 0)
{
cout << "Please enter a move selection 0-8: " << endl;
cin >> choice;
}
if (board[choice] == 'X' || board[choice] == 'O')
{
cout << "That spot is taken on the board. Choose another 0-8: " << endl;
cin >> choice;
}
board[choice] = player;
input = 1;
}
}
char checkWinner(char player, char winner){
if (board[0] == player && board[1] == player && board[2] == player) // Across top
{
return player;
}
else if (board[0] == player && board[4] == player && board[8] == player) //Top left bottom right
{
return player;
}
else if (board[0] == player && board[3] == player && board[6] == player) // Left side
{
return player;
}
else if (board[1] == player && board[4] == player && board[7] == player) //Down middle
{
return player;
}
else if (board[2] == player && board[5] == player && board[8] == player) //right side
{
return player;
}
else if (board[6] == player && board[7] == player && board[8] == player) // bottom row
{
return player;
}
else if(board[3] == player && board[4] == player && board[5] == player) //middle accross
{
return player;
}
else if(board[6] == player && board[4] == player && board[2] == player) // bottom left to top right
{
return player;
}
else
{
return 'f'; // NO ONE FUCKING WON, KEEP GOING.
}
}
void winMessage(char player){
system("CLS");
showBoard();
cout << "\n\nThe player '" << player << "' has won the match!" << endl;
system("PAUSE");
}
bool tieCheck(){
if ((board[0] == 'X' || board[0] == 'O') && (board[1] == 'X' || board[1] == 'O') && (board[2] == 'X' || board[2] == 'O') &&
(board[3] == 'X' || board[3] == 'O') && (board[4] == 'X' || board[4] == 'O') && (board[5] == 'X' || board[5] == 'O') &&
(board[6] == 'X' || board[6] == 'O') && (board[7] == 'X' || board[7] == 'O') && (board[8] == 'X' || board[8] == 'O')){
cout << "The match has ended in a tie!\n\n";
system("PAUSE");
return 1;
}
return 0; // Game did not end in a tie.
}
int main(){
bool winner = false;
char theWinner = 'f';
cout << "Tic Tac Toe, C++ Style." << endl;
while (winner == false)
{
system("CLS");
showBoard();
move('X');
theWinner = checkWinner('X', theWinner);
if (theWinner == 'X')
{
winMessage('X');
return 0; // end the bitch 'X' won.
}
winner = tieCheck();
if (winner == 1)
{
return 0; // end the bitch 'O' won.
}
system("CLS");
showBoard();
move('O');
theWinner = checkWinner('O', theWinner);
if (theWinner == 'O')
{
winMessage('O');
return 0; // end the bitch 'O' won.
}
winner = tieCheck();
if (winner == 1)
{
return 0; // end the bitch 'O' won.
}
// else re-loop while
}
}