/
a07.c
113 lines (92 loc) · 1.99 KB
/
a07.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
#include <stdio.h>
#ifndef __TRUE_FALSE__
#define __TRUE_FALSE__
#define TRUE 1
#define FALSE 0
#endif
#define ROWS 7
#define COLS 7
#define MARKONE 'X'
#define MARKTWO 'O'
#define BLANK ' '
void InitializeBoard(char[ROWS][COLS]);
void DisplayBoard(char[ROWS][COLS]);
int PlayerMove(int, int, char[ROWS][COLS], char);
int main()
{
// declare variables
char board[ROWS][COLS];
// initialize board
InitializeBoard(board);
// populate the board with moves
PlayerMove( 1, 1, board, MARKONE );
PlayerMove( 1, 2, board, MARKONE );
PlayerMove( 4, 3, board, MARKONE );
PlayerMove( 1, 1, board, MARKTWO );
PlayerMove( 6, 2, board, MARKTWO );
PlayerMove( 4, 12, board, MARKTWO );
// display the board
DisplayBoard(board);
// exit program
return 0;
}
// Function Implementations
void InitializeBoard(char board[ROWS][COLS]) {
int i;
for (i = 0; i < ROWS; i++) {
int k;
for (k = 0; k < COLS; k++)
board[i][k] = BLANK;
}
}
void DisplayBoard(char board[ROWS][COLS]) {
int row;
void topRow() {
int col;
printf("\tTICK ATTACKS TOE\n\n");
printf(" ");
for (col = 1; col <= COLS; col++) {
printf(" %1d", col );
}
printf("\n\n");
}
void middleRow() {
int col;
printf(" -");
for (col = 2; col <= COLS; col++) {
printf("+-");
}
printf("\n");
}
void majorRow(char board[ROWS][COLS], int row) {
int col;
printf("%d %c", row, board[row-1][0] );
for (col = 2; col <= COLS; col++) {
printf("|%c", board[row-1][col - 1]);
}
printf("\n");
}
topRow();
majorRow(board, 1);
for (row = 2; row <= ROWS; row++) {
middleRow();
majorRow( board, row );
}
}
int PlayerMove(int row, int col, char board[ROWS][COLS], char symbol) {
// Check for move legality
if ((row>0 && row<=ROWS) && (col>0 && col<=COLS)) {
if (board[row-1][col-1] == BLANK) {
board[row-1][col-1] = symbol;
return TRUE;
}
else {
printf( "That space is already occupied.\n", row, col );
return FALSE;
}
}
else {
printf( "That move is not on the board.\n" );
return FALSE;
}
}