-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheckers.c
247 lines (229 loc) · 5.77 KB
/
checkers.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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/* project.c
*Tom O'Connor
*Fundamentals of Computing
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "gfx.h"
#include <math.h>
#define squareLen 100
#define boardWidth 800
#define boardHeight 800
#define boardSize 8
typedef struct{
int color; //-1=not playable 0=not 1=r or 2=b
char type; //n for normal k for king
int xpos;
int ypos;
}piece;
void drawBoard(piece [boardSize][boardSize]);
void initBoard(piece [boardSize][boardSize]);
void drawSquare(int, int);
void inMove(piece [boardSize][boardSize], int *,int *);
void makeMove(piece [boardSize][boardSize],int, int ,int ,int);
int isValid(piece [boardSize][boardSize], int *,int,int,int,int);
int kingValid(piece [boardSize][boardSize], int *,int,int,int,int);
int main()
{
piece board[boardSize][boardSize];
gfx_open(boardWidth,boardHeight,"Checkers");
initBoard(board);//initialize the board
int firstMove=1;//set red to move first
int *move;
move=&firstMove;
int q=0;
int *quit=&q;
while(*quit==0)//check to make sure that user did not input 'q'
{
drawBoard(board);//draw the board
inMove(board,move, quit); //take input for the move
}
return 0;
}
void initBoard(piece board[boardSize][boardSize])
{
for(int i=0;i<boardSize;i++){
for(int j=0;j<boardSize;j++){
if(i<3 && (i+j)%2==0){//set up the red pieces
board[i][j].color=1;
board[i][j].type='n';//set the piece types to normal
}else if(i>4 &&(i+j)%2==0){//set up black pieces
board[i][j].color=2;
board[i][j].type='n';
}else if(i>=3&&i<=4&&(i+j)%2==0){//set the color for the playable squares
board[i][j].color=0;
}
else {
board[i][j].color=-1;//mark the unplayable squares
}
}
}
}
void drawBoard(piece board[boardSize][boardSize])
{
for (int i=0; i<8; i++){
for (int j=0; j<8;j++){ //alternate the color of the squares
if((i+j)%2==0){
gfx_color(22,184,135);
}else{
gfx_color(255,255,255);
}
drawSquare(j*squareLen,i*squareLen);
}
}
for(int i=0; i<8; i++){
for(int j=0;j<8;j++){
if(board[i][j].color==1){
gfx_color(255,0,0);
for(int r=35; r>0 ;r--){
gfx_circle(j*squareLen+50,i*squareLen+50,r);
}
if(board[i][j].type=='k'){//signal that is king
for(int r=10;r>0; r--){
gfx_color(255,255,255);
gfx_circle(j*squareLen+50,i*squareLen+50,r);
}
}
}else if(board[i][j].color==2){
for(int r=35; r>0 ;r--){
gfx_color(0,0,0);
gfx_circle(j*squareLen+50,i*squareLen+50,r);
}
if(board[i][j].type=='k'){//signal that a piece is a king
for(int r=10;r>0; r--){
gfx_color(255,255,255);
gfx_circle(j*squareLen+50,i*squareLen+50,r);
}
}
}else if(board[i][j].color==0){
for(int r=35;r>0;r--){
gfx_color(22,184,135);
gfx_circle(j*squareLen+50,i*squareLen+50,r); }
}
}
}
}
void drawSquare(int x, int y)
{
int xmax=x+squareLen;
int ymax=y+squareLen;
while(x<xmax)//keep making squares until both x values meet to fill them
{
gfx_line(x,y,xmax,y);
gfx_line(x, y, x, ymax);
gfx_line(x, ymax, xmax, ymax);
gfx_line(xmax, y, xmax, ymax);
x++;//increase lower bounds
y++;
xmax--;//decrease upper bounds
ymax--;
}
}
void inMove(piece board[boardSize][boardSize], int *move, int *quit)
{
int xclick1, yclick1, xclick2, yclick2;
char c=gfx_wait();
if(c==1){//get first click
xclick1=gfx_xpos();
yclick1=gfx_ypos();
}
if (c=='q')//check if quitting
{
*quit=1;
return;
}
char d=gfx_wait();
if(d==1){//get second click
xclick2=gfx_xpos();
yclick2=gfx_ypos();
}
if (d=='q')//check if quitting
{
*quit=1;
return;
}
int x1=xclick1/100;
int y1=yclick1/100;
int x2=xclick2/100;
int y2=yclick2/100;
if (board[y1][x1].type=='k'){
if(kingValid(board, move, x1, y1,x2,y2)==1){//check if valid king move
makeMove(board,x1,y1,x2,y2);
if(*move==1){//change what color's move it is
*move=2;
}
else{
*move=1;
}
}
}
else if(board[y1][x1].type=='n'){
if(isValid(board, move, x1, y1, x2, y2)==1){//check if valid normal move
makeMove(board,x1,y1,x2,y2);
if(*move==1){//chenge whose move it is
*move=2;
}
else if(*move==2){
*move=1;
}
}
}
else{
return;
}
}
void makeMove(piece board[boardSize][boardSize], int x1, int y1, int x2, int y2)
{
piece tempPiece=board[y2][x2];
board[y2][x2]=board[y1][x1];
board[y1][x1]= tempPiece;
if(y2==0 || y2== 7){//check if reached the end of board
board[y2][x2].type='k';//make it a king if reaches the end
}
}
int isValid(piece board[boardSize][boardSize], int *move,int x1,int y1,int x2,int y2)
{
if(x1==x2||y1==y2){//make sure piece is trying to move
return 0;
}
if(board[y1][x1].color==*move){
if(*move==1&& board[y2][x2].color==0){//check if space is open and red's move
if(y2==y1+1 && abs(x2-x1)==1){//make sure diagonal move and correct direction
return 1;
}else if(board[(y1+y2)/2][(x1+x2)/2].color==2&&y2>y1){
board[(y1+y2)/2][(x1+x2)/2].color=0;//jump the piece and remove the piece being
//jumped
return 1;
}
}
else if(board[y2][x2].color==0&&*move==2){//check if space is open and black's move
if(board[y2][x2].color==0&&y2==y1-1 && abs(x2-x1)==1){
return 1;
}else if(board[(y1+y2)/2][(x1+x2)/2].color==1&&y2<y1){
board[(y1+y2)/2][(x1+x2)/2].color=0;//jump the piece
return 1;
}
}
}
return 0;//return invalid
}
int kingValid(piece board[boardSize][boardSize], int *move,int x1,int y1,int x2,int y2)
{
if(x1==x2||y1==y2){//make sure trying to move to different square
return 0;
}
if(board[y1][x1].color==*move){//check that correct color's turn
if(board[y2][x2].color==0){
if(abs(y2-y1)==1 && abs(x2-x1)==1)//check that moving one space
{
return 1;
}
else if(board[(y1+y2)/2][(x1+x2)/2].color!=board[y1][x1].color){
board[(y1+y2)/2][(x1+x2)/2].color=0;//jump the piece
return 1;
}
}
}
return 0;
}