Monday, January 9, 2012

Tutorial: A Card Guessing Game in Java

by eturo


Hi guys out there! Are you playing guessing games? Today, I will show a simple Card Guessing Game applet I made using Java. You can copy the source code below and try to play the simple game.

I designed and build the simple game using Swing in Java. Here is the interface: (see the image below)

The GUI Design:





This is a single player card guessing game wherein you have to guess the Random Card shown whether it is higher or lower to your Guessed Card by clicking the Higher or Lower buttons. If your guessed is correct, then a message will appear displaying your number of tries and score. You have only three tries to guess.


The Game Proper:


How to start the Game?
By clicking the Start Game button, then the first Random Card game will appear from the box just like the image below.



How to guess the Random Card?
Now, if the first Random Card appear, then its time to give your Guess whether the Next Random Card is higher or lower to the previous Random Card. If your guessed is correct or not, then a message will prompt. (see the image below)



Here, the player clicked the Lower button, and his guessed (Card No. 8) is lower to the first Random Card (Card No. 9), it means that his guessed is correct. A message dialog box appeared. The Card No. 8, will serve as the new Random Card to guessed by the player.

Note: In Java, you can build message dialog boxes by using the JOptionPane class.



Now, the player has two remaining tries to guess. By clicking the OK button of the message dialog box, the game will continue. The player again guessed whether the next Card will be lower or higher from the previous Card which is the No. 8. Your score is also shown below the game board. (see the image below)



Here, the player clicked the Higher button, so again, he wins because his guessed Card No (10) is higher to the previous Card No. which is 8. The player has only 1 try guess the next random Card No. Now, the player has a score of 2 out of 2 tries.





And finally, the last Card No. is shown but it is equal to the previous Card No. which means that the third try is a Draw and it will not be counted from the total score.





This is the final message displayed after the last try of the game. You can actually continue or try another guessing game by clicking the New Game button then the game will reset or restart.


The Program Source Code:

Here is the source code of the game. It is free to everybody!

/**
 * This is Simple Higher or Lower Card Guessing Game.
 *
 * @author (Vilchor G. Perdido)
 * @version (September 11, 2010)
 */
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.applet.*;
import java.util.*;


public class HigherLowerCardGame extends JApplet implements ActionListener
{
    JTextField cardMain,card1,card2,card3;
    JTextField score1,score2;
    JButton btnNewGame,btnLower,btnHigher,btnStartGame;
   
    int main,c1,c2,c3;
    String card[] = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
    String hint,first,second,third;
    int TRIES = 1,chances = 3,score = 0;;
    int R, G, B;
    final int MAX_SCORE = 3;
   
    public void init()
    {


        JOptionPane.showMessageDialog(null,"Welcome to the Lower or Higher Java Applet Game.\n\nEnjoy the Game!");
       
        setLayout(null);
        setSize(1024,460);
               
        int R = (int) (250 * Math.random());
        int G = (int) (250 * Math.random());
        int B = (int) (250 * Math.random());
       
        Color color = new Color(R,G,B);


        cardMain = new JTextField(20);
        cardMain.setBounds(10,30,250,270);
        cardMain.setFont(new Font("Arial",Font.BOLD,220));
        cardMain.setEditable(false);
        cardMain.setBackground(Color.lightGray);
        cardMain.setForeground(color);
        cardMain.setBorder(BorderFactory.createEtchedBorder());
        add(cardMain);
        card1 = new JTextField(20);
        card1.setBounds(260,30,250,270);
        card1.setFont(new Font("Arial",Font.BOLD,220));
        card1.setEditable(false);
        card1.setVisible(false);
        card1.setBackground(Color.lightGray);
        card1.setForeground(color);
        card1.setBorder(BorderFactory.createEtchedBorder());
        add(card1);
        card2 = new JTextField(20);
        card2.setBounds(510,30,250,270);
        card2.setFont(new Font("Arial",Font.BOLD,220));
        card2.setEditable(false);
        card2.setVisible(false);
        card2.setBackground(Color.lightGray);
        card2.setForeground(color);
        card2.setBorder(BorderFactory.createEtchedBorder());
        add(card2);
        card3 = new JTextField(20);
        card3.setBounds(760,30,240,270);
        card3.setFont(new Font("Arial",Font.BOLD,220));
        card3.setEditable(false);
        card3.setVisible(false);
        card3.setBackground(Color.lightGray);
        card3.setForeground(color);
        card3.setBorder(BorderFactory.createEtchedBorder());
        add(card3);


       
        btnStartGame = new JButton("Start Game");
        btnStartGame.setBounds(150,320,250,60);
        btnStartGame.setFont(new Font("Serif",Font.BOLD,32));
        btnStartGame.setForeground(Color.green);
        add(btnStartGame);
        btnNewGame = new JButton("New Game");
        btnNewGame.setBounds(150,320,250,60);
        btnNewGame.setFont(new Font("Serif",Font.BOLD,32));
        btnNewGame.setForeground(Color.green);
        btnNewGame.setVisible(false);
        add(btnNewGame);
       
        btnHigher = new JButton("Higher");
        btnHigher.setBounds(450,320,180,60);
        btnHigher.setFont(new Font("CenturyGothic",Font.BOLD,32));
        btnHigher.setForeground(Color.blue);
        btnHigher.setEnabled(false);
        add(btnHigher);
        btnLower = new JButton("Lower");
        btnLower.setBounds(650,320,180,60);
        btnLower.setFont(new Font("CenturyGothic",Font.BOLD,32));
        btnLower.setForeground(Color.red);
        btnLower.setEnabled(false);
        add(btnLower);
       
        score1 = new JTextField("Your Score: " + score);
        score1.setBounds(240,400,500,50);
        score1.setFont(new Font("CenturyGothic",Font.BOLD,32));
        score1.setBackground(Color.black);
        score1.setForeground(Color.yellow);
        score1.setEditable(false);
        score1.setBorder(BorderFactory.createEtchedBorder());
        score1.setHorizontalAlignment(JTextField.CENTER);
        add(score1);


        btnNewGame.requestFocus();
        btnStartGame.addActionListener(this);
        btnNewGame.addActionListener(this);
        btnHigher.addActionListener(this);
        btnLower.addActionListener(this);
    }


