文档库 最新最全的文档下载
当前位置:文档库 › java编写的记忆游戏代码

java编写的记忆游戏代码

Block.java

import javax.swing.*;
import java.awt.event.*;
public class Block extends JButton {
ImageIcon openStateIcon;
public ImageIcon getOpenStateIcon(){
return openStateIcon;
}
public void setOpenStateIcon(ImageIcon icon){
openStateIcon=icon;
}
}

MenoryGame.java

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.LinkedList;
public class MemoryGame extends JFrame implements ActionListener{
JMenuBar bar;
JMenu menuGrade,menuResult,menuIcon;
JMenuItem oneGradeItem,twoGradeItem,threeGradeItem;
JMenuItem oneGradeResult,twoGradeResult,threeGradeResult;
JMenuItem carImageIcon,animalImageIcon;
File fileOneGrade,fileTwoGrade,fileThreeGrade,gradeFile;
String imageName[];
MemoryTestArea memoryArea;
ShowRecordDialog showDiolag=null;
int m=5,n=6;
final int imageNumber=7;
MemoryGame(){
fileOneGrade=new File("初级记忆排行榜.txt");
fileTwoGrade=new File("中级记忆排行榜.txt");
fileThreeGrade=new File("高级记忆排行榜.txt");
bar=new JMenuBar();
menuGrade=new JMenu("选择级别");
oneGradeItem=new JMenuItem("初级");
twoGradeItem=new JMenuItem("中级");
threeGradeItem=new JMenuItem("高级");
menuGrade.add(oneGradeItem);
menuGrade.add(twoGradeItem);
menuGrade.add(threeGradeItem);
menuResult=new JMenu("查看排行榜");
oneGradeResult=new JMenuItem("初级排行榜");
twoGradeResult=new JMenuItem("中级排行榜");
threeGradeResult=new JMenuItem("高级排行榜");
menuResult.add(oneGradeResult);
menuResult.add(twoGradeResult);
menuResult.add(threeGradeResult);
menuIcon=new JMenu("选择图标");
carImageIcon=new JMenuItem("汽车图标");
animalImageIcon=new JMenuItem("动物图标");
animalImageIcon.addActionListener(this);
carImageIcon.addActionListener(this);
menuIcon.add(carImageIcon);
menuIcon.add(animalImageIcon);
bar.add(menuGrade);
bar.add(menuResult);
bar.add(menuIcon);
setJMenuBar(bar);
oneGradeItem.addActionListener(this);
twoGradeItem.addActionListener(this);
threeGradeItem.addActionListener(this);
oneGradeResult.addActionListener(this);
twoGradeResult.addActionListener(this);
threeGradeResult.addActionListener(this);
if(!fileOneGrade.exists()){
try { fileOneGrade.createNewFile();
}
catch(IOException exp){}
}
if(!fileTwoGrade.exists()){
try { fileTwoGrade.createNewFile();
}
catch(IOException exp){}
}
if(!fileThreeGrade.exists()){
try { fileThreeGrade.createNewFile();
}
catch(IOException

exp){}
}
setBounds(100,100,400,360);
setVisible(true);
memoryArea=new MemoryTestArea();
imageName=new String[imageNumber];
for(int i=0;iimageName[i]=new String("car"+i+".jpg");
}
m=5;
n=6;
gradeFile=fileOneGrade;
memoryArea.initBlock(m,n,imageName,gradeFile);
add(memoryArea,BorderLayout.CENTER);
showDiolag=new ShowRecordDialog();
validate();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent event){
if(event.getSource()==oneGradeItem){
m=5;
n=6;
gradeFile=fileOneGrade;
memoryArea.initBlock(m,n,imageName,gradeFile);
}
if(event.getSource()==twoGradeItem){
m=6;
n=7;
gradeFile=fileTwoGrade;
memoryArea.initBlock(m,n,imageName,gradeFile);
}
if(event.getSource()==threeGradeItem){
m=7;
n=8;
gradeFile=fileThreeGrade;
memoryArea.initBlock(m,n,imageName,gradeFile);
}
if(event.getSource()==carImageIcon){
for(int i=0;iimageName[i]=new String("car"+i+".jpg");
}
memoryArea.setImageName(imageName);
memoryArea.initBlock(m,n,imageName,gradeFile);
}
if(event.getSource()==animalImageIcon){
for(int i=0;iimageName[i]=new String("ani"+i+".jpg");
}
memoryArea.setImageName(imageName);
memoryArea.initBlock(m,n,imageName,gradeFile);
}
if(event.getSource()==oneGradeResult){
showDiolag.setGradeFile(fileOneGrade);
showDiolag.showRecord();
showDiolag.setVisible(true);
}
if(event.getSource()==twoGradeResult){
showDiolag.setGradeFile(fileTwoGrade);
showDiolag.showRecord();
showDiolag.setVisible(true);
}
if(event.getSource()==threeGradeResult){
showDiolag.setGradeFile(fileThreeGrade);
showDiolag.showRecord();
showDiolag.setVisible(true);
}
}
public static void main(String args[]){
new MemoryGame();
}
}

