1
+ #include <stdio.h> // includes printf / scanf ; allows for inputs & outputs
2
+ #include <stdlib.h> // includes random number generating functions
3
+ #include <time.h> // includes time() function ; seconds passed since 1970
4
+
5
+ #define side 3
6
+
7
+
8
+ // Initializing functions (allows to call other functions within another function)
9
+
10
+
11
+ void CreateBoard ();
12
+ void ShowInstruction ();
13
+ void PlayerMove ();
14
+ void ComputerMove ();
15
+ void UpdateBoard ();
16
+ int FreeSpacesLeft ();
17
+ char checkWinner ();
18
+ void ShowWinner (char winner );
19
+
20
+
21
+ // Global variables
22
+
23
+
24
+ int PlayerScore , ComputerScore , Ties ;
25
+ int GameNumber = 1 ;
26
+ int row , col ;
27
+ char BoardArr [side ][side ];
28
+ char winner ;
29
+ char PlayAgain = 'Y' ;
30
+ const char player = 'X' ;
31
+ const char computer = 'O' ;
32
+
33
+
34
+ // Beginning of main-code function
35
+
36
+
37
+ int main () {
38
+
39
+ printf ("\t\t Welcome to Tic-Tac-Toe" );
40
+
41
+ do {
42
+
43
+ GameNumber = 1 ; // resets # of wins for each plater and resets winner to '_' i.e. no winner. Occurs after each BO3 game
44
+ PlayerScore = 0 ;
45
+ ComputerScore = 0 ;
46
+ Ties = 0 ;
47
+
48
+ do {
49
+
50
+ winner = '_' ;
51
+ CreateBoard ();
52
+ ShowInstruction ();
53
+
54
+ while (winner == '_' && FreeSpacesLeft () != 0 ) {
55
+
56
+ PlayerMove ();
57
+ UpdateBoard ();
58
+ winner = checkWinner ();
59
+
60
+ if (winner != '_' || FreeSpacesLeft () == 0 ) {
61
+
62
+ break ; // will break out of while loop; means there is either a winner or no spaces left i.e. a tie
63
+ }
64
+
65
+ ComputerMove ();
66
+ UpdateBoard ();
67
+ winner = checkWinner ();
68
+ if (winner != '_' || FreeSpacesLeft () == 0 ) {
69
+ winner = checkWinner ();
70
+ break ;
71
+ }
72
+ }
73
+
74
+ ShowWinner (winner );
75
+ printf (" Total Wins" );
76
+ printf ("\n --------" );
77
+ printf ("\nPlayer : %d" , PlayerScore );
78
+ printf ("\nComputer : %d" , ComputerScore );
79
+ printf ("\nTies : %d\n" , Ties );
80
+
81
+ GameNumber ++ ;
82
+
83
+ } while (GameNumber <= 3 ); // will exit do-while loop once GameNumber == 3 (i.e. after 3 games as GameNumber increases by 1 per game)
84
+
85
+ printf ("\nWould you like to play again? Enter 'Y' for yes & to play again, enter 'N' for no & to exit program.\n" );
86
+ printf ("Selection: " ); scanf ("%s" , & PlayAgain );
87
+
88
+ if (PlayerScore > ComputerScore ) {
89
+ printf ("\nPlayer wins best of 3! Thanks for playing.\n" );
90
+
91
+ }
92
+ else if (ComputerScore > PlayerScore ) {
93
+ printf ("\nComputer wins best of 3! Better luck next time..\n" );
94
+
95
+ }
96
+ else if (ComputerScore == PlayerScore ) {
97
+ printf ("\nIt's a tie! Player wins = Computer wins, close games!\n" );
98
+
99
+ }
100
+
101
+
102
+ } while (PlayAgain == 'Y' ); //end of most outer do-while loop; exits program out when PlayAgain != 'Y'
103
+
104
+ printf ("\nClosing program, goodbye :( ." );
105
+ return 0 ;
106
+
107
+ }
108
+
109
+
110
+ // List of functions begin here
111
+
112
+ void CreateBoard () {
113
+ for (int i = 0 ; i < side ; i ++ ) {
114
+ for (int j = 0 ; j < side ; j ++ ) {
115
+ BoardArr [i ][j ] = '_' ;
116
+
117
+ }
118
+ }
119
+ }
120
+
121
+
122
+ void ShowInstruction () {
123
+
124
+ printf ("\n\t 3 games are played, best of 3 wins!" );
125
+ printf ("\n\n\t\t Game number : %d\n\n" , GameNumber );
126
+ printf ("Please pick a number 1-9 as shown below to pick your play :\n\n" );
127
+ printf ("\t\t\t 1 | 2 | 3 \n" );
128
+ printf ("\t\t\t ---------\n" ); // col1 col2 col3
129
+ printf ("\t\t\t 4 | 5 | 6 \n" ); // row 1 [0][0] [0][1] [0][2] == 1 2 3
130
+ printf ("\t\t\t --------- \n" ); // note: row 2 [1][0] [1][1] [1][2] == 4 5 6
131
+ printf ("\t\t\t 7 | 8 | 9 \n" ); // row 3 [2][0] [2][1] [2][2] == 7 8 9
132
+ printf ("\n-\t-\t-\t-\t-\t-\t-\t-\t-\n" );
133
+ printf ("\t\t Player = 'X' | CPU = 'O'\n" );
134
+ }
135
+
136
+
137
+ void PlayerMove () {
138
+
139
+ int SpaceChoice ;
140
+
141
+ do {
142
+ printf ("\nEnter a number (1-9) to place" );
143
+ printf ("\nSelection : " ); scanf ("%d" , & SpaceChoice );
144
+
145
+ row = (SpaceChoice - 1 ) / 3 ;
146
+ col = (SpaceChoice - 1 ) % 3 ;
147
+
148
+ if (BoardArr [row ][col ] != '_' ) {
149
+
150
+ printf ("\nInvalid move, space already taken, please pick a different number.\n" );
151
+ }
152
+
153
+ else {
154
+
155
+ BoardArr [row ][col ] = player ;
156
+ break ;
157
+ }
158
+ } while ( BoardArr [row ][col ] != '_' );
159
+ }
160
+
161
+
162
+ void ComputerMove () {
163
+ //creates a seed of random numbers based off time since code has been running
164
+ srand (time (0 ));
165
+ int ComputerPick ;
166
+
167
+ if (FreeSpacesLeft () > 0 ) {
168
+
169
+ do {
170
+ ComputerPick = ((rand () % 9 ) + 1 ); //allows to randomly generated numbers between 1-9
171
+ row = (ComputerPick - 1 ) / 3 ;
172
+ col = (ComputerPick - 1 ) % 3 ; //converts (1-9) to equivalent BoardArr mem. location
173
+
174
+ } while ( BoardArr [row ][col ] != '_' );
175
+ BoardArr [row ][col ] = computer ;
176
+ }
177
+ else {
178
+
179
+ ShowWinner ('_' ); // draw!
180
+ }
181
+ }
182
+
183
+
184
+ void UpdateBoard () {
185
+
186
+ printf ("\t\t\t %c | %c | %c \n" , BoardArr [0 ][0 ], BoardArr [0 ][1 ], BoardArr [0 ][2 ] );
187
+ printf ("\t\t\t ---------\n" );
188
+ printf ("\t\t\t %c | %c | %c \n" , BoardArr [1 ][0 ], BoardArr [1 ][1 ], BoardArr [1 ][2 ] );
189
+ printf ("\t\t\t --------- \n" );
190
+ printf ("\t\t\t %c | %c | %c \n" , BoardArr [2 ][0 ], BoardArr [2 ][1 ], BoardArr [2 ][2 ] );
191
+ printf ("\n-\t-\t-\t-\t-\t-\t-\t-\t-\n" );
192
+ }
193
+
194
+
195
+ int FreeSpacesLeft () {
196
+
197
+ int SpacesLeft = 9 ;
198
+ for (int i = 0 ; i < side ; i ++ ){
199
+ for ( int j = 0 ; j < side ; j ++ ) {
200
+
201
+ if (BoardArr [i ][j ] != '_' ) {
202
+ SpacesLeft -- ;
203
+ }
204
+ }
205
+ }
206
+ return SpacesLeft ;
207
+ }
208
+
209
+
210
+ char checkWinner () {
211
+
212
+ //checking rows for winner
213
+
214
+ for (int i = 0 ; i < 3 ; i ++ ) {
215
+
216
+ if (( BoardArr [i ][0 ] == BoardArr [i ][1 ] && BoardArr [i ][0 ] == BoardArr [i ][2 ] ) ) {
217
+ return BoardArr [i ][0 ];
218
+ }
219
+ }
220
+
221
+ //checking columns for winner
222
+
223
+ for (int i = 0 ; i < 3 ; i ++ ) {
224
+
225
+ if ( (BoardArr [0 ][i ] == BoardArr [1 ][i ] && BoardArr [0 ][i ] == BoardArr [2 ][i ]) ) {
226
+ return BoardArr [0 ][i ];
227
+ }
228
+ }
229
+
230
+ //checking diagonals for winner
231
+
232
+ if ( (BoardArr [0 ][0 ] == BoardArr [1 ][1 ] && BoardArr [0 ][0 ] == BoardArr [2 ][2 ]) ) {
233
+ return BoardArr [0 ][0 ];
234
+ }
235
+
236
+ if ( (BoardArr [2 ][0 ] == BoardArr [1 ][1 ] && BoardArr [2 ][0 ] == BoardArr [0 ][2 ]) ) {
237
+
238
+ return BoardArr [2 ][0 ];
239
+ }
240
+
241
+ return winner = '_' ; //No winner yet : Winner stays = '_' i.e. there is no winner yet, and the game continues
242
+
243
+ }
244
+
245
+
246
+ void ShowWinner (char winner ) {
247
+
248
+ if (winner == player ) {
249
+
250
+ printf ("\nPlayer wins! Computer loses! :)\n\n" );
251
+ PlayerScore ++ ;
252
+ }
253
+ else if (winner == computer ) {
254
+
255
+ printf ("\nComputer wins! Player loses! :(\n\n" ); // will print the winner after each match, and add 1 to their # of wins, resets every 3 matches played
256
+ ComputerScore ++ ;
257
+ }
258
+ else {
259
+
260
+ printf ("\nTie! No winner.\n\n" );
261
+ Ties ++ ;
262
+ }
263
+ }
0 commit comments