Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ticTacToe game in code #2816

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
143 changes: 143 additions & 0 deletions games/TicTacToe.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
#include <iostream>
#include <vector>
#include <algorithm>
#include <random>
#include <ctime>
#include <cstdlib>

using namespace std;

char current_marker;
int current_player;
vector<char> board(9);

/**
* @brief Draws the current state of the board.
* @returns void
*/
void drawBoard() {
cout << " " << board[0] << " | " << board[1] << " | " << board[2] << " \n";
cout << "---|---|---\n";
cout << " " << board[3] << " | " << board[4] << " | " << board[5] << " \n";
cout << "---|---|---\n";
cout << " " << board[6] << " | " << board[7] << " | " << board[8] << " \n";
}

/**
* @brief Places the current marker on the board.
* @param slot The position where the marker should be placed.
* @returns bool True if the marker is placed successfully, false otherwise.
*/
bool placeMarker(int slot) {
if(board[slot] != 'X' && board[slot] != 'O') {
board[slot] = current_marker;
return true;
}
return false;
}

/**
* @brief Determines the winner of the game.
* @returns int The current player if they are the winner, 0 otherwise.
*/
int winner() {
// Check rows, columns, and diagonals
for(int i = 0; i < 3; i++) {
if(board[i*3] == board[i*3 + 1] && board[i*3 + 1] == board[i*3 + 2])
return current_player;
if(board[i] == board[i+3] && board[i+3] == board[i+6])
return current_player;
}
if(board[0] == board[4] && board[4] == board[8])
return current_player;
if(board[2] == board[4] && board[4] == board[6])
return current_player;
return 0;
}

/**
* @brief Swaps the current player and their marker.
* @returns void
*/
void swapPlayerAndMarker() {
current_marker = (current_marker == 'X') ? 'O' : 'X';
current_player = (current_player == 1) ? 2 : 1;
}

/**
* @brief Checks if the input is a number.
* @param input The input string to check.
* @returns bool True if the input is a number, false otherwise.
*/
bool is_number(const string& input) {
return !input.empty() && all_of(input.begin(), input.end(), ::isdigit);
}

/**
* @brief Asks the player for their slot input and validates it.
* @param slot Reference to the slot variable to store the player's input.
* @returns void
*/
void askData(int& slot) {
string input;
while(true) {
cout << "It's player " << current_player << "'s turn. Enter your slot: ";
cin >> input;
if(is_number(input)) {
slot = stoi(input);
if(slot >= 1 && slot <= 9 && board[slot-1] != 'X' && board[slot-1] != 'O') {
slot--;
break;
}
}
cout << "Invalid input! Try again.\n";
}
}

/**
* @brief Initializes the game board with default values.
* @returns void
*/
void initBoard() {
for(int i = 0; i < 9; i++) {
board[i] = '1' + i;
}
}

int main() {
initBoard();

cout << "Player 1, choose your marker: ";
char marker_p1;
cin >> marker_p1;

current_player = 1;
current_marker = marker_p1;

drawBoard();

for(int i = 0; i < 9; i++) {
int slot;
askData(slot);

placeMarker(slot);

drawBoard();

int player_won = winner();
if(player_won == 1) {
cout << "Player 1 won! Congrats!\n";
break;
}
if(player_won == 2) {
cout << "Player 2 won! Congrats!\n";
break;
}

swapPlayerAndMarker();
}

cout << "It's a draw!\n";

return 0;
}