    public void paint(Graphics g)
    {
        showStatus("Author: Vilchor G. Perdido");
    }
   
    public void gameStart()
    {
        main = (int) (12 * Math.random());
        c1 = (int) (12 * Math.random());
        c2 = (int) (12 * Math.random());
        c3 = (int) (12 * Math.random());
       
        hint= card[main];
        first = card[c1];
        second = card[c2];
        third = card[c3];
        cardMain.setText(""+hint);
    }


    public void displayStat()
    {
        if(TRIES<3)
        {
            ++TRIES;
        }
        else
        {
            btnHigher.setEnabled(false);
            btnLower.setEnabled(false);
            btnNewGame.setEnabled(true);
            btnNewGame.requestFocus();
           
            if(score==3)
            {
                JOptionPane.showMessageDialog(null,"YOU GOT " + score + " out of " + MAX_SCORE + ". \nEXCELLENT!You are a GENIUS!.");
            }
            else if(score==2)
            {
                JOptionPane.showMessageDialog(null,"YOU GOT " + score + " out of " + MAX_SCORE + ".\nVERY GOOD! You are SMART.");
            }
            else if(score==1)
            {
                JOptionPane.showMessageDialog(null,"YOU GOT " + score + " out of " + MAX_SCORE + ".\nGOOD! You are NORMAL.");
            }
            else
            {
                JOptionPane.showMessageDialog(null,"YOU GOT " + score + " out of " + MAX_SCORE + ".\nBETTER LUCK NEXT TIME!!!\nYou are an IDIOT!");
            }
            continueGame();
        }
    }
       
    public void guessHigher()
    {
        if (TRIES==1)
        {
            btnNewGame.setEnabled(false);
            if(c1>main)
            {
                card1.setVisible(true);
                card1.setText(""+first);
                JOptionPane.showMessageDialog(null,"You win!\nYou still have " + --chances + " tries.");
                score++;
                score1.setText("Your Score: " + score);   
            }
            else if(c1==main)
            {
                card1.setVisible(true);
                card1.setText(""+first);
                JOptionPane.showMessageDialog(null,"Draw!\nYou still have " + --chances + " tries.");
            }
            else
            {
                card1.setVisible(true);
                card1.setText(""+first);
                JOptionPane.showMessageDialog(null,"You lose!\nYou still have " + --chances + " tries.");
            }
           
        }
        else if (TRIES==2)
        {
            btnNewGame.setEnabled(false);
            if(c2>c1)
            {
                card2.setVisible(true);
                card2.setText(""+second);
                JOptionPane.showMessageDialog(null,"You win!\nYou still have " + --chances + " tries.");
                score++;
                score1.setText("Your Score: " + score);
            }
            else if(c2==c1)
            {
                card2.setVisible(true);
                card2.setText(""+second);
                JOptionPane.showMessageDialog(null,"Draw!\nYou still have " + --chances + " tries.");
            }
            else
            {
                card2.setVisible(true);
                card2.setText(""+second);
                JOptionPane.showMessageDialog(null,"You lose!\nYou still have " + --chances + " tries.");
            }
           
        }
        else
        {
            btnNewGame.setEnabled(false);
            if(c3>c2)
            {
                card3.setVisible(true);
                card3.setText(""+third);
                JOptionPane.showMessageDialog(null,"You win!\nSorry, No More TRIES!");
                score++;
                score1.setText("Your Score: " + score);
               
            }
            else if(c3==c2)
            {
                card3.setVisible(true);
                card3.setText(""+third);
                JOptionPane.showMessageDialog(null,"Draw!\nSorry, No More TRIES!");
            }
            else
            {
                card3.setVisible(true);
                card3.setText(""+third);
                JOptionPane.showMessageDialog(null,"You lose!\nSorry, No More TRIES!");
            }          
        }
    }
       
