-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathttt.c
110 lines (87 loc) · 2.06 KB
/
ttt.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
#include <stdio.h>
#include <stdlib.h>
#define ONGOING 0
#define STALEMATE 1
#define X_WON 2
#define O_WON 3
#define INVALID 4
static int checkState_(char board[3][3]);
static void printBoard_(char board[3][3]);
static const char *stateName_(int state);
/*----------------------------------------------------------------------------*/
int
main(int argc, char *argv[])
{
char board[3][3];
int i;
int j;
int state;
if (argc != 10)
{
printf("Invalid parameters! Usage:\n");
printf("ttt b11 b12 b13 b21 b22 b23 b31 b32 b33\n");
printf("Where bij indicates the board position at i-row and "
"j-column,\n");
printf("being either X or O. Use _ for empty positions.\n");
printf("As suggestions, one could invoke the command with line "
"breaks, using the \\ character at the end of a line.\n");
exit(1);
}
for(i = 0; i < 3; ++i)
{
for(j = 0; j < 3; ++j)
{
board[i][j] = argv[i*3 + j][0];
}
}
state = checkState_(board);
printf("Tic Tac Toe board\n");
printBoard_(board);
printf("state is %s.\n", stateName_(state));
return 0;
}
/*----------------------------------------------------------------------------*/
static int
checkState_(char board[3][3])
{
(void)board;
return INVALID;
}
/*----------------------------------------------------------------------------*/
static void
printBoard_(char board[3][3])
{
printf("%c|%c|%c\n", board[0][0], board[0][1], board[0][2]);
printf("-----\n");
printf("%c|%c|%c\n", board[1][0], board[1][1], board[1][2]);
printf("-----\n");
printf("%c|%c|%c\n", board[2][0], board[2][1], board[2][2]);
}
/*----------------------------------------------------------------------------*/
static const char *
stateName_(int state)
{
const char *stateName;
switch(state)
{
case ONGOING:
stateName = "on going";
break;
case STALEMATE:
stateName = "stalemate";
break;
case X_WON:
stateName = "won by X";
break;
case O_WON:
stateName = "won by O";
break;
case INVALID:
stateName = "invalid";
break;
default:
stateName = "unknown";
break;
}
return stateName;
}