Make a simple tic tac toe game with C++

I write simple tic tac toe game with c++ for my web page.
This is very basic code.
I write this game with artificial intelligence for my web page soon.

You can ask me your question.

Arda Mavi
My web page : In my profile.
[email protected]

Code :

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

#include <iostream>

using namespace std;

char zemin[5][5] = {{'1','|','2','|','3'},
                    {'-','|','-','|','-'},
                    {'4','|','5','|','6'},
                    {'-','|','-','|','-'},
                    {'7','|','8','|','9'}};

char oyuncu[2] = {'X','O'};

int yer = 0;

int tur;

void yaz(int, char);


void zeminciz(){

    cout << endl;

    for (int i = 0; i <= 4; i++) {
        for (int j = 0; j <= 4; j++) {
            cout << zemin[i][j] << " " ;
        }
        cout <<  endl;
    }

    cout << endl;

}

void oyna(char player){

    cout << "Sıra "<< player << "'de : " ;

    cin >> yer;

    player == 'X' ? yaz(yer,'X') : yaz(yer,'O');

}

void yaz(int yer, char oyuncu){

    switch(yer){

        case 1:
            zemin[0][0] = oyuncu;
            break;
        case 2:
            zemin[0][2] = oyuncu;
            break;
        case 3:
            zemin[0][4] = oyuncu;
            break;
        case 4:
            zemin[2][0] = oyuncu;
            break;
        case 5:
            zemin[2][2] = oyuncu;
            break;
        case 6:
            zemin[2][4] = oyuncu;
            break;
        case 7:
            zemin[4][0] = oyuncu;
            break;
        case 8:
            zemin[4][2] = oyuncu;
            break;
        case 9:
            zemin[4][4] = oyuncu;
            break;
        default:
            cout << "Hatalı Giriş !" << endl;
            if(oyuncu == 'X') {
                oyna('X');
            }else {
                oyna('Y');
            }

    }

}

int main(){

    zeminciz();

    tur = 1;

    while(tur <= 9){

        oyna(tur%2 == 0 ? oyuncu[1] : oyuncu[0]);

        zeminciz();

        tur++;
    }

    cout << "Oyun Bitti !" << endl;

    return 0;
}
Last edited on
Topic archived. No new replies allowed.