-
Notifications
You must be signed in to change notification settings - Fork 0
/
tictactoe.c
114 lines (100 loc) · 2.63 KB
/
tictactoe.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char board[10] = {'o', '1', '2', '3', '4', '5', '6', '7', '8', '9'}; // our "map"
void drawBoard();
int checkIfWon();
void clearScreen() // clears screen by spamming newlines
{
int n;
for (n = 0; n < 10; n++)
printf("\n\n\n\n\n\n\n\n\n\n");
}
int main()
{
int choice, player = 1, i;
char mark;
do
{
drawBoard(); // draw board
player = (player % 2) ? 1 : 2; // change player each run
printf("Player %d, enter a number: ", player);
scanf("%d", &choice); // get choice
char choiceChar = choice + '0'; // convert int to char
if (player == 1)
mark = 'X';
else
mark = 'O';
if (choiceChar == board[choice]) // check if field is already occupied
{
board[choice] = mark;
}
else
{
char ch[10];
printf("Invalid move!\n");
player--;
getchar();
getchar(); // we're calling getchar twice, due to an issue with scanf
}
i = checkIfWon();
player++;
} while (i == -1);
drawBoard();
if (i == 1)
{
printf("Player %d won!\n", --player); // we need to substract 1 from player
}
else
{
printf("Game draw!\n");
}
getchar();
}
void drawBoard()
{
clearScreen();
printf("\n");
printf("|%c|%c|%c|\n", board[1], board[2], board[3]);
printf("\n");
printf("|%c|%c|%c|\n", board[4], board[5], board[6]);
printf("\n");
printf("|%c|%c|%c|\n", board[7], board[8], board[9]);
printf("\n");
}
int checkIfWon()
{
if (board[1] == board[2] && board[2] == board[3])
return 1;
else if (board[4] == board[5] && board[5] == board[6])
return 1;
else if (board[7] == board[8] && board[8] == board[9])
return 1;
else if (board[1] == board[4] && board[4] == board[7])
return 1;
else if (board[2] == board[5] && board[5] == board[8])
return 1;
else if (board[3] == board[6] && board[6] == board[9])
return 1;
else if (board[1] == board[5] && board[5] == board[9])
return 1;
else if (board[3] == board[5] && board[5] == board[7])
return 1;
char arr[] = "X";
int i;
for (i = 1; i < 9; i++) // draw check
{
int field;
arr[0] = board[i];
field = atoi(arr);
if (i != field){
continue;
}
else{
break;
}
}
if (i == 9) // all of the values are different, draw
return 0;
return -1; // win check failed, still playing
}