Share
Explore

s23 Java Class project: Due last week of class


The game will be a simple guessing game with three cards: two cards will be black, one will be red. The player will attempt to guess which card is red. If they guess correctly, they win; if not, the computer wins. Let's call it "Red Guess".
Here's a basic implementation of this game:
Card class:

class Card {
String color;

public Card(String color) {
this.color = color;
}

public String getColor() {
return color;
}
}

Game class:

import java.util.Collections;
import java.util.Arrays;
import java.util.List;

class Game {
List<Card> cards;
Card selectedCard;

public Game() {
cards = Arrays.asList(new Card("Black"), new Card("Black"), new Card("Red"));
Collections.shuffle(cards);
}

public void selectCard(int index) {
selectedCard = cards.get(index);
}

public boolean checkWin() {
return selectedCard.getColor().equals("Red");
}
}

GUI class (with Swing):

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class GameGUI {
private JFrame frame;
private Game game;
private JButton[] cardButtons;

public GameGUI() {
game = new Game();
cardButtons = new JButton[3];
createAndShowGUI();
}

private void createAndShowGUI() {
frame = new JFrame("Red Guess");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridLayout(1, 3));

for(int i = 0; i < 3; i++) {
cardButtons[i] = new JButton("Card " + (i+1));
int index = i;
cardButtons[i].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
game.selectCard(index);
if(game.checkWin()) {
JOptionPane.showMessageDialog(frame, "You Win!");
} else {
JOptionPane.showMessageDialog(frame, "You Lose!");
}
frame.dispose();
}
});
frame.getContentPane().add(cardButtons[i]);
}

frame.pack();
frame.setVisible(true);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new GameGUI();
}
});
}
}

This code creates a game where the player selects one of three cards, each represented by a button in the GUI. The cards are shuffled at the beginning of each game, and when the player selects a card, the game checks whether the selected card is red. If it is, the player wins and a dialog box is shown with a winning message; otherwise, the player loses and a dialog box is shown with a losing message.
This is a basic implementation of the game and its GUI. You can extend it with more complex rules and a more sophisticated graphical interface.

How does player select card with mouse click? Study the code.

The player selects a card by clicking a button with the mouse. The JButton class in Swing allows attaching ActionListeners to buttons, which react when the button is clicked.
Here's the relevant part of the code:
javaCopy code
for(int i = 0; i < 3; i++) {
cardButtons[i] = new JButton("Card " + (i+1));
int index = i;
cardButtons[i].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
game.selectCard(index);
if(game.checkWin()) {
JOptionPane.showMessageDialog(frame, "You Win!");
} else {
JOptionPane.showMessageDialog(frame, "You Lose!");
}
frame.dispose();
}
});
frame.getContentPane().add(cardButtons[i]);
}

In this loop, we create three buttons, each representing a card. We then add an ActionListener to each button. When a button is clicked, the actionPerformed method of its ActionListener is called.
In the actionPerformed method, we first select the card corresponding to the clicked button by calling game.selectCard(index). We then check whether the selected card is red by calling game.checkWin(). If it returns true, we display a dialog box with the message "You Win!"; if it returns false, we display a dialog box with the message "You Lose!". Finally, we close the window by calling frame.dispose().
Please note that JOptionPane.showMessageDialog(frame, "message") is a Swing method that displays a dialog box with a message. In our case, the message depends on whether the player has won or lost. The frame parameter is the parent of the dialog box. It's used to position the dialog box relative to the frame. If you pass null, the dialog box is centered on the screen.
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.