Push version 0.3.0
This commit is contained in:
parent
f660377768
commit
bb344cb956
7 changed files with 311 additions and 108 deletions
85
connect4.c
85
connect4.c
|
|
@ -28,7 +28,7 @@
|
||||||
#include "types.h"
|
#include "types.h"
|
||||||
#include "ui.h"
|
#include "ui.h"
|
||||||
|
|
||||||
#define VERSION 0.2.0
|
#define VERSION 0.3.0
|
||||||
|
|
||||||
#ifndef GITHASH
|
#ifndef GITHASH
|
||||||
#define FULLVERSION VERSION
|
#define FULLVERSION VERSION
|
||||||
|
|
@ -49,10 +49,14 @@ int main( int argc, char *argv[] ){
|
||||||
board_t playboard = {
|
board_t playboard = {
|
||||||
.player = 0,
|
.player = 0,
|
||||||
.rows = BOARD_HEIGHT,
|
.rows = BOARD_HEIGHT,
|
||||||
.columns = BOARD_WIDTH
|
.columns = BOARD_WIDTH,
|
||||||
|
.count0 = {0},
|
||||||
|
.count1 = {0},
|
||||||
};
|
};
|
||||||
FILE *outputfile = NULL;
|
FILE *outputfile = NULL;
|
||||||
char *filename = NULL;
|
char *filename = NULL;
|
||||||
|
wins_t *wins = malloc( sizeof(wins_t) );
|
||||||
|
bool randomMoves = 0;
|
||||||
// Parse options
|
// Parse options
|
||||||
for(;;){
|
for(;;){
|
||||||
// Allowed options
|
// Allowed options
|
||||||
|
|
@ -63,12 +67,14 @@ int main( int argc, char *argv[] ){
|
||||||
{ "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' },
|
{ "output", required_argument, NULL, 'o' },
|
||||||
|
{ "slow-calcWins", no_argument, NULL, '\0' },
|
||||||
|
{ "random-moves", no_argument, NULL, '\0' },
|
||||||
{ 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:o:",
|
char c = (char)getopt_long( argc, argv, "hlc:r:o:",
|
||||||
long_options, &option_index
|
long_options, &option_index
|
||||||
);
|
);
|
||||||
if( c == -1 ) break;
|
if( c == -1 ) break;
|
||||||
|
|
@ -79,6 +85,13 @@ int main( int argc, char *argv[] ){
|
||||||
case 2: // --version
|
case 2: // --version
|
||||||
printf( VERSIONSTRING );
|
printf( VERSIONSTRING );
|
||||||
return 0;
|
return 0;
|
||||||
|
case 6: // --slow-calcWins
|
||||||
|
free( wins );
|
||||||
|
wins = NULL;
|
||||||
|
break;
|
||||||
|
case 7: // --random-moves
|
||||||
|
randomMoves = 1;
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
fprintf(
|
fprintf(
|
||||||
stderr,
|
stderr,
|
||||||
|
|
@ -100,6 +113,9 @@ int main( int argc, char *argv[] ){
|
||||||
" -r n --rows n Set the amount of rows\n"
|
" -r n --rows n Set the amount of rows\n"
|
||||||
"\n"
|
"\n"
|
||||||
" -o file --output file Ouput moves taken in game to file\n"
|
" -o file --output file Ouput moves taken in game to file\n"
|
||||||
|
"\n"
|
||||||
|
" --slow-calcwins Use the 32bit reference implementation of calcWins()\n"
|
||||||
|
" --random-moves Play random moves instead of asking for input\n"
|
||||||
);
|
);
|
||||||
return 0;
|
return 0;
|
||||||
case 'l': // --license
|
case 'l': // --license
|
||||||
|
|
@ -150,36 +166,29 @@ int main( int argc, char *argv[] ){
|
||||||
"under certain condtions. See `%s --license`\n", argv[0]
|
"under certain condtions. See `%s --license`\n", argv[0]
|
||||||
);
|
);
|
||||||
// board, innit?
|
// board, innit?
|
||||||
wins_t wins = {
|
|
||||||
.count0 = {
|
|
||||||
.total = 0,
|
|
||||||
.vertical = 0,
|
|
||||||
.horizontal = 0,
|
|
||||||
.diagonalUp = 0,
|
|
||||||
.diagonalDown = 0,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
wins.count1 = wins.count0;
|
|
||||||
// Allocate the board
|
// Allocate the board
|
||||||
playboard.height = calloc( playboard.columns, sizeof(int) );
|
playboard.height = calloc( playboard.columns, sizeof(rowsint_t) );
|
||||||
playboard.column = calloc( playboard.columns, sizeof(column_t) );
|
playboard.column = calloc( playboard.columns, sizeof(column_t) );
|
||||||
wins.win0 = calloc( playboard.columns, sizeof(column_t) );
|
// Initialize wins struct if used
|
||||||
wins.win1 = calloc( playboard.columns, sizeof(column_t) );
|
if( wins != NULL ){
|
||||||
wins.same.vertical2 = calloc( playboard.columns, sizeof(column_t) );
|
wins->win0 = calloc( playboard.columns, sizeof(column_t) );
|
||||||
wins.same.horizontal2 = calloc( playboard.columns, sizeof(column_t) );
|
wins->win1 = calloc( playboard.columns, sizeof(column_t) );
|
||||||
wins.same.diagonalUp2 = calloc( playboard.columns, sizeof(column_t) );
|
wins->same.vertical2 = calloc( playboard.columns, sizeof(column_t) );
|
||||||
wins.same.diagonalDown2 = calloc( playboard.columns, sizeof(column_t) );
|
wins->same.horizontal2 = calloc( playboard.columns, sizeof(column_t) );
|
||||||
wins.same.vertical4 = calloc( playboard.columns, sizeof(column_t) );
|
wins->same.diagonalUp2 = calloc( playboard.columns, sizeof(column_t) );
|
||||||
wins.same.horizontal4 = calloc( playboard.columns, sizeof(column_t) );
|
wins->same.diagonalDown2 = calloc( playboard.columns, sizeof(column_t) );
|
||||||
wins.same.diagonalUp4 = calloc( playboard.columns, sizeof(column_t) );
|
wins->same.vertical4 = calloc( playboard.columns, sizeof(column_t) );
|
||||||
wins.same.diagonalDown4 = calloc( playboard.columns, sizeof(column_t) );
|
wins->same.horizontal4 = calloc( playboard.columns, sizeof(column_t) );
|
||||||
|
wins->same.diagonalUp4 = 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 ){
|
if( outputfile != NULL ){
|
||||||
fprintf(
|
fprintf(
|
||||||
outputfile,
|
outputfile,
|
||||||
"# File generated by " VERSIONSTRING
|
"# File generated by " VERSIONSTRING
|
||||||
"# First define the config\n"
|
"# Here's the board size\n"
|
||||||
"--rows %d\n"
|
"--rows %d\n"
|
||||||
"--columns %d\n"
|
"--columns %d\n"
|
||||||
"# Now follows the moves taken, zero indexed\n"
|
"# Now follows the moves taken, zero indexed\n"
|
||||||
|
|
@ -189,16 +198,36 @@ int main( int argc, char *argv[] ){
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
for(;; playboard.player = !playboard.player ){
|
for(;; playboard.player = !playboard.player ){
|
||||||
columnsint_t column = askColumn( playboard );
|
columnsint_t column;
|
||||||
|
if( randomMoves ){
|
||||||
|
column = randomColumn( playboard );
|
||||||
|
}else{
|
||||||
|
column = askColumn( playboard );
|
||||||
|
};
|
||||||
if( column == QUITCOLUMN ) break;
|
if( column == QUITCOLUMN ) break;
|
||||||
playMove( &playboard, column );
|
playMove( &playboard, column );
|
||||||
calcWins( &wins, playboard, column );
|
calcWins( wins, &playboard, column );
|
||||||
updateBoard( wins, playboard, column );
|
updateBoard( playboard, column );
|
||||||
if( outputfile != NULL ){
|
if( outputfile != NULL ){
|
||||||
fprintf( outputfile, "%d\n", column );
|
fprintf( outputfile, "%d\n", column );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if( outputfile != NULL ){
|
if( outputfile != NULL ){
|
||||||
|
fprintf( outputfile,
|
||||||
|
"# Wins\n"
|
||||||
|
"{\n"
|
||||||
|
"total,%d,%d\n"
|
||||||
|
"horizontal,%d,%d\n"
|
||||||
|
"vertical,%d,%d\n"
|
||||||
|
"diagonalUp,%d,%d\n"
|
||||||
|
"diagonalDown,%d,%d\n"
|
||||||
|
"}\n",
|
||||||
|
playboard.count0.total, playboard.count1.total,
|
||||||
|
playboard.count0.horizontal, playboard.count1.horizontal,
|
||||||
|
playboard.count0.vertical, playboard.count1.vertical,
|
||||||
|
playboard.count0.diagonalUp, playboard.count1.diagonalUp,
|
||||||
|
playboard.count0.diagonalDown, playboard.count1.diagonalDown
|
||||||
|
);
|
||||||
fclose( outputfile );
|
fclose( outputfile );
|
||||||
printf( "Output is written to %s\n", filename );
|
printf( "Output is written to %s\n", filename );
|
||||||
}
|
}
|
||||||
|
|
|
||||||
213
logic.c
213
logic.c
|
|
@ -16,11 +16,13 @@
|
||||||
* You should have received a copy of the GNU General Public License
|
* You should have received a copy of the GNU General Public License
|
||||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdbit.h>
|
||||||
#include "logic.h"
|
#include "logic.h"
|
||||||
#include "macros.h"
|
#include "macros.h"
|
||||||
|
|
||||||
static column_t heightMask( const rowsint_t a ){
|
static column_t heightMask( const rowsint_t a ){
|
||||||
return ( 1 << a ) - 1;
|
return ( 1 << (column_t)a ) - (column_t)1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static column_t safeHeightMask( const rowsint_t a ){
|
static column_t safeHeightMask( const rowsint_t a ){
|
||||||
|
|
@ -31,6 +33,51 @@ static column_t bottomHeightMask( const rowsint_t a, const rowsint_t b ){
|
||||||
return safeHeightMask( bottom( a, b ) );
|
return safeHeightMask( bottom( a, b ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void updateTotal( wincount_t *count ){
|
||||||
|
count->total =
|
||||||
|
count->horizontal
|
||||||
|
+ count->vertical
|
||||||
|
+ count->diagonalUp
|
||||||
|
+ count->diagonalDown;
|
||||||
|
}
|
||||||
|
|
||||||
|
// popcount for a column
|
||||||
|
static winint_t popcnt_column( column_t column ){
|
||||||
|
winint_t i = (winint_t)stdc_count_ones( column );
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reset a wincount
|
||||||
|
static void resetcount( wincount_t *count ){
|
||||||
|
count->total = 0;
|
||||||
|
count->horizontal = 0;
|
||||||
|
count->vertical = 0;
|
||||||
|
count->diagonalUp = 0;
|
||||||
|
count->diagonalDown = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Returns 0 if counts are the same
|
||||||
|
winint_t countcmp( const board_t a, const board_t b ){
|
||||||
|
winint_t i = 0;
|
||||||
|
if( a.count0.total != b.count0.total ) i+=1;
|
||||||
|
if( a.count1.total != b.count1.total ) i+=2;
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
|
||||||
|
// return a random valid column
|
||||||
|
columnsint_t randomColumn( const board_t board ){
|
||||||
|
columnsint_t i = 0;
|
||||||
|
do{
|
||||||
|
if( i == board.columns ) return QUITCOLUMN;
|
||||||
|
}while( board.height[ i++ ] == board.rows );
|
||||||
|
columnsint_t column;
|
||||||
|
do{
|
||||||
|
column = (columnsint_t)rand() % board.columns;
|
||||||
|
}while( board.height[ column ] == board.rows );
|
||||||
|
return column;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Play a move
|
||||||
void playMove(
|
void playMove(
|
||||||
board_t *boardptr,
|
board_t *boardptr,
|
||||||
const columnsint_t column
|
const columnsint_t column
|
||||||
|
|
@ -40,17 +87,107 @@ void playMove(
|
||||||
boardptr->height [ column ]++;
|
boardptr->height [ column ]++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Reference implementation of calcWins()
|
||||||
|
// Not intended for use in the main program, only for testing purposes
|
||||||
|
// Has silly behaviour for unknown reasons
|
||||||
|
void calcWins_slow( board_t *boardptr ){
|
||||||
|
resetcount( &boardptr->count0 );
|
||||||
|
resetcount( &boardptr->count1 );
|
||||||
|
for( columnsint_t column = 0; column < boardptr->columns; column++ ){
|
||||||
|
boardptr->count0.vertical += popcnt_column(
|
||||||
|
~(boardptr->column[ column ]
|
||||||
|
| boardptr->column[ column ] >> 1
|
||||||
|
| boardptr->column[ column ] >> 2
|
||||||
|
| boardptr->column[ column ] >> 3
|
||||||
|
)&safeHeightMask( boardptr->height[ column ] - 3 )
|
||||||
|
);
|
||||||
|
boardptr->count1.vertical += popcnt_column(
|
||||||
|
boardptr->column[ column ]
|
||||||
|
& boardptr->column[ column ] >> 1
|
||||||
|
& boardptr->column[ column ] >> 2
|
||||||
|
& boardptr->column[ column ] >> 3
|
||||||
|
);
|
||||||
|
};
|
||||||
|
for( columnsint_t column = 0; column < boardptr->columns - 3; column++ ){
|
||||||
|
boardptr->count0.horizontal += popcnt_column(
|
||||||
|
~(boardptr->column[ column ]
|
||||||
|
| boardptr->column[ column + 1 ]
|
||||||
|
| boardptr->column[ column + 2 ]
|
||||||
|
| boardptr->column[ column + 3 ]
|
||||||
|
)
|
||||||
|
& safeHeightMask( boardptr->height[ column ] )
|
||||||
|
& safeHeightMask( boardptr->height[ column + 1 ] )
|
||||||
|
& safeHeightMask( boardptr->height[ column + 2 ] )
|
||||||
|
& safeHeightMask( boardptr->height[ column + 3 ] )
|
||||||
|
);
|
||||||
|
boardptr->count1.horizontal += popcnt_column(
|
||||||
|
boardptr->column[ column ]
|
||||||
|
& boardptr->column[ column + 1 ]
|
||||||
|
& boardptr->column[ column + 2 ]
|
||||||
|
& boardptr->column[ column + 3 ]
|
||||||
|
);
|
||||||
|
boardptr->count0.diagonalUp += popcnt_column(
|
||||||
|
~(boardptr->column[ column ]
|
||||||
|
| boardptr->column[ column + 1 ] >> 1
|
||||||
|
| boardptr->column[ column + 2 ] >> 2
|
||||||
|
| boardptr->column[ column + 3 ] >> 3
|
||||||
|
)
|
||||||
|
& safeHeightMask( boardptr->height[ column ] )
|
||||||
|
& safeHeightMask( boardptr->height[ column + 1 ] - 1 )
|
||||||
|
& safeHeightMask( boardptr->height[ column + 2 ] - 2 )
|
||||||
|
& safeHeightMask( boardptr->height[ column + 3 ] - 3 )
|
||||||
|
);
|
||||||
|
boardptr->count1.diagonalUp += popcnt_column(
|
||||||
|
boardptr->column[ column ]
|
||||||
|
& boardptr->column[ column + 1 ] >> 1
|
||||||
|
& boardptr->column[ column + 2 ] >> 2
|
||||||
|
& boardptr->column[ column + 3 ] >> 3
|
||||||
|
);
|
||||||
|
boardptr->count0.diagonalDown += popcnt_column(
|
||||||
|
~(boardptr->column[ column ]
|
||||||
|
| boardptr->column[ column + 1 ] << 1
|
||||||
|
| boardptr->column[ column + 2 ] << 2
|
||||||
|
| boardptr->column[ column + 3 ] << 3
|
||||||
|
| 0x07 // A line diagonal down don't start at one of the bottom 3
|
||||||
|
)
|
||||||
|
& bottomHeightMask(
|
||||||
|
bottom( boardptr->height[ column ],
|
||||||
|
boardptr->height[ column + 1 ] + 1
|
||||||
|
),
|
||||||
|
bottom( boardptr->height[ column + 2 ] + 2,
|
||||||
|
boardptr->height[ column + 3 ] + 3
|
||||||
|
)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
boardptr->count1.diagonalDown += popcnt_column(
|
||||||
|
boardptr->column[ column ]
|
||||||
|
& boardptr->column[ column + 1 ] << 1
|
||||||
|
& boardptr->column[ column + 2 ] << 2
|
||||||
|
& boardptr->column[ column + 3 ] << 3
|
||||||
|
);
|
||||||
|
};
|
||||||
|
updateTotal( &boardptr->count0 );
|
||||||
|
updateTotal( &boardptr->count1 );
|
||||||
|
}
|
||||||
|
|
||||||
|
// Less slow implementation of calcWins();
|
||||||
|
// Only works if called after avery move
|
||||||
void calcWins(
|
void calcWins(
|
||||||
wins_t *wins,
|
wins_t *wins,
|
||||||
const board_t board,
|
board_t *boardptr,
|
||||||
const columnsint_t column
|
const columnsint_t column
|
||||||
){
|
){
|
||||||
|
// If no remembering between runs, run the reference implementation
|
||||||
|
if( wins == NULL ) {
|
||||||
|
calcWins_slow( boardptr );
|
||||||
|
return;
|
||||||
|
}
|
||||||
// First the simplest win, the humble tower
|
// First the simplest win, the humble tower
|
||||||
// Check for lil towers
|
// Check for lil towers
|
||||||
wins->same.vertical2 [ column ] =
|
wins->same.vertical2 [ column ] =
|
||||||
~( board.column [ column ]
|
~( boardptr->column [ column ]
|
||||||
^ board.column [ column ] >> 1 )
|
^ boardptr->column [ column ] >> 1 )
|
||||||
& safeHeightMask( board.height [ column ] - 1 );
|
& safeHeightMask( boardptr->height [ column ] - 1 );
|
||||||
// Actually check for wins
|
// Actually check for wins
|
||||||
column_t newcolumn =
|
column_t newcolumn =
|
||||||
wins->same.vertical2 [ column ]
|
wins->same.vertical2 [ column ]
|
||||||
|
|
@ -61,11 +198,11 @@ void calcWins(
|
||||||
if(
|
if(
|
||||||
( newcolumn
|
( newcolumn
|
||||||
^ wins->same.vertical4 [ column ]
|
^ wins->same.vertical4 [ column ]
|
||||||
) & board.column [ column ]
|
) & boardptr->column [ column ]
|
||||||
){
|
){
|
||||||
wins->count1.vertical ++;
|
boardptr->count1.vertical ++;
|
||||||
} else {
|
} else {
|
||||||
wins->count0.vertical ++;
|
boardptr->count0.vertical ++;
|
||||||
}
|
}
|
||||||
wins->same.vertical4 [ column ] = newcolumn;
|
wins->same.vertical4 [ column ] = newcolumn;
|
||||||
}
|
}
|
||||||
|
|
@ -73,36 +210,36 @@ void calcWins(
|
||||||
// First connect 2
|
// First connect 2
|
||||||
for(
|
for(
|
||||||
columnsint_t i = clipped_subtract( column, 1 );
|
columnsint_t i = clipped_subtract( column, 1 );
|
||||||
i < bottom( column + 1, board.columns - 1 );
|
i < bottom( column + 1, boardptr->columns - 1 );
|
||||||
i++
|
i++
|
||||||
){
|
){
|
||||||
wins->same.horizontal2 [ i ] =
|
wins->same.horizontal2 [ i ] =
|
||||||
~( board.column [ i ]
|
~( boardptr->column [ i ]
|
||||||
^ board.column [ i + 1 ] )
|
^ boardptr->column [ i + 1 ] )
|
||||||
& bottomHeightMask(
|
& bottomHeightMask(
|
||||||
board.height [ i ],
|
boardptr->height [ i ],
|
||||||
board.height [ i + 1 ]
|
boardptr->height [ i + 1 ]
|
||||||
);
|
);
|
||||||
wins->same.diagonalUp2 [ i ] =
|
wins->same.diagonalUp2 [ i ] =
|
||||||
~( board.column [ i ]
|
~( boardptr->column [ i ]
|
||||||
^ board.column [ i + 1 ] >> 1 )
|
^ boardptr->column [ i + 1 ] >> 1 )
|
||||||
& bottomHeightMask(
|
& bottomHeightMask(
|
||||||
board.height [ i ],
|
boardptr->height [ i ],
|
||||||
board.height [ i + 1 ] - 1
|
boardptr->height [ i + 1 ] - 1
|
||||||
);
|
);
|
||||||
wins->same.diagonalDown2 [ i ] =
|
wins->same.diagonalDown2 [ i ] =
|
||||||
~( board.column [ i ]
|
~( boardptr->column [ i ]
|
||||||
^ board.column [ i + 1 ] << 1 )
|
^ boardptr->column [ i + 1 ] << 1 )
|
||||||
& bottomHeightMask(
|
& bottomHeightMask(
|
||||||
board.height [ i ],
|
boardptr->height [ i ],
|
||||||
board.height [ i + 1 ] + 1
|
boardptr->height [ i + 1 ] + 1
|
||||||
)
|
)
|
||||||
& ~1; // A diagonal line down ain't starts at the floor innit?
|
& ~(column_t)1; // A diagonal line down ain't starts at the floor innit?
|
||||||
}
|
}
|
||||||
// Then stitch the twos together and count
|
// Then stitch the twos together and count
|
||||||
for(
|
for(
|
||||||
columnsint_t i = clipped_subtract( column, 3 );
|
columnsint_t i = clipped_subtract( column, 3 );
|
||||||
i < bottom( column + 1, board.columns - 3 );
|
i < bottom( column + 1, boardptr->columns - 3 );
|
||||||
i++
|
i++
|
||||||
){
|
){
|
||||||
newcolumn =
|
newcolumn =
|
||||||
|
|
@ -113,11 +250,11 @@ void calcWins(
|
||||||
if(
|
if(
|
||||||
( newcolumn
|
( newcolumn
|
||||||
^ wins->same.horizontal4 [ i ]
|
^ wins->same.horizontal4 [ i ]
|
||||||
) & board.column [ i ]
|
) & boardptr->column [ i ]
|
||||||
){
|
){
|
||||||
wins->count1.horizontal++;
|
boardptr->count1.horizontal++;
|
||||||
} else {
|
} else {
|
||||||
wins->count0.horizontal++;
|
boardptr->count0.horizontal++;
|
||||||
}
|
}
|
||||||
wins->same.horizontal4 [ i ] = newcolumn;
|
wins->same.horizontal4 [ i ] = newcolumn;
|
||||||
}
|
}
|
||||||
|
|
@ -129,11 +266,11 @@ void calcWins(
|
||||||
if(
|
if(
|
||||||
( newcolumn
|
( newcolumn
|
||||||
^ wins->same.diagonalUp4 [ i ]
|
^ wins->same.diagonalUp4 [ i ]
|
||||||
) & board.column [ i ]
|
) & boardptr->column [ i ]
|
||||||
){
|
){
|
||||||
wins->count1.diagonalUp++;
|
boardptr->count1.diagonalUp++;
|
||||||
} else {
|
} else {
|
||||||
wins->count0.diagonalUp++;
|
boardptr->count0.diagonalUp++;
|
||||||
}
|
}
|
||||||
wins->same.diagonalUp4 [ i ] = newcolumn;
|
wins->same.diagonalUp4 [ i ] = newcolumn;
|
||||||
}
|
}
|
||||||
|
|
@ -145,23 +282,15 @@ void calcWins(
|
||||||
if(
|
if(
|
||||||
( newcolumn
|
( newcolumn
|
||||||
^ wins->same.diagonalDown4 [ i ]
|
^ wins->same.diagonalDown4 [ i ]
|
||||||
) & board.column [ i ]
|
) & boardptr->column [ i ]
|
||||||
){
|
){
|
||||||
wins->count1.diagonalDown++;
|
boardptr->count1.diagonalDown++;
|
||||||
} else {
|
} else {
|
||||||
wins->count0.diagonalDown++;
|
boardptr->count0.diagonalDown++;
|
||||||
}
|
}
|
||||||
wins->same.diagonalDown4 [ i ] = newcolumn;
|
wins->same.diagonalDown4 [ i ] = newcolumn;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
wins->count0.total =
|
updateTotal( &boardptr->count0 );
|
||||||
wins->count0.vertical
|
updateTotal( &boardptr->count1 );
|
||||||
+ wins->count0.horizontal
|
|
||||||
+ wins->count0.diagonalUp
|
|
||||||
+ wins->count0.diagonalDown;
|
|
||||||
wins->count1.total =
|
|
||||||
wins->count1.vertical
|
|
||||||
+ wins->count1.horizontal
|
|
||||||
+ wins->count1.diagonalUp
|
|
||||||
+ wins->count1.diagonalDown;
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
40
logic.h
40
logic.h
|
|
@ -1,16 +1,54 @@
|
||||||
/* SPDX-License-Identifier: GPL-2.0-only */
|
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <stddef.h>
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
#include "types.h"
|
#include "types.h"
|
||||||
|
|
||||||
|
// Types for using a bit more ram to avoid duplicated computations
|
||||||
|
typedef struct {
|
||||||
|
column_t *vertical2;
|
||||||
|
column_t *horizontal2;
|
||||||
|
column_t *diagonalUp2;
|
||||||
|
column_t *diagonalDown2;
|
||||||
|
column_t *vertical4;
|
||||||
|
column_t *horizontal4;
|
||||||
|
column_t *diagonalUp4;
|
||||||
|
column_t *diagonalDown4;
|
||||||
|
} directions_t;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
column_t *win0;
|
||||||
|
column_t *win1;
|
||||||
|
directions_t same;
|
||||||
|
} wins_t;
|
||||||
|
|
||||||
|
extern winint_t countcmp(
|
||||||
|
const board_t a,
|
||||||
|
const board_t b
|
||||||
|
);
|
||||||
|
|
||||||
|
// Get a random column in the board
|
||||||
|
extern columnsint_t randomColumn(
|
||||||
|
const board_t board
|
||||||
|
);
|
||||||
|
|
||||||
|
// Put a piece in the board
|
||||||
extern void playMove(
|
extern void playMove(
|
||||||
board_t *boardptr,
|
board_t *boardptr,
|
||||||
const columnsint_t column
|
const columnsint_t column
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// Calculate wins
|
||||||
extern void calcWins(
|
extern void calcWins(
|
||||||
|
// Null pointer on systems where the extra ram is not available
|
||||||
wins_t *wins,
|
wins_t *wins,
|
||||||
const board_t board,
|
board_t *boardptr,
|
||||||
const columnsint_t column
|
const columnsint_t column
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// Reference implementation for calculating wins,
|
||||||
|
// slow AF especially on big boards
|
||||||
|
extern void calcWins_slow(
|
||||||
|
board_t *boardptr
|
||||||
|
);
|
||||||
|
|
|
||||||
29
types.h
29
types.h
|
|
@ -23,25 +23,6 @@ typedef uint_fast8_t column_t;
|
||||||
#define WININT_FORMAT "%3d"
|
#define WININT_FORMAT "%3d"
|
||||||
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
bool player;
|
|
||||||
rowsint_t *height;
|
|
||||||
column_t *column;
|
|
||||||
rowsint_t rows;
|
|
||||||
columnsint_t columns;
|
|
||||||
} board_t;
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
column_t *vertical2;
|
|
||||||
column_t *horizontal2;
|
|
||||||
column_t *diagonalUp2;
|
|
||||||
column_t *diagonalDown2;
|
|
||||||
column_t *vertical4;
|
|
||||||
column_t *horizontal4;
|
|
||||||
column_t *diagonalUp4;
|
|
||||||
column_t *diagonalDown4;
|
|
||||||
} directions_t;
|
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
winint_t total;
|
winint_t total;
|
||||||
winint_t horizontal;
|
winint_t horizontal;
|
||||||
|
|
@ -51,9 +32,11 @@ typedef struct {
|
||||||
} wincount_t;
|
} wincount_t;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
|
bool player;
|
||||||
|
rowsint_t *height;
|
||||||
|
column_t *column;
|
||||||
|
rowsint_t rows;
|
||||||
|
columnsint_t columns;
|
||||||
wincount_t count0;
|
wincount_t count0;
|
||||||
wincount_t count1;
|
wincount_t count1;
|
||||||
column_t *win0;
|
} board_t;
|
||||||
column_t *win1;
|
|
||||||
directions_t same;
|
|
||||||
} wins_t;
|
|
||||||
|
|
|
||||||
1
ui.h
1
ui.h
|
|
@ -9,7 +9,6 @@ extern void initBoard(
|
||||||
);
|
);
|
||||||
|
|
||||||
extern void updateBoard(
|
extern void updateBoard(
|
||||||
const wins_t wins,
|
|
||||||
const board_t board,
|
const board_t board,
|
||||||
const columnsint_t column
|
const columnsint_t column
|
||||||
);
|
);
|
||||||
|
|
|
||||||
21
ui_ncurses.c
21
ui_ncurses.c
|
|
@ -137,7 +137,6 @@ void initBoard( const board_t board ){
|
||||||
}
|
}
|
||||||
|
|
||||||
void updateBoard(
|
void updateBoard(
|
||||||
const wins_t wins,
|
|
||||||
const board_t board,
|
const board_t board,
|
||||||
const columnsint_t column
|
const columnsint_t column
|
||||||
){
|
){
|
||||||
|
|
@ -148,61 +147,61 @@ void updateBoard(
|
||||||
board.column[ column ] & 1 << ( height - 1 ) ? "1" : "0"
|
board.column[ column ] & 1 << ( height - 1 ) ? "1" : "0"
|
||||||
);
|
);
|
||||||
char num[4];
|
char num[4];
|
||||||
snprintf( num, sizeof(num), WININT_FORMAT, wins.count0.vertical );
|
snprintf( num, sizeof(num), WININT_FORMAT, board.count0.vertical );
|
||||||
mvaddstr(
|
mvaddstr(
|
||||||
SCOREBOARD_Y + 3,
|
SCOREBOARD_Y + 3,
|
||||||
SCOREBOARD_X + 17,
|
SCOREBOARD_X + 17,
|
||||||
num
|
num
|
||||||
);
|
);
|
||||||
snprintf( num, sizeof(num), WININT_FORMAT, wins.count1.vertical );
|
snprintf( num, sizeof(num), WININT_FORMAT, board.count1.vertical );
|
||||||
mvaddstr(
|
mvaddstr(
|
||||||
SCOREBOARD_Y + 3,
|
SCOREBOARD_Y + 3,
|
||||||
SCOREBOARD_X + 22,
|
SCOREBOARD_X + 22,
|
||||||
num
|
num
|
||||||
);
|
);
|
||||||
snprintf( num, sizeof(num), WININT_FORMAT, wins.count0.horizontal );
|
snprintf( num, sizeof(num), WININT_FORMAT, board.count0.horizontal );
|
||||||
mvaddstr(
|
mvaddstr(
|
||||||
SCOREBOARD_Y + 4,
|
SCOREBOARD_Y + 4,
|
||||||
SCOREBOARD_X + 17,
|
SCOREBOARD_X + 17,
|
||||||
num
|
num
|
||||||
);
|
);
|
||||||
snprintf( num, sizeof(num), WININT_FORMAT, wins.count1.horizontal );
|
snprintf( num, sizeof(num), WININT_FORMAT, board.count1.horizontal );
|
||||||
mvaddstr(
|
mvaddstr(
|
||||||
SCOREBOARD_Y + 4,
|
SCOREBOARD_Y + 4,
|
||||||
SCOREBOARD_X + 22,
|
SCOREBOARD_X + 22,
|
||||||
num
|
num
|
||||||
);
|
);
|
||||||
snprintf( num, sizeof(num), WININT_FORMAT, wins.count0.diagonalUp );
|
snprintf( num, sizeof(num), WININT_FORMAT, board.count0.diagonalUp );
|
||||||
mvaddstr(
|
mvaddstr(
|
||||||
SCOREBOARD_Y + 5,
|
SCOREBOARD_Y + 5,
|
||||||
SCOREBOARD_X + 17,
|
SCOREBOARD_X + 17,
|
||||||
num
|
num
|
||||||
);
|
);
|
||||||
snprintf( num, sizeof(num), WININT_FORMAT, wins.count1.diagonalUp );
|
snprintf( num, sizeof(num), WININT_FORMAT, board.count1.diagonalUp );
|
||||||
mvaddstr(
|
mvaddstr(
|
||||||
SCOREBOARD_Y + 5,
|
SCOREBOARD_Y + 5,
|
||||||
SCOREBOARD_X + 22,
|
SCOREBOARD_X + 22,
|
||||||
num
|
num
|
||||||
);
|
);
|
||||||
snprintf( num, sizeof(num), WININT_FORMAT, wins.count0.diagonalDown );
|
snprintf( num, sizeof(num), WININT_FORMAT, board.count0.diagonalDown );
|
||||||
mvaddstr(
|
mvaddstr(
|
||||||
SCOREBOARD_Y + 6,
|
SCOREBOARD_Y + 6,
|
||||||
SCOREBOARD_X + 17,
|
SCOREBOARD_X + 17,
|
||||||
num
|
num
|
||||||
);
|
);
|
||||||
snprintf( num, sizeof(num), WININT_FORMAT, wins.count1.diagonalDown );
|
snprintf( num, sizeof(num), WININT_FORMAT, board.count1.diagonalDown );
|
||||||
mvaddstr(
|
mvaddstr(
|
||||||
SCOREBOARD_Y + 6,
|
SCOREBOARD_Y + 6,
|
||||||
SCOREBOARD_X + 22,
|
SCOREBOARD_X + 22,
|
||||||
num
|
num
|
||||||
);
|
);
|
||||||
snprintf( num, sizeof(num), WININT_FORMAT, wins.count0.total );
|
snprintf( num, sizeof(num), WININT_FORMAT, board.count0.total );
|
||||||
mvaddstr(
|
mvaddstr(
|
||||||
SCOREBOARD_Y + 8,
|
SCOREBOARD_Y + 8,
|
||||||
SCOREBOARD_X + 17,
|
SCOREBOARD_X + 17,
|
||||||
num
|
num
|
||||||
);
|
);
|
||||||
snprintf( num, sizeof(num), WININT_FORMAT, wins.count1.total );
|
snprintf( num, sizeof(num), WININT_FORMAT, board.count1.total );
|
||||||
mvaddstr(
|
mvaddstr(
|
||||||
SCOREBOARD_Y + 8,
|
SCOREBOARD_Y + 8,
|
||||||
SCOREBOARD_X + 22,
|
SCOREBOARD_X + 22,
|
||||||
|
|
|
||||||
26
ui_stub.c
Normal file
26
ui_stub.c
Normal file
|
|
@ -0,0 +1,26 @@
|
||||||
|
#include "ui.h"
|
||||||
|
#include "logic.h"
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
void initBoard(
|
||||||
|
__attribute__((unused)) const board_t board
|
||||||
|
){
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
void updateBoard(
|
||||||
|
__attribute__((unused)) const board_t board,
|
||||||
|
__attribute__((unused)) const columnsint_t column
|
||||||
|
){
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
columnsint_t askColumn(
|
||||||
|
const board_t board
|
||||||
|
){
|
||||||
|
return randomColumn( board );
|
||||||
|
}
|
||||||
|
|
||||||
|
void exit_ui(void){
|
||||||
|
return;
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue