/// <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; } } if (Players == NumberOfPlayers.One && !mBoard.IsFinished) { var bestMove = mGameAi.FindBestMove(mBoard); if (bestMove != null) { mBoard.ApplyMove(bestMove as OthelloMove); } } RebindState(); if (mBoard.IsFinished) { GameFinished?.Invoke(this, new EventArgs()); } }
public async Task ApplyMove(BoardPosition position) { try { String path = @"C:\Users\Nick\Documents\TimeWithAlphaBeta.txt"; var possMoves = mBoard.GetPossibleMoves() as IEnumerable <OthelloMove>; foreach (var move in possMoves) { if (move.Position.Equals(position)) { mBoard.ApplyMove(move); break; } } RebindState(); if (Players == NumberOfPlayers.One && !mBoard.IsFinished) { //Initialize Time DateTime start = DateTime.Now; DateTime end; var bestMove = await Task.Run(() => mGameAi.FindBestMove(mBoard)); //Record finish time end = DateTime.Now; System.IO.File.AppendAllText(path, "Move " + (++mCount) + ": " + (end - start).ToString() + Environment.NewLine); //file.WriteLine((end - start)); if (bestMove != null) { mBoard.ApplyMove(bestMove); RebindState(); } } } catch (Exception e) { Console.WriteLine(e.Message); } if (mBoard.PassCount == 2) { GameFinished?.Invoke(this, new EventArgs()); } if (PossibleMoves.Count == 1 && PossibleMoves.First().Row == -1) { CurrentPlayerMustPass?.Invoke(this, new EventArgs()); } if (PossibleMoves.Count == 0 || mBoard.IsFinished) { GameFinished?.Invoke(this, new EventArgs()); } }
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; } } if (Players == NumberOfPlayers.One && !mBoard.IsFinished) { var bestMove = mGameAi.FindBestMove(mBoard); if (bestMove != null) { mBoard.ApplyMove(bestMove); } } 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)); if (mBoard.PassCount == 2) { GameFinished?.Invoke(this, new EventArgs()); } if (PossibleMoves.Count == 1 && PossibleMoves.First().Row == -1) { CurrentPlayerMustPass?.Invoke(this, new EventArgs()); } if (PossibleMoves.Count == 0 || mBoard.IsFinished) { GameFinished?.Invoke(this, new EventArgs()); } }
public void ValueAfterMultiDirectionMove() { // Value updated correctly after doing a multi-directional move. OthelloBoard board = new OthelloBoard(); board.ApplyMove(new OthelloMove(new BoardPosition(3, 2))); board.ApplyMove(new OthelloMove(new BoardPosition(4, 2))); board.Value.Should().Be(0); board.ApplyMove(new OthelloMove(new BoardPosition(5, 2))); board.Value.Should().Be(5); }
/// <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 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)); }
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 = BoardPosition.GetRectangularPositions(8, 8); int i = 0; foreach (var pos in newSquares) { mSquares[i].Player = mBoard.GetPlayerAtPosition(pos); i++; } OnPropertyChanged(nameof(CurrentAdvantage)); OnPropertyChanged(nameof(CurrentPlayer)); }
public async Task ApplyMove(BoardPosition position) { var possMoves = mBoard.GetPossibleMoves() as IEnumerable <OthelloMove>; foreach (var move in possMoves) { if (move.Position.Equals(position)) { mBoard.ApplyMove(move); break; } } RebindState(); if (Players == NumberOfPlayers.One && !mBoard.IsFinished) { var bestMove = await Task.Run(() => mGameAi.FindBestMove(mBoard)); if (bestMove != null) { mBoard.ApplyMove(bestMove); RebindState(); } } if (mBoard.PassCount == 2) { GameFinished?.Invoke(this, new EventArgs()); } if (PossibleMoves.Count == 1 && PossibleMoves.First().Row == -1) { CurrentPlayerMustPass?.Invoke(this, new EventArgs()); } if (PossibleMoves.Count == 0 || mBoard.IsFinished) { GameFinished?.Invoke(this, new EventArgs()); } }
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; } } UpdateSquares(); UpdateBoardState(); }
/// <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; } } RebindState(); }
/// <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; } } RebindState(); if (mBoard.IsFinished) { GameFinished?.Invoke(this, new EventArgs()); } }
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)); }
static void Main(string[] args) { // The model and view for the game. OthelloBoard board = new OthelloBoard(); OthelloView view = new OthelloView(); while (true) { // Print the view. Console.WriteLine(); Console.WriteLine(); view.PrintView(Console.Out, board); Console.WriteLine(); Console.WriteLine(); // Print the possible moves. var possMoves = board.GetPossibleMoves(); Console.WriteLine("Possible moves:"); Console.WriteLine(String.Join(", ", possMoves)); // Print the turn indication. Console.WriteLine(); Console.Write("{0}'s turn: ", view.GetPlayerString(board.CurrentPlayer)); // Parse user input and apply their command. string input = Console.ReadLine(); if (input.StartsWith("move ")) { // Parse the move and validate that it is one of the possible moves before applying it. OthelloMove move = view.ParseMove(input.Substring(5)); bool foundMove = false; foreach (var poss in possMoves) { if (poss.Equals(move)) { board.ApplyMove(poss); foundMove = true; break; } } if (!foundMove) { Console.WriteLine("That is not a possible move, please try again."); } } else if (input.StartsWith("undo ")) { // Parse the number of moves to undo and repeatedly undo one move. int undoCount = Convert.ToInt32(input.Substring(5)); while (undoCount > 0 && board.MoveHistory.Count > 0) { board.UndoLastMove(); undoCount--; } } else if (input == "showHistory") { // Show the move history in reverse order. Console.WriteLine("History:"); bool playerIsBlack = board.CurrentPlayer != 1; // if board.CurrentPlayer == 1, then black is CURRENT player, not the most recent player. foreach (var move in board.MoveHistory.Reverse()) { Console.WriteLine("{0}: {1}", move.Player == 1 ? "Black" : "White", move); } } else if (input == "showAdvantage") { Console.WriteLine("Advantage: {0} in favor of {1}", board.CurrentAdvantage.Advantage, board.CurrentAdvantage.Player == 1 ? "Black" : "White"); } } }