Can anybody help me with this? **It is programmed using NetBeans IDE 8.0.2** Int
ID: 3769797 • Letter: C
Question
Can anybody help me with this? **It is programmed using NetBeans IDE 8.0.2**
Introduction
This assignment is meant to give you practice in designing and implementing a simple visual application in Java, as well as designing a separate support class for that application.
In this assignment you are to implement two classes related to a card game: One called CardsApp that implements the visual application, and another called Hand that stores the card values and implements the logic for the game.
Game Structure and Rules
Initially, the player is dealt 3 cards at random. In this game, we don’t care about anything but the suit of each card (C, D, H, S).
The goal of the player is to have in their hand all 3 cards in the same suit, at which point the game is over.
At each turn, the player discards one of the cards in their hand. That card is then replaced by another randomly chosen card.
The “twist” in this game (stolen from the game “sabacc” in the Star Wars universe) is that all cards in a hand can randomly change at any time, turning a good hand into a bad one.
Specifically, each turn there is a 20% chance that the entire hand will be randomized, effectively dealing the player a new hand.
Visual Application Requirements
I have provided a very basic “skeleton” version of the CardsApp visual application on my web page to act as a starting point. The exact design of the application is up to you, but you do have the following requirements:
Required Visual Components
Your visual application (CardsApp) is required to contain the following components:
Three buttons (labelled 1 – 3) that let the user choose which card they wish to discard.
A textfield that displays the current hand, as a string of three characters.
For example:
-Each time a button is, pressed, the corresponding card is replaced by another random card, and the hand is redisplayed.
-If the hand is randomized, a message should be displayed, and the new hand displayed (possibly using a JOptionPane):
-This continues until all three cards are the same suit, at which time a message should be displayed:
Layouts and Appearance
Your application does not have to look exactly like the one above (in fact, I encourage you to come up with something that looks better), as long as it has the basic functionality described above. You are required, however, to use the Java Layout classes to develop a good “look and feel” for your application, and are required to use more than one panel in your visual application.
Components and Arrays
Since your application will contain three buttons that more or less look and act the same way, you might consider (but are not required to) use an array to store and manipulate the buttons.
Requirements for the Hand class
The Hand class will act as a support class that contains all of the game logic for the game. In other words, it will do the following:
Keeping track of the three cards currently in the hand.
Generating new random cards to either fill in for a discarded card, or for generating and entire new hand.
Determining when to randomize the hand.
Determining when the game is over (that is, when all three cards are the same suit).
Note that I am not specifying any particular constructors or methods for the Hand class. It is up to you to determine what methods the Hand class should have. This is the main design decision for this assignment.
Separation of UI from Logic
The main requirement of your program is that it separate the user interface of the CardsApp class from the game logic of the Hand class. The CardsApp class will contain an instance of a Hand class as a member variable.
This means that the CardsApp application should not keep track of or determine anything related to the hand, including:
The list of cards
Generating random cards
Determining when the game is over
These things are to be done by the Hand class, which provide methods and constructors for anything the CardsApp class needs to do. The exact methods you provide are up to you, but you might consider providing the following methods:
A method called when the player discards a card.
A method called after each discard which (possibly) randomizes the cards (this decision is up to the Hand class).
A method that returns the list of current cards for display by the application (possibly as a single string).
A method that determines whether the game is over or not.
Again, determining what these methods are, and maintaining the separation between user interface and game logic is one of the major design decisions you will make on this assignment, and will be an important part of the grading.
Of course, you are also required to document both your Hand and CardApp classes. However, since you will be designing your own methods for the Hand class, it is particularly important that you document it well. Specifically:
Give an overall description of the class itself at the beginning of the class.
Describe what each member variable represents.
Document each constructor and method in the class, describing:
What it does in terms of the state of the hand.
How it is used in the context of an application that plays this game.
package cardsapp; /** * * @author James */ import javax.swing.*; import java.awt.*; import java.awt.event.*; public class CardsApp extends JFrame implements ActionListener { //Creates a main panel for the user to see the game JPanel mainPanel = new JPanel(new BorderLayout()); //Creates new buttons for each position of the card private JButton LeftButton, MiddleButton, RightButton; //Creates a Text Field private JTextField CurrentHand = new JTextField(3); //Help the user see what their current hand is private JLabel Status = new JLabel("Your Current Hand is:", JLabel.CENTER); public CardsApp() { //Displays Buttons in a Grid Format JPanel ButtonPanel = new JPanel(new GridLayout(1,3)); //Presents each button for the user to click LeftButton = new JButton("Card 1"); MiddleButton = new JButton("Card 2"); RightButton = new JButton("Card 3"); //Recognizes when the button is pressed LeftButton.addActionListener(this); MiddleButton.addActionListener(this); RightButton.addActionListener(this); //Adds the buttons to the Button Panel ButtonPanel.add(LeftButton); ButtonPanel.add(MiddleButton); ButtonPanel.add(RightButton); //Makes the programs content visible on the main panel setSize(300,100); getContentPane().add(mainPanel); mainPanel.add(Status, BorderLayout.NORTH); mainPanel.add(CurrentHand, BorderLayout.CENTER); mainPanel.add(ButtonPanel, BorderLayout.SOUTH); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public void actionPerformed(ActionEvent e) { if (e.getSource() == LeftButton){ nextCard.CardsApp(); } if (e.getSource() == MiddleButton){ System.out.print(Hand.class); } if (e.getSource() == RightButton){ System.out.print(Hand.class); } } public static void main(String[] args) { CardsApp s = new CardsApp(); } }
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package cardsapp;
import java.util.*;
/**
*
* @author James
*/
public abstract class Hand implements Enumeration{
int currentHand;
int counter = 0;
String[] Cards = new String[]{"C","H","D","S"};
public Hand(String[] C, int cH){
Cards = C;
currentHand = cH;
}
public String nextCard(){
if(!hasMoreCards()) throw new NoSuchElementException();
return Cards[counter++];
}
public boolean hasMoreCards(){
return counter < currentHand;
}
public static void shuffle(Object[] array){
for(int i = 0; i < 4; i++){
}
}
}
Explanation / Answer
Executable Code:
// import the required packages
import java.util.Random;
import javax.swing.JOptionPane;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
// class for CardsApp
public class CardsApp extends JFrame implements ActionListener
{
// Declare the required java components
private JButton Button1,
Button2,
Button3;
private int Winwidth = 25;
private int Winheight = 15;
private JTextField gmTextField = new JTextField(30);
// Constructor
public CardsApp()
{
JPanel brdPanel = new JPanel(new GridLayout(Winheight,Winwidth));
JTextField gmTextField = new JTextField(30);
Hand setTheText = new Hand();
gmTextField.setText(setTheText.ListOfCards());
}
// Action event handler
public void actionPerformed(ActionEvent evt)
{
int btnpressed = 0;
if (evt.getSource() == Button1){
btnpressed = 1;}
if (evt.getSource() == Button2){
btnpressed = 2;}
if (evt.getSource() == Button3){
btnpressed = 3;}
Hand handObject = new Hand();
String screenText = handObject.ListOfCards();
gmTextField.setText(screenText);
}
// Main method
public static void main(String[] args)
{
CardsApp ca = new CardsApp();
}
}
// hand class
class Hand
{
String [] cSuits = {"C", "H", "S", "D"};
String [] cprobability = {"C","H","R","D"};
Random rnInt = new Random ();
String RndSuit;
String RndShuffle;
String Suits3;
String LeftSuit;
String MiddleSuit;
String RightSuit;
int btnpressed = 0;
// Method to discard the card
public int Discards(int pressedNumber)
{
return btnpressed;
}
//Method to randomize the cards
public void Randomizer ()
{
int RanSuitNumber = rnInt.nextInt(4);
// Code to check the button press event
if (btnpressed==1)
{
LeftSuit= cSuits[RanSuitNumber];
}
if (btnpressed==2)
{
MiddleSuit=cSuits[RanSuitNumber];
}
if (btnpressed==3)
{
RightSuit=cSuits[RanSuitNumber];
}
int ProabilityRandomNum = rnInt.nextInt(5);
RndShuffle= cprobability[ProabilityRandomNum];
if (RndShuffle.equals("R"))
{
JOptionPane.showMessageDialog(null, "Randomized Hand!");
int lftNumber = rnInt.nextInt(4);
int mdleNumber = rnInt.nextInt(4);
int ritNumber = rnInt.nextInt(4);
LeftSuit= cSuits[lftNumber];
MiddleSuit= cSuits[mdleNumber];
RightSuit= cSuits[ritNumber];
}
Suits3 = (LeftSuit + MiddleSuit + RightSuit);
}
// Method to print the list of cards
public String ListOfCards ()
{
return Suits3;
}
// Method to process the game over
public void GameOver()
{
if (LeftSuit == MiddleSuit && MiddleSuit == RightSuit &&
RightSuit== LeftSuit)
{
JOptionPane.showMessageDialog(null, "WINNER!!");
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.