connect4

Connect 4 Board Game
Log | Files | Refs

commit 599029950fec9bc8f199875cd9d2d701639144f6
parent 45c7a3eb49175a5f88c87b7bd5599c369c68c0a7
Author: sej <sej@sejdt.localhost>
Date:   Tue,  1 Oct 2024 00:47:06 +0200

Implemented board class

Diffstat:
MBoard.cs | 143+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----
MConnect4.cs | 4++--
2 files changed, 138 insertions(+), 9 deletions(-)

diff --git a/Board.cs b/Board.cs @@ -3,15 +3,144 @@ using System.Collections.Generic; using System.Linq; public class Board { - int n; - int m; - char[,] board; + private int _n; // number of columns + private int _m; // number of rows + private Cell[,] _board; + private int[] _height; + private Cell _next; + private Status _status; - public Board() { - + public enum Cell { + Free = 0, // make sure free is the default value + PlayerX, + PlayerO, } - public void Hello() { - Console.WriteLine( "Hello from Board" ); + public enum Status { + InProgress, + PlayerXWon, + PlayerOWon, + Draw, + } + + /* + * Constructs a board with m rows and n columns. + */ + public Board( int n, int m ) { + _m = m; + _n = n; + _board = new Cell[ m, n ]; + _height = new int[ n ]; + _next = Cell.PlayerX; // player X starts + _status = Status.InProgress; + } + + /* + * Determines if it is legal to play a piece in the given column + */ + public bool IsLegal( int column ) { + return _height[ column ] < _m; + } + + /* + * assumes play is legal + */ + public void Play( int column ) { + _board[ _m - _height[ column ] - 1, column ] = _next; + _height[ column ] ++; + _next = ( _next == Cell.PlayerX ) ? Cell.PlayerO : Cell.PlayerX; + ComputeStatus(); + } + + /* + * returns whos turn it is + */ + public Cell getNext() { + return _next; + } + + /* + * Pretty prints the board + */ + public void PrettyPrint() { + char[] prettyChar = { '.', 'X', 'O' }; + for ( int j = 0; j < _m; j++ ) { + for ( int i = 0; i < _n; i++ ) { + Console.Write( prettyChar[ (int) _board[ j, i ] ] ); + } + Console.Write( "\n" ); + } + Console.WriteLine( _status ); + } + + /* + * Returns a list of all legal moves on this board + */ + public List< int > LegalMoves() { + List< int > legalMoves = new List< int >(); + for ( int i = 0; i < _m; i ++ ) { + if ( IsLegal( i ) ) { + legalMoves.Add( i ); + } + } + return legalMoves; + } + + /* + * Computes the status of this board, ie. has someone won by obtaining four in a row. + */ + public Status ComputeStatus() { + foreach ( Cell player in new Cell[] { Cell.PlayerX, Cell.PlayerO } ) { + // Check for vertical four in a row + ComputeStatus( + ( j, i, k ) => _board[ j + k, i ], + j => j < _m - 3, + _ => true, + player + ); + // Check for horizontal four in a row + ComputeStatus( + ( j, i, k ) => _board[ j, i + k ], + _ => true, + i => i < _n - 3, + player + ); + // Check for diagonal four in a row + ComputeStatus( + ( j, i, k ) => _board[ j + k, i + k ], + j => j < _m - 3, + i => i < _n - 3, + player + ); + // Check for alternate diagonal four in a row + ComputeStatus( + ( j, i, k ) => _board[ j - k, i + k ], + j => j > 3, + i => i < _n - 3, + player + ); + } + return _status; + } + + /* + * Helper method + */ + private void ComputeStatus( Func< int, int, int, Cell > f, Predicate< int > YOkay, Predicate< int > XOkay, Cell player ) { + int j = 0; + while ( _status == Status.InProgress && j < _m ) { + int i = 0; + while ( _status == Status.InProgress && i < _n ) { + int k = 0; + while ( XOkay( i ) && YOkay( j ) && k < 4 && f( j, i, k ) == player ) { + k ++; + } + if ( k == 4 ) { + _status = player == Cell.PlayerX ? Status.PlayerXWon : Status.PlayerOWon; + } + i ++; + } + j ++; + } } } diff --git a/Connect4.cs b/Connect4.cs @@ -2,7 +2,7 @@ using System; public class Connect4 { public static void Main( String[] args ) { - Board board = new Board(); - board.Hello(); + Board board = new Board( 7, 6 ); + board.PrettyPrint(); } }