Compare commits

..

No commits in common. "bb344cb956bf70befbcddac32aee2eb4358355c4" and "a5fcaa00d750566b1f4ff0254e9923a424e3bed2" have entirely different histories.

8 changed files with 111 additions and 320 deletions

View file

@ -28,7 +28,7 @@
#include "types.h"
#include "ui.h"
#define VERSION 0.3.0
#define VERSION 0.2.0
#ifndef GITHASH
#define FULLVERSION VERSION
@ -49,14 +49,10 @@ int main( int argc, char *argv[] ){
board_t playboard = {
.player = 0,
.rows = BOARD_HEIGHT,
.columns = BOARD_WIDTH,
.count0 = {0},
.count1 = {0},
.columns = BOARD_WIDTH
};
FILE *outputfile = NULL;
char *filename = NULL;
wins_t *wins = malloc( sizeof(wins_t) );
bool randomMoves = 0;
// Parse options
for(;;){
// Allowed options
@ -67,14 +63,12 @@ int main( int argc, char *argv[] ){
{ "columns", required_argument, NULL, 'c' },
{ "rows", required_argument, NULL, 'r' },
{ "output", required_argument, NULL, 'o' },
{ "slow-calcWins", no_argument, NULL, '\0' },
{ "random-moves", no_argument, NULL, '\0' },
{ 0 }
};
// Index of current option
int option_index = 0;
// Get next option
char c = (char)getopt_long( argc, argv, "hlc:r:o:",
char c = getopt_long( argc, argv, "hlc:r:o:",
long_options, &option_index
);
if( c == -1 ) break;
@ -85,13 +79,6 @@ int main( int argc, char *argv[] ){
case 2: // --version
printf( VERSIONSTRING );
return 0;
case 6: // --slow-calcWins
free( wins );
wins = NULL;
break;
case 7: // --random-moves
randomMoves = 1;
break;
default:
fprintf(
stderr,
@ -113,9 +100,6 @@ int main( int argc, char *argv[] ){
" -r n --rows n Set the amount of rows\n"
"\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;
case 'l': // --license
@ -166,29 +150,36 @@ int main( int argc, char *argv[] ){
"under certain condtions. See `%s --license`\n", argv[0]
);
// board, innit?
wins_t wins = {
.count0 = {
.total = 0,
.vertical = 0,
.horizontal = 0,
.diagonalUp = 0,
.diagonalDown = 0,
},
};
wins.count1 = wins.count0;
// Allocate the board
playboard.height = calloc( playboard.columns, sizeof(rowsint_t) );
playboard.column = calloc( playboard.columns, sizeof(column_t) );
// Initialize wins struct if used
if( wins != NULL ){
wins->win0 = calloc( playboard.columns, sizeof(column_t) );
wins->win1 = calloc( playboard.columns, sizeof(column_t) );
wins->same.vertical2 = calloc( playboard.columns, sizeof(column_t) );
wins->same.horizontal2 = calloc( playboard.columns, sizeof(column_t) );
wins->same.diagonalUp2 = calloc( playboard.columns, sizeof(column_t) );
wins->same.diagonalDown2 = calloc( playboard.columns, sizeof(column_t) );
wins->same.vertical4 = 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) );
}
playboard.height = calloc( playboard.columns, sizeof(int) );
playboard.column = calloc( playboard.columns, sizeof(column_t) );
wins.win0 = calloc( playboard.columns, sizeof(column_t) );
wins.win1 = calloc( playboard.columns, sizeof(column_t) );
wins.same.vertical2 = calloc( playboard.columns, sizeof(column_t) );
wins.same.horizontal2 = calloc( playboard.columns, sizeof(column_t) );
wins.same.diagonalUp2 = calloc( playboard.columns, sizeof(column_t) );
wins.same.diagonalDown2 = calloc( playboard.columns, sizeof(column_t) );
wins.same.vertical4 = 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 );
// Begin loopin
if( outputfile != NULL ){
fprintf(
outputfile,
"# File generated by " VERSIONSTRING
"# Here's the board size\n"
"# First define the config\n"
"--rows %d\n"
"--columns %d\n"
"# Now follows the moves taken, zero indexed\n"
@ -198,36 +189,16 @@ int main( int argc, char *argv[] ){
);
}
for(;; playboard.player = !playboard.player ){
columnsint_t column;
if( randomMoves ){
column = randomColumn( playboard );
}else{
column = askColumn( playboard );
};
columnsint_t column = askColumn( playboard );
if( column == QUITCOLUMN ) break;
playMove( &playboard, column );
calcWins( wins, &playboard, column );
updateBoard( playboard, column );
calcWins( &wins, playboard, column );
updateBoard( wins, playboard, column );
if( outputfile != NULL ){
fprintf( outputfile, "%d\n", column );
}
}
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 );
printf( "Output is written to %s\n", filename );
}

213
logic.c
View file

@ -16,13 +16,11 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <stdbit.h>
#include "logic.h"
#include "macros.h"
static column_t heightMask( const rowsint_t a ){
return ( 1 << (column_t)a ) - (column_t)1;
return ( 1 << a ) - 1;
}
static column_t safeHeightMask( const rowsint_t a ){
@ -33,51 +31,6 @@ static column_t bottomHeightMask( const rowsint_t a, const rowsint_t 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(
board_t *boardptr,
const columnsint_t column
@ -87,107 +40,17 @@ void playMove(
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(
wins_t *wins,
board_t *boardptr,
const board_t board,
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
// Check for lil towers
wins->same.vertical2 [ column ] =
~( boardptr->column [ column ]
^ boardptr->column [ column ] >> 1 )
& safeHeightMask( boardptr->height [ column ] - 1 );
~( board.column [ column ]
^ board.column [ column ] >> 1 )
& safeHeightMask( board.height [ column ] - 1 );
// Actually check for wins
column_t newcolumn =
wins->same.vertical2 [ column ]
@ -198,11 +61,11 @@ void calcWins(
if(
( newcolumn
^ wins->same.vertical4 [ column ]
) & boardptr->column [ column ]
) & board.column [ column ]
){
boardptr->count1.vertical ++;
wins->count1.vertical ++;
} else {
boardptr->count0.vertical ++;
wins->count0.vertical ++;
}
wins->same.vertical4 [ column ] = newcolumn;
}
@ -210,36 +73,36 @@ void calcWins(
// First connect 2
for(
columnsint_t i = clipped_subtract( column, 1 );
i < bottom( column + 1, boardptr->columns - 1 );
i < bottom( column + 1, board.columns - 1 );
i++
){
wins->same.horizontal2 [ i ] =
~( boardptr->column [ i ]
^ boardptr->column [ i + 1 ] )
~( board.column [ i ]
^ board.column [ i + 1 ] )
& bottomHeightMask(
boardptr->height [ i ],
boardptr->height [ i + 1 ]
board.height [ i ],
board.height [ i + 1 ]
);
wins->same.diagonalUp2 [ i ] =
~( boardptr->column [ i ]
^ boardptr->column [ i + 1 ] >> 1 )
~( board.column [ i ]
^ board.column [ i + 1 ] >> 1 )
& bottomHeightMask(
boardptr->height [ i ],
boardptr->height [ i + 1 ] - 1
board.height [ i ],
board.height [ i + 1 ] - 1
);
wins->same.diagonalDown2 [ i ] =
~( boardptr->column [ i ]
^ boardptr->column [ i + 1 ] << 1 )
~( board.column [ i ]
^ board.column [ i + 1 ] << 1 )
& bottomHeightMask(
boardptr->height [ i ],
boardptr->height [ i + 1 ] + 1
board.height [ i ],
board.height [ i + 1 ] + 1
)
& ~(column_t)1; // A diagonal line down ain't starts at the floor innit?
& ~1; // A diagonal line down ain't starts at the floor innit?
}
// Then stitch the twos together and count
for(
columnsint_t i = clipped_subtract( column, 3 );
i < bottom( column + 1, boardptr->columns - 3 );
i < bottom( column + 1, board.columns - 3 );
i++
){
newcolumn =
@ -250,11 +113,11 @@ void calcWins(
if(
( newcolumn
^ wins->same.horizontal4 [ i ]
) & boardptr->column [ i ]
) & board.column [ i ]
){
boardptr->count1.horizontal++;
wins->count1.horizontal++;
} else {
boardptr->count0.horizontal++;
wins->count0.horizontal++;
}
wins->same.horizontal4 [ i ] = newcolumn;
}
@ -266,11 +129,11 @@ void calcWins(
if(
( newcolumn
^ wins->same.diagonalUp4 [ i ]
) & boardptr->column [ i ]
) & board.column [ i ]
){
boardptr->count1.diagonalUp++;
wins->count1.diagonalUp++;
} else {
boardptr->count0.diagonalUp++;
wins->count0.diagonalUp++;
}
wins->same.diagonalUp4 [ i ] = newcolumn;
}
@ -282,15 +145,23 @@ void calcWins(
if(
( newcolumn
^ wins->same.diagonalDown4 [ i ]
) & boardptr->column [ i ]
) & board.column [ i ]
){
boardptr->count1.diagonalDown++;
wins->count1.diagonalDown++;
} else {
boardptr->count0.diagonalDown++;
wins->count0.diagonalDown++;
}
wins->same.diagonalDown4 [ i ] = newcolumn;
}
}
updateTotal( &boardptr->count0 );
updateTotal( &boardptr->count1 );
wins->count0.total =
wins->count0.vertical
+ wins->count0.horizontal
+ wins->count0.diagonalUp
+ wins->count0.diagonalDown;
wins->count1.total =
wins->count1.vertical
+ wins->count1.horizontal
+ wins->count1.diagonalUp
+ wins->count1.diagonalDown;
}

42
logic.h
View file

@ -1,54 +1,16 @@
/* SPDX-License-Identifier: GPL-2.0-only */
#pragma once
#include <stddef.h>
#include "config.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(
board_t *boardptr,
const columnsint_t column
);
// Calculate wins
extern void calcWins(
// Null pointer on systems where the extra ram is not available
wins_t *wins,
board_t *boardptr,
wins_t *wins,
const board_t board,
const columnsint_t column
);
// Reference implementation for calculating wins,
// slow AF especially on big boards
extern void calcWins_slow(
board_t *boardptr
);

View file

@ -4,7 +4,7 @@
ARCH = native
# Optimization level
O = 2
O = 3
# Flags for the compiler
FLAGS = -std=c23
@ -18,13 +18,7 @@ FLAGS += -pedantic
FLAGS += -Wall
FLAGS += -Wextra
FLAGS += -Werror
FLAGS += -Wconversion
FLAGS += -Wmissing-prototypes
FLAGS += -Wlogical-op
FLAGS += -Wdisabled-optimization
FLAGS += -fanalyzer
# These flags are because of the compiler otherwise giving too much warning
FLAGS += -Wno-sign-conversion
# This flag is because of the compiler otherwise giving too much warning
#FLAGS += -Wformat-truncation=0
# Compile flag for defining GITHASH to put at the end of the version string
@ -55,7 +49,7 @@ MSG_CLEANING_OBJ = Cleaning $(OBJDIR):
MSG_CLEANING_OUT = Cleaning $(OUTDIR):
# Compile for all UI targets
all: $(addprefix $(OUTDIR)/connect4_,ncurses.elf vt100.elf stub.elf)
all: $(addprefix $(OUTDIR)/connect4_,ncurses.elf vt100.elf)
# Compile and run the default UI target
run: $(OUTDIR)/connect4_$(UI_TARGET).elf

29
types.h
View file

@ -23,6 +23,25 @@ typedef uint_fast8_t column_t;
#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 {
winint_t total;
winint_t horizontal;
@ -32,11 +51,9 @@ typedef struct {
} wincount_t;
typedef struct {
bool player;
rowsint_t *height;
column_t *column;
rowsint_t rows;
columnsint_t columns;
wincount_t count0;
wincount_t count1;
} board_t;
column_t *win0;
column_t *win1;
directions_t same;
} wins_t;

1
ui.h
View file

@ -9,6 +9,7 @@ extern void initBoard(
);
extern void updateBoard(
const wins_t wins,
const board_t board,
const columnsint_t column
);

View file

@ -137,6 +137,7 @@ void initBoard( const board_t board ){
}
void updateBoard(
const wins_t wins,
const board_t board,
const columnsint_t column
){
@ -147,61 +148,61 @@ void updateBoard(
board.column[ column ] & 1 << ( height - 1 ) ? "1" : "0"
);
char num[4];
snprintf( num, sizeof(num), WININT_FORMAT, board.count0.vertical );
snprintf( num, sizeof(num), WININT_FORMAT, wins.count0.vertical );
mvaddstr(
SCOREBOARD_Y + 3,
SCOREBOARD_X + 17,
num
);
snprintf( num, sizeof(num), WININT_FORMAT, board.count1.vertical );
snprintf( num, sizeof(num), WININT_FORMAT, wins.count1.vertical );
mvaddstr(
SCOREBOARD_Y + 3,
SCOREBOARD_X + 22,
num
);
snprintf( num, sizeof(num), WININT_FORMAT, board.count0.horizontal );
snprintf( num, sizeof(num), WININT_FORMAT, wins.count0.horizontal );
mvaddstr(
SCOREBOARD_Y + 4,
SCOREBOARD_X + 17,
num
);
snprintf( num, sizeof(num), WININT_FORMAT, board.count1.horizontal );
snprintf( num, sizeof(num), WININT_FORMAT, wins.count1.horizontal );
mvaddstr(
SCOREBOARD_Y + 4,
SCOREBOARD_X + 22,
num
);
snprintf( num, sizeof(num), WININT_FORMAT, board.count0.diagonalUp );
snprintf( num, sizeof(num), WININT_FORMAT, wins.count0.diagonalUp );
mvaddstr(
SCOREBOARD_Y + 5,
SCOREBOARD_X + 17,
num
);
snprintf( num, sizeof(num), WININT_FORMAT, board.count1.diagonalUp );
snprintf( num, sizeof(num), WININT_FORMAT, wins.count1.diagonalUp );
mvaddstr(
SCOREBOARD_Y + 5,
SCOREBOARD_X + 22,
num
);
snprintf( num, sizeof(num), WININT_FORMAT, board.count0.diagonalDown );
snprintf( num, sizeof(num), WININT_FORMAT, wins.count0.diagonalDown );
mvaddstr(
SCOREBOARD_Y + 6,
SCOREBOARD_X + 17,
num
);
snprintf( num, sizeof(num), WININT_FORMAT, board.count1.diagonalDown );
snprintf( num, sizeof(num), WININT_FORMAT, wins.count1.diagonalDown );
mvaddstr(
SCOREBOARD_Y + 6,
SCOREBOARD_X + 22,
num
);
snprintf( num, sizeof(num), WININT_FORMAT, board.count0.total );
snprintf( num, sizeof(num), WININT_FORMAT, wins.count0.total );
mvaddstr(
SCOREBOARD_Y + 8,
SCOREBOARD_X + 17,
num
);
snprintf( num, sizeof(num), WININT_FORMAT, board.count1.total );
snprintf( num, sizeof(num), WININT_FORMAT, wins.count1.total );
mvaddstr(
SCOREBOARD_Y + 8,
SCOREBOARD_X + 22,

View file

@ -1,26 +0,0 @@
#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;
}