예제 #1
0
        public void computerMove(PieceColour colour)
        {
            //TODO: Figure out why computer modifies the entire board when checking states, then finally moves a piece that's not theirs.

            gameState.Clear();
            gameState = Board.board;
            StateNode sn = new StateNode();

            Tuple <char, int, char, int> move = sn.miniMax(gameState, colour);


            Point pieceToMove      = new Point(move.Item1, move.Item2);
            Point positionToMoveTo = new Point(move.Item3, move.Item4);

            Logic.ExecuteMove(pieceToMove, positionToMoveTo);

            Console.WriteLine($"Moved {Board.board[positionToMoveTo].type} from {pieceToMove.X}{pieceToMove.Y} to {positionToMoveTo.X}{positionToMoveTo.Y}");
        }
예제 #2
0
        private void GenerateStates(StateNode root, int depth, PieceColour startingPlayer)
        {
            var ownPieces = root.gs.state.Where(o => o.Value.colour == startingPlayer).ToList();

            List <Point> possibleMoves = root.gs.state.Keys.ToList();

            foreach (var ownPieceKV in ownPieces)
            {
                foreach (var possiblePosition in possibleMoves)
                {
                    if (depth > 0 && logic.CheckMove(startingPlayer, ownPieceKV.Key, possiblePosition, root.gs))
                    {
                        if (IsTerminalState(root.gs, startingPlayer) == false)
                        {
                            logic.ExecuteMove(ownPieceKV.Key, possiblePosition, root.gs);

                            if (!logic.KingCheck(startingPlayer, root.gs))
                            {
                                var copiedGameState = root.gs.Copy();
                                copiedGameState.currentPlayer = (startingPlayer == PieceColour.Blue) ? PieceColour.Red : PieceColour.Blue;

                                var newNode = new StateNode();
                                newNode.gs     = copiedGameState;
                                newNode.parent = root;
                                root.children.Add(newNode);

                                newNode.action = Tuple.Create(ownPieceKV.Key.X, ownPieceKV.Key.Y, possiblePosition.X, possiblePosition.Y);

                                GenerateStates(newNode, depth - 1, copiedGameState.currentPlayer);

                                logic.Reverse(root.gs);
                            }
                        }
                    }
                }
            }

            if (root.children.Count == 0)
            {
                root.minimaxValue = Utility(root, root.player);
            }
            else
            {
                if (root.player == PieceColour.Blue)
                {
                    var max = int.MinValue;

                    foreach (var child in root.children)
                    {
                        if (child.minimaxValue > max)
                        {
                            max         = child.minimaxValue;
                            root.action = child.action;
                        }
                    }

                    root.minimaxValue = max;
                }
                else
                {
                    var min = int.MaxValue;

                    foreach (var child in root.children)
                    {
                        if (child.minimaxValue < min)
                        {
                            min         = child.minimaxValue;
                            root.action = child.action;
                        }
                    }

                    root.minimaxValue = min;
                }
            }
        }
예제 #3
0
 private int Utility(StateNode root, PieceColour playerColour)
 {
     return(logic.BoardCheck(player, root.gs));
 }