コード例 #1
0
ファイル: MoveMaker.cs プロジェクト: jfreeman81/CheckersAI
        public void MakeMove(Move move)
        {
            if (IsValidMove(move))
            {
                _board.RemovePiece(move.Piece);
                int newRow = move.Piece.Row;
                int newCol = move.Piece.Col;
                foreach (MoveDirection direction in move.Direction)
                {
                    newRow += MoveUtil.GetRowMoveAmountByColor(move.Piece.Owner, direction);
                    newCol += MoveUtil.GetColMoveAmount(direction);

                    CheckerPiece pieceInPosition = _board.GetPiece(newRow, newCol);
                    if (pieceInPosition != null)
                    {
                        _board.RemovePiece(pieceInPosition);
                        newRow += MoveUtil.GetRowMoveAmountByColor(move.Piece.Owner, direction);
                        newCol += MoveUtil.GetColMoveAmount(direction);
                    }
                }

                var pieceAfterMove = new CheckerPiece(move.Piece);
                pieceAfterMove.Row = newRow;
                pieceAfterMove.Col = newCol;
                _board.AddPiece(pieceAfterMove);
            }
            else
            {
                throw new ArgumentException("Invalid Move");
            }
        }
コード例 #2
0
        private void MakeMove(MoveRequest request)
        {
            char       player     = request.CurrentUser;
            List <int> moves      = new List <int>(request.Moves);
            List <int> savedMoves = new List <int>(request.Moves);

            bool isJump = false;

            while (moves.Count > 1)
            {
                int a = moves[0];
                int b = moves[1];
                int c = 0;
                if (moves.Count >= 3)
                {
                    c = moves[2];
                }

                if (TheCheckerBoard.anyJumps())
                {
                    //if there is a jump, they have to take it
                    isJump = true;
                }
                //If a and b are neighbors and only two inputs, normal move
                if (TheCheckerBoard.areNeigbors(a, b) && (moves.Count == 2))
                {
                    //normal move

                    if (isJump)
                    {
                        //throw error
                        Console.WriteLine("Required Jump! Re-Enter Your Move");
                        var RawInput = Console.ReadLine();
                        InputControl.UserInput = new MoveRequest();
                        var UsrInput = InputControl.ParseInput(RawInput);
                        MakeMove(UsrInput);
                        return;
                    }
                    break;
                }


                //See if a and b can jump

                if (TheCheckerBoard.isItAJump(a, b, c))
                {
                    var x = moves[0];
                    moves.Remove(b);
                    moves.Remove(c);
                    if (moves.Count < 2)
                    {
                        //ALL jumps correct
                        break;
                    }
                }
                else if (TheCheckerBoard.isItAJump(a, b))
                {
                    var x = moves[0];
                    moves.Remove(b);
                    if (moves.Count < 2)
                    {
                        //ALL jumps correct
                        break;
                    }
                }
                else
                {
                    //Invalid jump.Force new input
                    Console.WriteLine("Invalid Jump! Re-Enter Your Move");
                    var input = Console.ReadLine();
                    //request = null;
                    MakeMove(InputControl.ParseInput(input));
                    return;
                }
            }

            //Make the moves using savedMoves
            for (int i = 0; i < request.Moves.Count - 1; i++)
            {
                try {
                    TheCheckerBoard.MovePiece(player, request.Moves[i], request.Moves[i + 1]);
                }catch (Exception e) {
                    Console.WriteLine("Invalid Input! Re-Enter Your Move");
                    var input = Console.ReadLine();
                    MakeMove(InputControl.ParseInput(input));
                    return;
                }

                if (isJump)
                {
                    //Find shared neigbor, delete it
                    List <int?> aList = new List <int?>();
                    List <int?> bList = new List <int?>();
                    aList = TheCheckerBoard.getNeighbors(request.Moves[i]);
                    bList = TheCheckerBoard.getNeighbors(request.Moves[i + 1]);
                    foreach (int?k in aList)
                    {
                        foreach (int?j in bList)
                        {
                            if (k == j)
                            {
                                TheCheckerBoard.RemovePiece((int)k);
                            }
                        }
                    }
                }
            }
            //Clears out cached moves
            request.Moves.Clear();
            moves      = null;
            savedMoves = null;
            request    = null;
            TheCheckerBoard.player1 = !TheCheckerBoard.player1;
        }
コード例 #3
0
        public static void ProcessMove(MoveRequest Request = null, CheckerBoard TheBoard = null)
        {
            // Build Test Data When Params are Null
            if (Request == null)
            {
                Request = new MoveRequest {
                    RawUserInput = "b,1,2,3,4",
                    CurrentUser  = '******'
                };
                for (int i = 1; i < 5; i++)
                {
                    Request.Moves.Add(i);
                }
                Debug.WriteLine(Request);
            }
            if (TheBoard == null)
            {
                TheBoard = new CheckerBoard();
                Debug.WriteLine(TheBoard);
                TheBoard.MovePiece('b', 12, 16);
                TheBoard.MovePiece('w', 24, 20);
                TheBoard.MovePiece('b', 11, 15);
                TheBoard.MovePiece('w', 23, 19);
                TheBoard.MovePiece('b', 10, 14);
                TheBoard.MovePiece('w', 22, 18);
                TheBoard.MovePiece('b', 9, 13);
                TheBoard.MovePiece('w', 21, 17);
                TheBoard.RemovePiece(32);
                TheBoard.RemovePiece(29);
                Debug.WriteLine(TheBoard);
            }

            var  BoardArray    = TheBoard.Board;      // Getting a copy of the array
            bool CurrentPlayer = TheBoard.whosTurn(); // Black == false;  White == True
            int  PieceToMove   = Request.Moves[0];    // refrence of whats being moved

            // Define what a side is
            int[] RightSideNums  = { 1, 5, 13, 21, 29 };
            int[] LeftSideNums   = { 4, 8, 12, 20, 28 };
            int[] TopSideNums    = { 29, 30, 31, 32 };
            int[] BottomSideNums = { 1, 2, 3, 4 };

            // Vars to tell what side a piece is on
            bool RightSide  = IsSide(PieceToMove, RightSideNums);
            bool LeftSide   = IsSide(PieceToMove, LeftSideNums);
            bool TopSide    = IsSide(PieceToMove, TopSideNums);
            bool BottomSide = IsSide(PieceToMove, BottomSideNums);

            // Group Data Into Lists
            List <Piece> BlackPieces = GetSpaces(false, TheBoard);
            List <Piece> WhitePieces = GetSpaces(true, TheBoard);
            List <Piece> AllPieces   = new List <Piece>(BlackPieces); AllPieces.AddRange(WhitePieces);

            //List<Piece> MovesToMake = new List<Piece>(); // Currently Empty On Instatiation

            // Reset Global vars
            numOfBlackPieces = BlackPieces.Count;
            numOfWhitePieces = WhitePieces.Count;

            // Logic
            // Start itterating through the pieces and find all jumps for any given piece
            // Need to make a move object that holds all possible moves for said piece
        }