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