Ejemplo n.º 1
0
        public int Score( IGrid<Player> board, Player player )
        {
            var result = 0;

            foreach ( var p in board.AllPositions() )
            {
                var owner = board[p];

                if ( owner != null )
                {
                    var factor = owner == player ? 1 : -1;
                    var weight = Weight( board, p );

                    result = result + factor * weight;
                }
            }

            return result;
        }
Ejemplo n.º 2
0
 public InProgress( ICell<State> state, GameBoard board, Player player )
     : base(state, board)
 {
     this.currentPlayer = player;
 }
Ejemplo n.º 3
0
 private GameState( GameBoard initialBoard, Player initialPlayer )
     : this(CreateInitialState( initialBoard, initialPlayer ))
 {
     // NOP
 }
Ejemplo n.º 4
0
        private static ICell<State> CreateInitialState( GameBoard initialBoard, Player initialPlayer )
        {
            if ( !initialBoard.HasValidMove( initialPlayer ) )
            {
                throw new ArgumentException( "No move available for given player" );
            }
            else
            {
                var state = new Cell<State>();
                state.Value = new InProgress( state, initialBoard, initialPlayer );

                return state;
            }
        }
Ejemplo n.º 5
0
 public int StoneCount( Player player )
 {
     if ( player == null )
     {
         throw new ArgumentNullException();
     }
     else
     {
         return state.Value.Board.CountStones( player );
     }
 }
Ejemplo n.º 6
0
 /// <summary>
 /// Returns the number of stones the specified player has.
 /// </summary>
 /// <param name="player">Player</param>
 /// <returns>Number of stones</returns>
 public ICell<int> StoneCount( Player player )
 {
     if ( player == null )
     {
         throw new ArgumentNullException( "player" );
     }
     else
     {
         return stoneCounts[player.ArrayIndex];
     }
 }
Ejemplo n.º 7
0
 public int CountStones( Player player )
 {
     return grid.Count( p => p.Value == player );
 }
Ejemplo n.º 8
0
 public int Score( IGrid<Player> board, Player player )
 {
     return board.Count( player ) - board.Count( player.Other );
 }
Ejemplo n.º 9
0
 private void CaptureStones( Vector2D position, Player player )
 {
     foreach ( var p in CapturedStones( position, player ) )
     {
         grid[p].Value = player;
     }
 }
Ejemplo n.º 10
0
        private IEnumerable<Vector2D> CapturedStones( Vector2D position, Vector2D direction, Player player )
        {
            var distanceToTwin = FindDistanceToCaptureTwin( position, direction, player );

            if ( distanceToTwin.HasValue )
            {
                for ( var i = 1; i != distanceToTwin; ++i )
                {
                    var p = position + i * direction;

                    yield return p;
                }
            }
        }
Ejemplo n.º 11
0
 public IEnumerable<Vector2D> ValidMoves( Player player )
 {
     return this.grid.AllPositions().Where( p => IsValidMove( p, player ) );
 }
Ejemplo n.º 12
0
 public void PlaceStone( Vector2D position, Player player )
 {
     if ( !IsValidMove( position, player ) )
     {
         throw new ArgumentException( string.Format( "{0} cannot place stone on position {1}", player.ToString(), position.ToString() ) );
     }
     else
     {
         this.grid[position].Value = player;
         CaptureStones( position, player );
     }
 }
Ejemplo n.º 13
0
        public bool IsValidMove( Vector2D position, Player player )
        {
            if ( position == null )
            {
                throw new ArgumentNullException( "position" );
            }
            else if ( player == null )
            {
                throw new ArgumentNullException( "player" );
            }
            else if ( grid[position].Value == null )
            {
                foreach ( var direction in Vector2D.Directions )
                {
                    var distance = FindDistanceToCaptureTwin( position, direction, player );

                    if ( distance.HasValue && distance.Value > 1 )
                    {
                        return true;
                    }
                }

                return false;
            }
            else
            {
                return false;
            }
        }
Ejemplo n.º 14
0
 public bool HasValidMove( Player player )
 {
     return this.grid.AllPositions().Any( p => IsValidMove( p, player ) );
 }
Ejemplo n.º 15
0
        public int? FindDistanceToCaptureTwin( Vector2D position, Vector2D direction, Player player )
        {
            var slice = grid.Slice( position, direction );

            for ( var i = 1; i < slice.End; ++i )
            {
                if ( slice[i].Value == null )
                {
                    return null;
                }
                else if ( slice[i].Value == player )
                {
                    return i;
                }
            }

            return null;
        }
Ejemplo n.º 16
0
            public override void MakeMove( Vector2D position )
            {
                if ( !IsValidMove( position ) )
                {
                    throw new ArgumentException( "Not a valid move" );
                }
                else
                {
                    board.PlaceStone( position, currentPlayer );

                    if ( board.HasValidMove( currentPlayer.Other ) )
                    {
                        currentPlayer = currentPlayer.Other;
                    }
                    else if ( !board.HasValidMove( currentPlayer ) )
                    {
                        state.Value = new GameOver( state, board );
                    }
                }
            }
Ejemplo n.º 17
0
        public static GameState CreateInProgress( IGrid<Player> board, Player nextPlayer )
        {
            var gameBoard = GameBoard.Create( board );

            return new GameState( gameBoard, nextPlayer );
        }
Ejemplo n.º 18
0
 public int getnStones(Player player)
 {
     return game.StoneCount(player).Value;
 }
Ejemplo n.º 19
0
 /// <summary>
 /// Creates a game from a given board and next player.
 /// </summary>
 /// <param name="board">Board</param>
 /// <param name="nextPlayer">Next player</param>
 /// <returns>A game object</returns>
 public static Game CreateInProgress(IGrid<Player> board, Player nextPlayer)
 {
     return new Game( GameState.CreateInProgress( board, nextPlayer ) );
 }
Ejemplo n.º 20
0
 public IEnumerable<Vector2D> CapturedStones( Vector2D position, Player player )
 {
     foreach ( var direction in Vector2D.Directions )
     {
         foreach ( var p in CapturedStones( position, direction, player ) )
         {
             yield return p;
         }
     }
 }