Added support for quitting without ^C, and outputting to file
This commit is contained in:
parent
6d879da5fa
commit
d0c257b310
5 changed files with 68 additions and 23 deletions
44
connect4.c
44
connect4.c
|
|
@ -21,13 +21,14 @@
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
#include <getopt.h>
|
#include <getopt.h>
|
||||||
|
#include <string.h>
|
||||||
#include "macros.h"
|
#include "macros.h"
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
#include "logic.h"
|
#include "logic.h"
|
||||||
#include "types.h"
|
#include "types.h"
|
||||||
#include "ui.h"
|
#include "ui.h"
|
||||||
|
|
||||||
#define VERSION 0.1.2
|
#define VERSION 0.2.0
|
||||||
|
|
||||||
#ifndef GITHASH
|
#ifndef GITHASH
|
||||||
#define FULLVERSION VERSION
|
#define FULLVERSION VERSION
|
||||||
|
|
@ -50,6 +51,8 @@ int main( int argc, char *argv[] ){
|
||||||
.rows = BOARD_HEIGHT,
|
.rows = BOARD_HEIGHT,
|
||||||
.columns = BOARD_WIDTH
|
.columns = BOARD_WIDTH
|
||||||
};
|
};
|
||||||
|
FILE *outputfile = NULL;
|
||||||
|
char *filename = NULL;
|
||||||
// Parse options
|
// Parse options
|
||||||
for(;;){
|
for(;;){
|
||||||
// Allowed options
|
// Allowed options
|
||||||
|
|
@ -59,12 +62,15 @@ int main( int argc, char *argv[] ){
|
||||||
{ "version", no_argument, NULL, '\0' },
|
{ "version", no_argument, NULL, '\0' },
|
||||||
{ "columns", required_argument, NULL, 'c' },
|
{ "columns", required_argument, NULL, 'c' },
|
||||||
{ "rows", required_argument, NULL, 'r' },
|
{ "rows", required_argument, NULL, 'r' },
|
||||||
|
{ "output", required_argument, NULL, 'o' },
|
||||||
{ 0 }
|
{ 0 }
|
||||||
};
|
};
|
||||||
// Index of current option
|
// Index of current option
|
||||||
int option_index = 0;
|
int option_index = 0;
|
||||||
// Get next option
|
// Get next option
|
||||||
char c = getopt_long( argc, argv, "hlc:r:", long_options, &option_index );
|
char c = getopt_long( argc, argv, "hlc:r:o:",
|
||||||
|
long_options, &option_index
|
||||||
|
);
|
||||||
if( c == -1 ) break;
|
if( c == -1 ) break;
|
||||||
// Handle option
|
// Handle option
|
||||||
switch( c ){
|
switch( c ){
|
||||||
|
|
@ -86,12 +92,14 @@ int main( int argc, char *argv[] ){
|
||||||
case 'h': // --help
|
case 'h': // --help
|
||||||
printf( "Usage: %s [OPTIONS]\n\n", argv[0] );
|
printf( "Usage: %s [OPTIONS]\n\n", argv[0] );
|
||||||
printf(
|
printf(
|
||||||
" -h --help Print this help text\n"
|
" -h --help Print this help text\n"
|
||||||
" -l --license Print the LICENSE file\n"
|
" -l --license Print the LICENSE file\n"
|
||||||
" --version Print the version string\n"
|
" --version Print the version string\n"
|
||||||
"\n"
|
"\n"
|
||||||
" -c n --columns n Set the amount of columns\n"
|
" -c n --columns n Set the amount of columns\n"
|
||||||
" -r n --rows n Set the amount of rows\n"
|
" -r n --rows n Set the amount of rows\n"
|
||||||
|
"\n"
|
||||||
|
" -o file --output file Ouput moves taken in game to file\n"
|
||||||
);
|
);
|
||||||
return 0;
|
return 0;
|
||||||
case 'l': // --license
|
case 'l': // --license
|
||||||
|
|
@ -109,6 +117,10 @@ int main( int argc, char *argv[] ){
|
||||||
case 'r': // --rows
|
case 'r': // --rows
|
||||||
playboard.rows = atoi(optarg);
|
playboard.rows = atoi(optarg);
|
||||||
break;
|
break;
|
||||||
|
case 'o':
|
||||||
|
filename = strdup(optarg);
|
||||||
|
outputfile = fopen( filename, "w" );
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Check for unhandled options
|
// Check for unhandled options
|
||||||
|
|
@ -163,10 +175,32 @@ int main( int argc, char *argv[] ){
|
||||||
wins.same.diagonalDown4 = calloc( playboard.columns, sizeof(column_t) );
|
wins.same.diagonalDown4 = calloc( playboard.columns, sizeof(column_t) );
|
||||||
initBoard( playboard );
|
initBoard( playboard );
|
||||||
// Begin loopin
|
// Begin loopin
|
||||||
|
if( outputfile != NULL ){
|
||||||
|
fprintf(
|
||||||
|
outputfile,
|
||||||
|
"# File generated by " VERSIONSTRING
|
||||||
|
"# First define the config\n"
|
||||||
|
"--rows %d\n"
|
||||||
|
"--columns %d\n"
|
||||||
|
"# Now follows the moves taken, zero indexed\n"
|
||||||
|
,
|
||||||
|
playboard.rows,
|
||||||
|
playboard.columns
|
||||||
|
);
|
||||||
|
}
|
||||||
for(;; playboard.player = !playboard.player ){
|
for(;; playboard.player = !playboard.player ){
|
||||||
columnsint_t column = askColumn( playboard );
|
columnsint_t column = askColumn( playboard );
|
||||||
|
if( column == QUITCOLUMN ) break;
|
||||||
playMove( &playboard, column );
|
playMove( &playboard, column );
|
||||||
calcWins( &wins, playboard, column );
|
calcWins( &wins, playboard, column );
|
||||||
updateBoard( wins, playboard, column );
|
updateBoard( wins, playboard, column );
|
||||||
|
if( outputfile != NULL ){
|
||||||
|
fprintf( outputfile, "%d\n", column );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
if( outputfile != NULL ){
|
||||||
|
fclose( outputfile );
|
||||||
|
printf( "Output is written to %s\n", filename );
|
||||||
|
}
|
||||||
|
exit_ui();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
9
types.h
9
types.h
|
|
@ -2,22 +2,27 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
#include <limits.h>
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
|
|
||||||
|
// Only use big ints on architectures where it doesn't impact speed
|
||||||
#if INT_FAST16_MAX == INT_FAST64_MAX
|
#if INT_FAST16_MAX == INT_FAST64_MAX
|
||||||
typedef int rowsint_t;
|
typedef int rowsint_t;
|
||||||
typedef int columnsint_t;
|
typedef unsigned columnsint_t;
|
||||||
|
#define QUITCOLUMN UINT_MAX
|
||||||
typedef unsigned winint_t;
|
typedef unsigned winint_t;
|
||||||
typedef uintmax_t column_t;
|
typedef uintmax_t column_t;
|
||||||
#else
|
#else
|
||||||
typedef int_fast8_t rowsint_t;
|
typedef int_fast8_t rowsint_t;
|
||||||
typedef int_fast8_t columnsint_t;
|
typedef uint_fast8_t columnsint_t;
|
||||||
|
#define QUITCOLUMN UINT_FAST8_MAX
|
||||||
typedef uint_fast8_t winint_t;
|
typedef uint_fast8_t winint_t;
|
||||||
typedef uint_fast8_t column_t;
|
typedef uint_fast8_t column_t;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#define WININT_FORMAT "%3d"
|
#define WININT_FORMAT "%3d"
|
||||||
|
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
bool player;
|
bool player;
|
||||||
rowsint_t *height;
|
rowsint_t *height;
|
||||||
|
|
|
||||||
4
ui.h
4
ui.h
|
|
@ -14,6 +14,8 @@ extern void updateBoard(
|
||||||
const columnsint_t column
|
const columnsint_t column
|
||||||
);
|
);
|
||||||
|
|
||||||
extern int askColumn(
|
extern columnsint_t askColumn(
|
||||||
const board_t board
|
const board_t board
|
||||||
);
|
);
|
||||||
|
|
||||||
|
extern void exit_ui(void);
|
||||||
|
|
|
||||||
11
ui_ncurses.c
11
ui_ncurses.c
|
|
@ -207,7 +207,7 @@ void updateBoard(
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
int askColumn(
|
columnsint_t askColumn(
|
||||||
const board_t board
|
const board_t board
|
||||||
){
|
){
|
||||||
columnsint_t column = 0;
|
columnsint_t column = 0;
|
||||||
|
|
@ -228,7 +228,8 @@ int askColumn(
|
||||||
column += ( column < board.columns - 1 );
|
column += ( column < board.columns - 1 );
|
||||||
break;
|
break;
|
||||||
case KEY_LEFT:
|
case KEY_LEFT:
|
||||||
column -= ( column > 0 );
|
if( column == 0 ) return QUITCOLUMN;
|
||||||
|
column --;
|
||||||
break;
|
break;
|
||||||
case KEY_DOWN:
|
case KEY_DOWN:
|
||||||
return column;
|
return column;
|
||||||
|
|
@ -239,7 +240,7 @@ int askColumn(
|
||||||
else column++;
|
else column++;
|
||||||
break;
|
break;
|
||||||
case KEY_LEFT:
|
case KEY_LEFT:
|
||||||
if( column == 0 ) ch = KEY_RIGHT;
|
if( column == 0 ) return QUITCOLUMN;
|
||||||
else column--;
|
else column--;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
@ -272,6 +273,10 @@ int askColumn(
|
||||||
#endif /* ! ARROWS */
|
#endif /* ! ARROWS */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void exit_ui(void){
|
||||||
|
endwin();
|
||||||
|
}
|
||||||
|
|
||||||
#undef INPUT_X
|
#undef INPUT_X
|
||||||
#undef INPUT_Y
|
#undef INPUT_Y
|
||||||
#undef SCOREBOARD_X
|
#undef SCOREBOARD_X
|
||||||
|
|
|
||||||
23
ui_vt100.c
23
ui_vt100.c
|
|
@ -94,7 +94,7 @@ void updateBoard(
|
||||||
printf( "\033[2K\n" );
|
printf( "\033[2K\n" );
|
||||||
}
|
}
|
||||||
|
|
||||||
int askColumn(
|
columnsint_t askColumn(
|
||||||
const board_t board
|
const board_t board
|
||||||
){
|
){
|
||||||
columnsint_t column;
|
columnsint_t column;
|
||||||
|
|
@ -102,21 +102,16 @@ int askColumn(
|
||||||
printf( "Wher player %d put? ", board.player );
|
printf( "Wher player %d put? ", board.player );
|
||||||
printf( "\033[K" ); // clear everything after cursor
|
printf( "\033[K" ); // clear everything after cursor
|
||||||
fflush( stdout );
|
fflush( stdout );
|
||||||
column = -1;
|
column = 0;
|
||||||
for( char ch = '0'; ch != '\n'; read( STDIN_FILENO, &ch, 1 ) ){
|
for( char ch = '0'; ch != '\n'; read( STDIN_FILENO, &ch, 1 ) ){
|
||||||
if( ch >= '0' && ch <= '9' ){
|
if( ch >= '0' && ch <= '9' ){
|
||||||
if( column == -1 )
|
column *= 10;
|
||||||
column = 0;
|
column += (columnsint_t)( ch - '0' );
|
||||||
else
|
} else return QUITCOLUMN;
|
||||||
column *= 10;
|
|
||||||
if( column >= 0 )
|
|
||||||
column += (columnsint_t)( ch - '0' );
|
|
||||||
} else {
|
|
||||||
column = -2;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
if( column < FIRST_NUMBER ) return QUITCOLUMN;
|
||||||
column -= FIRST_NUMBER;
|
column -= FIRST_NUMBER;
|
||||||
if( column >= 0 && column < board.columns )
|
if( column < board.columns )
|
||||||
if( board.height [ column ] < board.rows )
|
if( board.height [ column ] < board.rows )
|
||||||
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" );
|
||||||
|
|
@ -126,6 +121,10 @@ int askColumn(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void exit_ui(void){
|
||||||
|
printf( "\n" );
|
||||||
|
}
|
||||||
|
|
||||||
#undef BOARD_WIDTH_CHARS
|
#undef BOARD_WIDTH_CHARS
|
||||||
#undef SCOREBOARD_OFFSET
|
#undef SCOREBOARD_OFFSET
|
||||||
#undef SCOREBOARD_WIDTH
|
#undef SCOREBOARD_WIDTH
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue