Today I started getting into C++ with my previous experiences with Pascal. I tried writing a simple Tictactoe program in the console. There were some difficulties, but overall I'm quite happy with how it worked out :D. It's probably not very effective and the conditions whether a player won or not are pretty clustered, but for a first try I'm content.
Here's the code.
#includeusing namespace std; void main() { // Declarations char rgBoard[9] = {'1','2','3','4','5','6','7','8','9'}; bool bIsGameOver = false; int iPlayer = 1; int iTurn; bool bTurn = false; // Print initial board cout << rgBoard[0] << "|" << rgBoard[1] << "|" << rgBoard[2] << endl; cout << "-----" << endl; cout << rgBoard[3] << "|" << rgBoard[4] << "|" << rgBoard[5] << endl; cout << "-----" << endl; cout << rgBoard[6] << "|" << rgBoard[7] << "|" << rgBoard[8] << endl << endl; // Game loop while (bIsGameOver == false) { // Assignments bTurn = false; // Play a turn cout << "Player " << iPlayer << ", it's your turn." << endl; while (bTurn == false) { cin >> iTurn; if (iTurn >= 1 && iTurn <= 9 && rgBoard[iTurn-1] != 'X' && rgBoard[iTurn-1] != 'O') { bTurn = true; cout << endl; } else { cout << "Invalid move, please try again." << endl; } } if (iPlayer == 1) { rgBoard[iTurn-1] = 'X'; iPlayer = 2; } else if (iPlayer == 2) { rgBoard[iTurn-1] = 'O'; iPlayer = 1; } // Check if the game is over if ((rgBoard[0] == 'X' && rgBoard[1] == 'X' && rgBoard[2] == 'X') || (rgBoard[0] == 'X' && rgBoard[3] == 'X' && rgBoard[6] == 'X') || (rgBoard[0] == 'X' && rgBoard[4] == 'X' && rgBoard[8] == 'X') || (rgBoard[1] == 'X' && rgBoard[4] == 'X' && rgBoard[7] == 'X') || (rgBoard[2] == 'X' && rgBoard[5] == 'X' && rgBoard[8] == 'X') || (rgBoard[3] == 'X' && rgBoard[4] == 'X' && rgBoard[5] == 'X') || (rgBoard[6] == 'X' && rgBoard[7] == 'X' && rgBoard[8] == 'X') || (rgBoard[2] == 'X' && rgBoard[4] == 'X' && rgBoard[6] == 'X')) { cout << "Player 1 wins!" << endl << endl; bIsGameOver = true; } else if ((rgBoard[0] == 'O' && rgBoard[1] == 'O' && rgBoard[2] == 'O') || (rgBoard[0] == 'O' && rgBoard[3] == 'O' && rgBoard[6] == 'O') || (rgBoard[0] == 'O' && rgBoard[4] == 'O' && rgBoard[8] == 'O') || (rgBoard[1] == 'O' && rgBoard[4] == 'O' && rgBoard[7] == 'O') || (rgBoard[2] == 'O' && rgBoard[5] == 'O' && rgBoard[8] == 'O') || (rgBoard[3] == 'O' && rgBoard[4] == 'O' && rgBoard[5] == 'O') || (rgBoard[6] == 'O' && rgBoard[7] == 'O' && rgBoard[8] == 'O') || (rgBoard[2] == 'O' && rgBoard[4] == 'O' && rgBoard[6] == 'O')) { cout << "Player 2 wins!" << endl << endl; bIsGameOver = true; } else if (rgBoard[0] != '1' && rgBoard[1] != '2' && rgBoard[2] != '3' && rgBoard[3] != '4' && rgBoard[4] != '5' && rgBoard[5] != '6' && rgBoard[6] != '7' && rgBoard[7] != '8' && rgBoard[8] != '9') { cout << "It's a tie!" << endl << endl; bIsGameOver = true; } // Print board cout << rgBoard[0] << "|" << rgBoard[1] << "|" << rgBoard[2] << endl; cout << "-----" << endl; cout << rgBoard[3] << "|" << rgBoard[4] << "|" << rgBoard[5] << endl; cout << "-----" << endl; cout << rgBoard[6] << "|" << rgBoard[7] << "|" << rgBoard[8] << endl << endl; } cin.ignore(); cin.get(); }
And this is how it looks when ran.
0 comments:
Post a Comment