From 4e767de0d2245b754e9d6b13f356c2fff3c6d3d0 Mon Sep 17 00:00:00 2001 From: Fazal Ali Date: Mon, 22 Jun 2020 03:31:36 -0500 Subject: [PATCH] Added example and updated readme with usage --- README.md | 15 ++++ .../2PlayerNoFeatures/2PlayerNoFeatures.c | 81 +++++++++++++++++++ .../2PlayerNoFeatures/2PlayerNoFeatures.h | 15 ++++ examples/2PlayerNoFeatures/README.md | 9 +++ examples/2PlayerNoFeatures/makefile | 12 +++ 5 files changed, 132 insertions(+) create mode 100644 examples/2PlayerNoFeatures/2PlayerNoFeatures.c create mode 100644 examples/2PlayerNoFeatures/2PlayerNoFeatures.h create mode 100644 examples/2PlayerNoFeatures/README.md create mode 100644 examples/2PlayerNoFeatures/makefile diff --git a/README.md b/README.md index 83c402f..32f3b95 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,21 @@ Mr. Queen is a chess engine written in C. * Unaware of draw by repetition. * Basic evaluation includes only material and piece-square values. +#### Usage +* Use 'make' to build the program as is. This program gives you access to the UCI implementation. +* When implementing your own programs, you can create objects of the different parts. +..* For example, if you need to create a board you can use 'Board board;' +..* Then you can use the functions in board.c such as 'board_print(&boar);' + --- +##Examples + +###2PlayerNoFeatures +As the name suggests, this implemntation is of a chess game with 2 players but with absolutely no features. That means, the users have to account for everything including castling, checkmates, promotions, captures etc. + +It is inteded to give you a basic understanding of how to use the library in an actual program. + +####Usage +Navigate into the examples folder. Then, in your command line, use the 'make' command to build the program.

diff --git a/examples/2PlayerNoFeatures/2PlayerNoFeatures.c b/examples/2PlayerNoFeatures/2PlayerNoFeatures.c new file mode 100644 index 0000000..7bb4a6a --- /dev/null +++ b/examples/2PlayerNoFeatures/2PlayerNoFeatures.c @@ -0,0 +1,81 @@ +#include "2PlayerNoFeatures.h" + + +int main(int argc, char **argv) { + print_commands(); + + //Initialize game variable + Board board; + int currentPlayer = 1; + + + //Initiallize game + board_reset(&board); + board_print(&board); + + while(1){ + int currentRoundEnd = play_round(currentPlayer, &board); + if(currentRoundEnd == 1){ + return 0; + }else{ + currentPlayer = currentPlayer == 1? 2: 1; + } + } + return 0; +} + +int play_round(int currentPlayer, Board *board) +{ + string s = malloc(sizeof(char)*5); + while(1){ + printf("Player %i: ", currentPlayer); + scanf("%s", s); + lower(&s); + int playerChoiceResult = handle_player_choice(board, s); + if(playerChoiceResult == 0 || playerChoiceResult == 1) {return playerChoiceResult;} + } + free(s); +} + +int handle_player_choice(Board *board, string s){ + if(strcmp(s, "quit") == 0){ + return 1; + } else if(strcmp(s, "show") == 0){ + board_print(board); + return -1; + } else if (strcmp(s, "help" == 0)){ + print_commands(); + return -1; + } else{ + process_move(board, s); + return 0; + } +} + +void process_move(Board *board, string s){ + const string moveString = s; + Move move; + printf("MOVE:%s\n", s); + move_from_string(&move, moveString); + make_move(board, &move); + board_print(board); +} + +void lower(char **s){ + char *temp = *s; + int slen = strlen(*s); + char tempHolder[slen]; + for(int i = 0; i < slen; i++){ + tempHolder[i] = tolower(temp[i]); + } + *s = tempHolder; +} + +void print_commands(void){ + printf( + "-Type help to get this list of commands.\n" + "-To make a move, type your move in long algebraic notation.\n" + "-To See the board in it\'s current state, type show. \n" + "-To quit the program, type quit\n" + ); +} \ No newline at end of file diff --git a/examples/2PlayerNoFeatures/2PlayerNoFeatures.h b/examples/2PlayerNoFeatures/2PlayerNoFeatures.h new file mode 100644 index 0000000..cc6f494 --- /dev/null +++ b/examples/2PlayerNoFeatures/2PlayerNoFeatures.h @@ -0,0 +1,15 @@ +#include "../../src/board.h" +#include "../../src/move.h" +#include +#include +#include +#include +#include + +#define string char * + +void print_commands(void); +int play_round(int currentPlayer, Board *board); +void lower(char **s); +int handle_player_choice(Board *board, string s); +void process_move(Board *board, string s); \ No newline at end of file diff --git a/examples/2PlayerNoFeatures/README.md b/examples/2PlayerNoFeatures/README.md new file mode 100644 index 0000000..fb2def0 --- /dev/null +++ b/examples/2PlayerNoFeatures/README.md @@ -0,0 +1,9 @@ +#Examples + +##2PlayerNoFeatures +As the name suggests, this implemntation is of a chess game with 2 players but with absolutely no features. That means, the users have to account for everything including castling, checkmates, promotions, captures etc. + +It is inteded to give you a basic understanding of how to use the library in an actual program. + +###Usage +Navigate into the examples folder. Then, in your command line, use the 'make' command to build the program. \ No newline at end of file diff --git a/examples/2PlayerNoFeatures/makefile b/examples/2PlayerNoFeatures/makefile new file mode 100644 index 0000000..47ea7c5 --- /dev/null +++ b/examples/2PlayerNoFeatures/makefile @@ -0,0 +1,12 @@ +CC=gcc +CFLAGS=-I. +DEPS = ../../src/board.c ../../src/move.c ../../src/gen.c ../../src/bb.c ../../src/util.c + +%.o: %.c $(DEPS) + $(CC) -c -o $@ $< $(CFLAGS) + +2PlayerNoFeatures : 2PlayerNoFeatures.c + $(CC) $(CFLAGS) 2PlayerNoFeatures.c -o 2PlayerNoFeatures $(DEPS) + +clean: + rm 2PlayerNoFeatures \ No newline at end of file