Tic Tac Toe

In this program I need help shortening the code
don't change the include functions because i use chrome os which is very limited

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
#include <cstdlib>
#include <time.h>
#include <iostream>
#include <string>
using namespace std;
int main()
{
char s1='_';
char s2='_';   
char s3='_';
char s4='_';
char s5='_';
char s6='_';
char s7=' ';
char s8=' ';   
char s9=' ';
int gametype;
int human;    
int cpu;
int counter = 0;
cin>>gametype;
cout<<"Heya there this is a game,\nhowever not many know of it/n this is tic tacs are pretty good\n 1.Play against a bud.\n 2.Play against a cpu\n 3.How to play\n";
if(gametype==3){cout<<"How to play\n there is a 3*3 grid and you have to choose one of \n1|2|3\n4|5|6\n7|8|9\n get three x's or o's in a row to win. good luck (;";gametype=1;}
if(gametype==2){cout<<"sorry bud were in beta here is reg mode\n";
while(0==0){    
cout<<s1<<"|"<<s2<<"|"<<s3<<"\n"<<s4<<"|"<<s5<<"|"<<s6<<"\n"<<s7<<"|"<<s8<<"|"<<s9<<"\n";
cout<<"Your move (You're X)\n";
cin>>human;
if(human == 1 && s1=='_'){s1='X';}
if(human == 2 && s2=='_'){s2='X';}
if(human == 3 && s3=='_'){s3='X';}
if(human == 4 && s4=='_'){s4='X';}
if(human == 5 && s5=='_'){s5='X';}
if(human == 6 && s6=='_'){s6='X';}
if(human == 7 && s7==' '){s7='X';}
if(human == 8 && s8==' '){s8='X';}
if(human == 9 && s9==' '){s9='X';}
if(s1=='X'&&s2=='X'&&s3=='X'){cout<<"X wins";break;}
if(s4=='X'&&s5=='X'&&s6=='X'){cout<<"X wins";break;}
if(s7=='X'&&s8=='X'&&s9=='X'){cout<<"X wins";break;}
if(s1=='X'&&s4=='X'&&s7=='X'){cout<<"X wins";break;}
if(s2=='X'&&s5=='X'&&s8=='X'){cout<<"X wins";break;}
if(s3=='X'&&s6=='X'&&s9=='X'){cout<<"X wins";break;}
if(s1=='X'&&s5=='X'&&s9=='X'){cout<<"X wins";break;}
if(s7=='X'&&s5=='X'&&s3=='X'){cout<<"X wins";break;}
cout<<s1<<"|"<<s2<<"|"<<s3<<"\n"<<s4<<"|"<<s5<<"|"<<s6<<"\n"<<s7<<"|"<<s8<<"|"<<s9<<"\n";
counter++;
if(counter==9){cout<<"it's a draw, stand down";break;}
cout<<"Player 2’s move(Y)\n";
cin>>cpu;
if(cpu == 1 && s1=='_'){s1='O';}
if(cpu == 2 && s2=='_'){s2='O';}
if(cpu == 3 && s3=='_'){s3='O';}
if(cpu == 4 && s4=='_'){s4='O';}
if(cpu == 5 && s5=='_'){s5='O';}
if(cpu == 6 && s6=='_'){s6='O';}
if(cpu == 7 && s7==' '){s7='O';}
if(cpu == 8 && s8==' '){s8='O';}
if(cpu == 9 && s9==' '){s9='O';}
if(s1=='O'&&s2=='O'&&s3=='O'){cout<<"0 wins";break;}
if(s4=='O'&&s5=='O'&&s6=='O'){cout<<"0 wins";break;}
if(s7=='O'&&s8=='O'&&s9=='O'){cout<<"0 wins";break;}
if(s1=='O'&&s4=='O'&&s7=='O'){cout<<"0 wins";break;}
if(s2=='O'&&s5=='O'&&s8=='O'){cout<<"0 wins";break;}
if(s3=='O'&&s6=='O'&&s9=='O'){cout<<"0 wins";break;}
if(s1=='O'&&s5=='O'&&s9=='O'){cout<<"0 wins";break;}
if(s7=='O'&&s5=='O'&&s3=='O'){cout<<"0 wins";break;}
counter++;}

}
while(gametype==1){
cout<<s1<<"|"<<s2<<"|"<<s3<<"\n"<<s4<<"|"<<s5<<"|"<<s6<<"\n"<<s7<<"|"<<s8<<"|"<<s9<<"\n";
cout<<"Your move (You're X)\n";
cin>>human;
if(human == 1 && s1=='_'){s1='X';}
if(human == 2 && s2=='_'){s2='X';}
if(human == 3 && s3=='_'){s3='X';}
if(human == 4 && s4=='_'){s4='X';}
if(human == 5 && s5=='_'){s5='X';}
if(human == 6 && s6=='_'){s6='X';}
if(human == 7 && s7==' '){s7='X';}
if(human == 8 && s8==' '){s8='X';}
if(human == 9 && s9==' '){s9='X';}
if(s1=='X'&&s2=='X'&&s3=='X'){cout<<"X wins";break;}
if(s4=='X'&&s5=='X'&&s6=='X'){cout<<"X wins";break;}
if(s7=='X'&&s8=='X'&&s9=='X'){cout<<"X wins";break;}
if(s1=='X'&&s4=='X'&&s7=='X'){cout<<"X wins";break;}
if(s2=='X'&&s5=='X'&&s8=='X'){cout<<"X wins";break;}
if(s3=='X'&&s6=='X'&&s9=='X'){cout<<"X wins";break;}
if(s1=='X'&&s5=='X'&&s9=='X'){cout<<"X wins";break;}
if(s7=='X'&&s5=='X'&&s3=='X'){cout<<"X wins";break;}
cout<<s1<<"|"<<s2<<"|"<<s3<<"\n"<<s4<<"|"<<s5<<"|"<<s6<<"\n"<<s7<<"|"<<s8<<"|"<<s9<<"\n";
counter++;
if(counter==9){cout<<"it's a draw, stand down";break;}
cout<<"Player 2’s move(Y)\n";
cin>>cpu;
if(cpu == 1 && s1=='_'){s1='O';}
if(cpu == 2 && s2=='_'){s2='O';}
if(cpu == 3 && s3=='_'){s3='O';}
if(cpu == 4 && s4=='_'){s4='O';}
if(cpu == 5 && s5=='_'){s5='O';}
if(cpu == 6 && s6=='_'){s6='O';}
if(cpu == 7 && s7==' '){s7='O';}
if(cpu == 8 && s8==' '){s8='O';}
if(cpu == 9 && s9==' '){s9='O';}
if(s1=='O'&&s2=='O'&&s3=='O'){cout<<"0 wins";break;}
if(s4=='O'&&s5=='O'&&s6=='O'){cout<<"0 wins";break;}
if(s7=='O'&&s8=='O'&&s9=='O'){cout<<"0 wins";break;}
if(s1=='O'&&s4=='O'&&s7=='O'){cout<<"0 wins";break;}
if(s2=='O'&&s5=='O'&&s8=='O'){cout<<"0 wins";break;}
if(s3=='O'&&s6=='O'&&s9=='O'){cout<<"0 wins";break;}
if(s1=='O'&&s5=='O'&&s9=='O'){cout<<"0 wins";break;}
if(s7=='O'&&s5=='O'&&s3=='O'){cout<<"0 wins";break;}
counter++;
}
cout<<"\n"<<s1<<"|"<<s2<<"|"<<s3<<"\n"<<s4<<"|"<<s5<<"|"<<s6<<"\n"<<s7<<"|"<<s8<<"|"<<s9<<"\n";
}


