0

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
Nhami
  • 15
  • 6

2 Answers2

0

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.

Robert Panzer
  • 1,419
  • 12
  • 14
-2

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.

AlterV
  • 301
  • 1
  • 3
  • 10
  • maybe due to `Strings` being immutable – Scary Wombat May 16 '19 at 02:42
  • @ScaryWombat I think it generally wouldn't work for other data types either – AlterV May 16 '19 at 02:43
  • Hi Nhami, Can you please tell me what would be the input for this question "Please drop a RED disk at the column between 0 and 6:" ? I mean what am I supposed to give as an input? Also, in the dropDisk(...) method, you probably should use `if (brd[6][position].equals("R"))`. Same goes for `if (brd[6][position].equals("Y"))`. Not sure if your assignment to the string array actually goes through. – Ashish May 16 '19 at 02:51
  • @AlterV you are correct for primtive arrays, but object arrays would be OK as long as the object itself is muttable – Scary Wombat May 16 '19 at 02:54
  • Objects are not passed by value in Java, the object references are passed by value! This is a huge difference. If you update an object that was passed to a method, the caller will afterwards see the changes. The method will just not be able to make the reference point to a different object. That means brd[0][0]=“R” will be visible to the caller, but brd=null not. – Robert Panzer May 16 '19 at 02:57
  • @ScaryWombat yes you're right, completely forgot to account for other data types. Updated my answer! – AlterV May 16 '19 at 02:57
  • @Ashish Hey user input would be the numbers 0 - 6 depending where you wanna drop, I will try out the .equals thanks! – Nhami May 16 '19 at 02:59
  • @AlterV nvm. I can change the method just not the. main I tried to but returning it does nothing – Nhami May 16 '19 at 04:08
  • @Nhami Did you set the board in main equal to the return value? – AlterV May 16 '19 at 04:24
  • @AlterV Appears I didn't have to but the board takes the inputs but the board won't put them between the bars it works I just need to find out how – Nhami May 16 '19 at 04:27