From 11f4b887553b96b236d2bd747470052d62d46551 Mon Sep 17 00:00:00 2001 From: "[Divyansh Jain]" <[jdivyansh51@gmail.com]> Date: Sun, 6 Oct 2024 12:02:15 +0530 Subject: [PATCH 1/3] made a very simple tic-tac-toe game in cpp --- games/tic_tac_toe_game.cpp | 114 +++++++++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 games/tic_tac_toe_game.cpp diff --git a/games/tic_tac_toe_game.cpp b/games/tic_tac_toe_game.cpp new file mode 100644 index 00000000000..64458fdd9d6 --- /dev/null +++ b/games/tic_tac_toe_game.cpp @@ -0,0 +1,114 @@ +#include +using namespace std; + + + +char board[3][3] = { {'1','2','3'}, {'4','5','6'}, {'7','8','9'} }; +char current_marker; +int current_player; + + + + +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] = current_marker; + return true; + } else { + return false; + } +} + + +int winner() { + // Rows + for (int i = 0; i < 3; i++) { + if (board[i][0] == board[i][1] && board[i][1] == board[i][2]) return current_player; + } + + // Columns + for (int i = 0; i < 3; i++) { + if (board[0][i] == board[1][i] && board[1][i] == board[2][i]) return current_player; + } + + // Diagonals + if (board[0][0] == board[1][1] && board[1][1] == board[2][2]) return current_player; + if (board[0][2] == board[1][1] && board[1][1] == board[2][0]) return current_player; + + return 0; +} + +void swapPlayerAndMarker() { + if (current_marker == 'X') current_marker = 'O'; + else current_marker = 'X'; + + if (current_player == 1) current_player = 2; + else current_player = 1; +} + + + + +void game() { + cout << "Player 1, choose your marker: X or O: "; + char marker_p1; + cin >> marker_p1; + + current_player = 1; + current_marker = marker_p1; + + drawBoard(); + + int player_won; + + for (int i = 0; i < 9; i++) { + cout << "It's player " << current_player << "'s turn. Enter your slot: "; + int slot; + cin >> slot; + + if (slot < 1 || slot > 9 || !placeMarker(slot)) { + cout << "Invalid move, try again.\n"; + i--; + continue; + } + + drawBoard(); + player_won = winner(); + + if (player_won == 1) { cout << "Player 1 wins!\n"; break; } + if (player_won == 2) { cout << "Player 2 wins!\n"; break; } + + swapPlayerAndMarker(); + } + + if (player_won == 0) cout << "It's a tie!\n"; +} + +int main() { + bool yorn=true; + while(yorn){ + cout<<"Do you want to play?"<<"\n Y/N"<>ch; + if(ch=='Y'){ + yorn==true; + } + else{ + yorn==false; + break; + } + game(); + } + return 0; +} \ No newline at end of file From 6982d0c40ca78df655cbb108a1b375c0cf6a5e21 Mon Sep 17 00:00:00 2001 From: Divyansh-jain2 Date: Sun, 6 Oct 2024 13:35:27 +0530 Subject: [PATCH 2/3] added pascal_triangle algorithm under the strings folder --- strings/pascal_triangle.cpp | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 strings/pascal_triangle.cpp diff --git a/strings/pascal_triangle.cpp b/strings/pascal_triangle.cpp new file mode 100644 index 00000000000..9a00e698128 --- /dev/null +++ b/strings/pascal_triangle.cpp @@ -0,0 +1,28 @@ +#include +#include +using namespace std; + +int factorial(int x){ + if(x==0){ + return 1; + } + return x*factorial(x-1); +} +int binomial(int n,int k){ + return factorial(n)/(factorial(n-k)*factorial(k)); +} +int main(){ + int height=0; + cout<<"Enter the height of the triangle:"; + cin>>height; + for(int i=0;i Date: Sun, 6 Oct 2024 13:37:43 +0530 Subject: [PATCH 3/3] removed a file --- strings/pascal_triangle.cpp | 28 ---------------------------- 1 file changed, 28 deletions(-) delete mode 100644 strings/pascal_triangle.cpp diff --git a/strings/pascal_triangle.cpp b/strings/pascal_triangle.cpp deleted file mode 100644 index 9a00e698128..00000000000 --- a/strings/pascal_triangle.cpp +++ /dev/null @@ -1,28 +0,0 @@ -#include -#include -using namespace std; - -int factorial(int x){ - if(x==0){ - return 1; - } - return x*factorial(x-1); -} -int binomial(int n,int k){ - return factorial(n)/(factorial(n-k)*factorial(k)); -} -int main(){ - int height=0; - cout<<"Enter the height of the triangle:"; - cin>>height; - for(int i=0;i