#include "stdafx.h" #include using namespace std; void DrawGrid(char szBoxes[]){ cout << "-------" << endl; for(int i = 0; i < 3; i++) //Loop for rows { cout << "|"; for(int ii = i*3; ii < ((i+1)*3); ii++)//Loop for columns cout << szBoxes[ii] << "|"; cout << endl << "-------" << endl; } } bool AWinner(char szBoxes[]) { for(int i = 0; i < 3; i ++) { if(szBoxes[i] == szBoxes[i+1] && szBoxes[i+1] == szBoxes[i+2])//Return true for a full row return true; else if(szBoxes[i] == szBoxes[i+3] && szBoxes[i+3] == szBoxes[i+6])//Return true for full column return true; else if(i == 0) { if(szBoxes[0] == szBoxes[4] && szBoxes[4] == szBoxes[8])//Diagonal t left to b right return true; } else if(i == 2) { if(szBoxes[2] == szBoxes[4] && szBoxes[4] == szBoxes[6])//Diagonal t right to b left return true; } } return false; } int main() { char chPlayer = 'X'; // Holds the appropriate character for the current player char chInput = NULL; int nInput = 0; cout << "NOUGHTS AND CROSSES" << endl; while(true){ cout << "Enter any key to start a new game." << endl; cin >> chInput; bool bWinner = false; char szBoxes[] = "012345678"; int nTurn = 1; //1 or 2 DrawGrid(szBoxes); //Draw the initial grid while(!bWinner) { cout << "Player " << nTurn << " pick a box." << endl; while(true) { cin >> nInput; if(szBoxes[nInput] != 'X' && szBoxes[nInput] != 'Y') break; cout << "Box has already been played, please choose another." << endl; } szBoxes[nInput] = (nTurn == 1) ? 'X' : 'O';//Player 1 is X. Player 2 is O. DrawGrid(szBoxes); if(AWinner(szBoxes)) break; nTurn = (nTurn == 1) ? 2 : 1; } cout << "Player " << nTurn << " wins." << endl; } return 0; }