Added command-line option for setting size of board

This commit is contained in:
AnnaSnoeijs 2025-06-06 21:31:59 +02:00
parent e47d63b430
commit 72b6290798
6 changed files with 101 additions and 74 deletions

View file

@ -21,22 +21,22 @@
#include <unistd.h>
#include "macros.h"
#define BOARD_WIDTH_CHARS ( 3 * BOARD_WIDTH + 6 )
#define BOARD_WIDTH_CHARS ( 3 * board.columns + 6 )
#define SCOREBOARD_OFFSET 5
#define SCOREBOARD_WIDTH 27
#define SCOREBOARD_HEIGHT 9
void initBoard( void ){
for( int i = SCOREBOARD_HEIGHT - ( BOARD_HEIGHT + 2 ); i > 0; i-- )
void initBoard( const board_t board ){
for( int i = SCOREBOARD_HEIGHT - ( board.rows + 2 ); i > 0; i-- )
putchar( '\n' );
printf( "\n " );
for( int column = 0; column < BOARD_WIDTH; column++ )
for( int column = 0; column < board.columns; column++ )
printf( "%2d ", column + FIRST_NUMBER );
putchar( '\n' );
for( int row = BOARD_HEIGHT - 1; row > -1; row-- ){
for( int row = board.rows - 1; row > -1; row-- ){
// begin row
printf( "%2d " , row + FIRST_NUMBER );
for( int column = 0; column < BOARD_WIDTH; column++ )
for( int column = 0; column < board.columns; column++ )
printf( "[ ]" );
// end of row
printf( "%2d" , row + FIRST_NUMBER );
@ -46,7 +46,7 @@ void initBoard( void ){
}
// end of board
printf( " " );
for( int column = 0; column < BOARD_WIDTH; column++ )
for( int column = 0; column < board.columns; column++ )
printf( "%2d ", column + FIRST_NUMBER );
#define SCOREBOARD_LINE_END "\033[B\033["XSTR(SCOREBOARD_WIDTH)"D"
printf("\033["XSTR(SCOREBOARD_OFFSET)"C\033["XSTR(SCOREBOARD_HEIGHT)"A"
@ -85,7 +85,7 @@ void updateBoard(
break;
default: // Print all the pieces in the column and the air above
printf( "\033[%dA", height + 4 );
if( height < BOARD_HEIGHT ) printf( "\033[%dC ", column * 3 + 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(
@ -101,7 +101,7 @@ void updateBoard(
"%3d │%3d\033[B\033[8D"
"%3d │%3d\033[2B\033[8D"
"%3d │%3d\033[2B"
,BOARD_WIDTH_CHARS + SCOREBOARD_WIDTH - 8
,( 3 * board.columns + 6 ) + SCOREBOARD_WIDTH - 8
,wins.count0.vertical, wins.count1.vertical
,wins.count0.horizontal, wins.count1.horizontal
,wins.count0.diagonalUp, wins.count1.diagonalUp
@ -138,10 +138,10 @@ int askColumn(
}
}
column -= FIRST_NUMBER;
if( column >= 0 && column < BOARD_WIDTH )
if( column >= 0 && column < board.columns )
switch( move ){
case PUT:
if( board.height [ column ] < BOARD_HEIGHT )
if( board.height [ column ] < board.rows )
return column;
else printf( "\033[2Apls enter a column that ain't full" );
break;