Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to Split a deck of cards in half?
    primarykey
    data
    text
    <p>I'm trying to create a really simple game of war. User and computer draws 1 card per turn, they either win, tie, or lose. Game is over when cards are out. I have 4 classes. My war class I was mostly just testing out if things were working for the deck. What I need to do is split the deck in half so each player draws 1 card each turn (A total of 26 by the end of the game) and I don't know how to do that. I'm guessing I need a for loop, but other than that I don't know how I would give each player a card. Ignore the user input in Class War, like I said, I was just testing things out. I have 3 classes displayed here, my last one is a JApplet which isn't need here so I haven't included it.</p> <p><strong>EDIT</strong>: Thanks for the help guys. I have one last problem and then I should be good. I want to display Card.toString inside my JApplet class but when I call Card card = new Card(); it refuses to work. I'm guessing this is because of the constructor. How would I go about getting that toString to display, or bypass the constructor.</p> <p><strong>Edit</strong>: Figure everything out, Thanks for the help guys.</p> <pre><code>import java.util.ArrayList; import java.util.Random; public class Card { private int suit, number; String [] suits = {"Heart" , "Diamond" , "Spade" , "Club"}; //suits String [] numbers = { "2" , "3" , "4" , "5" , "6" , "7" , "8" , "9" , "10" , "Jack" , "Queen" , "King" , "Ace" }; //card values String card = ""; public Card(int suits, int numbers) { suit = suits; number = numbers; } public String toString() //displays card suit/value { String finalCard = numbers[number] + " 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.util.Scanner; public class War { public static void main(String[] args) { Scanner scan = new Scanner(System.in); Card C; FullDeck hand1 = new FullDeck(); FullDeck hand2 = new FullDeck(); System.out.println("Enter number of cards to be dealt: "); int numberCards = scan.nextInt(); System.out.println("Cards drawn: "); for (int i = 0; i &lt; numberCards; i++) { C = hand1.drawRandomCard(); System.out.println(C.toString()); } } } </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.
    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