public void UndoMove() { mBoard.UndoLastMove(); // In one-player mode, Undo has to remove an additional move to return to the // human player's turn. if (Players == NumberOfPlayers.One) { mBoard.UndoLastMove(); } PossibleMoves = new HashSet <BoardPosition>( from OthelloMove m in mBoard.GetPossibleMoves() select m.Position ); var newSquares = from r in Enumerable.Range(0, 8) from c in Enumerable.Range(0, 8) select new BoardPosition(r, c); int i = 0; foreach (var pos in newSquares) { mSquares[i].Player = mBoard.GetPieceAtPosition(pos); i++; } OnPropertyChanged(nameof(BoardValue)); OnPropertyChanged(nameof(CurrentPlayer)); OnPropertyChanged(nameof(CanUndo)); }
public void ApplyMove(BoardPosition position) { var possMoves = mBoard.GetPossibleMoves() as IEnumerable <OthelloMove>; foreach (var move in possMoves) { if (move.Position.Equals(position)) { mBoard.ApplyMove(move); break; } } PossibleMoves = new HashSet <BoardPosition>(mBoard.GetPossibleMoves().Select(m => m.Position)); var newSquares = from r in Enumerable.Range(0, 8) from c in Enumerable.Range(0, 8) select new BoardPosition(r, c); int i = 0; foreach (var pos in newSquares) { mSquares[i].Player = mBoard.GetPieceAtPosition(pos); i++; } OnPropertyChanged(nameof(BoardValue)); OnPropertyChanged(nameof(CurrentPlayer)); OnPropertyChanged(nameof(CanUndo)); }
/// <summary> /// Applies a move for the current player at the given position. /// </summary> public void ApplyMove(BoardPosition position) { var possMoves = mBoard.GetPossibleMoves() as IEnumerable <OthelloMove>; // Validate the move as possible. foreach (var move in possMoves) { if (move.Position.Equals(position)) { mBoard.ApplyMove(move); break; } } // Rebind the possible moves, now that the board has changed. PossibleMoves = new HashSet <BoardPosition>( from OthelloMove m in mBoard.GetPossibleMoves() select m.Position ); // Update the collection of squares by examining the new board state. var newSquares = from r in Enumerable.Range(0, 8) from c in Enumerable.Range(0, 8) select new BoardPosition(r, c); int i = 0; foreach (var pos in newSquares) { mSquares[i].Player = mBoard.GetPieceAtPosition(pos); i++; } OnPropertyChanged(nameof(BoardValue)); OnPropertyChanged(nameof(CurrentPlayer)); }
public void UndoMove() { mBoard.UndoLastMove(); PossibleMoves = new HashSet <BoardPosition>( from OthelloMove m in mBoard.GetPossibleMoves() select m.Position ); var newSquares = from r in Enumerable.Range(0, 8) from c in Enumerable.Range(0, 8) select new BoardPosition(r, c); int i = 0; foreach (var pos in newSquares) { mSquares[i].Player = mBoard.GetPieceAtPosition(pos); i++; } OnPropertyChanged(nameof(BoardValue)); OnPropertyChanged(nameof(CurrentPlayer)); OnPropertyChanged(nameof(CanUndo)); }
public OthelloViewModel() { mBoard = new OthelloBoard(); mSquares = new ObservableCollection <OthelloSquare>( from pos in ( from r in Enumerable.Range(0, 8) from c in Enumerable.Range(0, 8) select new BoardPosition(r, c) ) select new OthelloSquare() { Position = pos, Player = mBoard.GetPieceAtPosition(pos) } ); PossibleMoves = new HashSet <BoardPosition>(mBoard.GetPossibleMoves().Select(m => m.Position)); }
public OthelloViewModel() { mBoard = new OthelloBoard(); // Initialize the squares objects based on the board's initial state. mSquares = new ObservableCollection <OthelloSquare>( from pos in ( from r in Enumerable.Range(0, 8) from c in Enumerable.Range(0, 8) select new BoardPosition(r, c) ) select new OthelloSquare() { Position = pos, Player = mBoard.GetPieceAtPosition(pos) } ); PossibleMoves = new HashSet <BoardPosition>( from OthelloMove m in mBoard.GetPossibleMoves() select m.Position ); }
public void ApplyMove(BoardPosition position) { if (mBoard.PassCount == 2) { GameFinished?.Invoke(this, new EventArgs()); } else { var possMoves = mBoard.GetPossibleMoves() as IEnumerable <OthelloMove>; foreach (var move in possMoves) { if (move.Position.Equals(position)) { mBoard.ApplyMove(move); break; } } PossibleMoves = new HashSet <BoardPosition>( from OthelloMove m in mBoard.GetPossibleMoves() select m.Position ); var newSquares = from r in Enumerable.Range(0, 8) from c in Enumerable.Range(0, 8) select new BoardPosition(r, c); int i = 0; foreach (var pos in newSquares) { mSquares[i].Player = mBoard.GetPieceAtPosition(pos); i++; } } OnPropertyChanged(nameof(BoardValue)); OnPropertyChanged(nameof(CurrentPlayer)); OnPropertyChanged(nameof(CanUndo)); }