Hey All,
So I'm running thru Jumping into C++ and I have just finished my Tic Tac Toe game and just wanted to get some input on the final Product. Its not perfect but it plays a game, it can check for a Valid move thats Played between 0 and 8 but goes into an endless loop if a Char is entered.
So anyway. any hints or Tips would be great.
Code://Tic Tac Toe Program. 2 Players Play until the computer determines if 1 player has won#include <iostream> using namespace std; /*Used to draw the Game board on the Screen*/ void cDrawBoard(char cBoard[]) { cout << endl << endl << endl; cout << " " << cBoard[0] << " | " << cBoard[1] << " | " << cBoard[2] << endl; cout << "------------" << endl; cout << " " << cBoard[3] << " | " << cBoard[4] << " | " << cBoard[5] << endl; cout << "------------" << endl; cout << " " << cBoard[6] << " | " << cBoard[7] << " | " << cBoard[8] << endl; cout << endl << endl << endl; } int nCheckifXWins(char cBoard[], int& nPlayerXWins) { nPlayerXWins = 0; if ((cBoard[0] == 'X' && cBoard[1] == 'X' && cBoard[2] == 'X') || (cBoard[0] == 'X' && cBoard[3] == 'X' && cBoard[6] == 'X') || (cBoard[0] == 'X' && cBoard[4] == 'X' && cBoard[8] == 'X') || (cBoard[1] == 'X' && cBoard[4] == 'X' && cBoard[7] == 'X') || (cBoard[2] == 'X' && cBoard[5] == 'X' && cBoard[8] == 'X') || (cBoard[2] == 'X' && cBoard[4] == 'X' && cBoard[6] == 'X') || (cBoard[3] == 'X' && cBoard[4] == 'X' && cBoard[5] == 'X') || (cBoard[6] == 'X' && cBoard[7] == 'X' && cBoard[8] == 'X')) { //cout << "X Wins"; nPlayerXWins = 1; return nPlayerXWins; } else { return nPlayerXWins; } } int nCheckifOWins(char cBoard[], int& nPlayerOWins) { nPlayerOWins = 0; if ((cBoard[0] == 'O' && cBoard[1] == 'O' && cBoard[2] == 'O') || (cBoard[0] == 'O' && cBoard[3] == 'O' && cBoard[6] == 'O') || (cBoard[0] == 'O' && cBoard[4] == 'O' && cBoard[8] == 'O') || (cBoard[1] == 'O' && cBoard[4] == 'O' && cBoard[7] == 'O') || (cBoard[2] == 'O' && cBoard[5] == 'O' && cBoard[8] == 'O') || (cBoard[2] == 'O' && cBoard[4] == 'O' && cBoard[6] == 'O') || (cBoard[3] == 'O' && cBoard[4] == 'O' && cBoard[5] == 'O') || (cBoard[6] == 'O' && cBoard[7] == 'O' && cBoard[8] == 'O')) { //cout << "O Wins"; nPlayerOWins = 1; return nPlayerOWins; } else { return nPlayerOWins; } } int main() { int nPlayerXWins = 0; int nPlayerOWins = 0; bool nPlayer = true; //true = X int nMove; char cBoard[9] = {'0','1','2','3','4','5','6','7','8'}; // Sets the Board to have 0 - 8 in each Square. //cDrawBoard(cBoard); //Draws the Initial Board on the screen for (int nGame = 0; nGame < 9; nGame++) // Sets the game to run for 9 moves { if (nPlayer == true) //when nplayer == true - X's turn { for (int nValidMove = 0; nValidMove != 1;) //Variable if a valid move or asks for a new move. { cDrawBoard(cBoard); //Draws the Board cout << "Player X - Please Enter your move: "; cin >> nMove; if ((cBoard[nMove] != 'X' || cBoard[nMove] != 'O') && (nMove >= 0 && nMove <= 8)) //Checks the move is Valid? { cBoard[nMove] = 'X'; //Places an X at the selected Square nValidMove = 1; nCheckifXWins(cBoard, nPlayerXWins); //Checks to see if wins and returns a value of 1 to end the game nPlayer = false; // Switches the player if (nPlayerXWins > 0) //ends the game { cout << endl << "Player X has Won!"; nGame = 10; break; } } else { cout << "Please enter a Valid Move."; } } } else if (nPlayer == false) { for (int nValidMove = 0; nValidMove != 1;) { cDrawBoard(cBoard); cout << "Player O - Please Enter your move: "; cin >> nMove; if ((cBoard[nMove] != 'X' || cBoard[nMove] != 'O') && (nMove >= 0 && nMove <= 8)) { cBoard[nMove] = 'O'; nValidMove = 1; nCheckifOWins(cBoard, nPlayerOWins); nPlayer = true; if (nPlayerOWins > 0) { cout << endl <<"Player O has won!"; nGame = 10; break; } } else { cout << "Please enter a Valid Move."; } } } } }