0
votes

I managed to figure out how to print the array for my connect four program but I cannot get the board to update with my code, I looked at it and ran it the code works in theory but however the array won't take the new inputs

Ive tried running it through with a for loop but that turned out wrong and I was thinking about putting the drop method in the print board method but I feel that that would result in an error

public class Connect4 {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    // DON'T MODIFY THE MAIN METHOD UNLESS FOR DEBUGGING

 //MAKE SURE YOU GET RID OF YOUR MODIFICATIONS HERE BEFORE  

SUBMISSION

String[][] board = createEmptyBoard();

Scanner input = new Scanner(System.in);

boolean bl = true;

printPattern(board);

while(bl) {

    int player1 = 1 , player2 = 2 ,  userInput;



 System.out.println("Please drop a RED disk at the column between 0 

and 6:");
    userInput = input.nextInt();
    dropDisk(board, userInput , player1);
    printPattern(board);

   System.out.println("Please drop a YELLOW disk at the column  

between 0 and 6:");
   userInput  = input.nextInt();
   dropDisk(board, userInput , player2);
   printPattern(board);  


    String win = checkWinner(board);
    /*
    Write code to announce  if there is  winner and end the game
    */


} 
 }
 public static String[][] createEmptyBoard() {
   /* This method prints the first empty pattern for the game
   DON'T MODIFY THIS METHOD
   */

    String[][] f = new String[7][15];
    for (int i =0;i<f.length;i++) {
          for (int j =0;j<f[i].length;j++) {

             if (j% 2 == 0) f[i][j] ="|";
             else f[i][j] = " ";
             if (i==6) f[i][j]= "-";
         }
       }
    return f;

} // end of createEmptyBoard 


  public static void printPattern(String[][] brd) {
  for (int i = 0; i < 7; i++){

System.out.println(brd[i][0] + brd[i][1]+ brd[i][2]+ brd[i][3]+ 

brd[i][4]+ brd[i][5]+ brd[i][6]+ brd[i][7]+ brd[i][8]+ brd[i][9]+   

brd[i][10]+ brd[i][11]+ brd[i][12]+ brd[i][13]+ brd[i][14]);

  }

  } // end of printPattern

  public static void dropDisk(String[][] brd, int position, int   

  player) {

       if (player == 1){

            brd[6][position] = "R";

           if(brd[6][position] == "R"){

           brd[6][position]  =  brd[6 - 1][position];

       } 

       }

       else if (player == 2){

           brd[6][position] = "Y";

           if(brd[6][position] == "Y"){

           brd[6][position]  =  brd[6 - 1][position];

       }

       }
/*Write your code to drop the disk at the position the user entered 
   depending on which player*/ 

} // end of dropDisk
2

2 Answers

0
votes

The logic of dropDisk seems to be not finished yet. It sets the brd[6][position] to R or Y, just to immediately after that set it to the current value of brd[5][position].

And this should always be null.

-2
votes

In Java, objects are passed into methods by value. This means that when you pass a parameter into a function, the JVM makes a copy of that object which can be modified in the method.

In this case, when you pass brd into dropDisk, it is copied, and you make changes to the copy inside dropDisk. But once dropDisk ends, that copy is discarded. No changes are made to the board from your main method. This is because your board is an array of Strings, and Strings are immutable, meaning that they cannot be changed after instantiation.

If you wanted the board from your main method to update, consider returning brd in dropDisk.