Note that there are some explanatory texts on larger screens.

plurals
  1. POWar card game Crashes on start
    primarykey
    data
    text
    <p>I'm trying to create a program that plays the card game war(I haven't gotten to the war part yet). Right now I've made a Japplet class and a Deck Class and can figure out why my program isn't working. I don't have any red lines. Right now my Deck class creates the deck and I'm trying to create a string called "card" to bring over to WarUI class to see if it is working, but when I start the applet it crashes.</p> <p><strong>Edit</strong>: Now that that is working, some cards repeat before it hits 52 cards. How would I go about making it so cards don't repeat. I've been trying to search and it seems like array list would work but I don't understand how to use that properly</p> <pre><code>public class FullDeck { String card = ""; public FullDeck() { int[]deck = new int[52]; String [] suits = {"Heart" + "Diamond" + "Spade" + "Club"}; String [] numbers = { "2" + "3" + "4" + "5" + "6" + "7" + "8" + "9" + "10" + "Jack" + "Queen" + "King" + "Ace" }; for (int i = 0; i &lt; deck.length; i++)deck[i]=i; for(int i = 0; i &lt; deck.length; i++) { int index = (int)(Math.random()*deck.length); int temp = deck[i]; deck[i] = deck[index]; deck[index]=temp; } for( int i = 0; i &lt; deck.length; i++) { String suit = suits[deck[i] / 13]; String num = numbers[deck[i] % 13]; card = "Card number " + deck[i] + ": " + num + " of " + suit; } } } } import java.applet.Applet; import java.awt.Container; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; public class WarUI extends JApplet implements ActionListener { FullDeck deck = new FullDeck(); JTextArea displayLabel = new JTextArea("Enter a letter to guess the phrase."); //sets label to display message JTextField inputBox = new JTextField(40); //sets text field JButton runButton = new JButton("Run"); //button that starts program Container con = getContentPane(); //gets container public void init() { con.setLayout(new FlowLayout());//sets flowlayout con.add(new JLabel()); //jlabel container con.add(inputBox); //input box container con.add(runButton); //run button container con.add(displayLabel); //display label container runButton.addActionListener(this);//looks to see if run is clicked inputBox.addActionListener(this);//looks to see if input box is used } public void actionPerformed(ActionEvent e) { displayLabel.setText("You Drew: " + deck.card); //displays hiddenPhrase } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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