Tic Tac Toe in objective C on mac Xcode

Ok so i am extremely new to coding anything, I am taking an online course and I am trying to build Tic Tac Toe for an assigment. I don't want people to write my code and i dont want to just cut/paste someone else's code without understanding what I am doing, so i've been searching TTT games and using them as examples of what I've written below. it is far from finished, I am using the output window to play the game, not building it for an iphone or anything else.

I just have a few basic questions:

1) how do I get the [#]'s in the TTT board to change to X or O
2) how do I get the computer to alternate between player 1 and player 2


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
 
#include <stdio.h>

char square[10] = {'o','1','2','3','4','5','6','7','8','9'};
int checkwin();
void board();
int coordinate;

int main(int argc, const char * argv[])
{
    int player=0;
    





    {
  
    void board();
        {
    
        printf( "\n\n\tTic Tac Toe\n\n");
        printf( "Player 1 (X)  -  Player 2 (O) \n \n");
        printf("\n");
        printf( "     |     |     \n" );
        printf( " [1] | [2] | [3] \n");
        printf( "_____|_____|_____\n") ;
        printf( "     |     |     \n") ;
        printf( " [4  | [5] | [6] \n");
        printf( "_____|_____|_____\n") ;
        printf( "     |     |     \n") ;
        printf( " [7] | [8] | [9] \n");
        printf( "     |     |     \n \n") ;
        printf("Player %d, enter a number: ", player);
        scanf("%d", &coordinate);
     
        }

    
    int winner();
        {
        if (square [1] == square [2] && square [2] == square [3])               //these ifstatements will check and compare each box in order to decide if the game
            return 1;                                                           // continues or if there is a winner/loser/draw. if all the boxes are same value (x or o)
                                                                                // then it will return to the 'winner/loser' line depending on who won.
        else if (square [4] == square [5] && square [5] == square [6])
            return 1;
    
        else if (square [7] == square [8] && square [8] == square [9])
            return 1;
        
        else if (square [1] == square [5] && square [5] == square [9])
            return 1;
        
        else if (square [3] == square [5] && square [5] == square [7])
            return 1;
        
        else if (square [1] != '1' && square [2] != '2' && square [3] !='3'         //these lines will check for inequalities that will then tell the computer that there
                 && square [4] != '4' && square [5] != '5' && square [6] != '6'     //there is no winner yet and to return a draw
                 && square [7] != '7' && square [8] != '8' && square [9] != '9')
            return 0;
        else
            return -1;
        
        }
    
    }
  
    return 0;
}

I managed to do the things you mentioned, in C++ though, but they're almost the same I think. Maybe you could take a look into my code, it may not be so effective or short, because I'm a beginner at this. But it works, most of the time.

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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <windows.h>
using namespace std;

char ruta[10] = "123456789";
void determiningWinner();
void computerChoice();
int turn = 0;

void board(){
    system("CLS");
    int square = 0, counter=0;
    cout << "       -= Tic Tac Toe (BETA) =-\n"
         << "Player 1 - (X)  Player 2 - (O)\n\n"
         << " - - - - -   - - - - -   - - - - -\n"
         << "|          |           |          |\n"
         << "|   [" << ruta[0] << "]    |    [" << ruta[1] << "]    |    [" << ruta[2] << "]   |\n"
         << "|          |           |          |\n"
         << " - - - - -   - - - - -   - - - - -\n"
         << "|          |           |          |\n"
         << "|   [" << ruta[3] << "]    |    [" << ruta[4] << "]    |    [" << ruta[5] << "]   |\n"
         << "|          |           |          |\n"
         << " - - - - -   - - - - -   - - - - -\n"
         << "|          |           |          |\n"
         << "|   [" << ruta[6] << "]    |    [" << ruta[7] << "]    |    [" << ruta[8] << "]   |\n"
         << "|          |           |          |\n"
         << " - - - - -   - - - - -   - - - - -\n\n";
    while(square < 1 || square > 9){
        cout << "Enter the number of the square you want to choose: "; cin >> square;
    }
    switch(square){
        case 1:
            if(ruta[0]!='1'){
                counter++;
                break;
            }
            else
                ruta[0]='X';
            break;
        case 2:
            if(ruta[1]!='2'){
                counter++;
                break;
            }
            else
                ruta[1]='X';
            break;
        case 3:
            if(ruta[2]!='3'){
                counter++;
                break;
            }
            else
                ruta[2]='X';
            break;
        case 4:
            if(ruta[3]!='4'){
                counter++;
                break;
            }
            else
                ruta[3]='X';
            break;
        case 5:
            if(ruta[4]!='5'){
                counter++;
                break;
            }
            else
                ruta[4]='X';
            break;
        case 6:
            if(ruta[5]!='6'){
                counter++;
                break;
            }
            else
                ruta[5]='X';
            break;
        case 7:
            if(ruta[6]!='7'){
                counter++;
                break;
            }
            else
                ruta[6]='X';
            break;
        case 8:
            if(ruta[7]!='8'){
                counter++;
                break;
            }
            else
                ruta[7]='X';
            break;
        case 9:
            if(ruta[8]!='9'){
                counter++;
                break;
            }
            else
                ruta[8]='X';
    }
    if(counter==1){
        cout << "You chose a square that already had been chosen!\nPlease try again!";
    }
    else{
        turn++;
        determiningWinner();
    }
}

