ICS 201 Final Project

عملت على هذا المشروع مدة 3 ساعات وقد كان في الأسبوع الأخير من الترم الصيفي 083

Design and implement a memory game. In this game you show the user a sequence of random integer numbers in the range of 0 to 99, and then ask him to type these umbers. Display how many he has remembered as a score. The level of game depends on how many numbers are there in the sequence. The duration of the display for the sequence is the speed. The user can adjust the level and the speed.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Random;
public class project extends JFrame
{
Random generator = new Random();
int ran = 0;
int count = 1;
int score = 0;
String input = "";
String rand = "";
String theLevel = "Beginner";
String Speed = "3 Seconds";
int theSpeed = 3000;
Timer t;
JButton button = new JButton("Play");
JButton button1 = new JButton("Reset");
JLabel num = new JLabel("Press the button \"Play\" to play");
JLabel s = new JLabel("You score is: " + score + "| Your Level is: " + theLevel + "| your Speed is: " + Speed);
public project()
{
super("The Memory Game");
setSize(500,150);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout( ));
JMenu level = new JMenu("Level");
JMenuItem beginner = new JMenuItem("Beginner");
beginner.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
theLevel = "Beginner";
s.setText("You score is: " + score + "| Your Level is: " + theLevel + "| your Speed is: " + Speed);
}
});
level.add(beginner);
JMenuItem average = new JMenuItem("Average");
average.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
theLevel = "Average";
s.setText("You score is: " + score + "| Your Level is: " + theLevel + "| your Speed is: " + Speed);
}
});
level.add(average);
JMenuItem professional = new JMenuItem("Professional");
professional.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
theLevel = "Professional";
s.setText("You score is: " + score + "| Your Level is: " + theLevel + "| your Speed is: " + Speed);
}
});
level.add(professional);
JMenu speed = new JMenu("Speed");
JMenuItem three_sec = new JMenuItem("3 Seconds");
three_sec.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
theSpeed = 3000;
Speed = "3 Seconds";
s.setText("You score is: " + score + "| Your Level is: " + theLevel + "| your Speed is: " + Speed);
}
});
speed.add(three_sec);
JMenuItem six_sec = new JMenuItem("6 Seconds");
six_sec.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
theSpeed = 6000;
Speed = "6 Seconds";
s.setText("You score is: " + score + "| Your Level is: " + theLevel + "| your Speed is: " + Speed);
}
});
speed.add(six_sec);
JMenuItem ten_sec = new JMenuItem("10 Seconds");
ten_sec.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
theSpeed = 10000;
Speed = "10 Seconds";
s.setText("You score is: " + score + "| Your Level is: " + theLevel + "| your Speed is: " + Speed);
}
});
speed.add(ten_sec);
JMenu about = new JMenu("About");
JMenuItem aboutme = new JMenuItem("About");
aboutme.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
JOptionPane.showMessageDialog(null, "This program developed by:\nEmad Daghreri\nemad.daghreri@gmail.com","alert" , JOptionPane.ERROR_MESSAGE);
}
});
about.add(aboutme);
JMenuBar bar = new JMenuBar( );
bar.add(level);
bar.add(speed);
bar.add(about);
setJMenuBar(bar);
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new GridLayout (4,1));
add(mainPanel,BorderLayout.CENTER);
mainPanel.setBackground(Color.WHITE);
mainPanel.add(num);
mainPanel.add(s);
mainPanel.add(button);
mainPanel.add(button1);
setVisible(true);
button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
if (theLevel.equals("Beginner"))
{
for(int i = 0; i<2;i++)
{
button.setVisible(false);
button1.setVisible(false);
rand = rand + generator.nextInt(99);
num.setText("The number is: "+rand);
}
t = new Timer(theSpeed, new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
num.setText("");
input=JOptionPane.showInputDialog("Pleas enter the number: ");
if(input.equalsIgnoreCase(rand))
{
score ++;
s.setText("You score is: " + score + "| Your Level is: " + theLevel + "| your Speed is: " + Speed);
num.setText("Nice !");
} else num.setText("Sorry, try again!");
button.setVisible(!false);
button1.setVisible(!false);
rand = "";
t.stop();
}
});
t.start();
}
if (theLevel.equals("Average"))
{
for(int i = 0; i<3;i++)
{
button.setVisible(false);
button1.setVisible(false);
rand = rand + generator.nextInt(99);
num.setText("The number is: "+rand);
}
t = new Timer(theSpeed, new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
num.setText("");
input=JOptionPane.showInputDialog("Pleas enter the number: ");
if(input.equalsIgnoreCase(rand))
{
score ++;
s.setText("You score is: " + score + "| Your Level is: " + theLevel + "| your Speed is: " + Speed);
num.setText("Nice !");
} else num.setText("Sorry, try again!");
button.setVisible(!false);
button1.setVisible(!false);
rand = "";
t.stop();
}
});
t.start();
}
if (theLevel.equals("Professional"))
{
for(int i = 0; i<4;i++)
{
button.setVisible(false);
button1.setVisible(false);
rand = rand + generator.nextInt(99);
num.setText("The number is: "+rand);
}
t = new Timer(theSpeed, new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
num.setText("");
input=JOptionPane.showInputDialog("Pleas enter the number: ");
if(input.equalsIgnoreCase(rand))
{
score ++;
s.setText("You score is: " + score + "| Your Level is: " + theLevel + "| your Speed is: " + Speed);
num.setText("Nice !");
} else num.setText("Sorry, try again!");
button.setVisible(!false);
button1.setVisible(!false);
rand = "";
t.stop();
}
});
t.start();
}
}
});
button1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
num.setText("Press the button \"Play\" to play");
score = 0;
theLevel = "Beginner";
Speed = "3 Seconds";
theSpeed = 3000;
s.setText("You score is: " + score + "| Your Level is: " + theLevel + "| your Speed is: " + Speed);
}
});
}
public static void main(String[]args)
{
new project();
}
}