Added a define for only letting the user put new pieces

This commit is contained in:
AnnaSnoeijs 2025-06-06 21:31:59 +02:00
parent 9662cb0b14
commit 7d0a0e1803

View file

@ -26,10 +26,15 @@
#define XSTR(s) STR(s)
#define STR(s) #s
#define ONLYPUT
#ifndef ONLYPUT
typedef enum {
PUT,
POP
} move_t;
#endif /* ! ONLYPUT */
typedef struct {
int player;
@ -131,12 +136,16 @@ static void initBoard( void ){
static void updateBoard(
const wins_t wins,
const board_t board,
#ifndef ONLYPUT
const move_t move,
#endif /* ! ONLYPUT */
const int column
){
int heigth = board.heigth [ column ];
#ifndef ONLYPUT
switch( move ){
case PUT: // Only print the put one
#endif /* ! ONLYPUT */
printf(
"\033[%dA\033[%dC%c\033[%dB",
heigth + 3,
@ -144,6 +153,7 @@ static void updateBoard(
'0' + !!( board.player ),
heigth + 1
);
#ifndef ONLYPUT
break;
default: // Print all the pieces in the column and the air above
printf( "\033[%dA", heigth + 4 );
@ -156,6 +166,7 @@ static void updateBoard(
);
printf( "\033[2B" );
}
#endif /* ! ONLYPUT */
printf(
"\r\033[7A\033[%dC"
"%3d │%3d\033[B\033[8D"
@ -179,59 +190,79 @@ static void updateBoard(
static void playMove(
board_t *boardptr,
#ifndef ONLYPUT
const move_t move,
#endif /* ! ONLYPUT */
const int column
){
#ifndef ONLYPUT
switch( move ){
case PUT: // Put a new piece in the board
#endif /* ! ONLYPUT */
boardptr->column [ column ] |=
boardptr->player << boardptr->heigth [ column ];
boardptr->heigth [ column ]++;
#ifndef ONLYPUT
break;
case POP: // Pop out the lowest and make all above fall down
boardptr->column [ column ] >>= 1;
boardptr->heigth [ column ]--;
}
#endif /* ! ONLYPUT */
}
static int askColumn(
#ifndef ONLYPUT
const board_t board,
const move_t move
#else /* ONLYPUT */
const board_t board
#endif /* ONLYPUT */
){
int column;
for(;;){
#ifndef ONLYPUT
switch( move ){
case PUT: printf( "Wher player %d put? ", board.player );
break;
case POP: printf( "Wher player %d pop? ", board.player );
}
#else /* ONLYPUT */
printf( "Wher player %d put? ", board.player );
#endif /* ONLYPUT */
printf( "\033[K" ); // clear everything after cursor
scanf( "%d", &column );
column -= FIRST_NUMBER;
if( column >= 0 && column < BOARD_WIDTH )
#ifndef ONLYPUT
switch( move ){
case PUT:
#endif /* ! ONLYPUT */
if( board.heigth [ column ] < BOARD_HEIGTH )
return column;
else printf( "\033[2Apls enter a column that ain't full" );
#ifndef ONLYPUT
break;
case POP:
if( board.heigth [ column ] != 0 )
return column;
else printf( "\033[2Apls enter a column that ain't empty" );
}
#endif /* ! ONLYPUT */
else
printf( "\033[2Apls enter valid column" );
printf( "\033[K\n" );
}
}
#ifndef ONLYPUT
static move_t askMove( void ){
move_t move = PUT;
// TODO: actually ask what move
return move;
}
#endif /* ! ONLYPUT */
static void calcWins(
wins_t *wins,
const board_t board
@ -367,13 +398,24 @@ int main(){
.win0 = {0},
.win1 = {0},
};
#ifndef ONLYPUT
move_t move = PUT;
#endif /* ! ONLYPUT */
initBoard();
for( int column = 0;; playboard.player = !playboard.player ){
#ifndef ONLYPUT
move = askMove();
column = askColumn( playboard, move );
playMove( &playboard, move, column );
#else /* ONLYPUT */
column = askColumn( playboard );
playMove( &playboard, column );
#endif /* ONLYPUT */
calcWins( &wins, playboard );
#ifndef ONLYPUT
updateBoard( wins, playboard, move, column );
#else /* ONLYPUT */
updateBoard( wins, playboard, column );
#endif /* ONLYPUT */
}
}