24-Point Card Game GUI

Help with GUI and algorithms for card game.

Page 1 of 1

3 Replies - 6600 Views - Last Post: 25 October 2010 - 06:52 AM Rate Topic: -----

#1 meglizhem  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 19-August 10

24-Point Card Game GUI

Posted 24 October 2010 - 04:10 PM

The program description is in the first few lines of code. I don't have very much, but I need help with the GUI part. I don't know why I have an error in public Cards(){}. Any help is much appreciated. Also, am I on the right track with the program? Thanks in advance.

/*
 * CSCI 2410
 * Meghan E. Hembree
 * Monday, October 25, 2010, 11:00:00am
 *
 * Description: (24-point card game) The 24-point game is to pick any
 * four cards from 52 cards, as shown in the figure below. Note that two
 * Jokers are excluded. Each card represents a number. An Ace, King, Queen,
 * and Jack represent 1, 13, 12, and 11, respectively. Enter an expression
 * that uses the four numbers from the four selected cards. Each number must
 * be used once and only once. You can use the operators (addition,
 * subtraction, multiplication, and division) and parentheses in the
 * expression. The expression must evaluate to 24.
 *
 * After entering the expression, click the Verify button to check whether
 * the numbers in the expression are currently selected and whether the
 * result of the expression is correct. Display the verification in a dialog
 * box. Note that such an expression might not exist. In this case, click the
 * Refresh button to get another set of four cards. Assume that images are
 * stored in files named 1.png, 2.png, ..., 52.png, in the order of spades,
 * hearts, diamonds, and clubs. So, the first 13 images are for spades 1, 2,
 * 3, ..., and 13.
 *
 * Your program should also enable the computer to display
 * the expression if one exists, as shown in the figure. Otherwise, report
 * that the expression does not exist.
 *
 */

package exercise25_09;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.ImageIcon;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.Image;
import javax.swing.JLabel;

public class Exercise25_09 extends JApplet{

    private ArrayList<Cards> cards = new ArrayList<Cards>();
    private JTextField jtfTry = new JTextField("Enter a Solution:");
    private JButton jbtRefresh = new JButton("Refresh");
    private JButton jbtSolution = new JButton("Find a Solution");
    private JButton jbtVerify = new JButton("Verify");
    private Cards view = new Cards();

    @Override
    public void init() {

        JPanel panel = new JPanel();

        this.add(panel, BorderLayout.CENTER);
        this.add(jbtRefresh, BorderLayout.NORTH);
        this.add(jbtSolution, BorderLayout.NORTH);
        this.add(jbtVerify, BorderLayout.SOUTH);
        this.add(jtfTry, BorderLayout.SOUTH);

        jbtRefresh.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

          }
        });

        jbtSolution.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

          }
        });
        jbtVerify.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {


          }
        });
        jtfTry.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {


          }
        });
  }

    public class Cards extends JPanel {

        int card = (int) (Math.floor(Math.random() * 51) + 1);

        Image card1 = new ImageIcon("C:Users/Megapeg/Documents/Cards/card" +
                card + ".png").getImage();
        Image card2 = new ImageIcon("C:Users/Megapeg/Documents/Cards/card" +
                card + ".png").getImage();
        Image card3 = new ImageIcon("C:Users/Megapeg/Documents/Cards/card" +
                card + ".png").getImage();
        Image card4 = new ImageIcon("C:Users/Megapeg/Documents/Cards/card" +
                card + ".png").getImage();

    // Constructors
    public Cards(){

        setLayout(new GridLayout(1, 4, 2, 2));
        add(new JLabel(card1));
        add(new JLabel(card2));
        add(new JLabel(card3));
        add(new JLabel(card4));
    }
    }

    public static void main(String[] args) {

    JFrame frame = new JFrame("24 Point Card Game");
    frame.setSize(500, 250);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }
    }






Is This A Good Question/Topic? 0
  • +

Replies To: 24-Point Card Game GUI

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8329
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: 24-Point Card Game GUI

Posted 24 October 2010 - 08:36 PM

You are writting an Application or an Applet ?
Your class extends JApplet but has a main() method. JApplet do not have main() method so that introduce some confusion.

An ArrayList<Card> can be used for what ? The only thing the constructor of your Card class does is to load 4 .png image. A Card by itself does not have any definition (color, value,...) this ArrayList is useless it will only contain the same 4 JLabel.
Was This Post Helpful? 0
  • +
  • -