    public void guessLower()
    {
        if(TRIES==1)
        {
            btnNewGame.setEnabled(false);
            if(c1            {
                card1.setVisible(true);
                card1.setText(""+first);
                JOptionPane.showMessageDialog(null,"You win!\nYou still have " + --chances + " tries.");
                score++;
                score1.setText("Your Score: " + score);
            }
            else if(c1==main)
            {
                card1.setVisible(true);
                card1.setText(""+first);
                JOptionPane.showMessageDialog(null,"Draw!\nYou still have " + --chances + " tries.");
            }
            else
            {
                card1.setVisible(true);
                card1.setText(""+first);
                JOptionPane.showMessageDialog(null,"You lose!\nYou still have " + --chances + " tries.");
            }
        }
         else if (TRIES==2)
        {
            btnNewGame.setEnabled(false);
            if(c2            {
                card2.setVisible(true);
                card2.setText(""+second);
                JOptionPane.showMessageDialog(null,"You win!\nYou still have " + --chances + " tries.");
                score++;
                score1.setText("Your Score: " + score);
            }
            else if(c2==c1)
            {
                card2.setVisible(true);
                card2.setText(""+second);
                JOptionPane.showMessageDialog(null,"Draw!\nYou still have " + --chances + " tries.");
            }
            else
            {
                card2.setVisible(true);
                card2.setText(""+second);
                JOptionPane.showMessageDialog(null,"You lose!\nYou still have " + --chances + " tries.");
            }
        }
        else
        {
            btnNewGame.setEnabled(false);
            if(c3            {
                card3.setVisible(true);
                card3.setText(""+third);
                JOptionPane.showMessageDialog(null,"You win!\nSorry, No More TRIES!");
                score++;
                score1.setText("Your Score: " + score);
            }
            else if(c3==c2)
            {
                card3.setVisible(true);
                card3.setText(""+third);
                JOptionPane.showMessageDialog(null,"Draw!\nSorry, No More TRIES!");
            }
            else
            {
                card3.setVisible(true);
                card3.setText(""+third);
                JOptionPane.showMessageDialog(null,"You lose!\nSorry, No More TRIES!");
            }
        }
    }
   
    public void randomColor()
    {
        R = (int) (250 * Math.random());
        G = (int) (250 * Math.random());
        B = (int) (250 * Math.random());
       
        cardMain.setForeground(new Color(R,G,B));
        card1.setForeground(new Color(R,G,B));
        card2.setForeground(new Color(R,G,B));
        card3.setForeground(new Color(R,G,B));
    }
   
    public void newGame()
    {
       
        TRIES = 1;
        chances = 3;
        score = 0;
        score1.setText("Your Score: " + score);
        card1.setText("");
        card2.setText("");
        card3.setText("");
               
        main = (int) (12 * Math.random());
        c1 = (int) (12 * Math.random());
        c2 = (int) (12 * Math.random());
        c3 = (int) (12 * Math.random());
       
        hint = card[main];
        first = card[c1];
        second = card[c2];
        third = card[c3];
        cardMain.setText(""+hint);
       
        btnHigher.setEnabled(true);
        btnLower.setEnabled(true);
       
    }
   
    public void invisible()
    {
        card1.setVisible(false);
        card2.setVisible(false);
        card3.setVisible(false);
    }
   
    public void startGame()
    {
        btnStartGame.setVisible(false);
        btnNewGame.setVisible(true);
        btnNewGame.setEnabled(false);
        btnHigher.setEnabled(true);
        btnLower.setEnabled(true);
    }
   
    public void stopGame()
    {
        btnStartGame.setVisible(false);
        btnNewGame.setVisible(false);
        btnHigher.setVisible(false);
        btnLower.setVisible(false);
        score1.setVisible(false);
        cardMain.setVisible(false);
        card1.setVisible(false);
        card2.setVisible(false);
        card3.setVisible(false);
    }
   
    public void continueGame()
    {
        int n = JOptionPane.showConfirmDialog(null,"Play Again?","Lower or Higher",JOptionPane.YES_NO_OPTION);
        if(n==0)
        {
            startGame();
            btnNewGame.setEnabled(true);
            btnHigher.setEnabled(false);
            btnLower.setEnabled(false);
           
        }
        else
        {
            stopGame();
        }
    }
   
    public void actionPerformed(ActionEvent e)
    {
        if(e.getSource()==btnStartGame)
        {
            gameStart();
            startGame();
        }
               
        if(e.getSource()==btnHigher)
        {
            guessHigher();
            displayStat();
        }
       
        if(e.getSource()==btnLower)
        {
            guessLower();
            displayStat();
        }

        if(e.getSource()==btnNewGame)
        {
            newGame();           
            randomColor();
            invisible();
        }
    }
}




Note: If you think this game or program is not enough for you, you can actually modify the game by adding some features you want. Don't forget to leave or post your comments or suggestions about this tutorial. If you want more sample codes of Java programs and tutorials, please visit this blog regularly. Happy coding....

Like Us on Facebook

Related Posts Plugin for WordPress, Blogger...