A view model that encapsulates the logic of the game of Reversi
Наследование: ViewModelBase
        private void MakeNextMove()
        {
            Move bestMove = new Move()
              {
            Column = -1,
            Row = -1
              };
              int bestScore = int.MinValue;

              // check every valid move for this particular board, then select the one with the best 'score'
              List<Move> moves = ValidMovesForBoard(_viewModel);
              foreach (Move nextMove in moves)
              {
            // clone the current board and make this move
            GameBoardViewModel testBoard = new GameBoardViewModel(_viewModel);
            testBoard.MakeMove(nextMove.Row, nextMove.Column);

            // determine the score for this move
            int scoreForMove = ScoreForBoard(testBoard, 1);

            // pick the best
            if (scoreForMove > bestScore || bestScore == int.MinValue)
            {
              bestScore = scoreForMove;
              bestMove.Row = nextMove.Row;
              bestMove.Column = nextMove.Column;
            }
              }

              if (bestMove.Column != -1 && bestMove.Row != -1)
              {
            _viewModel.MakeMove(bestMove.Row, bestMove.Column);
              }
        }
 public GameBoardViewModel(GameBoardViewModel copy)
 {
     _squares = copy._squares.Select(s => new GameBoardSquareViewModel(s.Row, s.Column, this)
       {
     State = s.State
       }).ToList();
       _nextMove = copy._nextMove;
 }
        public ReversiView()
        {
            this.InitializeComponent();

              var vm = new GameBoardViewModel();
              var comp = new ComputerOpponent(vm, BoardSquareState.WHITE, 5);
              DataContext = vm;
        }
        public ComputerOpponent(GameBoardViewModel viewModel, BoardSquareState computerColor, int maxDepth)
        {
            _maxDepth = maxDepth;
              _computerColor = computerColor;
              _viewModel = viewModel;
              _viewModel.PropertyChanged += GameBoardViewModel_PropertyChanged;

              MakeMoveIfCorrectTurn();
        }
Пример #5
0
        // Computes the score for the given board
        private int ScoreForBoard(GameBoardViewModel board, int depth)
        {
            // if we have reached the maximum search depth, then just compute the score of the current
            // board state
            if (depth >= _maxDepth)
            {
                return(_computerColor == BoardSquareState.WHITE ?
                       board.WhiteScore - board.BlackScore :
                       board.BlackScore - board.WhiteScore);
            }

            int minMax = int.MinValue;

            // check every valid next move for this particular board
            List <Move> moves = ValidMovesForBoard(board);

            foreach (Move nextMove in moves)
            {
                // clone the current board and make the move
                GameBoardViewModel testBoard = new GameBoardViewModel(board);
                testBoard.MakeMove(nextMove.Row, nextMove.Column);

                // compute the score for this board
                int score = ScoreForBoard(testBoard, depth + 1);

                // pick the best score
                if (depth % 2 == 0)
                {
                    if (score > minMax || minMax == int.MinValue)
                    {
                        minMax = score;
                    }
                }
                else
                {
                    if (score < minMax || minMax == int.MinValue)
                    {
                        minMax = score;
                    }
                }
            }

            return(minMax);
        }
Пример #6
0
        // returns an array of valid next moves for the given board
        private List <Move> ValidMovesForBoard(GameBoardViewModel viewModel)
        {
            List <Move> moves = new List <Move>();

            for (int row = 0; row < 8; row++)
            {
                for (int col = 0; col < 8; col++)
                {
                    if (viewModel.IsValidMove(row, col))
                    {
                        moves.Add(new Move()
                        {
                            Row    = row,
                            Column = col
                        });
                    }
                }
            }

            return(moves);
        }
Пример #7
0
 public GameBoardSquareViewModel(int row, int col, GameBoardViewModel parent)
 {
     Column  = col;
     Row     = row;
     _parent = parent;
 }
 public GameBoardSquareViewModel(int row, int col, GameBoardViewModel parent)
 {
     Column = col;
       Row = row;
       _parent = parent;
 }
        // Computes the score for the given board
        private int ScoreForBoard(GameBoardViewModel board, int depth)
        {
            // if we have reached the maximum search depth, then just compute the score of the current
              // board state
              if (depth >= _maxDepth)
              {
            return _computerColor == BoardSquareState.WHITE ?
                                  board.WhiteScore - board.BlackScore :
                                  board.BlackScore - board.WhiteScore;
              }

              int minMax = int.MinValue;

              // check every valid next move for this particular board
              List<Move> moves = ValidMovesForBoard(board);
              foreach (Move nextMove in moves)
              {
            // clone the current board and make the move
            GameBoardViewModel testBoard = new GameBoardViewModel(board);
            testBoard.MakeMove(nextMove.Row, nextMove.Column);

            // compute the score for this board
            int score = ScoreForBoard(testBoard, depth + 1);

            // pick the best score
            if (depth % 2 == 0)
            {
              if (score > minMax || minMax == int.MinValue)
              {
            minMax = score;
              }
            }
            else
            {
              if (score < minMax || minMax == int.MinValue)
              {
            minMax = score;
              }
            }
              }

              return minMax;
        }
        // returns an array of valid next moves for the given board
        private List<Move> ValidMovesForBoard(GameBoardViewModel viewModel)
        {
            List<Move> moves = new List<Move>();

              for (int row = 0; row < 8; row++)
              {
            for (int col = 0; col < 8; col++)
            {
              if (viewModel.IsValidMove(row, col))
              {
            moves.Add(new Move()
            {
              Row = row,
              Column = col
            });
              }
            }
              }

              return moves;
        }