#3 meglizhem  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 19-August 10

Re: 24-Point Card Game GUI

Posted 24 October 2010 - 09:55 PM

View Postpbl, on 24 October 2010 - 07:36 PM, said:

You are writting an Application or an Applet ?
Your class extends JApplet but has a main() method. JApplet do not have main() method so that introduce some confusion.

An ArrayList<Card> can be used for what ? The only thing the constructor of your Card class does is to load 4 .png image. A Card by itself does not have any definition (color, value,...) this ArrayList is useless it will only contain the same 4 JLabel.


So this should extend? JFrame? I'm posting my updated code as I have been working on it for hours now. It has some pseudo code mixed in though so beware. There are several errors in it. I'm just trying to get some thoughts down because I feel somewhat lost as to how to complete this. As always, any suggestions or pointers are much appreciated. Thanks.

/*
 * CSCI 2410
 * Meghan E. Hembree
 * Monday, October 25, 2010, 11:00:00am
 *
 * Description: (24-point card game) The 24-point game is to pick any
 * four cards from 52 cards, as shown in the figure below. Note that two
 * Jokers are excluded. Each card represents a number. An Ace, King, Queen,
 * and Jack represent 1, 13, 12, and 11, respectively. Enter an expression
 * that uses the four numbers from the four selected cards. Each number must
 * be used once and only once. You can use the operators (addition,
 * subtraction, multiplication, and division) and parentheses in the
 * expression. The expression must evaluate to 24.
 *
 * After entering the expression, click the Verify button to check whether
 * the numbers in the expression are currently selected and whether the
 * result of the expression is correct. Display the verification in a dialog
 * box. Note that such an expression might not exist. In this case, click the
 * Refresh button to get another set of four cards. Assume that images are
 * stored in files named 1.png, 2.png, ..., 52.png, in the order of spades,
 * hearts, diamonds, and clubs. So, the first 13 images are for spades 1, 2,
 * 3, ..., and 13.
 *
 * Your program should also enable the computer to display
 * the expression if one exists, as shown in the figure. Otherwise, report
 * that the expression does not exist.
 *
 */

package exercise25_09;

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

public class Exercise25_09 extends JFrame{

    private ArrayList<Image> cards = new ArrayList<Image>();
        for (int i = 0; i < 53; i++)
            cards.add(new ImageIcon("C:Users/Megapeg/Documents/Cards/card" +
                i + ".png").getImage());
    private Cards view = new Cards();
    private JTextField jtfTry = new JTextField(20);
    private JTextField jtfAnswer = new JTextField(20);
    private JButton jbtRefresh = new JButton("Refresh");
    private JButton jbtSolution = new JButton("Find a Solution");
    private JButton jbtVerify = new JButton("Verify");


    public Exercise25_09() {

        JPanel top = new JPanel();
        add(top, BorderLayout.NORTH);
        top.add(jbtRefresh);
        top.add(jbtSolution);
        top.add(jtfAnswer);

        add(view, BorderLayout.CENTER);

        JPanel bottom = new JPanel();
        add(bottom, BorderLayout.SOUTH);
        bottom.add(new JLabel("Try a solution?"));
        bottom.add(jtfTry);
        bottom.add(jbtVerify);

        jbtRefresh.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                jtfTry.repaint();
                jtfAnswer.repaint();
                view.repaint();
                Collections.shuffle(cards);

          }
        });

        jbtSolution.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                

          }
        });
        jbtVerify.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                evaluateExpression(jtfTry);
                if(jtfTry == Solutions){
                    JOptionPane.showMessageDialog(null, "Correct!");
                }else{
                    JOptionPane.showMessageDialog(null, "Try again.");
                }

          }
        });
        jtfTry.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                

          }
        });
  }

    public class Solutions(){
        public Array[][] solutions = 
        
    }
    
    public class Cards extends JPanel {

        int card = (int) (Math.floor(Math.random() * 51) + 1);

        public Image card1 = new ImageIcon("C:Users/Megapeg/Documents/Cards/card" +
                card + ".png").getImage();
        public Image card2 = new ImageIcon("C:Users/Megapeg/Documents/Cards/card" +
                card + ".png").getImage();
        public Image card3 = new ImageIcon("C:Users/Megapeg/Documents/Cards/card" +
                card + ".png").getImage();
        public Image card4 = new ImageIcon("C:Users/Megapeg/Documents/Cards/card" +
                card + ".png").getImage();

    public Cards(){

        setLayout(new GridLayout(1, 4, 2, 2));
        add(new JLabel(card1));
        add(new JLabel(card2));
        add(new JLabel(card3));
        add(new JLabel(card4));
    }
    }
    
