connect4

Connect 4 Board Game
Log | Files | Refs

commit 9aaee4de53df1a61d74042639d695bda578b3496
parent 599029950fec9bc8f199875cd9d2d701639144f6
Author: sej <sej@sejdt.localhost>
Date:   Fri,  4 Oct 2024 23:21:51 +0200

Added another constructor used in Minimax + empty list is now returned for LegalMoves() on finished board.

Diffstat:
MBoard.cs | 33+++++++++++++++++++++++++++------
1 file changed, 27 insertions(+), 6 deletions(-)

diff --git a/Board.cs b/Board.cs @@ -24,7 +24,7 @@ public class Board { } /* - * Constructs a board with m rows and n columns. + * Constructs a new board with m rows and n columns. */ public Board( int n, int m ) { _m = m; @@ -36,6 +36,25 @@ public class Board { } /* + * Constructs a new board with the given board + */ + public Board( Board board ) { + _m = board._m; + _n = board._n; + _board = new Cell[ _m, _n ]; + _height = new int[ _n ]; + // why does it have to be so difficult to deep copy a 2d array? + for ( int j = 0; j < _m; j ++ ) { + for ( int i = 0; i < _n; i ++ ) { + _board[ j, i ] = board._board[ j, i ]; + } + } + board._height.CopyTo( _height, 0 ); + _next = board._next; + _status = board._status; + } + + /* * Determines if it is legal to play a piece in the given column */ public bool IsLegal( int column ) { @@ -64,13 +83,13 @@ public class Board { */ public void PrettyPrint() { char[] prettyChar = { '.', 'X', 'O' }; + Console.WriteLine( $"{ _status }:" ); for ( int j = 0; j < _m; j++ ) { for ( int i = 0; i < _n; i++ ) { - Console.Write( prettyChar[ (int) _board[ j, i ] ] ); + Console.Write( $" { prettyChar[ (int) _board[ j, i ] ] } " ); } Console.Write( "\n" ); } - Console.WriteLine( _status ); } /* @@ -78,9 +97,11 @@ public class Board { */ public List< int > LegalMoves() { List< int > legalMoves = new List< int >(); - for ( int i = 0; i < _m; i ++ ) { - if ( IsLegal( i ) ) { - legalMoves.Add( i ); + if ( _status == Status.InProgress ) { + for ( int i = 0; i < _n; i ++ ) { + if ( IsLegal( i ) ) { + legalMoves.Add( i ); + } } } return legalMoves;