From b95a33abda8faac3638691dde70de0d59fadb30f Mon Sep 17 00:00:00 2001 From: Kevin Worth Date: Mon, 28 Nov 2005 18:51:44 +0000 Subject: [PATCH] 2005-11-28 Kevin Worth * PROTOCOL: Changed board cell numbering to start with zero instead of one. * src/Makefile.am: * src/test-board.c: (main): Added new file for testing. * src/ttt-board.c: (ttt_board_init), (ttt_board_to_string): * src/ttt-board.h: Implemented board_to_string. --- ChangeLog | 11 +++++++++++ PROTOCOL | 6 +++--- src/Makefile.am | 7 +++++++ src/test-board.c | 17 +++++++++++++++++ src/ttt-board.c | 21 ++++++++++++--------- src/ttt-board.h | 10 +++++++++- 6 files changed, 59 insertions(+), 13 deletions(-) create mode 100644 src/test-board.c diff --git a/ChangeLog b/ChangeLog index d1a6ced..3e62744 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,14 @@ +2005-11-28 Kevin Worth + + * PROTOCOL: Changed board cell numbering to start with zero + instead of one. + + * src/Makefile.am: + * src/test-board.c: (main): Added new file for testing. + + * src/ttt-board.c: (ttt_board_init), (ttt_board_to_string): + * src/ttt-board.h: Implemented board_to_string. + 2005-11-28 kevin worth * src/ttt-board.c: diff --git a/PROTOCOL b/PROTOCOL index 519b1ba..c507ffe 100644 --- a/PROTOCOL +++ b/PROTOCOL @@ -173,9 +173,9 @@ Document Conventions indicates a number in the tic-tac-toe grid as follows: - 1|2|3 - 4|5|6 - 7|8|9 + 0|1|2 + 3|4|5 + 6|7|8 Possible errors: NOTINGAME, NOTYOURMOVE, NOTGRID diff --git a/src/Makefile.am b/src/Makefile.am index c205ef9..1d5de3f 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,4 +1,5 @@ bin_PROGRAMS = ttt-server ttt-client +noinst_PROGRAMS = test-board ttt_common_sources = \ ttt-args.c \ @@ -22,7 +23,13 @@ ttt_server_SOURCES = \ ttt-client.c \ ttt-server.c +test_board_SOURCES = \ + $(ttt_common_sources) \ + test-board.c + AM_CFLAGS = $(WARN_CFLAGS) $(TTT_CFLAGS) AM_LFLAGS = --header-file=ttt-lex.h -Cr ttt_client_LDFLAGS = $(TTT_LIBS) ttt_server_LDFLAGS = $(TTT_LIBS) -lpthread +test_board_LDFLAGS = $(TTT_LIBS) + diff --git a/src/test-board.c b/src/test-board.c new file mode 100644 index 0000000..d36b36d --- /dev/null +++ b/src/test-board.c @@ -0,0 +1,17 @@ +/* Test ttt-board.c */ + +#include "ttt-board.h" + +int +main (void) +{ + ttt_board_t board; + + ttt_board_init (&board); + + printf ("This is the board \""); + ttt_board_write (&board, stdout); + printf ("\"\n"); + + return 0; +} diff --git a/src/ttt-board.c b/src/ttt-board.c index 27fb19e..dd068b5 100644 --- a/src/ttt-board.c +++ b/src/ttt-board.c @@ -21,18 +21,14 @@ #include "ttt-board.h" -struct _ttt_board { - int cells[TTT_BOARD_MAX_CELLS]; -}; - /* Initialize an empty board. */ void ttt_board_init (ttt_board_t *board) { - int i; - for (i = 0; i < TTT_BOARD_MAX_CELLS; i++) + int i; + for (i = 0; i < TTT_BOARD_MAX_CELLS; i++) { - board->cells[i] = 0; + board->cells[i] = TTT_CELL_EMPTY; } } @@ -58,8 +54,15 @@ ttt_board_init_from_string (ttt_board_t *board, char * ttt_board_to_string (ttt_board_t *board) { - /* XXX: NYI */ - return NULL; + char *s; + xasprintf (&s, "\r\n" + "%c|%c|%c\r\n" + "%c|%c|%c\r\n" + "%c|%c|%c", + board->cells[0], board->cells[1], board->cells[2], + board->cells[3], board->cells[4], board->cells[5], + board->cells[6], board->cells[7], board->cells[8]); + return s; } /* Write a string representation of a board to the provided file. diff --git a/src/ttt-board.h b/src/ttt-board.h index e9498cc..b04d4b3 100644 --- a/src/ttt-board.h +++ b/src/ttt-board.h @@ -26,7 +26,15 @@ #define TTT_BOARD_MAX_CELLS 9 -typedef struct _ttt_board ttt_board_t; +typedef enum { + TTT_CELL_EMPTY = '_', + TTT_CELL_X = 'X', + TTT_CELL_O = 'O' +} ttt_cell_t; + +typedef struct _ttt_board { + ttt_cell_t cells[TTT_BOARD_MAX_CELLS]; +} ttt_board_t; void ttt_board_init (ttt_board_t *board); -- 2.43.0