Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

So I have 3 classes here, I just want to know how to remove the large spaces aft

ID: 3838767 • Letter: S

Question

So I have 3 classes here, I just want to know how to remove the large spaces after "Press any key to start the game" also, in the class MemoryGameBoard (in all of toString method), how can I replace the append/buffer code with something simpler?

import java.util.Scanner;
public class PlayMemoryGame {
   private static Scanner kb = new Scanner(System.in);
   private static MemoryGameBoard game;
   public static void main(String[] args) {
       int bestScore = 500;
       int turnCount;
       int pairsLeft;
       System.out.print("CSC");
       System.out.print(" Welcome to the Memory Game."
               + " The goal is to find all the matching pairs in as few turns as possible."
               + " At each turn select two different positions on the board to see if they match. ");
       String response = "yes";
       String answer = " ";
       System.out.println(" ");
       System.out.print("Press any key to start the game.");
       answer = kb.nextLine();

       while (answer.equals(answer)) {
           game = new MemoryGameBoard() ;   
           game.shuffleCards();
           turnCount = 0;
           pairsLeft = (MemoryGameBoard.BOARD_SIZE * MemoryGameBoard.BOARD_SIZE) / 2;

           while(pairsLeft > 0) {
               game.showBoard();
               System.out.println("Where is the first card you wish to see?");
               int row1 = getValidInt(" Row: ", 1, MemoryGameBoard.BOARD_SIZE);
               int col1 = getValidInt(" Col: ", 1, MemoryGameBoard.BOARD_SIZE);

               // this checks if the card is facing up.
               while (game.isFaceUp(row1, col1)) {
                   System.out.println("Card already face up.");
                   row1 = getValidInt(" Row: ", 1, MemoryGameBoard.BOARD_SIZE);
                   col1 = getValidInt(" Col: ", 1, MemoryGameBoard.BOARD_SIZE);
               }
               game.flipCard(row1, col1);

               System.out.println("Where is the second card you wish to see?");
               int row2 = getValidInt(" Row: ", 1, MemoryGameBoard.BOARD_SIZE);
               int col2 = getValidInt(" Col: ", 1, MemoryGameBoard.BOARD_SIZE);

               // this also checks if the card is already facing up.
               while (game.isFaceUp(row2, col2)) {
                   System.out.println("Card already face up.");
                   row2 = getValidInt(" Row: ", 1, MemoryGameBoard.BOARD_SIZE);
                   col2 = getValidInt(" Col: ", 1, MemoryGameBoard.BOARD_SIZE);
               }
               kb.nextLine();
               game.flipCard(row2, col2);
               game.showBoard();

               if (game.cardsMatch(row1, col1, row2, col2)) {
                   System.out.println("You found a match");

                   pairsLeft -= 1;
               }
else {
                   System.out.println("Sorry, no match");
                   game.flipCard(row1, col1);
                   game.flipCard(row2, col2);
               }
               turnCount += 1;
System.out.print("Press any key to continue.");
               kb.nextLine();
           }
while (pairsLeft != 0);
           System.out.println("Congrats, you found all the matching pairs!");
           System.out.println("You did it in " + turnCount + " turns.");
           if (turnCount < bestScore) {
                bestScore = turnCount;
                System.out.println("That is your best score so far!");
           }

           System.out.print("Do you want to begin another game? ");
           response = kb.next().toLowerCase();
       }
       while (response.equals("yes"));
       System.out.println("Goodbye");
   }
   // This is when the program is getting a number from the user, then it returns it.

   public static int getValidInt(String prompt, int min, int max) {
       String num = "";
       int n = 0;
       while ((n < min) || (n > max))
       {
           System.out.print(prompt);
           num = kb.next();
           n = Integer.parseInt(num);
           if ((n < min) || (n > max))
               System.out.println("error : " + num + " is not in the valid range (1...4)");
       }

       return n;
   }

}

