Now my code might look "messy" since I did this half asleep and I don't believe I did any non-required checks for wins/draws.
// Tic Tac Toe// By: MrCpaw// [Notes: This is just a general tic tac toe, but with a game loop to redraw the screen.]#include #include using namespace std;// Public STARTchar PlayerSelect = ' ';// Setupbool GameActive = true;bool GameOver = false;bool Draw = false;char cOne = '1';char cTwo = '2';char cThree = '3';char cFour = '4';char cFive = '5';char cSix = '6';char cSeven = '7';char cEight = '8';char cNine = '9';bool PlayerXTurn = true;bool PlayAgain = true;// Public END// Win Checksint WinCheck();// Sqaure Check For Xint SquareCheckX(char cSquareCheckCharPX);// Sqaure Check For Oint SquareCheckO(char cSquareCheckCharPO);// Boardvoid GameBoard();int main(){ // Game Loop while (GameActive == true) { // Draw Game Board GameBoard(); // Player X Turn UPDATE while (PlayerXTurn == true) { cout << "\nPlayer X, your turn! Enter move:_\b"; cin >> PlayerSelect; cin.get(); // Check For Sqaures SquareCheckX(PlayerSelect); // Call Win Check WinCheck(); // Skip O Turn if Player X Won if (GameOver == true || Draw == true) { PlayerXTurn = true; break; } } // Draw Game Board GameBoard(); // Player O Turn UPDATE while (PlayerXTurn == false) { cout << "\nPlayer O, your turn! Enter move:_\b"; cin >> PlayerSelect; cin.get(); // Call Win Check WinCheck(); // Check For Sqaures SquareCheckO(PlayerSelect); // Call Win Check WinCheck(); // Skip X Turn if Player O Won if (GameOver == true || Draw == true) { PlayerXTurn = false; break; } } // Check to END if (GameOver == true && Draw != true) { system("cls"); GameBoard(); cout << "\n\n\nYou Win!\n\n"; cout << "Type 'P' or 'p' to play again! Type any key to quit!\n_\b"; cin >> PlayerSelect; if (PlayerSelect == 'P' || PlayerSelect == 'p') { GameActive = true; cOne = '1'; cTwo = '2'; cThree = '3'; cFour = '4'; cFive = '5'; cSix = '6'; cSeven = '7'; cEight = '8'; cNine = '9'; PlayerXTurn = true; GameOver = false; } else { GameActive = false; } } // Check to END DRAW if (GameOver == true && Draw == true) { system("cls"); GameBoard(); cout << "\n\n\nDraw!\n\n"; cout << "Type 'P' or 'p' to play again! Type any key to quit!\n_\b"; cin >> PlayerSelect; if (PlayerSelect == 'P' || PlayerSelect == 'p') { GameActive = true; cOne = '1'; cTwo = '2'; cThree = '3'; cFour = '4'; cFive = '5'; cSix = '6'; cSeven = '7'; cEight = '8'; cNine = '9'; PlayerXTurn = true; GameOver = false; Draw = false; } else { GameActive = false; } } } system("cls"); GameBoard(); cout << "\n\n\nYou Win!"; cin.get(); return 0;}void GameBoard(){ // Clear Then Draw system("cls"); // Draw Board cout << " | " << cOne << " | " << cTwo << " | " << cThree << " | " << endl; cout << " | " << cFour << " | " << cFive << " | " << cSix << " | " << endl; cout << " | " << cSeven << " | " << cEight << " | " << cNine << " | " << endl;}int WinCheck(){ // For X // 1 - 2 - 3 if (cOne == 'X' && cTwo == 'X' && cThree == 'X') { PlayerXTurn = false; GameOver = true; return 0; } // 4 - 5 - 6 else if (cFour == 'X' && cFive == 'X' && cSix == 'X') { PlayerXTurn = false; GameOver = true; return 0; } // 7 - 8 - 9 else if (cSeven == 'X' && cEight == 'X' && cNine == 'X') { PlayerXTurn = false; GameOver = true; return 0; } // 1 - 4 - 7 else if (cOne == 'X' && cFour == 'X' && cSeven == 'X') { PlayerXTurn = false; GameOver = true; return 0; } // 2 - 5 - 8 else if (cTwo == 'X' && cFive == 'X' && cEight == 'X') { PlayerXTurn = false; GameOver = true; return 0; } // 3 - 6 - 9 else if (cThree == 'X' && cSix == 'X' && cNine == 'X') { PlayerXTurn = false; GameOver = true; return 0; } // 1 - 5 - 9 else if (cOne == 'X' && cFive == 'X' && cNine == 'X') { PlayerXTurn = false; GameOver = true; return 0; } // 3 - 5 - 7 else if (cThree == 'X' && cFive == 'X' && cSeven == 'X') { PlayerXTurn = false; GameOver = true; return 0; } // For O // 1 - 2 - 3 else if (cOne == 'O' && cTwo == 'O' && cThree == 'O') { GameOver = true; return 0; } // 4 - 5 - 6 else if (cFour == 'O' && cFive == 'O' && cSix == 'O') { GameOver = true; return 0; } // 7 - 8 - 9 else if (cSeven == 'O' && cEight == 'O' && cNine == 'O') { GameOver = true; return 0; } // 1 - 4 - 7 else if (cOne == 'O' && cFour == 'O' && cSeven == 'O') { GameOver = true; return 0; } // 2 - 5 - 8 else if (cTwo == 'O' && cFive == 'O' && cEight == 'O') { GameOver = true; return 0; } // 3 - 6 - 9 else if (cThree == 'O' && cSix == 'O' && cNine == 'O') { GameOver = true; return 0; } // 1 - 5 - 9 else if (cOne == 'O' && cFive == 'O' && cNine == 'O') { GameOver = true; return 0; } // 3 - 5 - 7 else if (cThree == 'O' && cFive == 'O' && cSeven == 'O') { GameOver = true; return 0; } // Check for Draw else if (cOne != '1' && cTwo != '2' && cThree != '3' && cFour != '4' && cFive != '5' && cSix != '6' && cSeven != '7' && cEight != '8' && cNine != '9') { GameOver = true; Draw = true; return 0; } else { return 0; }}int SquareCheckX(char cSquareCheckCharPX){ if (cSquareCheckCharPX == '1') { if (cOne == '1') { cOne = 'X'; PlayerXTurn = false; } else { return 0; } } if (cSquareCheckCharPX == '2') { if (cTwo == '2') { cTwo = 'X'; PlayerXTurn = false; } else { return 0; } } if (cSquareCheckCharPX == '3') { if (cThree == '3') { cThree = 'X'; PlayerXTurn = false; } else { return 0; } } if (cSquareCheckCharPX == '4') { if (cFour == '4') { cFour = 'X'; PlayerXTurn = false; } else { return 0; } } if (cSquareCheckCharPX == '5') { if (cFive == '5') { cFive = 'X'; PlayerXTurn = false; } else { return 0; } } if (cSquareCheckCharPX == '6') { if (cSix == '6') { cSix = 'X'; PlayerXTurn = false; } else { return 0; } } if (cSquareCheckCharPX == '7') { if (cSeven == '7') { cSeven = 'X'; PlayerXTurn = false; } else { return 0; } } if (cSquareCheckCharPX == '8') { if (cEight == '8') { cEight = 'X'; PlayerXTurn = false; } else { return 0; } } if (cSquareCheckCharPX == '9') { if (cNine == '9') { cNine = 'X'; PlayerXTurn = false; } else { return 0; } } // For Whatever reason if the checks go bad return else { return 0; }}int SquareCheckO(char cSquareCheckCharPO){ if (cSquareCheckCharPO == '1') { if (cOne == '1') { cOne = 'O'; PlayerXTurn = true; } else { return 0; } } if (cSquareCheckCharPO == '2') { if (cTwo == '2') { cTwo = 'O'; PlayerXTurn = true; } else { return 0; } } if (cSquareCheckCharPO == '3') { if (cThree == '3') { cThree = 'O'; PlayerXTurn = true; } else { return 0; } } if (cSquareCheckCharPO == '4') { if (cFour == '4') { cFour = 'O'; PlayerXTurn = true; } else { return 0; } } if (cSquareCheckCharPO == '5') { if (cFive == '5') { cFive = 'O'; PlayerXTurn = true; } else { return 0; } } if (cSquareCheckCharPO == '6') { if (cSix == '6') { cSix = 'O'; PlayerXTurn = true; } else { return 0; } } if (cSquareCheckCharPO == '7') { if (cSeven == '7') { cSeven = 'O'; PlayerXTurn = true; } else { return 0; } } if (cSquareCheckCharPO == '8') { if (cEight == '8') { cEight = 'O'; PlayerXTurn = true; } else { return 0; } } if (cSquareCheckCharPO == '9') { if (cNine == '9') { cNine = 'O'; PlayerXTurn = true; } else { return 0; } } // For Whatever reason if the checks go bad return else { return 0; }}
Well that's my Tic Tac Toe game for now, I'm off to bed then back to hitting the books.
I started out with VB as well - I think you'll enjoy the transition. C++ is much closer to the metal than VB or C#, so you get a better feel for how a computer works too.
Welcome to the journal club too!