Untitled
unknown
plain_text
9 months ago
2.4 kB
4
Indexable
// Online C++ compiler to run C++ program online
#include <iostream>
using namespace std;
char board[3][3] = { {'1','2','3'}, {'4','5','6'}, {'7','8','9'} };
char currentMarker;
int currentPlayer;
void drawBoard() {
cout << " " << board[0][0] << " | " << board[0][1] << " | " << board[0][2] << endl;
cout << "---|---|---" << endl;
cout << " " << board[1][0] << " | " << board[1][1] << " | " << board[1][2] << endl;
cout << "---|---|---" << endl;
cout << " " << board[2][0] << " | " << board[2][1] << " | " << board[2][2] << endl;
}
bool placeMarker(int slot) {
int row = (slot - 1) / 3;
int col = (slot - 1) % 3;
if (board[row][col] != 'X' && board[row][col] != 'O') {
board[row][col] = currentMarker;
return true;
} else {
return false;
}
}
int winner() {
for (int i = 0; i < 3; i++) {
// Check rows and columns
if (board[i][0] == board[i][1] && board[i][1] == board[i][2]) return currentPlayer;
if (board[0][i] == board[1][i] && board[1][i] == board[2][i]) return currentPlayer;
}
// Check diagonals
if (board[0][0] == board[1][1] && board[1][1] == board[2][2]) return currentPlayer;
if (board[0][2] == board[1][1] && board[1][1] == board[2][0]) return currentPlayer;
return 0;
}
void swapPlayerAndMarker() {
if (currentMarker == 'X') currentMarker = 'O';
else currentMarker = 'X';
currentPlayer = (currentPlayer == 1) ? 2 : 1;
}
void game() {
cout << "Player 1, choose your marker: X or O: ";
char markerP1;
cin >> markerP1;
currentPlayer = 1;
currentMarker = markerP1;
drawBoard();
int playerWon;
for (int i = 0; i < 9; i++) {
cout << "It's player " << currentPlayer << "'s turn. Enter your slot: ";
int slot;
cin >> slot;
if (slot < 1 || slot > 9) {
cout << "Invalid slot! Try again.\n";
i--;
continue;
}
if (!placeMarker(slot)) {
cout << "Slot already taken! Try again.\n";
i--;
continue;
}
drawBoard();
playerWon = winner();
if (playerWon == 1) { cout << "Player 1 wins!\n"; break; }
if (playerWon == 2) { cout << "Player 2 wins!\n"; break; }
swapPlayerAndMarker();
}
if (playerWon == 0) {
cout << "It's a tie!\n";
}
}
int main() {
game();
return 0;
}Editor is loading...
Leave a Comment