public ButtonViewModel(int row,int col,Game game) { this.game = game; pos = new Vector2D(row-1,col-1); Owner = game.Board[pos].Owner; IsValidMove = game.Board[pos].IsValidMove; Square = game.Board[pos]; click=new ClickCommand(this); }
private int Weight( IGrid<Player> board, Vector2D p ) { if ( board.InCorner( p ) ) { return cornerWeight; } else if ( board.OnRim( p ) ) { return rimWeight; } else { return 1; } }
public bool IsValidMove( Vector2D position ) { return state.Value.IsValidMove( position ); }
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; }
public Square( Game game, Vector2D position ) { this.game = game; this.position = position; this.owner = Cell.Derived( () => game.GetOwnerAt( position ) ); this.isValidMove = Cell.Derived( () => game.IsValidMove( position ) ); }
public abstract bool IsValidMove( Vector2D position );
public IEnumerable<Vector2D> Captured( Vector2D position ) { return state.Value.Captured( position ); }
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 ); } }
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 ); } } }
public int ManhattanDistanceTo( Vector2D v ) { if ( v == null ) { throw new ArgumentNullException( "v" ); } else { return Math.Abs( X - v.X ) + Math.Abs( Y - v.Y ); } }
public override bool IsValidMove( Vector2D position ) { return false; }
public Player this[Vector2D position] { get { return this.grid[position].Value; } }
public bool Equals( Vector2D v ) { return !object.ReferenceEquals( v, null ) && this.x == v.x && this.y == v.y; }
private void CaptureStones( Vector2D position, Player player ) { foreach ( var p in CapturedStones( position, player ) ) { grid[p].Value = player; } }
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; } } }
private static Player StoneAtPositionInDefaultInitialGrid( Vector2D position ) { if ( position == new Vector2D( 3, 4 ) || position == new Vector2D( 4, 3 ) ) { return Player.ONE; } else if ( position == new Vector2D( 3, 3 ) || position == new Vector2D( 4, 4 ) ) { return Player.TWO; } else { return null; } }
public void MakeMove( Vector2D position ) { state.Value.MakeMove( position ); }
public override IEnumerable<Vector2D> Captured( Vector2D position ) { if ( IsValidMove( position ) ) { return board.CapturedStones( position, currentPlayer ); } else { return Enumerable.Empty<Vector2D>(); } }
public override IEnumerable<Vector2D> Captured( Vector2D position ) { throw new InvalidOperationException(); }
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; } }
public override void MakeMove( Vector2D position ) { throw new InvalidOperationException( "Game is over, cannot make moves" ); }
public override void MakeMove( Vector2D position ) { throw new InvalidOperationException(); }
public override bool IsValidMove( Vector2D position ) { return board.IsValidMove( position, this.currentPlayer ); }
/// <summary> /// Computes which positions would be captured if the current player /// were to put his next stone at the specified position. /// </summary> /// <param name="position">Position at which the current player intends to put his stone.</param> /// <returns>A sequence of all stones that would be captured by the specified move.</returns> private IEnumerable<Vector2D> CapturedBy( Vector2D position ) { return gameState.Captured( position ); }
public abstract IEnumerable<Vector2D> Captured( Vector2D position );
private Player GetOwnerAt( Vector2D position ) { return gameState.Board[position]; }
public abstract void MakeMove( Vector2D position );
private bool IsValidMove( Vector2D position ) { return gameState.IsValidMove( position ); }
private void PlaceStone( Vector2D position ) { gameState.MakeMove( position ); Refresh(); }
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; } } }