コード例 #1
0
        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());
            }
        }
コード例 #2
0
ファイル: OthelloViewModel.cs プロジェクト: b06to587/Project3
        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());
            }
        }
コード例 #3
0
        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());
            }
        }