Use an array for s:
char s[9] = {'-', '-', '-', '-', '-', '-', '-', '-', '-'};
Keep in mind that arrays are accessed starting at 0, not 1, so s goes from s[0] to s[8] rather than s[1] to s[9].

Now lines 28-37 become:
1
2
3
cin>>human;
--human;  // convert from human friendly 1-9 to computer friendly 0-8
if s[human]=='_'){s[human]='X';}

Lines 51-59 can get similar treatment.

There's a problem here (and in your orignal code). What if the user enters an invalid number? Let's worry about that later.

You could create a function to tell if someone has won:
1
2
3
4
5
6
7
8
9
10
11
12
bool isWinner(char mark)
{
if(s[0]==mark&&s[1]==mark&&s[2]==mark){return true;}
if(s[3]==mark&&s[4]==mark&&s[5]==mark){return true;}
if(s[6]==mark&&s[7]==mark&&s[8]==mark){return true;}
if(s[0]==mark&&s[3]==mark&&s[6]==mark){return true;}
if(s[1]==mark&&s[4]==mark&&s[7]==mark){return true;}
if(s[2]==mark&&s[5]==mark&&s[8]==mark){return true;}
if(s[0]==mark&&s[4]==mark&&s[8]==mark){return true;}
if(s[6]==mark&&s[4]==mark&&s[2]==mark){return true;}
return false;
}

Now lines 38-45 and 84-91 become
if (isWinner('X')) {cout << "X wins";break;}
and lines 60-67 and 106-113 become:
if (isWinner('O')) {cout << "O wins";break;}

In general, any time you find yourself writing the same or very similar code more than once, try to figure a way to turn it into a function. Also, anytime you find yourself writing variables with names like x1, x2, x3, consider making an array (or vector) instead.
Last edited on
Topic archived. No new replies allowed.