AS 2019 DisplayNoOfMovesByLiz

From TRCCompSci - AQA Computer Science
Jump to: navigation, search

A more efficient way to add a moves-made counter for each player.

Where

Like Wayne's solution, the best place to put this is in the below if statement, found in the Game method:

  if (nextPlayer == "a")
                {
                    ListPossibleMoves(board, A, nextPlayer, listOfMoves);
                    if (!ListEmpty(listOfMoves))
                    {
                        pieceIndex = SelectMove(listOfMoves);
                        MakeMove(board, A, B, listOfMoves, pieceIndex);
                        nextPlayer = SwapPlayer(nextPlayer);
                    }
                    else
                    {
                        gameEnd = true;
                    }
                }

This controls Player A's turn, displaying the list of possible moves and checking if moves are available. There is an identical part for Player B that is directly below this in the program code.

Solution

Wayne's solution was to use the line below which required initialising new variables and incrementing them with each player's turn.

Console.WriteLine("Player A as made " + amoves + " moves")
AS2019NoOfMovesArray.png


However, there is an array that already stores this value, which can be seen at the top of the screen at the start of each turn.

Therefore, it's only a matter of finding this array and knowing how to call it. Fortunately for us, this array is already local to the Game method, defined on the first two lines of it - int[,]A and int[,]B.

This means we can simply call this first value in order to have the number of turns for that player:


  if (nextPlayer == "a")
                {
                    Console.WriteLine("Player A has made " + Convert.ToString(A[0, 0]) + " moves");
                    ListPossibleMoves(board, A, nextPlayer, listOfMoves);
                    if (!ListEmpty(listOfMoves))
                    {
                        pieceIndex = SelectMove(listOfMoves);
                        MakeMove(board, A, B, listOfMoves, pieceIndex);
                        nextPlayer = SwapPlayer(nextPlayer);
                    }
                    else
                    {
                        gameEnd = true;
                    }
                }

The same can be done for Player B - remember to change the letter when you copy and paste it into B's section.

Just The Line

Console.WriteLine("Player A has made " + Convert.ToString(A[0, 0]) + " moves");