-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchess.c
71 lines (63 loc) · 1.73 KB
/
chess.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
//
// Created by William on 3/15/2019.
// Contains functions for general game control
//
#include <stdint.h>
#include "chess_board.h"
#include "chess.h"
/**
* Checks that the game has not ended
* @param board The board to check
* @return True if both kings are on the board
*/
uint8_t validate_board(chessBoard board){
uint8_t has_king_white = 0;
uint8_t has_king_black = 0;
for (uint8_t x = 0; x < 8; x++){
for (uint8_t y = 0; y < 8; y++){
switch (get_piece(board,x,y)){
case WHITE_KING_NUMBER:
has_king_white = 1;
if (has_king_black){
return 1;
}
break;
case BLACK_KING_NUMBER:
has_king_black = 1;
if (has_king_white){
return 1;
}
default:break;
}
}
}
return has_king_black && has_king_white;
}
/**
* Figures out who Won the game
* @param board The board to check
* @return WHITE_WINS, BLACK_WINS or STALEMATE
*/
uint8_t get_winner(chessBoard board){
uint8_t has_king_white = 0;
uint8_t has_king_black = 0;
for (uint8_t x = 0; x < 8; x++){
for (uint8_t y = 0; y < 8; y++){
switch (get_piece(board,x,y)){
case WHITE_KING_NUMBER:
has_king_white = 1;
break;
case BLACK_KING_NUMBER:
has_king_black = 1;
default:break;
}
}
}
if (has_king_white && has_king_black){
return STALEMATE;
}else if (has_king_black){
return BLACK_WINS;
}else{
return WHITE_WINS;
}
}