Refactored a bit
This commit is contained in:
parent
99f5c6cd6e
commit
5379f50802
8 changed files with 534 additions and 449 deletions
165
logic.c
Normal file
165
logic.c
Normal file
|
|
@ -0,0 +1,165 @@
|
|||
/* 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.tech
|
||||
*
|
||||
* 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"
|
||||
|
||||
static inline int top( const int a, const int b ){
|
||||
return a < b ? b : a;
|
||||
}
|
||||
|
||||
static inline int bottom( const int a, const int b ){
|
||||
return a < b ? a : b;
|
||||
}
|
||||
|
||||
static inline int heigthMask( const int a ){
|
||||
return ( 1 << a ) - 1;
|
||||
}
|
||||
|
||||
static inline int safeHeigthMask( const int a ){
|
||||
return a > 0 ? heigthMask( a ) : 0;
|
||||
}
|
||||
|
||||
static inline int bottomHeigthMask( const int a, const int b ){
|
||||
return safeHeigthMask( bottom( a, b ) );
|
||||
}
|
||||
|
||||
void playMove(
|
||||
board_t *boardptr,
|
||||
#ifndef ONLYPUT
|
||||
const move_t move,
|
||||
#endif /* ! ONLYPUT */
|
||||
const int column
|
||||
){
|
||||
#ifndef ONLYPUT
|
||||
switch( move ){
|
||||
case PUT: // Put a new piece in the board
|
||||
#endif /* ! ONLYPUT */
|
||||
boardptr->column [ column ] |=
|
||||
boardptr->player << boardptr->heigth [ column ];
|
||||
boardptr->heigth [ column ]++;
|
||||
#ifndef ONLYPUT
|
||||
break;
|
||||
case POP: // Pop out the lowest and make all above fall down
|
||||
boardptr->column [ column ] >>= 1;
|
||||
boardptr->heigth [ column ]--;
|
||||
}
|
||||
#endif /* ! ONLYPUT */
|
||||
}
|
||||
|
||||
void calcWins(
|
||||
wins_t *wins,
|
||||
const board_t board
|
||||
){
|
||||
// First the simplest win, the humble tower
|
||||
wins->count0.vertical = 0;
|
||||
wins->count1.vertical = 0;
|
||||
for( int i = 0; i < BOARD_WIDTH; i++ ){
|
||||
// Check for lil towers
|
||||
wins->same.vertical2 [ i ] =
|
||||
~( board.column [ i ]
|
||||
^ board.column [ i ] >> 1 )
|
||||
& safeHeigthMask( board.heigth [ i ] - 1 );
|
||||
// Actually check for win
|
||||
wins->same.vertical4 [ i ] =
|
||||
wins->same.vertical2 [ i ]
|
||||
& wins->same.vertical2 [ i ] >> 1
|
||||
& wins->same.vertical2 [ i ] >> 2;
|
||||
// Count 'em
|
||||
wins->count0.vertical += __builtin_popcount(
|
||||
wins->same.vertical4 [ i ] & ~board.column [ i ]
|
||||
);
|
||||
wins->count1.vertical += __builtin_popcount(
|
||||
wins->same.vertical4 [ i ] & board.column [ i ]
|
||||
);
|
||||
}
|
||||
// Now the rest of the wins
|
||||
wins->count0.horizontal = 0;
|
||||
wins->count1.horizontal = 0;
|
||||
wins->count0.diagonalUp = 0;
|
||||
wins->count1.diagonalUp = 0;
|
||||
wins->count0.diagonalDown = 0;
|
||||
wins->count1.diagonalDown = 0;
|
||||
// First connect 2
|
||||
for( int i = 0; i < BOARD_WIDTH - 1; i++ ){
|
||||
wins->same.horizontal2 [ i ] =
|
||||
~( board.column [ i ]
|
||||
^ board.column [ i + 1 ] )
|
||||
& bottomHeigthMask(
|
||||
board.heigth [ i ],
|
||||
board.heigth [ i + 1 ]
|
||||
);
|
||||
wins->same.diagonalUp2 [ i ] =
|
||||
~( board.column [ i ]
|
||||
^ board.column [ i + 1 ] >> 1 )
|
||||
& bottomHeigthMask(
|
||||
board.heigth [ i ],
|
||||
board.heigth [ i + 1 ] - 1
|
||||
);
|
||||
wins->same.diagonalDown2 [ i ] =
|
||||
~( board.column [ i ]
|
||||
^ board.column [ i + 1 ] << 1 )
|
||||
& bottomHeigthMask(
|
||||
board.heigth [ i ],
|
||||
board.heigth [ i + 1 ] + 1
|
||||
)
|
||||
& ~1; // A diagonal line down ain't starts at the floor innit?
|
||||
}
|
||||
// Then stitch the twos together and count
|
||||
for( int i = 0; i < BOARD_WIDTH - 3; i++ ){
|
||||
wins->same.horizontal4 [ i ] =
|
||||
wins->same.horizontal2 [ i ]
|
||||
& wins->same.horizontal2 [ i + 1 ]
|
||||
& wins->same.horizontal2 [ i + 2 ];
|
||||
wins->same.diagonalUp4 [ i ] =
|
||||
wins->same.diagonalUp2 [ i ]
|
||||
& wins->same.diagonalUp2 [ i + 1 ] >> 1
|
||||
& wins->same.diagonalUp2 [ i + 2 ] >> 2;
|
||||
wins->same.diagonalDown4 [ i ] =
|
||||
wins->same.diagonalDown2 [ i ]
|
||||
& wins->same.diagonalDown2 [ i + 1 ] << 1
|
||||
& wins->same.diagonalDown2 [ i + 2 ] << 2;
|
||||
wins->count0.horizontal += __builtin_popcount(
|
||||
wins->same.horizontal4 [ i ] & ~board.column [ i ]
|
||||
);
|
||||
wins->count1.horizontal += __builtin_popcount(
|
||||
wins->same.horizontal4 [ i ] & board.column [ i ]
|
||||
);
|
||||
wins->count0.diagonalUp += __builtin_popcount(
|
||||
wins->same.diagonalUp4 [ i ] & ~board.column [ i ]
|
||||
);
|
||||
wins->count1.diagonalUp += __builtin_popcount(
|
||||
wins->same.diagonalUp4 [ i ] & board.column [ i ]
|
||||
);
|
||||
wins->count0.diagonalDown += __builtin_popcount(
|
||||
wins->same.diagonalDown4 [ i ] & ~board.column [ i ]
|
||||
);
|
||||
wins->count1.diagonalDown += __builtin_popcount(
|
||||
wins->same.diagonalDown4 [ i ] & board.column [ i ]
|
||||
);
|
||||
}
|
||||
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;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue