AS 2019 TakePiece

From TRCCompSci - AQA Computer Science
Revision as of 12:44, 1 April 2019 by Admin (talk | contribs) (Issues)
Jump to: navigation, search

Issues

I have completed this improvement, and there are many sections to make this work.I think it is unlikely to be asked to do this, but you need to:

  • Add to the MoveRecord Struct (CanTake boolean, TakeRow & TakeColumn)
  • Duplicate ValidJump and create ValidTake
  • Add loads to ListPossibleMoves
  • Changes to MakeMove

Changes to Struct

Find the current MoveRecord struct:

        struct MoveRecord
        {
            public string Piece;
            public int NewRow;
            public int NewColumn;
            public bool CanJump;
        }

Add the following variables to get:

        struct MoveRecord
        {
            public string Piece;
            public int NewRow;
            public int NewColumn;
            public bool CanJump;
            public bool CanTake;
            public int TakeRow;
            public int TakeColumn;
        }

ValidTake

Create the following method by duplicating valid jump, then change to this:

        private static bool ValidTake(string[,] board, int[,] playersPieces, string piece, int newRow, int newColumn)
        {
            bool valid = false;
            string middlePiece = "";
            string player, oppositePiecePlayer, middlePiecePlayer;
            int index, currentRow, currentColumn, middlePieceRow, middlePieceColumn;
            player = piece[0].ToString().ToLower();
            index = Convert.ToInt32(piece.Substring(1));
            if (player == "a")
            {
                oppositePiecePlayer = "b";
            }
            else
            {
                oppositePiecePlayer = "a";
            }
            if (newRow >= 0 && newRow < BoardSize &&
                newColumn >= 0 && newColumn < BoardSize)
            {
                if (board[newRow, newColumn] == Space)
                {
                    currentRow = playersPieces[index, Row];
                    currentColumn = playersPieces[index, Column];
                    middlePieceRow = (currentRow + newRow) / 2;
                    middlePieceColumn = (currentColumn + newColumn) / 2;
                    middlePiece = board[middlePieceRow, middlePieceColumn];
                    middlePiecePlayer = middlePiece[0].ToString().ToLower();
                    if (middlePiecePlayer == oppositePiecePlayer && middlePiecePlayer != " ") //change this
                    {
                        valid = true;
                    }
                }
            }
            return valid;
        }

List Possible Moves

Make Move

Change the if statement:

                if (jumping)
                {
                    middlePieceRow = (currentRow + newRow) / 2;
                    middlePieceColumn = (currentColumn + newColumn) / 2;
                    middlePiece = board[middlePieceRow, middlePieceColumn];
                    Console.WriteLine("jumped over " + middlePiece);
                }

to this:

                if (taking)
                {
                    middlePieceRow = (currentRow + newRow) / 2;
                    middlePieceColumn = (currentColumn + newColumn) / 2;
                    middlePiece = board[middlePieceRow, middlePieceColumn];
                    board[middlePieceRow, middlePieceColumn]=" ";
                    opponentsPieces[Convert.ToInt32(middlePiece.Substring(1)), Row] = -1;
                    opponentsPieces[Convert.ToInt32(middlePiece.Substring(1)), Column] = -1;
                    Console.WriteLine("Taken " + middlePiece);
                }
                else if (jumping)
                {
                    middlePieceRow = (currentRow + newRow) / 2;
                    middlePieceColumn = (currentColumn + newColumn) / 2;
                    middlePiece = board[middlePieceRow, middlePieceColumn];
                    Console.WriteLine("jumped over " + middlePiece);
                }