From b5a0934d080a1b8976ecd95803b182e73e20cad4 Mon Sep 17 00:00:00 2001 From: Rohit Kumar Pandey <56877888+Rohitkkpandey@users.noreply.github.com> Date: Wed, 6 Oct 2021 21:41:40 +0530 Subject: [PATCH] Create_Tic_Tac_Toe_game.cpp --- Tic_Tac_Toe_game.cpp | 210 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 210 insertions(+) create mode 100644 Tic_Tac_Toe_game.cpp diff --git a/Tic_Tac_Toe_game.cpp b/Tic_Tac_Toe_game.cpp new file mode 100644 index 0000000..06310ea --- /dev/null +++ b/Tic_Tac_Toe_game.cpp @@ -0,0 +1,210 @@ +// A C++ Program to play tic-tac-toe + +#include +using namespace std; + +#define COMPUTER 1 +#define HUMAN 2 + +#define SIDE 3 // Length of the board + +// Computer will move with 'O' +// and human with 'X' +#define COMPUTERMOVE 'O' +#define HUMANMOVE 'X' + +// A function to show the current board status +void showBoard(char board[][SIDE]) +{ + printf("\n\n"); + + printf("\t\t\t %c | %c | %c \n", board[0][0], + board[0][1], board[0][2]); + printf("\t\t\t--------------\n"); + printf("\t\t\t %c | %c | %c \n", board[1][0], + board[1][1], board[1][2]); + printf("\t\t\t--------------\n"); + printf("\t\t\t %c | %c | %c \n\n", board[2][0], + board[2][1], board[2][2]); + + return; +} + +// A function to show the instructions +void showInstructions() +{ + printf("\t\t\t Tic-Tac-Toe\n\n"); + printf("Choose a cell numbered from 1 to 9 as below" + " and play\n\n"); + + printf("\t\t\t 1 | 2 | 3 \n"); + printf("\t\t\t--------------\n"); + printf("\t\t\t 4 | 5 | 6 \n"); + printf("\t\t\t--------------\n"); + printf("\t\t\t 7 | 8 | 9 \n\n"); + + printf("-\t-\t-\t-\t-\t-\t-\t-\t-\t-\n\n"); + + return; +} + + +// A function to initialise the game +void initialise(char board[][SIDE], int moves[]) +{ + // Initiate the random number generator so that + // the same configuration doesn't arises + srand(time(NULL)); + + // Initially the board is empty + for (int i=0; i