Create Tic Tac Toe Game in C++

Namaste Friends,



Introduction

Today, In this tutorial I am going to share with you tic tac toe game source codes. so, that you can also practice it to polish your skills.

Requirement

Any C++ Compiler


How it's going to work


basically in these codes, I used C++ array char facilities to manage the Zero and X marks. Group of If, else condition to set the rules of the game and at the end, while loop to take user input regularly except Game Finished Or Tie.


Example Codes


  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include <stdio.h>
#include <stdlib.h>


char b[10] = {'.','.','.','.','.','.','.','.','.','.'};


/*
__author__ = suraj singh bisht
__email__  = surajsinghbisht054@gmail.com
__Github__ = https://github.com/surajsinghbisht054


####################################################################
#          Terminal : TIC TOE TOY Game                            #
####################################################################
*/


// Get Current Status Of board
void printboard(){
    printf("\n\
     |     |      \n\
  %c  |  %c  | %c    \n\
 ____|_____|_____  \n\
     |     |       \n\
  %c  |  %c  | %c    \n\
 ____|_____|_____  \n\
     |     |       \n\
  %c  |  %c  | %c    \n\
     |     |       \n\
\
        ",b[1],b[2],b[3],b[4],b[5],b[6],b[7],b[8],b[9] );

}



// Check Second Player Win
bool check_second_win(){
    if (b[1] == b[2] && b[2] == b[3] && b[3]=='0')
        return true;
    else if (b[4] == b[5] && b[5] == b[6] && b[6]=='0')
        return true;
    else if (b[7] == b[8] && b[8] == b[9] && b[9]=='0')
        return true;
    else if (b[1] == b[4] && b[4] == b[7] && b[7]=='0')
        return true;
    else if (b[2] == b[5] && b[5] == b[8] && b[8]=='0')
        return true;
    else if (b[3] == b[6] && b[6] == b[9] && b[9]=='0')
        return true;
    else if (b[1] == b[5] && b[5] == b[9] && b[9]=='0')
        return true;
    else if (b[3] == b[5] && b[5] == b[7] && b[7]=='0')
        return true;
    else
        return false;

    return true;
}


// Check First Player Win Status
bool check_first_win(){
    if (b[1] == b[2] && b[2] == b[3] && b[3]=='X')
        return true;
    else if (b[4] == b[5] && b[5] == b[6] && b[6]=='X')
        return true;
    else if (b[7] == b[8] && b[8] == b[9] && b[9]=='X')
        return true;
    else if (b[1] == b[4] && b[4] == b[7] && b[7]=='X')
        return true;
    else if (b[2] == b[5] && b[5] == b[8] && b[8]=='X')
        return true;
    else if (b[3] == b[6] && b[6] == b[9] && b[9]=='X')
        return true;
    else if (b[1] == b[5] && b[5] == b[9] && b[9]=='X')
        return true;
    else if (b[3] == b[5] && b[5] == b[7] && b[7]=='X')
        return true;
    else
        return false;

    return true;
}

// Check Tie
bool check_tie(){
    if ((b[0]!='.')&&(b[1]!='.')&&(b[2]!='.')&&(b[3]!='.')&&(b[4]!='.')&&(b[5]!='.')&&(b[6]!='.')&&(b[7]!='.')&&(b[8]!='.')&&(b[9]!='.')){
        return true;
    }
    else{
        return false;
    }
}

// First Player Turn
void FirstPlayerTurn(){
    int d = 1;
    printf("[+] Player 1 [X] Please Enter Your Choose : ");
    scanf("%d", &d);
    b[d]='X';


}

// Second Player Turn
void SecondPlayerTurn(){
    int d = 2;
    printf("[+] Player 2 [0] Please Enter Your Choose : ");
    scanf("%d", &d);
    b[d]='0';


}

// main trigger
main(){

    while(1){
        printf("[+] Current Status Of board\n");
        printboard();
        FirstPlayerTurn();
        if (check_first_win()){
            printf("[-]    First Player win   [-] \n");
            printboard();
            exit(0);
        }
        if (check_tie()){
            printf("Game Tie\n");
            //break;
            exit(0);
        }
        printf("[+] Current Status Of board\n");
        printboard();
        SecondPlayerTurn();
        if (check_second_win()){
            printf("[-]    Second Player Win  [-]\n");
            printboard();
            exit(0);
        }
        if (check_tie()){
            printf("Game Tie\n");
            //break;
            exit(0);
        }
        //win++;
    };

    }

Explanation


FirstPlayerTurn, SecondPlayerTurn functions are created to take User inputs.
check_tie, check_first_win, check_second_win function is to verify the state of games.
At the end, the main function is to manage other functions





I hope you enjoyed this tutorial
Have A Nice Day


Share this

Related Posts

Previous
Next Post »