void determiningWinner(){
    if(ruta[0]=='X'&&ruta[1]=='X'&&ruta[2]=='X'){
        cout << "You won!" << endl;
    }
    else if(ruta[0]=='O'&&ruta[1]=='O'&&ruta[2]=='O'){
        cout << "The computer won!" << endl;
    }
    else if(ruta[3]=='X'&&ruta[4]=='X'&&ruta[5]=='X'){
        cout << "You won!" << endl;
    }
    else if(ruta[3]=='O'&&ruta[4]=='O'&&ruta[5]=='O'){
        cout << "The computer won!" << endl;
    }
    else if(ruta[6]=='X'&&ruta[7]=='X'&&ruta[8]=='X'){
        cout << "You won!" << endl;
    }
    else if(ruta[6]=='O'&&ruta[7]=='O'&&ruta[8]=='O'){
        cout << "The computer won!" << endl;
    }
    else if(ruta[0]=='X'&&ruta[4]=='X'&&ruta[8]=='X'){
        cout << "You won!" << endl;
    }
    else if(ruta[0]=='O'&&ruta[4]=='O'&&ruta[8]=='O'){
        cout << "The computer won!" << endl;
    }
    else if(ruta[6]=='X'&&ruta[4]=='X'&&ruta[2]=='X'){
        cout << "You won!" << endl;
    }
    else if(ruta[6]=='O'&&ruta[4]=='O'&&ruta[2]=='O'){
        cout << "The computer won!" << endl;
    }
    else if(ruta[0]=='X'&&ruta[3]=='X'&&ruta[6]=='X'){
        cout << "You won!" << endl;
    }
    else if(ruta[0]=='O'&&ruta[3]=='O'&&ruta[6]=='O'){
        cout << "The computer won!" << endl;
    }
    else if(ruta[1]=='X'&&ruta[4]=='X'&&ruta[7]=='X'){
        cout << "You won!" << endl;
    }
    else if(ruta[1]=='O'&&ruta[4]=='O'&&ruta[7]=='O'){
        cout << "The computer won!" << endl;
    }
    else if(ruta[2]=='X'&&ruta[5]=='X'&&ruta[8]=='X'){
        cout << "You won!" << endl;
    }
    else if(ruta[2]=='O'&&ruta[5]=='O'&&ruta[8]=='O'){
        cout << "The computer won!" << endl;
    }
    else
        if(turn%2==0){
            board();
        }
        else{
            computerChoice();
        }
}

void computerChoice(){
    Sleep(1000);
    srand(time(0));
    int computerSquare = 1+(rand()%9);
    switch(computerSquare){
        case 1:
            if(ruta[0]!='1'){
                computerChoice();
            }
            else
                ruta[0]='O';
            break;
        case 2:
            if(ruta[1]!='2'){
                computerChoice();
                break;
            }
            else
                ruta[1]='O';
            break;
        case 3:
            if(ruta[2]!='3'){
                computerChoice();
                break;
            }
            else
                ruta[2]='O';
            break;
        case 4:
            if(ruta[3]!='4'){
                computerChoice();
                break;
            }
            else
                ruta[3]='O';
            break;
        case 5:
            if(ruta[4]!='5'){
                computerChoice();
                break;
            }
            else
                ruta[4]='O';
            break;
        case 6:
            if(ruta[5]!='6'){
                computerChoice();
                break;
            }
            else
                ruta[5]='O';
            break;
        case 7:
            if(ruta[6]!='7'){
                computerChoice();
                break;
            }
            else
                ruta[6]='O';
            break;
        case 8:
            if(ruta[7]!='8'){
                computerChoice();
                break;
            }
            else
                ruta[7]='O';
            break;
        case 9:
            if(ruta[8]!='9'){
                computerChoice();
                break;
            }
            else
                ruta[8]='O';
    }
    turn++;
    determiningWinner();
}

