Implementing JButton in this circumstance.
Hi all; I am new to Java, and definitely new to Swing. Basically, I am working on a University assignment that involves creating a game of Higher or Lower (the card game). Very simple I know. In the below code, I am taking input at the command line to choose the outcome of the conditionals. My question is, how can I do this using JButtons. So far, I have semi designed the UI in another class, the class also is holding the ActionListeners for the UI objects. I have also instantiated (think that the right term) an instance of each class in each others class so that they can "talk". Thanks in advance.
Code:
import java.util.Scanner;
public class CardGames
{
private ShowGui gui;
private PackOfCards cardPack;
//private CardDisplay display;
private Scanner scan;
private Card card;
private Card cardTwo;
private int winCounter;
public CardGames(ShowGui guiInstance)
{
this.gui = guiInstance;
cardPack = new PackOfCards();
scan = new Scanner(System.in);
winCounter = 0;
}
public void higherOrLower()
{
//Shuffle the pack.
cardPack.shuffleCards(2000);
//Prompt the user. See chart for Logic structure.
while (true)
{
System.out.println("*********************************************");
//Get the next card.
cardTwo = cardPack.getNextCard();
//Print the name of the first card.
card = cardPack.getNextCard();
System.out.println("The first card is:");
System.out.println(card.getName());
//display.displayCard(card);
gui.displayCard(card);
System.out.println("Higher or Lower?");
int userInt = scan.nextInt();
if (userInt == 0)
{
System.out.println("You selected Higher;");
if (card.getValue() < cardTwo.getValue())
{
System.out.println("Well done, the card was "+cardTwo.getName()+"");
winCounter++;
}
else
{
System.out.println("Sorry, that is not correct.");
winCounter--;
}
}
else if (userInt == 1)
{
System.out.println("You selected Lower;");
if(card.getValue() > cardTwo.getValue())
{
System.out.println("Well done, the card was "+cardTwo.getName()+"");
winCounter++;
}
else
{
System.out.println("Sorry, that is not correct.");
winCounter--;
}
}
else if (userInt == 2)
{
System.out.println("You have chosen to view your winnings.");
if (winCounter <= 0)
{
System.out.println("Sorry, your score is "+winCounter+". This does not qualify for a prize.");
}
else if (winCounter >=1 && winCounter <=3)
{
System.out.println("You have won a Tier 1 prize. Your score is "+winCounter+"");
}
else if (winCounter >=4 && winCounter <=7)
{
System.out.println("You have won a Tier 2 prize. Your score is "+winCounter+"");
}
else if (winCounter >=8)
{
System.out.println("You have won a Tier 3 prize. Your score is "+winCounter+"");
}
}
else
{
System.out.println("Please enter a valid option.");
}
}
}
}