-
29-07-2014, 06:26 PM #1Junior Member
- Ngày tham gia
- Sep 2015
- Bài viết
- 0
Game Memory ai giải thích giúp mình với !
Mình có đoạn code game Memory nhưng ko hiểu ý nghĩa từng hàm,từng dòng ai bik chỗ nào làm ơn giải thích mình chỗ đó với .Hoặc code đoạn nào là khi click 2 tấm hình trùng nhau thì lật và tăng điểm,còn sai thì tăng missed lên 1 rùi úp lại với !
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package client;
/**
*
* @author Admin
*/
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.io.ObjectOutputStream;
import javax.swing.*;
import java.lang.Math.*;
import java.net.Socket;
import java.net.UnknownHostException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import model.User;
public class Block extends JFrame implements ActionListener {
int diem;
private User user;
private JButton exitbut, startbut, helpbut, pausebut, mybutton[];
private JPanel lowpanel, highpanel, mainpanel;
private JLabel timel, missedl, dtime;
private JTextField timef, missedf;
private JComboBox timeCOMBO;
private GridLayout grid;
private ImageIcon myicons[], intro;
private int TimeCount = 0, MissedCount = 0, first = 0, placer, pos, selected = 0;
private int ranpos, rannum, takenAR[], doubleAR[], picAR[], match = 0;
private String iconbasename = "myicon", iconsuffix = ".gif";
private String[] myCombo = {"1/4 second", "1/2 second", "1 second", "2 seconds"};
private Timer increment, flipdelay;
private PicHandler picH = new PicHandler();
public Block(User user) {
super("Program 2 - Memory Game");
Container container = getContentPane();
lowpanel = new JPanel();
lowpanel.setLayout(new FlowLayout());
lowpanel.setBorder(BorderFactory.createRaisedBevel Border());
highpanel = new JPanel();
highpanel.setLayout(new FlowLayout());
highpanel.setBorder(BorderFactory.createTitledBord er("Menu"));
ButtonHandler handler = new ButtonHandler();
// top panels buttons:
startbut = new JButton("Start Game");
startbut.addActionListener(handler);
helpbut = new JButton("Instructions");
helpbut.addActionListener(handler);
pausebut = new JButton("Pause");
pausebut.addActionListener(handler);
exitbut = new JButton("Quit");
exitbut.addActionListener(handler);
dtime = new JLabel("Delay Time:");
final JComboBox timeCOMBO = new JComboBox(myCombo);
timeCOMBO.setSelectedIndex(2);
timeCOMBO.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String newMode = (String) timeCOMBO.getSelectedItem();
if (newMode.equals("1/4 second")) {
flipdelay = new Timer(250, picH);
} else if (newMode.equals("1/2 second")) {
flipdelay = new Timer(500, picH);
} else if (newMode.equals("1 second")) {
flipdelay = new Timer(1000, picH);
} else {
flipdelay = new Timer(2000, picH);
}
}
});
timel = new JLabel("Time:");
timef = new JTextField(6);
timef.setEditable(false);
missedl = new JLabel("Missed:");
missedf = new JTextField(3);
missedf.setEditable(false);
highpanel.add(startbut);
highpanel.add(helpbut);
highpanel.add(pausebut);
highpanel.add(exitbut);
highpanel.add(dtime);
highpanel.add(timeCOMBO);
lowpanel.add(timel);
lowpanel.add(timef);
lowpanel.add(missedl);
lowpanel.add(missedf);
myicons = new ImageIcon[12];
intro = new ImageIcon("Intro.gif");
for (int i = 0; i < (myicons.length); i++) {
myicons[i] = new ImageIcon(iconbasename + Integer.toString(i + 1) + iconsuffix);
}
takenAR = new int[myicons.length * 2];
mybutton = new JButton[myicons.length * 2];
picAR = new int[myicons.length * 2];
doubleAR = new int[myicons.length];
grid = new GridLayout(4, 6);
mainpanel = new JPanel();
mainpanel.setLayout(grid);
for (int i = 0; i < myicons.length * 2; i++) {
mybutton[i] = new JButton(intro);
mybutton[i].setToolTipText("Pic: " + (i + 1));
mybutton[i].addActionListener(picH);
mainpanel.add(mybutton[i]);
}
container.add(lowpanel, BorderLayout.SOUTH);
container.add(highpanel, BorderLayout.NORTH);
container.add(mainpanel, BorderLayout.CENTER);
increment = new Timer(1000, this);
timef.setText(Integer.toString(TimeCount));
missedf.setText(Integer.toString(MissedCount));
setSize(700, 580);
setVisible(true);
diem = user.getDiem();
JOptionPane.showMessageDialog(null, "Xin chào " + user.getUserName()
+ "
Điểm chơi tốt nhất của bạn là: " + user.getDiem());
}
// execute application
public static void main(String args[]) {
User user = null;
//model.User user=new model.User();
Block application = new Block(user);
application.addWindowListener(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == increment) {
TimeCount++;
timef.setText(Integer.toString(TimeCount)); //increment timer on display
}
}
private class ButtonHandler implements ActionListener {
private int ampaused = 0;
public void actionPerformed(ActionEvent e) {
// start game button:
if (e.getSource() == startbut) {
randompics();
if (first == 0) {
startbut.setText("Restart");
} else {
for (int i = 0; i < myicons.length * 2; i++) {
mybutton[i].setIcon(intro);
}
increment.stop();
TimeCount = MissedCount = match = selected = 0;
missedf.setText(Integer.toString(MissedCount));
}
first++;
increment.start();
} // instructions button:
else if (e.getSource() == helpbut) {
increment.stop();
JOptionPane.showMessageDialog(null,
"Instructions:
* Ấn Start Game để bắt đầu chơi Game."
+ "
* Trong lúc chơi bạn có thể ấn Restart để chơi lại từ đầu."
+ "
* Click vào một bức ảnh bất kỳ và hãy tìm 1 bức ảnh khác giống như vậy."
+ "
* Khi bạn lật được 2 bức ảnh giống nhau thì nó sẽ được lật lên."
+ "
* Khi bạn tìm sai số lần tìm sai sẽ tăng lên một và ảnh sẽ được up lại."
+ "
* Bạn có thể thay đổi thời gian up ảnh bằng cách thay đổi giá trị 'Delay Time'."
+ "
* Điểm của bạn bằng tổng thời gian bạn chơi với số lần chọn sai."
+ "
* Ai có điểm thấp nhất sẽ là người thắng cuộc !"
+ "
* Khi bạn lật được hết tất cả bức ảnh trên bảng bạn sẽ thắng."
+ "
* Thanks for Playing and Have Fun!", "Welcome To The Memory Game", 1);
if ((first != 0) && (pausebut.getText() != "Play") && (match != 12)) {
increment.start();
}
} else if (e.getSource() == pausebut) {
if ((increment.isRunning()) && (ampaused == 0)) {
pausebut.setText("Play");
increment.stop();
ampaused = 1;
} else if (ampaused != 0) {
pausebut.setText("Pause");
increment.start();
ampaused = 0;
}
} // exit button:
else if (e.getSource() == exitbut) {
increment.stop();
JOptionPane.showMessageDialog(null, "Thank You!
Come Again!!");
System.exit(0);
}
}
}
private class PicHandler implements ActionListener {
private int hold1, hold2, hold3, hold4, oldselection, j;
public PicHandler() {
flipdelay = new Timer(1000, this);
}
public void actionPerformed(ActionEvent e) {
if (increment.isRunning()) { //if game has started
if (e.getSource() == flipdelay) {
mybutton[hold4].setIcon(intro);
mybutton[hold3].setIcon(intro);
flipdelay.stop();
} else if (!flipdelay.isRunning()) {
for (j = 0; j < (myicons.length * 2); j++) {
if ((e.getSource() == mybutton[j]) && (takenAR[j] != 7)) {
//and not matched
if (selected < 1) {
selected++;
hold1 = oldselection = picAR[j];
hold3 = j;
mybutton[j].setIcon(myicons[hold1]);
} else {
if (!(j == hold3)) {
hold2 = picAR[j];
if (hold2 == oldselection) {
match++;
takenAR[hold3] = 7;
mybutton[j].setIcon(myicons[hold2]);
takenAR[j] = 7;
if (match == 12) {
increment.stop();
if (diem <= TimeCount + MissedCount) {
JOptionPane.showMessageDialog(null, "You Completed!
Time: "
+ TimeCount + " seconds
Misses: " + MissedCount
+ "
Total Score: " + (TimeCount + MissedCount)
+ "
Press Restart to play again or Quit to leave!");
} else {
JOptionPane.showMessageDialog(null, "You Completed!
Time: "
+ TimeCount + " seconds
Misses: " + MissedCount
+ "
Total Score: " + (TimeCount + MissedCount)
+ "
You have new Score: " + (TimeCount + MissedCount)
+ "
Press Restart to play again or Quit to leave!");
//u = new User(TimeCount + MissedCount,user.getUserName(),user.getPassword()) ;
//u.setDiem(TimeCount + MissedCount);
//u.setUserName(user.getUserName());
// u.setPassword(user.getPassword());
try {
Socket socket = new Socket("127.0.0.1", 8888);
ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
oos.writeObject(TimeCount + MissedCount);
} catch (Exception ex) {
}
}
}
} else {
mybutton[j].setIcon(myicons[hold2]);
hold4 = j;
flipdelay.start();
MissedCount++;
missedf.setText(Integer.toString(MissedCount));
}
selected = 0;
}
}
j = myicons.length * 2;
}
}
}
}
}
}
private void randompics() {
for (int i = 0; i < myicons.length * 2; i++) {
takenAR[i] = 0;
}
for (int i = 0; i < (myicons.length); i++) {
doubleAR[i] = 0;
}
for (int i = 0; i < myicons.length * 2; i++) {
placer = getran();
pos = getpos();
picAR[pos] = placer;
takenAR[pos] = 1;
doubleAR[placer]++;
}
}
private int getran() {
rannum = (int) Math.round((Math.random()) * (myicons.length - 1));
if (doubleAR[rannum] == 2) {
getran();
}
return rannum;
}
private int getpos() {
ranpos = (int) Math.round((Math.random()) * ((myicons.length * 2) - 1));
if (takenAR[ranpos] == 1) {
getpos();
}
return ranpos;
}
}View more random threads:
- Load Data không được , các bro chỉ dùm
- [Help] Lỗi Start TomCat
- mọi người giải thích giùm em đoạn code này với
- coder nào xem hộ e bài tính tổng với nó báo lỗi hoài
- Lỗi gì không biết
- có bài tập nhờ a e giúp
- Hàm nhập mảng
- Tìm chuỗi con trong đoạn text
- Học Java từ đầu như thế nào anh chị ơi và làm sao để học được ạ
- Hỏi về lỗi trong lập trình socket mutithread
Thông qua phân tích dữ liệu Google từ 86 quốc gia, mới đây, một công ty tại Anh đã công bố bảng xếp hạng kích tấc "cậu nhỏ" của các nước trên thế giới. Kết quả, hầu hết các nước xếp ở nhóm đầu của...
"Chim" của chàng trai Việt thuộc...