0

I cannot for the life of me figure out why the intergers that are set as perameters in the methods don't update in the main method after returning as an array. (I am unsure why the code isnt formatting correctly but everything below this is in the program.) I am also unable to call the array in the main method although I return it throught the other methdods.

package finalproject;

import java.util.*;

public class gawem 
{
public static void main (String [] args) 
{
    Scanner sc = new Scanner (System.in);

    //variables that go across methods
    int p1Points = 0;
    int p2Points = 0;
    int warCount = 1;


    //System.out.println("Player 1's card is: " + p1Cards[p1Random] + ". Player 2's card is: " + p2Cards[p2Random]);

    System.out.println("Welcome to the Game of War. The rules are simple: \n- Each player starts with 26 cards \n- Cards are played from each player \n- Whoever "
            + "had the highest card gets a point \n- In the case of the cards being equal, or, a war, players play cards again, winner gets 4 points \n- First player "
            + "to 26 points win \n- Type play to begin");

    String play = sc.next();

    while(!play.equalsIgnoreCase("play")) 
    {
        System.out.println("Try again.");
        play = sc.next();
    }

    //checks that both players have less than 26 cards each, no winner yet
    while(p1Points < 26 || p2Points < 26) 
    {
        int[] draw = drawCards(p1Points, p2Points, warCount);
    }

    //winner
    if(p1Points >= 26) 
    {
        System.out.println("Player 1 wins!");
    }
    else if(p2Points >= 26) 
    {
        System.out.println("Player 2 wins!");
    }

}   //end main

public static int[] drawCards(int p1Points, int p2Points, int warCount) 
{

    Random rand = new Random();
    int[] counter = new int[3];

    //set up the card system
    int[] p1Cards = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
    int[] p2Cards = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};

    int p1Random = rand.nextInt(26);
    int p2Random = rand.nextInt(26);

    Scanner sc2 = new Scanner (System.in);

    System.out.println("Type draw to start the next round.");
    String draw = sc2.next();

    while(!draw.equalsIgnoreCase("draw")) 
    {
        System.out.println("Error try again.");
        draw = sc2.next();
    }
    
    //the normal rounds
    while(draw.equalsIgnoreCase("draw")) 
    {
        System.out.println("Player 1 plays: " + p1Cards[p1Random] + ". Player 2 plays: " + p2Cards[p2Random]);
        if(p1Cards[p1Random] > p2Cards[p2Random]) 
        {
            p1Points = p1Points + 1;
            System.out.println("Player 1 wins the round! Points: " + p1Points);
            break;
        }
        else if(p1Cards[p1Random] < p2Cards[p2Random]) 
        {
            p2Points = p2Points + 1;
            System.out.println("Player 2 wins the round! Points: " + p2Points);
            break;
        }
        else if(p1Cards[p1Random] == p2Cards[p2Random])
        {
            int[] warMethod = War(p1Points, p2Points, warCount);
            System.out.println("Test not in war");
            break;
        }
    }

    counter[0] = p1Points;
    counter[1] = p2Points;
    counter[2] = warCount;
    
    return counter;

}   //end drawCards

public static int[] War(int p1Points, int p2Points, int warCount) 
{
    Random rand = new Random();
    Scanner sc = new Scanner (System.in);
    
    int[] counter = new int[3];

    //set up the card system again
    int[] p1CardsR = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
    int[] p2CardsR = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};

    int p1RandomR = rand.nextInt(26);
    int p2RandomR = rand.nextInt(26);

    System.out.println("War has been declared! Type W A R to begin!");
    String declare = sc.nextLine();

    //the war rounds
    while(declare.equalsIgnoreCase("W A R")) {
        System.out.println("Player 1 plays: " + p1CardsR[p1RandomR] + ". Player 2 plays: " + p2CardsR[p2RandomR]);

        if(p1CardsR[p1RandomR] > p2CardsR[p2RandomR]) 
        {
            p1Points = p1Points + (warCount * 4);
            System.out.println("Player 1 wins the war! Points: " + p1Points);
            break;
        }
        else if(p1CardsR[p1RandomR] < p2CardsR[p2RandomR]) 
        {
            p2Points = p2Points + (warCount * 4);
            System.out.println("Player 2 wins the war! Points: " + p2Points);
            break;
        }
        else if(p1CardsR[p1RandomR] == p2CardsR[p2RandomR])
        {
            warCount = warCount + 1;
            int[] warMethod = War(p1Points, p2Points, warCount);
            break;
        }
        else {
            System.out.println("TEST IN WAR METH");
        }
    }
    counter[0] = p1Points;
    counter[1] = p2Points;
    counter[2] = warCount;
    
    return counter;
}   //end playCards

}   //end class
Sozy
  • 177
  • 9
  • Well ints are passed by values and not by reference, that means that in `drawCards(p1Points, p2Points, warCount)` you copy the values of the 3 variables into the arguments of `drawCards` and the values in the returned array will not update the values of your variables in your main method. – Ackdari Jun 24 '21 at 06:57
  • 2
    Does this answer your question? [Is Java "pass-by-reference" or "pass-by-value"?](https://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value) – maloomeister Jun 24 '21 at 06:57
  • Java is pass-by-value. Your `p1Points` in the main method does not update. You will need to extract the resulting values for `p1Points` and `p2Points` from your returned `int[] draw`. – maloomeister Jun 24 '21 at 06:59

0 Answers0