MenoryTestArea.java

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.*;
import java.io.*;
public class MemoryTestArea extends JPanel implements ActionListener,Runnable {
int row,col;
File gradeFile;
ArrayList allBlockList;
String imageFileName[];
LinkedList openIconList;
LinkedList openBlockList;
int success=0;
Thread hintThead;
JButton hintButton;
int usedTime=0;
JTextField showUsedTime,hintMessage;
javax.swing.Timer timer;
Record record;
JPanel center,south;
MemoryTestArea(){
setLayout(new BorderLayout

());
allBlockList=new ArrayList();
openIconList=new LinkedList();
openBlockList=new LinkedList();
hintThead=new Thread(this);
hintMessage=new JTextField();
hintMessage.setHorizontalAlignment(JTextField.CENTER);
hintMessage.setEditable(false);
hintMessage.setFont(new Font("宋体",Font.BOLD,18));
center=new JPanel();
south=new JPanel();
hintButton=new JButton("提示");
hintButton.addActionListener(this);
showUsedTime=new JTextField(8);
showUsedTime.setEditable(false);
showUsedTime.setHorizontalAlignment(JTextField.CENTER);
south.add(new JLabel("用时:")) ;
south.add(showUsedTime);
south.add(new JLabel("提示图标位置(导致用时增加):")) ;
south.add(hintButton);
add(south,BorderLayout.SOUTH);
add(hintMessage,BorderLayout.NORTH);
timer=new javax.swing.Timer(1000,this);
record=new Record();
}
public void initBlock(int m,int n,String name[],File f){
row=m;
col=n;
gradeFile=f;
center.removeAll();
imageFileName=name;
ImageIcon icon[]=new ImageIcon[imageFileName.length];
for(int i=0;iicon[i]=new ImageIcon(imageFileName[i]);
}
if(allBlockList.isEmpty()){
for(int i=0;iallBlockList.add(new Block());
}
}
else{
allBlockList.clear();
for(int i=0;iallBlockList.add(new Block());
}
}
for(int i=0;iallBlockList.get(i).addActionListener(this);
allBlockList.get(i).setOpenStateIcon(icon[i%row]);
}
Collections.shuffle(allBlockList);
center.setLayout(new GridLayout(row,col));
for(int i=0;icenter.add(allBlockList.get(i));
}
add(center,BorderLayout.CENTER);
if(timer.isRunning()){
timer.stop();
}
hintMessage.setText("您需要用鼠标单击出"+col+"个同样图标的方块");
usedTime=0;
showUsedTime.setText(null);
validate();
}
public void setImageName(String name[]){
imageFileName=name;
}
public void actionPerformed(ActionEvent e){
if(e.getSource() instanceof Block){
if(!timer.isRunning())
timer.start();
Block block=(Block)e.getSource();
ImageIcon openStateIcon=block.getOpenStateIcon();
block.setIcon(openStateIcon);
if(openIconList.size()==0) {
openIconList.add(openStateIcon);
openBlockList.add(block);
success=1;
}
else{
ImageIcon temp=openIconList.getLast();
if(temp==openStateIcon&&!(openBlockList.contains(block))){
success=success+1;


openIconList.add(openStateIcon);
openBlockList.add(block);
if(success==col){
for(int i=0;iallBlockList.get(i).setEnabled(false);
}
for(int j=0;jBlock b=openBlockList.get(j);
b.setDisabledIcon(b.getOpenStateIcon());
}
timer.stop();
record.setTime(usedTime);
record.setGradeFile(gradeFile);
record.setVisible(true);
}
}
else if((temp!=openStateIcon)&&(!(openBlockList.contains(block)))){
openIconList.clear();
openBlockList.clear();
openIconList.add(openStateIcon);
openBlockList.add(block);
success=1;
for(int i=0;iif(allBlockList.get(i)!=block)
allBlockList.get(i).setIcon(null);
}
}
}
}
if(e.getSource()==hintButton){
if(!hintThead.isAlive())
hintThead=new Thread(this);
for(int i=0;iallBlockList.get(i).removeActionListener(this);
usedTime=usedTime+10;
try{
hintThead.start();
}
catch(IllegalThreadStateException ex){}
}
if(e.getSource()==timer){
usedTime++;
showUsedTime.setText("您的用时:"+usedTime+"秒");
}
}
public void run(){
for(int i=0;iallBlockList.get(i).setIcon(allBlockList.get(i).getOpenStateIcon());
try{ Thread.sleep(1200);
}
catch(InterruptedException exp){}
for(int i=0;iallBlockList.get(i).addActionListener(this);
for(int i=0;iif(!openBlockList.contains(allBlockList.get(i)))
allBlockList.get(i).setIcon(null);
}
}

people.java

import java.io.*;
public class People implements Serializable,Comparable{
String name=null;
int time=0;
public People(String name,int t){
https://www.wendangku.net/doc/702482365.html,=name;
time=t;
}
public int getTime(){
return time;
}
public String getName(){
return name;
}
public int compareTo(Object b){
People p=(People)b;
if((this.time-p.time)==0)
return 1;
else
return (this.time-p.time);
}
}


Record.java

import java.io.*;
import java.util.*;
import javax.swing.*;
import java.awt.ev

ent.*;
import java.awt.*;
public class Record extends JDialog implements ActionListener{
int time=0;
JTextField yourName;
JLabel label;
JButton enter,cancel;
File gradeFile=null;
public Record(){
setBounds(100,100,330,160);
setResizable(false);
setModal(true);
setVisible(false);
enter=new JButton("确定");
cancel=new JButton("取消");
yourName=new JTextField(8);
yourName.setText("匿名");
enter.addActionListener(this);
cancel.addActionListener(this);
setLayout(new GridLayout(2,1));
label=new JLabel();
add(label);
JPanel p=new JPanel();
p.add(yourName);
p.add(enter);
p.add(cancel);
add(p);
}
public void setGradeFile(File f){
gradeFile=f;
setTitle("保存成绩到"+gradeFile.getName());
label.setText("保存成绩到"+gradeFile.getName());
validate();
}
public void setTime(int time){
this.time=time;
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==enter){
LinkedList list=new LinkedList();
try{
RandomAccessFile out=new RandomAccessFile(gradeFile,"rw");
out.seek(out.length());
out.writeUTF(yourName.getText());
out.writeInt(time);
out.close();
}
catch(Exception event) {}
setVisible(false);
}
if(e.getSource()==cancel){
setVisible(false);
}
}
}

ShowRecord.java

import java.io.*;
import java.util.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class ShowRecord extends JDialog implements ActionListener
{
File gradeFile;
JButton clear;
JTextArea showArea=null;
People people;
TreeSet treeSet;
public ShowRecord(File f){
treeSet=new TreeSet();
showArea=new JTextArea(6,4);
clear=new JButton("清空排行榜");
clear.addActionListener(this);
add(new JScrollPane(showArea),BorderLayout.CENTER);
add(clear,BorderLayout.SOUTH);
setBounds(100,100,320,185);
setModal(true);
addWindowListener(new WindowAdapter(){
public void windwoClosing(WindowEvent e){
setVisible(false);
}
});
}
public void setGradeFile(File f){
gradeFile=f;
setTitle(f.getName());
}
public void showRecord(){
try{
RandomAccessFile in=new RandomAccessFile(gradeFile,"rw");
String name=null;
while((name=in.readUTF())!=null){
int time=in.readInt();
people =new People(name,time);
treeSet.add(people);
}
Iterator iter=treeSet.iterator();
while(iter.hasNext()){
People p=iter.next();
showArea.appe

nd("姓名:"+p.getName()+"成绩: "+p.getTime());
showArea.append("\n");
}

}
catch(IOException exp){}
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==clear){
try{
File f=gradeFile.getAbsoluteFile();
gradeFile.delete();
f.createNewFile();
showArea.setText("排行榜被清空");
}
catch(Exception ee){}
}
}
}

相关文档