public class EvaluateExpression {

  /** Evaluate an expression */
  public int evaluateExpression(String expression) {
    // Create operandStack to store operands
    GenericStack<Integer> operandStack = new GenericStack<Integer>();
  
    // Create operatorStack to store operators
    GenericStack<Character> operatorStack = new GenericStack<Character>();
  
    // Extract operands and operators
    java.util.StringTokenizer tokens =
      new java.util.StringTokenizer(expression, "()+-/*", true);
 
    //LINECOMMENTPhase 1: Scan tokens
    while (tokens.hasMoreTokens()) {
      String token = tokens.nextToken().trim(); //LINECOMMENTExtract a token
      if (token.length() == 0) //LINECOMMENTBlank space
        continue; //LINECOMMENTBack to the while loop to extract the next token
      else if (token.charAt(0) == '+' || token.charAt(0) == '-') {
        //LINECOMMENTProcess all +, -, *, / in the top of the operator stack 
        while (!operatorStack.isEmpty() &&
          (operatorStack.peek() == '+' ||
           operatorStack.peek() == '-' ||
           operatorStack.peek() == '*' ||
           operatorStack.peek() == '/')) {
          processAnOperator(operandStack, operatorStack);
        }

        //LINECOMMENTPush the + or - operator into the operator stack
        operatorStack.push(token.charAt(0));
      }
      else if (token.charAt(0) == '*' || token.charAt(0) == '/') {
        //LINECOMMENTProcess all *, / in the top of the operator stack 
        while (!operatorStack.isEmpty() &&
          (operatorStack.peek() == '*' ||
          operatorStack.peek() == '/')) {
          processAnOperator(operandStack, operatorStack);
        }

       // LINECOMMENTPush the * or / operator into the operator stack
        operatorStack.push(token.charAt(0));
      }
      else if (token.trim().charAt(0) == '(') {
        operatorStack.push('('); //LINECOMMENTPush '(' to stack
      }
      else if (token.trim().charAt(0) == ')') {
        //LINECOMMENTProcess all the operators in the stack until seeing '('
        while (operatorStack.peek() != '(') {
          processAnOperator(operandStack, operatorStack);
        }
        
        //operatorStack.pop(); LINECOMMENTPop the '(' symbol from the stack
      }
      else { //LINECOMMENTAn operand scanned
        //LINECOMMENTPush an operand to the stack
        operandStack.push(new Integer(token));
      }
    }

    //LINECOMMENTPhase 2: process all the remaining operators in the stack 
    while (!operatorStack.isEmpty()) {
      processAnOperator(operandStack, operatorStack);
    }

    //LINECOMMENTReturn the result
    return operandStack.pop();
  }

  /** Process one operator: Take an operator from operatorStack and
   *  apply it on the operands in the operandStack */
  public void processAnOperator(
      GenericStack<Integer> operandStack, 
      GenericStack<Character> operatorStack) {
    char op = operatorStack.pop();
    int op1 = operandStack.pop();
    int op2 = operandStack.pop();
    if (op == '+') 
      operandStack.push(op2 + op1);
    else if (op == '-') 
      operandStack.push(op2 - op1);
    else if (op == '*') 
      operandStack.push(op2 * op1);
    else if (op == '/') 
      operandStack.push(op2 / op1);
  }
}
    
    public static void main(String[] args) {

    JFrame frame = new JFrame("24 Point Card Game");
    frame.add(new Exercise25_09());
    frame.setSize(500, 250);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }
}


Was This Post Helpful? 0
  • +
  • -

#4 Luckless  Icon User is offline

  • </luck>
  • member icon

Reputation: 292
  • View blog
  • Posts: 1,146
  • Joined: 31-August 09

Re: 24-Point Card Game GUI

Posted 25 October 2010 - 06:52 AM

you almost had it right the first time, but you have to add an instance of your Exercise class to the JFrame then manually call init(). This way your program works with or without internet
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1