167 lines
4.4 KiB
C
167 lines
4.4 KiB
C
/* SPDX-License-Identifier: GPL-2.0-only */
|
|
/*
|
|
* AnnaConnect is a silly little text-based connect 4 game c:
|
|
* Copyright (C) 2025 Anna Snoeijs. anna.snoeijs@hotmail.com
|
|
*
|
|
* This program is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License as published by
|
|
* the Free Software Foundation, either version 2 of the License
|
|
* (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* 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 "logic.h"
|
|
#include "macros.h"
|
|
|
|
static column_t heightMask( const rowsint_t a ){
|
|
return ( 1 << a ) - 1;
|
|
}
|
|
|
|
static column_t safeHeightMask( const rowsint_t a ){
|
|
return a > 0 ? heightMask( a ) : 0;
|
|
}
|
|
|
|
static column_t bottomHeightMask( const rowsint_t a, const rowsint_t b ){
|
|
return safeHeightMask( bottom( a, b ) );
|
|
}
|
|
|
|
void playMove(
|
|
board_t *boardptr,
|
|
const columnsint_t column
|
|
){
|
|
boardptr->column [ column ] |=
|
|
boardptr->player << boardptr->height [ column ];
|
|
boardptr->height [ column ]++;
|
|
}
|
|
|
|
void calcWins(
|
|
wins_t *wins,
|
|
const board_t board,
|
|
const columnsint_t column
|
|
){
|
|
// First the simplest win, the humble tower
|
|
// Check for lil towers
|
|
wins->same.vertical2 [ column ] =
|
|
~( board.column [ column ]
|
|
^ board.column [ column ] >> 1 )
|
|
& safeHeightMask( board.height [ column ] - 1 );
|
|
// Actually check for wins
|
|
column_t newcolumn =
|
|
wins->same.vertical2 [ column ]
|
|
& wins->same.vertical2 [ column ] >> 1
|
|
& wins->same.vertical2 [ column ] >> 2;
|
|
// Count 'em if there's a new one
|
|
if( wins->same.vertical4 [ column ] != newcolumn ){
|
|
if(
|
|
( newcolumn
|
|
^ wins->same.vertical4 [ column ]
|
|
) & board.column [ column ]
|
|
){
|
|
wins->count1.vertical ++;
|
|
} else {
|
|
wins->count0.vertical ++;
|
|
}
|
|
wins->same.vertical4 [ column ] = newcolumn;
|
|
}
|
|
// Now the rest of the wins
|
|
// First connect 2
|
|
for(
|
|
columnsint_t i = clipped_subtract( column, 1 );
|
|
i < bottom( column + 1, board.columns - 1 );
|
|
i++
|
|
){
|
|
wins->same.horizontal2 [ i ] =
|
|
~( board.column [ i ]
|
|
^ board.column [ i + 1 ] )
|
|
& bottomHeightMask(
|
|
board.height [ i ],
|
|
board.height [ i + 1 ]
|
|
);
|
|
wins->same.diagonalUp2 [ i ] =
|
|
~( board.column [ i ]
|
|
^ board.column [ i + 1 ] >> 1 )
|
|
& bottomHeightMask(
|
|
board.height [ i ],
|
|
board.height [ i + 1 ] - 1
|
|
);
|
|
wins->same.diagonalDown2 [ i ] =
|
|
~( board.column [ i ]
|
|
^ board.column [ i + 1 ] << 1 )
|
|
& bottomHeightMask(
|
|
board.height [ i ],
|
|
board.height [ i + 1 ] + 1
|
|
)
|
|
& ~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, board.columns - 3 );
|
|
i++
|
|
){
|
|
newcolumn =
|
|
wins->same.horizontal2 [ i ]
|
|
& wins->same.horizontal2 [ i + 1 ]
|
|
& wins->same.horizontal2 [ i + 2 ];
|
|
if( wins->same.horizontal4 [ i ] != newcolumn ){
|
|
if(
|
|
( newcolumn
|
|
^ wins->same.horizontal4 [ i ]
|
|
) & board.column [ i ]
|
|
){
|
|
wins->count1.horizontal++;
|
|
} else {
|
|
wins->count0.horizontal++;
|
|
}
|
|
wins->same.horizontal4 [ i ] = newcolumn;
|
|
}
|
|
newcolumn =
|
|
wins->same.diagonalUp2 [ i ]
|
|
& wins->same.diagonalUp2 [ i + 1 ] >> 1
|
|
& wins->same.diagonalUp2 [ i + 2 ] >> 2;
|
|
if( wins->same.diagonalUp4 [ i ] != newcolumn ){
|
|
if(
|
|
( newcolumn
|
|
^ wins->same.diagonalUp4 [ i ]
|
|
) & board.column [ i ]
|
|
){
|
|
wins->count1.diagonalUp++;
|
|
} else {
|
|
wins->count0.diagonalUp++;
|
|
}
|
|
wins->same.diagonalUp4 [ i ] = newcolumn;
|
|
}
|
|
newcolumn =
|
|
wins->same.diagonalDown2 [ i ]
|
|
& wins->same.diagonalDown2 [ i + 1 ] << 1
|
|
& wins->same.diagonalDown2 [ i + 2 ] << 2;
|
|
if( wins->same.diagonalDown4 [ i ] != newcolumn ){
|
|
if(
|
|
( newcolumn
|
|
^ wins->same.diagonalDown4 [ i ]
|
|
) & board.column [ i ]
|
|
){
|
|
wins->count1.diagonalDown++;
|
|
} else {
|
|
wins->count0.diagonalDown++;
|
|
}
|
|
wins->same.diagonalDown4 [ i ] = newcolumn;
|
|
}
|
|
}
|
|
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;
|
|
}
|