summaryrefslogtreecommitdiff
path: root/lib/game.c
blob: 0ebd819086cb6c201a3968983f8906957e762431 (plain)
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
#include <stdio.h>

#include "board.h"
#include "game.h"
#include "input.h"
#include "move.h"
#include "print.h"

/*
 * Return 1 if the received player is checkmated. Returns 0 otherwise.
 * TODO
 */
int game_is_checkmate(Board board, Color player) {
    return 0;
}

/*
 * Check if a move is valid in the received board for the received player.
 * TODO.
 */
int game_is_move_valid(Board board, Color player, Move move) {
    return 1;
}

Board game_loop(Board b) {
    Board board = b;
    Color current_player = WHITE;
    Move m;
    int move_valid;

    while (!game_is_checkmate(board, current_player)) {
        print_board(board, current_player);

        putchar('\n');
        if (current_player == WHITE)
            puts("White's turn.");
        else
            puts("Black's turn");

        move_valid = 0;
        while (!move_valid) {
            m = input_move();

            if (game_is_move_valid(board, current_player, m))
                move_valid = 1;
            else
                printf("Invalid move. Please, try again. ");
        }

        board = board_make_move(board, m);
        current_player = current_player == WHITE ? BLACK : WHITE;
    }

    return 0;
}
nihil fit ex nihilo