We’d love to hear any feedback or comment from you!

© 2011-2021 Zookal Pty Ltd

# View question and answer

###### Engineering · Computer Science
Question details

Please apply the following modification to the original requirements below:

• Keep a tally that shows the number of games that the player has won, tied, or lost.

• Use the drawString() method to display your name, course, section number, and your ID (1234) in the lower left-hand corner of the applet.

========*Original requirements*==================================================

*Original requirements*

Create an application that plays a card game named Lucky Seven. In real life, the
game can be played with seven cards, each containing a number from 1 through 7.
The cards are shuffled and dealt number-side down. To start the game, a player turns
over any card. The exposed number on the card determines the position (reading
from left to right) of the next card that must be turned over. For example, if the player
turns over the first card and its number is 7, the next card turned must be the seventh
card (counting from left to right). If the player turns over a card whose number
denotes a position that was already turned, the player loses the game. If the player
succeeds in turning over all seven cards, the player wins.

Instead of cards, you will use seven buttons on a JPanel. The buttons are labeled
1 through 7 from left to right. Randomly associate one of the seven values 1 through 7
with each button. (In other words, the associated value might or might not be
equivalent to the button’s labeled value.) When the player clicks a button, reveal
the associated hidden value. If the value represents the position of a button already
clicked, the player loses. If the revealed number represents an available button, force
the user to click it; that is, do not take any action until the user clicks the correct button.
After a player clicks a button, remove the button from play.

For example, a player might click Button 7, revealing a 4. Then the player clicks
Button 4, revealing a 2. Then the player clicks Button 2, revealing a 7. The player loses
because Button 7 was already used. Save the game as JLuckySeven.java.

===========JLuckySeven.java==============================================

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class JLuckySeven extends JFrame implements ActionListener
{
final int NUM = 7;
JLabel greeting = new JLabel("Lucky Seven Game");
JLabel promptLabel = new JLabel(" Choose one button     ");
Font headlineFont = new Font("Arial", Font.BOLD, 30);
Font mediumFont = new Font("Arial", Font.BOLD, 18);
JButton[] button = new JButton[NUM];
String[] label = new String[NUM];
int[] value = {1, 2, 3, 4, 5, 6, 7};
int[] randomVal = new int[NUM];
JLabel result1 = new JLabel("");
JLabel result2 = new JLabel("");
Container con = getContentPane();
int random;
int x, pos;
int buttonVal;
int nextButtonPosition;
int hiddenVal;
int count = 1;
boolean[] isUsed = new boolean[NUM];
public JLuckySeven()
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
greeting.setFont(headlineFont);
promptLabel.setFont(mediumFont);
result1.setFont(mediumFont);
result2.setFont(mediumFont);
con.add(greeting);
con.add(promptLabel);

for(x = 0; x < NUM; ++x)
{
button[x] = new JButton("" + value[x]);
con.add(button[x]);
button[x].addActionListener(this);
}

con.add(result1);
con.add(result2);
con.setLayout(new FlowLayout());
int y = 0;
int numbersLeft = NUM;
boolean found = false;
for(x = 0; x < NUM; ++x)
{
found = false;
while(!found)
{
random = (int) ((Math.random() * 100) % NUM) + 1;
for(y = 0; y < numbersLeft; ++y)
{
if(random == value[y])
{
randomVal[x] = random;
pos = y;
found = true;
y = numbersLeft;
}
}
}
for(y = pos; y < numbersLeft - 1; ++y)
{
value[y] = value[y + 1];
}
--numbersLeft;
}

}
public void actionPerformed(ActionEvent e)
{
int pos;
JButton temp = temp = (JButton)e.getSource();
buttonVal = Integer.parseInt(temp.getText());
pos = buttonVal - 1;

if(count == 1)
{
nextButtonPosition = pos;
}
if(pos == nextButtonPosition)
{
hiddenVal = randomVal[pos];
result1.setText("The hidden number is " + hiddenVal + ".");

if(buttonVal == hiddenVal || isUsed[hiddenVal - 1])
{
result2.setText("Sorry, you lose.");
con.remove(promptLabel);
for(x = 0; x < NUM; ++x)
{
isUsed[x] = true;
button[x].setEnabled(false);
con.remove(button[x]);
}
}
else
{
con.remove(button[pos]);
isUsed[pos] = true;
nextButtonPosition = hiddenVal - 1;
++count;
}
}
if(count == NUM)
{
result2.setText("Congratulations - you got them all!");
con.remove(button[pos]);
con.remove(promptLabel);
for(x = 0; x < NUM; ++x)
{
isUsed[x] = true;
button[x].setEnabled(false);
}
}
validate();
repaint();

}
public static void main(String[] args)
{
JLuckySeven frame = new JLuckySeven();
frame.setSize(400, 200);
frame.setVisible(true);
}

}

Answer
Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.

Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.