-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconsole.c
More file actions
143 lines (121 loc) · 2.91 KB
/
console.c
File metadata and controls
143 lines (121 loc) · 2.91 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include "checkers.h"
// Display the board
void board_display() {
// Top coordinates
printf(" ");
for(int i = 0; i < BOARD_WIDTH; i++)
printf("%2d ", i+1);
printf("\n");
// Top line characters
printf(" ┌");
for(int i = 0; i < BOARD_WIDTH; i++) {
printf("───");
printf(i != BOARD_WIDTH-1 ? "┬" : "┐");
}
printf("\n");
// Rows of pieces
for(int y = 0; y < BOARD_HEIGHT; y++) {
printf("%2d│", y+1);
for(int x = 0; x < BOARD_WIDTH; x++) {
if(has_piece(x, y)) {
Piece p;
get_piece(x, y, &p);
char c = players[p.player].piece;
if(p.king) c = toupper(c);
printf(" %c │", c);
} else {
printf(" │");
}
}
printf("\n");
// Separator or bottom border
if(y != BOARD_HEIGHT-1) {
printf(" ├");
for(int i = 0; i < BOARD_WIDTH; i++) {
printf("───");
printf(i == BOARD_WIDTH-1 ? "┤" : "┼");
}
} else {
printf(" └");
for(int i = 0; i < BOARD_WIDTH; i++) {
printf("───");
printf(i == BOARD_WIDTH-1 ? "┘" : "┴");
}
}
printf("\n");
}
}
// Return a line of input, it's a static char* so don't free it
char *getinput(FILE *f) {
static char *buf = NULL;
static int buflen = 0;
if(buf == NULL) {
buf = malloc(16);
buflen = 16;
}
char *p = buf;
int len = buflen;
while(1) {
int c = fgetc(f);
if(c == EOF || c == '\n') {
*p = 0;
break;
}
if(len <= 0) {
len = buflen;
buflen *= 2;
buf = realloc(buf, buflen);
p = buf + len;
}
*p++ = c;
len--;
}
return buf;
}
int main(int argc, char *argv[]) {
int scores[NUM_PLAYERS] = {0};
board_init();
int current_player = 0;
while(1) {
get_scores(scores);
// End game if only one player is left
int players_left = 0;
int high_score = 0;
for(int pl = 0; pl < NUM_PLAYERS; pl++) {
if(scores[pl] == 0) continue;
players_left++;
if(scores[pl] > scores[high_score]) high_score = pl;
}
if(players_left == 1) {
printf("%s wins!\n", players[high_score].name);
return 0;
}
// Display and get input
board_display();
printf("%s, it is your turn.\n", players[current_player].name);
printf("Enter move: ");
char *line = getinput(stdin);
int x1, x2, y1, y2;
if(sscanf(line, "%d,%d %d,%d", &x1, &y1, &x2, &y2) != 4) {
printf("Invalid move.\n");
continue;
}
x1--;
y1--;
x2--;
y2--;
// Validate and perform move
if(!valid_move(x1, y1, x2, y2, current_player)) {
printf("Invalid move\n");
continue;
}
bool captured = move_piece(x1, y1, x2, y2);
if(y2 == players[current_player].king_row)
promote_piece(x2, y2);
if(!captured)
current_player = (current_player+1) % NUM_PLAYERS;
}
}