Sei sulla pagina 1di 3

import javax.swing.

*;
import java.awt.*;
import java.awt.event.*;
public class Game extends JFrame // Inheritance
{
Font f1,f2,f3;
Label l1,l2,l3,l4,l5,l6,l7,l8;
JButton b1,b2;
TextField t1;
static String guess1,guess2,guess3;

public Game() // Constructor


{

super("Number Guesser Game");


JOptionPane.showMessageDialog(null, "THIS GAME IS CALLED PINOY HENYO");
JOptionPane.showMessageDialog(null, "THIS WILL BE PLAYED BY TWO PERSONS.");
JOptionPane.showMessageDialog(null, "THE FIRST PERSON WILL BE THE ONE
WRITING THE WORD TO BE GUESSED");
JOptionPane.showMessageDialog(null, "THE SECOND PERSON WILL BE ONE GUESSING
THE WORD WRITTEN BY THE FIRST PERSON");
JOptionPane.showMessageDialog(null, "THE FIRST PERSON, HOWEVER, SHALL GIVE
THREE CLUES BASED ON THE WORD HE/SHE HAS GIVEN");
JOptionPane.showMessageDialog(null, "LET'S PLAY");

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(800,400);
setVisible(true);
setResizable(false);
setLayout(new GridBagLayout());
GridBagConstraints c=new GridBagConstraints();
f1=new Font("TimesNew Roman",Font.BOLD,32);
f2=new Font("Elephant",Font.ITALIC,24);
f3=new Font("Forte",Font.BOLD,28);
l8=new Label("");
c.gridx=0;
c.gridy=0;
add(l8,c);
l1=new Label("Word Guesser Game!");
c.gridx=1;
c.gridy=0;
add(l1,c);
l1.setFont(f1);
l1.setForeground(Color.RED);
l2=new Label("");
c.gridx=0;
c.gridy=1;
add(l2,c);
l5=new Label("");
c.gridx=0;
c.gridy=2;
add(l5,c);
l3=new Label("Enter your Word please:");
c.gridx=1;
c.gridy=2;
add(l3,c);
l3.setFont(f2);
l3.setBackground(Color.GREEN);
l6=new Label("");
c.gridx=0;
c.gridy=3;
add(l6,c);
t1=new TextField(10);
c.gridx=1;
c.gridy=3;
add(t1,c);
t1.setForeground(Color.GREEN);
t1.setFont(f2);
l4=new Label("");
c.gridx=0;
c.gridy=4;
add(l4,c);
b1=new JButton("Submit");
c.gridx=0;
c.gridy=5;
add(b1,c);
b1.setToolTipText("Click this button to submit your word");
b1.setFont(f3);
b1.setForeground(Color.RED);
b1.setBackground(Color.BLUE);
l7=new Label("");
c.gridx=1;
c.gridy=5;
add(l7,c);
b2=new JButton("Reset");
c.gridx=2;
c.gridy=5;
b2.setToolTipText("Reset the enty field");
b2.setFont(f3);
b2.setForeground(Color.BLUE);
b2.setBackground(Color.RED);
add(b2,c);
event e=new event();
b1.addActionListener(e);
b2.addActionListener(e);
}

public class event implements ActionListener // Implementing Interfaces


{
public void actionPerformed(ActionEvent e) // Overriding the method available in
the interface
{
String guess1=JOptionPane.showInputDialog(null,"Give the Second Person the
first Clue");
String guess2=JOptionPane.showInputDialog(null,"Give the Second Person the
second Clue");
String guess3=JOptionPane.showInputDialog(null,"Give the Second Person the
third Clue");

setVisible(false);
String op,word,opinion,satisfaction,word_guess;
int number,actual,difference,accuracy;
op=e.getActionCommand();
if(op.equals("Submit"))
{
word=t1.getText();
if(word.isEmpty())
{
JOptionPane.showMessageDialog(null,"Please input a word");
}
else
{
JOptionPane.showMessageDialog(null,"Hello, \nWelcome to the word guesser
game\n\nNow let's proceed to the general instructions");
JOptionPane.showMessageDialog(null,"Brief Summary\n\n\nThis is a very basic
word guessing game. \nClick the OK button to get started!");

JOptionPane.showMessageDialog(null,"YOU ONLY HAVE THREE TRIALS. DO YOUR BEST IN


GUESSING. ");
JOptionPane.showMessageDialog(null, "THE FIRST CLUE IS "+guess1);
JOptionPane.showMessageDialog(null, "THE SECOND CLUE IS "+guess2);
JOptionPane.showMessageDialog(null, "THE THIRD CLUE IS "+guess3);

int counter=0;
Loop:
while(counter!=3) {
word_guess=JOptionPane.showInputDialog(null,"GUESS ");

if(word.equals(word_guess)) {
JOptionPane.showMessageDialog(null, "CONGRATULATIONS! YOU HAVE GUESSED
THE CORRECT WORD");
break Loop;
}

if(word!=word_guess) {
counter++;
}

if(counter==3) {
JOptionPane.showMessageDialog(null, "YOU ARE OUT OF TRIALS! THE CORRECT WORD
IS "+word);
}

JOptionPane.showMessageDialog(null,"***************************\n\nCredits\n\nDevel
oped by Apple Kate Ambray\n\n***************************");
}
}
else if(op.equals("Reset"))
{
t1.setText(" ");
}
}
}

public static void main(String[]args)


{
Game gui=new Game(); // Instantiation
}
}

Potrebbero piacerti anche