Reputation: 115
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?
Edit: How do I get the user card value's so that I can compare them against each other?
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<Card> cards = new ArrayList<Card>();//card array list
public FullDeck()
{
for(int a =0; a<=3; a++) //loops through suits
{
for(int b =0; b<=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 < 1; i++) {
player1= hand1.drawRandomCard(); //draws cards for player 1
player1.toString();
}
for (int i = 0; i < 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
}
}
Upvotes: 1
Views: 97
Reputation: 2895
You have not initilise your number
by default whose value is 0 (instance member) so int well = numbers[number];
is giving first element which is 1
You can modify your method like following way and can get the value
public int getValue(int position) {
int well = numbers[position];
return well;
}
Upvotes: 1
Reputation: 2445
Whenever you are creating a Card, Card() constructor is called. It sets your instance variable number value to 0.
public int getValue() { // cant get this to work
int well = numbers[number];
return well;
}
In this method number = 0. getValue() returns numbers[0] which is 1. Instead create a card using argument constructor.
public Card(int suits, int numbers)
{
suit = suits;
number = numbers;
}
Example :
Card card = new Card(2,2);
Upvotes: 1