diff options
author | Raúl Benencia <rul@kalgan.cc> | 2015-03-31 15:45:21 -0300 |
---|---|---|
committer | Raúl Benencia <rul@kalgan.cc> | 2015-03-31 15:45:21 -0300 |
commit | 9e791e4e927b837c8647765ada6f25be1ac086cc (patch) | |
tree | 8a82113a036705ccfc59023ff2e0134e275394dd | |
parent | 9ad93043d107926b48196b9c9ba9588ded5f4b64 (diff) |
introduce game loop function
-rw-r--r-- | Makefile | 4 | ||||
-rw-r--r-- | include/game.h | 11 | ||||
-rw-r--r-- | lib/game.c | 36 |
3 files changed, 49 insertions, 2 deletions
@@ -2,8 +2,8 @@ CC = gcc CFLAGS = -Wall -Iinclude/ OUTPUT = main MAIN = main.c -DEPS = include/types.h include/board.h include/print.h include/piece.h include/coordinate.h include/move.h include/input.h -OBJ = lib/board.o lib/print.o lib/piece.o lib/coordinate.o lib/move.o lib/input.o +DEPS = include/types.h include/board.h include/print.h include/piece.h include/coordinate.h include/move.h include/input.h include/game.h +OBJ = lib/board.o lib/print.o lib/piece.o lib/coordinate.o lib/move.o lib/input.o lib/game.o %.o: %.c $(DEPS) $(CC) -c -o $@ $< $(CFLAGS) diff --git a/include/game.h b/include/game.h new file mode 100644 index 0000000..61239e7 --- /dev/null +++ b/include/game.h @@ -0,0 +1,11 @@ +#ifndef _GAME +#define _GAME + +#include "types.h" + +/* + * Implements the main game loop. Returns the final state of the board. + */ +Board game_loop(Board); + +#endif diff --git a/lib/game.c b/lib/game.c new file mode 100644 index 0000000..6fcc0c5 --- /dev/null +++ b/lib/game.c @@ -0,0 +1,36 @@ +#include <stdio.h> + +#include "board.h" +#include "game.h" +#include "input.h" +#include "move.h" +#include "print.h" + +static Color _toggle_current_player(Color c) { + if (c == WHITE) + return BLACK; + else + return WHITE; +} + +Board game_loop(Board board) { + Board b = board; + Color current_player = WHITE; + Coord orig, dest; + + while (1) { + print_board(b, current_player); + + putchar('\n'); + if (current_player == WHITE) + printf("White's turn. \n"); + else + printf("Black's turn. \n"); + + orig = input_orig_coord(); + dest = input_dest_coord(); + + b = board_make_move(b, move_init(orig, dest)); + current_player = _toggle_current_player(current_player); + } +} |