diff --git a/connect4.c b/connect4.c index 28a0955..ef18406 100644 --- a/connect4.c +++ b/connect4.c @@ -27,7 +27,7 @@ #include "types.h" #include "ui.h" -#define VERSION 0.1.1 +#define VERSION 0.1.2 #ifndef GITHASH #define FULLVERSION VERSION @@ -164,10 +164,9 @@ int main( int argc, char *argv[] ){ initBoard( playboard ); // Begin loopin for(;; playboard.player = !playboard.player ){ - move_t move = askMove(); - int column = askColumn( playboard, move ); - playMove( &playboard, move, column ); + int column = askColumn( playboard ); + playMove( &playboard, column ); calcWins( &wins, playboard, column ); - updateBoard( wins, playboard, move, column ); + updateBoard( wins, playboard, column ); } } diff --git a/logic.c b/logic.c index 4e0df6b..43e192e 100644 --- a/logic.c +++ b/logic.c @@ -38,25 +38,16 @@ static inline int bottomHeightMask( const int a, const int b ){ return safeHeightMask( bottom( a, b ) ); } -void playMove( +inline void playMove( board_t *boardptr, - const move_t move, const int column ){ - switch( move ){ - case PUT: // Put a new piece in the board - boardptr->column [ column ] |= - boardptr->player << boardptr->height [ column ]; - boardptr->height [ column ]++; - break; - case POP: // Pop out the lowest and make all above fall down - boardptr->column [ column ] >>= 1; - boardptr->height [ column ]--; - default: // Do nothing - } + boardptr->column [ column ] |= + boardptr->player << boardptr->height [ column ]; + boardptr->height [ column ]++; } -void calcWins( +inline void calcWins( wins_t *wins, const board_t board, const int column diff --git a/logic.h b/logic.h index ccf712d..4123f8a 100644 --- a/logic.h +++ b/logic.h @@ -6,7 +6,6 @@ extern void playMove( board_t *boardptr, - const move_t move, const int column ); diff --git a/types.h b/types.h index a31977c..49ee1eb 100644 --- a/types.h +++ b/types.h @@ -4,13 +4,6 @@ #include #include "config.h" -typedef enum { - QUIT=0, - PUT, - POP, - MOVECOUNT -} move_t; - typedef int column_t; typedef struct { diff --git a/ui.h b/ui.h index 18a1673..e9575a6 100644 --- a/ui.h +++ b/ui.h @@ -11,13 +11,9 @@ extern void initBoard( extern void updateBoard( const wins_t wins, const board_t board, - const move_t move, const int column ); extern int askColumn( - const board_t board, - const move_t move + const board_t board ); - -extern move_t askMove( void ); diff --git a/ui_ncurses.c b/ui_ncurses.c index 6f71f5b..6043abd 100644 --- a/ui_ncurses.c +++ b/ui_ncurses.c @@ -132,28 +132,14 @@ void initBoard( const board_t board ){ void updateBoard( const wins_t wins, const board_t board, - const move_t move, const int column ){ int height = board.height[ column ]; - switch( move ){ - case PUT: - mvaddstr( - BOARD_Y + BOARD_DY * ( board.rows - height + 1 ), - BOARD_X + 1 + BOARD_DX * ( column + 1 ), - board.column[ column ] & 1 << ( height - 1 ) ? "1" : "0" - ); - break; - default: - for( int row = 0; row < height; row++ ){ - mvaddstr( - BOARD_Y + BOARD_DY * ( board.rows - row ), - BOARD_X + 1 + BOARD_DX * ( column + 1 ), - board.column[ column ] & 1 << row ? "1" : "0" - ); - } - break; - } + mvaddstr( + BOARD_Y + BOARD_DY * ( board.rows - height + 1 ), + BOARD_X + 1 + BOARD_DX * ( column + 1 ), + board.column[ column ] & 1 << ( height - 1 ) ? "1" : "0" + ); char num[4]; sprintf( num, "%3d", wins.count0.vertical ); mvaddstr( @@ -218,8 +204,7 @@ void updateBoard( } int askColumn( - const board_t board, - const move_t move + const board_t board ){ int column = 0; #ifdef ARROWS @@ -244,33 +229,15 @@ int askColumn( case KEY_DOWN: return column; } - switch( move ){ - case PUT: - for(; board.height[ column ] >= board.rows; ) switch( ch ){ - case KEY_RIGHT: - if( column >= board.columns ) ch = KEY_LEFT; - else column++; - break; - case KEY_LEFT: - if( column == 0 ) ch = KEY_RIGHT; - else column--; - break; - } + for(; board.height[ column ] >= board.rows; ) switch( ch ){ + case KEY_RIGHT: + if( column >= board.columns ) ch = KEY_LEFT; + else column++; break; - case POP: - for(; board.height[ column ] <= 0; ) switch( ch ){ - case KEY_RIGHT: - if( column >= board.columns ) ch = KEY_LEFT; - else column++; - break; - case KEY_LEFT: - if( column == 0 ) ch = KEY_RIGHT; - else column--; - break; - } + case KEY_LEFT: + if( column == 0 ) ch = KEY_RIGHT; + else column--; break; - default: - // Do nothing } } #else /* !ARROWS */ @@ -282,14 +249,7 @@ int askColumn( ); if( board.player ) addstr( "1" ); else addstr( "0" ); - switch( move ){ - case PUT: - addstr( " put the piece? " ); - case POP: - addstr( " pop a piece? " ); - default: - addstr( " Undefined move a thingy? " ); - } + addstr( " put the piece? " ); clrtoeol(); refresh(); scanw( " %d", &column ); @@ -300,30 +260,14 @@ int askColumn( ); clrtoeol(); if( column >= 0 && column < board.columns ) - switch( move ){ - case PUT: - if( board.height [ column ] < board.columns ) - return column; - else addstr( "Pls enter a column that ain't full" ); - break; - case POP: - if( board.height [ column ] != 0 ) - return column; - else addstr( "Pls enter a column that ain't empty" ); - default: - return column; - } + if( board.height [ column ] < board.columns ) + return column; + else addstr( "Pls enter a column that ain't full" ); else addstr( "Pls enter a column that exists" ); } #endif /* ! ARROWS */ } -move_t askMove( void ){ - move_t move = PUT; - // TODO: actually ask what move - return move; -} - #undef INPUT_X #undef INPUT_Y #undef SCOREBOARD_X diff --git a/ui_vt100.c b/ui_vt100.c index 7598f18..625bd78 100644 --- a/ui_vt100.c +++ b/ui_vt100.c @@ -69,31 +69,16 @@ void initBoard( const board_t board ){ void updateBoard( const wins_t wins, const board_t board, - const move_t move, const int column ){ int height = board.height [ column ]; - switch( move ){ - case PUT: // Only print the put one - printf( - "\033[%dA\033[%dC%c\033[%dB", - height + 3, - column * 3 + 4, - '0' + !!( board.player ), - height + 1 - ); - break; - default: // Print all the pieces in the column and the air above - printf( "\033[%dA", height + 4 ); - if( height < board.rows ) printf( "\033[%dC ", column * 3 + 4 ); - else printf( "\033[%dC", column * 3 + 5 ); - for( int row = height; row --> 0; ) - printf( - "\033[B\033[D%c", - '0' + !!( board.column [ column ] & ( 1 << row ) ) - ); - printf( "\033[2B" ); - } + printf( + "\033[%dA\033[%dC%c\033[%dB", + height + 3, + column * 3 + 4, + '0' + !!( board.player ), + height + 1 + ); printf( "\r\033[7A\033[%dC" "%3d │%3d\033[B\033[8D" @@ -112,18 +97,11 @@ void updateBoard( } int askColumn( - const board_t board, - const move_t move + const board_t board ){ int column; for(;;){ - switch( move ){ - case PUT: printf( "Wher player %d put? ", board.player ); - break; - case POP: printf( "Wher player %d pop? ", board.player ); - break; - default: printf( "Wher player %d do the thing? ", board.player ); - } + printf( "Wher player %d put? ", board.player ); printf( "\033[K" ); // clear everything after cursor fflush( stdout ); column = -1; @@ -141,32 +119,15 @@ int askColumn( } column -= FIRST_NUMBER; if( column >= 0 && column < board.columns ) - switch( move ){ - case PUT: - if( board.height [ column ] < board.rows ) - return column; - else printf( "\033[2Apls enter a column that ain't full" ); - break; - case POP: - if( board.height [ column ] != 0 ) - return column; - else printf( "\033[2Apls enter a column that ain't empty" ); - break; - default: - return column; - } + if( board.height [ column ] < board.rows ) + return column; + else printf( "\033[2Apls enter a column that ain't full" ); else printf( "\033[2Apls enter valid column" ); printf( "\033[K\n" ); } } -move_t askMove( void ){ - move_t move = PUT; - // TODO: actually ask what move - return move; -} - #undef BOARD_WIDTH_CHARS #undef SCOREBOARD_OFFSET #undef SCOREBOARD_WIDTH