int main(){
    board();
}
Last edited on
I found a lot of C++ code that looks pretty similar, i just do not know how to translate bits of it into what i am doing with xcode on a mac. the >>array>> that you use doesnt work on xcode/mac.
I'm sorry then, because I'm not familiar with xcode :(
closed account (1v5E3TCk)
here is my code without usingn array:

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
152
153
154
155
/*
  Name:Ticktacktoe v4 
  Copyright: 
  Author: 
  Date: 25.06.13 16:25
  Description: 
*/


#include <iostream>
#include <string>
using namespace std;

int sinir, oyun=1;

int ticktacktoe(char a, char b, char c, char d, char e, char f, char g, char h, char i, string oyuncu, int tur)
{    int x;
     if(sinir<10){
     cout<<"|------TickTackToe------|"<<endl;
     cout<<"|-----------|---|---|---|"<<endl;
     cout<<"|-----------| "<<a<<" | "<<b<<" | "<<c<<" |"<<endl;
     cout<<"|-OYUN------|---|---|---|"<<endl;
     cout<<"|-"<<oyun<<"/"<<sinir<<"-------| "<<d<<" | "<<e<<" | "<<f<<" |"<<endl;
     cout<<"|-----------|---|---|---|"<<endl;
     cout<<"|-"<<tur<<".TUR-----| "<<g<<" | "<<h<<" | "<<i<<" |"<<endl;
     cout<<"|-----------|---|---|---|"<<endl;
     cout<<"|-----------------------|"<<endl;
     cout<<oyuncu<<"'nin sirasi:";}
     if(sinir>=10){
     cout<<"|------TickTackToe------|"<<endl;
     cout<<"|-----------|---|---|---|"<<endl;
     cout<<"|-----------| "<<a<<" | "<<b<<" | "<<c<<" |"<<endl;
     cout<<"|-OYUN------|---|---|---|"<<endl;
     cout<<"|-"<<oyun<<"/"<<sinir<<"------| "<<d<<" | "<<e<<" | "<<f<<" |"<<endl;
     cout<<"|-----------|---|---|---|"<<endl;
     cout<<"|-"<<tur<<".TUR-----| "<<g<<" | "<<h<<" | "<<i<<" |"<<endl;
     cout<<"|-----------|---|---|---|"<<endl;
     cout<<"|-----------------------|"<<endl;
     cout<<oyuncu<<"'nin sirasi:";}
     if(oyun>=10){
     cout<<"|------TickTackToe------|"<<endl;
     cout<<"|-----------|---|---|---|"<<endl;
     cout<<"|-----------| "<<a<<" | "<<b<<" | "<<c<<" |"<<endl;
     cout<<"|-OYUN------|---|---|---|"<<endl;
     cout<<"|-"<<oyun<<"/"<<sinir<<"------| "<<d<<" | "<<e<<" | "<<f<<" |"<<endl;
     cout<<"|-----------|---|---|---|"<<endl;
     cout<<"|-"<<tur<<".TUR-----| "<<g<<" | "<<h<<" | "<<i<<" |"<<endl;
     cout<<"|-----------|---|---|---|"<<endl;
     cout<<"|-----------------------|"<<endl;
     cout<<oyuncu<<"'nin sirasi:";}
     if(oyun>=10&&sinir>=10){
     cout<<"|------TickTackToe------|"<<endl;
     cout<<"|-----------|---|---|---|"<<endl;
     cout<<"|-----------| "<<a<<" | "<<b<<" | "<<c<<" |"<<endl;
     cout<<"|-OYUN------|---|---|---|"<<endl;
     cout<<"|-"<<oyun<<"/"<<sinir<<"-----| "<<d<<" | "<<e<<" | "<<f<<" |"<<endl;
     cout<<"|-----------|---|---|---|"<<endl;
     cout<<"|-"<<tur<<".TUR-----| "<<g<<" | "<<h<<" | "<<i<<" |"<<endl;
     cout<<"|-----------|---|---|---|"<<endl;
     cout<<"|-----------------------|"<<endl;
     cout<<oyuncu<<"'nin sirasi:";}
     
     cin>>x;
     cout<<"\n\n";
     return x;
     
     }


int main(){
    int puan1=0, puan2=0 , birinci, ikinci;
    
    string oyuncu1, oyuncu2;
    cout<<"1.oyuncunun adini giriniz:";
    cin>>oyuncu1;
    cout<<"2.oyuncunun adini giriniz:";
    cin>>oyuncu2;
    cout<<"Oynamak istediginiz oyun sayisini girin:";
    cin >>sinir;
    
    while (oyun<=sinir){
    char a=49;
    char b=50;
    char c=51;
    char d=52;
    char e=53;
    char f=54;
    char g=55;
    char h=56;
    char i=57; 
    if(oyun==1) {cout<<"TickTackToe'ye baslamak icin bir tusa basin.\n";}
    else  {cout<<oyun<<". oyuna baslamak icin bir tusa basin";}
    cin.ignore();
    cin.get();
    for(int tur=1; tur<10; ++tur){
    
  while(1){
   birinci=ticktacktoe(a,b,c,d,e,f,g,h,i,oyuncu1,tur);
   if(birinci==1&&a!=79&&a!=88) {a=88;++tur;break;}
   else if(birinci==2&&b!=79&&b!=88) {b=88;++tur;break;}
   else if(birinci==3&&c!=79&&c!=88) {c=88;++tur;break;}
   else if(birinci==4&&d!=79&&d!=88) {d=88;++tur;break;}
   else if(birinci==5&&e!=79&&e!=88) {e=88;++tur;break;}
   else if(birinci==6&&f!=79&&f!=88) {f=88;++tur;break;}
   else if(birinci==7&&g!=79&&g!=88) {g=88;++tur;break;}
   else if(birinci==8&&h!=79&&h!=88) {h=88;++tur;break;}
   else if(birinci==9&&i!=79&&i!=88) {i=88;++tur;break;}                        
   else {cout<<"Hatali giris yaptiniz.\n";continue;}                 
   }                                      
                                          
    if(a==88&&b==88&&c==88) {++puan1;break;} 
    else if(d==88&&e==88&&f==88) {++puan1;break;} 
    else if(g==88&&h==88&&i==88) {++puan1;break;}
    else if(a==88&&d==88&&g==88) {++puan1;break;}
    else if(b==88&&e==88&&h==88) {++puan1;break;}
    else if(c==88&&f==88&&i==88) {++puan1;break;}
    else if(a==88&&e==88&&i==88) {++puan1;break;}
    else if(g==88&&e==88&&c==88) {++puan1;break;}
  if(tur==10){break;}  
  
  while(1){
   ikinci=ticktacktoe(a,b,c,d,e,f,g,h,i,oyuncu2,tur);
   if(ikinci==1&&a!=79&&a!=88) {a=79;break;}
   else if(ikinci==2&&b!=79&&b!=88) {b=79;break;}
   else if(ikinci==3&&c!=79&&c!=88) {c=79;break;}
   else if(ikinci==4&&d!=79&&d!=88) {d=79;break;}
   else if(ikinci==5&&e!=79&&e!=88) {e=79;break;}
   else if(ikinci==6&&f!=79&&f!=88) {f=79;break;}
   else if(ikinci==7&&g!=79&&g!=88) {g=79;break;}
   else if(ikinci==8&&h!=79&&h!=88) {h=79;break;}
   else if(ikinci==9&&i!=79&&i!=88) {i=79;break;}                         
   else {cout<<"Hatali giris yaptiniz.\n";continue;}
}
   
    if(a==79&&b==79&&c==79) {++puan2;break;} 
   else  if(d==79&&e==79&&f==79) {++puan2;break;} 
   else  if(g==79&&h==79&&i==79) {++puan2;break;}
   else  if(a==79&&d==79&&g==79) {++puan2;break;}
   else  if(b==79&&e==79&&h==79) {++puan2;break;}
   else  if(c==79&&f==79&&i==79) {++puan2;break;}
   else  if(a==79&&e==79&&i==79) {++puan2;break;}
   else  if(g==79&&e==79&&c==79) {++puan2;break;}
    }
    cout<<oyuncu1<<":"<<puan1<<endl;
    cout<<oyuncu2<<":"<<puan2<<endl;
    ++oyun;    
}

if(puan1>puan2) {cout<<oyuncu1<<" kazandi.\n";}
else if(puan2>puan1) {cout<<oyuncu2<<" kazandi.\n";}
else if(puan1==puan2) {cout<<"Berabere.\n";}
cin.ignore();
cin.get();
}


if you dont understand anywhere i can help you with it
Topic archived. No new replies allowed.