import java.util.Random;
public class MemoryGameBoard {
public static final int BOARD_SIZE = 4;
private static final int NUM_SWAPS = 1000;
private Card[][] gameBoard;
public MemoryGameBoard() {
gameBoard = new Card[BOARD_SIZE + 1][BOARD_SIZE + 1];
initializeCards();
}
private void initializeCards() {
int count = 1;
for (int i = 1; i <= BOARD_SIZE; i++) {
for (int j = 1; j <= BOARD_SIZE; j++) {
if (count > ((BOARD_SIZE * BOARD_SIZE) / 2))
count = 1;
gameBoard[i][j] = new Card(count);
count += 1;
}
}
}
public void shuffleCards() {
turnCardsFaceDown();
//This is swapping the cards
Random r = new Random();
for (int i = 0; i < NUM_SWAPS; i++) {
int row1 = r.nextInt(BOARD_SIZE) + 1;
int col1 = r.nextInt(BOARD_SIZE) + 1;
int row2 = r.nextInt(BOARD_SIZE) + 1;
int col2 = r.nextInt(BOARD_SIZE) + 1;
Card temp = gameBoard[row1][col1];
gameBoard[row1][col1] = gameBoard[row2][col2];
gameBoard[row2][col2] = temp;
}
}
private void turnCardsFaceDown() {
for (int i = 1; i <= BOARD_SIZE; i++) {
for (int j = 1; j <= BOARD_SIZE; j++) {
gameBoard[i][j].setFaceDown();
}
}
}
public void showBoard() {
hideBoard();
System.out.println(this);
}
private void hideBoard() {
for (int i = 0; i < 25; i++)
System.out.println();
}
public boolean cardsMatch(int row1, int col1, int row2, int col2) {
return gameBoard[row1][col1].equals(gameBoard[row2][col2]);
}

public boolean isFaceUp(int row, int col) {
return gameBoard[row][col].isFaceUp();
}
public void flipCard(int row, int col) {
gameBoard[row][col].flipCard();
}

public String toString() {
StringBuffer board = new StringBuffer();
board.append(" ");
for (int i = 1; i <= BOARD_SIZE; i++)
board.append(i + " ");
board.append(" ");
board.append(" =");
for (int i = 1; i <= BOARD_SIZE; i++)
board.append("==");
board.append(" ");

for (int i = 1; i <= BOARD_SIZE; i++) {
board.append("| ");
for (int j = 1; j <= BOARD_SIZE; j++) {
board.append(gameBoard[i][j] + " ");
}
board.append("| ");
}
board.append(" =");
for (int i = 1; i <= BOARD_SIZE; i++)
board.append("==");
board.append(" ");

return board.toString();
}
public void displayGameBoardValues() {
for (int i = 1; i <= BOARD_SIZE; i++) {
for (int j = 1; j <= BOARD_SIZE; j++)
System.out.print(gameBoard[i][j].getValue() + " ");
System.out.println();
}

}
}

public class Card {
private int value;
private boolean isFaceUp;

public Card(int initValue) {
this.value = initValue;
this.isFaceUp = false;
}
public int getValue() {
return this.value;
}

public boolean isFaceUp() {
return isFaceUp;
}

public void flipCard() {
if (isFaceUp())
setFaceDown();
else
setFaceUp();
}

public void setFaceUp() {
this.isFaceUp = true;
}

public void setFaceDown() {
this.isFaceUp = false;
}

public boolean equals(Card otherCard) {
return (getValue() == otherCard.getValue());
}

public String toString() {
return (isFaceUp ? (getValue() + "") : "*");
}
}

Explanation / Answer

public String toString() {
       String board = " ";
       for (int i = 1; i <= BOARD_SIZE; i++)
           board += i + " ";
       board += " ";
       board += " =";
       for (int i = 1; i <= BOARD_SIZE; i++)
           board += "==";
       board += " ";

       for (int i = 1; i <= BOARD_SIZE; i++) {
           board += "| ";
           for (int j = 1; j <= BOARD_SIZE; j++) {
               board += gameBoard[i][j] + " ";
           }
           board += "| ";
       }
       board += " =";
       for (int i = 1; i <= BOARD_SIZE; i++)
           board += "==";
       board += " ";

       return board;
   }

This is the toString method class MemoryGameBoard. I have changed it from StringBuffer to String and used simpe '+' opperator to concat Strings.

When the game starts I couldnot find large spaces after "Press any key to start the game". Please re validate.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote