Note that there are some explanatory texts on larger screens.

plurals
  1. PONeed help getting int from array
    text
    copied!<p>I've got a war card game. Both users draw 1 card each which will be compared against each other to see who has the higher hand. I have most of the program done but I can't figure out how to get the card value over to WarUI. Every time I use my get method it displays a 1 so I must be doing something wrong. The array numbers is int value between 1-13 and I use an if statement to change 1 to ace, 11 to jack, ect. I'm having trouble with getValue() in card class. How do I go about this?</p> <p>Edit: How do I get the user card value's so that I can compare them against each other?</p> <pre><code>import java.util.ArrayList; import java.util.Random; public class Card { String finalCard = ""; int suit, number,number2; static String [] suits = {"Heart" , "Diamond" , "Spade" , "Club"}; //suits //static String [] numbers2 = { "Ace", "2" , "3" , "4" , "5" , "6" , "7" , "8" , "9" , "10" , "Jack" , "Queen" , "King" }; //card values static int [] numbers = { 1, 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 }; //card values String card = ""; public Card() { } public Card(int suits, int numbers) { suit = suits; number = numbers; } public int getValue() { // cant get this to work int well = numbers[number]; return well; } public int getSuit() { return suit; } public String toString() { String fakeValue = ""; if (numbers[number] == 1) { fakeValue = "Ace"; } else if (numbers[number] == 11) { fakeValue = "Jack"; } else if (numbers[number] == 12) { fakeValue = "Queen"; } else if (numbers[number] == 13) { fakeValue = "King"; } else fakeValue = Integer.toString(numbers[number]); String finalCard = fakeValue + " of " + suits[suit]; return finalCard; } } import java.util.ArrayList; import java.util.Random; public class FullDeck { private ArrayList&lt;Card&gt; cards = new ArrayList&lt;Card&gt;();//card array list public FullDeck() { for(int a =0; a&lt;=3; a++) //loops through suits { for(int b =0; b&lt;=12;b++) //loops through values { cards.add(new Card(a,b)); //creates adds cards to list } } } public Card drawRandomCard() { Random generator = new Random(); //picks random card int index = generator.nextInt(cards.size()); return cards.remove(index); //removes card from list } public String toString() { String result = "Cards remaining in deck: " + cards; //not currently used return result; } } import java.applet.Applet; import java.awt.Container; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Scanner; import javax.swing.*; public class WarUI extends JApplet implements ActionListener { JTextArea displayLabel = new JTextArea(""); //sets label to display message JTextArea displayLabel2 = new JTextArea(""); //sets label to display message JButton runButton = new JButton("Run"); //button that starts program Container con = getContentPane(); //gets container Card player1; Card player2; FullDeck hand1 = new FullDeck(); Card card = new Card(); public void init() { con.setLayout(new FlowLayout());//sets flowlayout con.add(new JLabel()); //jlabel container con.add(runButton); //run button container con.add(displayLabel); //display label container con.add(displayLabel2); //display label container runButton.addActionListener(this);//looks to see if run is clicked } public void actionPerformed(ActionEvent e) { for (int i = 0; i &lt; 1; i++) { player1= hand1.drawRandomCard(); //draws cards for player 1 player1.toString(); } for (int i = 0; i &lt; 1; i++) { player2= hand1.drawRandomCard(); //draws cards for player 2 player2.toString(); } displayLabel.setText(player1.toString() + "\n" + player2.toString()+ card.getValue()); //displays both players values/suits } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload