遊客:  註冊 | 登錄 | 幫助





標題: [求助] Java: 一個數字matching game 的問題 (完成了)
  CoolQQ     Rank: 3
青出藍
性別 保密
UID 301

精華 0
帖子 247
積分 474   詳情

閱讀權限 40
註冊 2006-6-28
來自
狀態 離線

 
 
 
 
發表於 2007-4-21 12:14 PM  資料  個人空間  短訊  加為好友 
Java: 一個數字matching game 的問題 (完成了)

完成了~~ 多謝晒兩位勁人!!!
萬分感激~~


heheehe

========================
Hello 各位, 有野想問問你地~
我而家做緊個數字 matching game
有 48張卡, 要generate 0-99 個數字.  (呢d都做好了)
(下面個 Integer method, 為左測試易d, 我改左佢做 MAX_VALUE = 20, 等佢做0-19個數)
(呢個integer method 係 teacher example 度修改的)

其實而家做左個簡單既 matching, 個 result 都岩既
(我做到去 get 到2張卡既數字, 可以比較兩張卡岩唔岩,
  岩既就 say "get pair"  唔岩就 say "sorry"    -.-)

Menu果度果d白痴 button, 整左但係未做action.

而家有幾個問題想問問各位:
1. 如果我想 click 一張卡 (button), 想佢 show 個數字出黎, 同埋 唔可以再click多次 (再click會出pop up), 點整??   <--- 整到一click出個數字Lu~~
2. 兩個button 數字唔match, 想enable 番兩個button 同埋 hide 番個數字, 又係點呢?  <--- 都做到lu
3. 搵到一對 match 之後, disable 晒兩個button   <--- Also 做完
4. Reset button <---都做好了(勁多謝米奇!!)

>>>>>>>>>>>仲有 timer 同 click number 咋
Timer... 努力中
click number.... 唔難~


... 另外我research 緊點樣加 Timer 入去... (可能遲d都會求救 -.-)


(大致上 major problem stuck o係呢度)


我係program 新手, 哈哈, 可能好多野寫到好 confusing,
我諗住做到佢work之後先再執執佢.


希望大家幫到我啦~~
唔該晒大家 ~~


我阿蛇既 sample game:  http://www2.cs.uh.edu/~jqchen/games/game.html
(睇左呢個可能會明我講乜 haha)





最後編輯: CoolQQ : 2007-4-23 09:15 AM
頂部

mickeyGoUp     Rank: 7Rank: 7Rank: 7
版主
性別 男
UID 5

精華 0
帖子 35511
積分 5235   詳情

閱讀權限 150
註冊 2006-3-24
來自 美國滴滴尼
狀態 離線

 
 
 
 
發表於 2007-4-22 01:56 AM  資料  個人空間  短訊  加為好友 
咁您想果個數字 show 響邊度呢?  係唔係應該加個 JLabel 落去﹐咁可以用 setText() 黎顯示個數字﹐之後可以 setText("") 去整走佢?

JButton 有個  setEnabled(boolean b) method﹐可以 enable 或 disable 個 button 既。 我估您可以加入去個 listener 度。 但之後要 enable 返佢﹐就需要有 button 既 reference。  我見到您有個  private JButton button;  ﹐可唔可以改做 private JButton[] button = new JButton[2]; 呢?
之後就響 addButton() 裡面﹐under JButton button = new JButton(); 下面加: this.button[count] = button;

咁您就可以響其他地方 call: this.button[0].setEnable(true) 去 enable 第一個 button。  this.button[1] 就係第二個 button。

試下得唔得?

頂部



  CoolQQ     Rank: 3
青出藍
性別 保密
UID 301

精華 0
帖子 247
積分 474   詳情

閱讀權限 40
註冊 2006-6-28
來自
狀態 離線

 
 
 
 
發表於 2007-4-22 04:39 AM  資料  個人空間  短訊  加為好友 
d數字一開始係唔show出黎既
之後一click個button 就會show係個 button 上面

即係好似玩呢隻game時 開牌咁.


將setenabled 加入actionlistener, 我試過~
咁樣加:  button.setEnabled(false);

但係加左入去, 就會所有button都disable左,
唔知點樣maintain到個mouse click落去先會disable一個 button.


teacher example:    http://www2.cs.uh.edu/~jqchen/games/game.html

===========

岩岩發現我講錯左, 而家改一改先:

問題係:

1. 如果我想 click 一張卡 (button),  唔可以再click多次 (再click會出pop up), 點整??     <--- 整到一click出個數字Lu~~
2. 兩個button 數字唔match, 想enable 番兩個button 同埋 hide 番個數字, 又係點呢?
3. 搵到一對 match 之後, disable 晒兩個button  <--program 點知係邊兩個button click過?



最後編輯: CoolQQ : 2007-4-22 06:36 AM
頂部

  lhy1     Rank: 2Rank: 2
藍之初
性別 保密
UID 30112

精華 0
帖子 3
積分 100   詳情

閱讀權限 30
註冊 2007-4-22
來自
狀態 離線

 
 
 
 
發表於 2007-4-22 10:33 AM  資料  個人空間  短訊  加為好友 
First of all the structure of you application is really really bad. I think it is better to make a new class extends from JButton and work with it. Inside the button class you can assign each button it's own number. So there is no need to store it into first or second. Because you have to find out which button you have clicked on anyway.

I have change your code a little bit. I have make it just like your teacher's example. But again the structure of your game is really really bad. If you have question. I am always ready to answer. Altho I am not a Java Expert but I can answer basic questions.

Find the changes with WinMerge or that kind of applications. I am too lazy to point them out.

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Shape;
import java.awt.Cursor;
import java.awt.event.ActionEvent;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import java.awt.geom.Area;

import java.awt.*;
import javax.swing.*;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;


public class matchgame {
        public static void main(String[] args)
        {
                GameFrame frame = new GameFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setVisible(true);
        }
}

class GameFrame extends JFrame
{
        private JButton button, firstButton, secondButton;
        JPanel buttonPanel;
        int cardnum = 0;
        private String first, second;
      
        public GameFrame()
        {
                setTitle("Game Frame");
                setSize(600, 500);

                JMenuBar menuBar = new JMenuBar();
               
                JRadioButton LGlayButton = new JRadioButton("LightGray", true);
                menuBar.add(LGlayButton);
                JRadioButton YellowButton = new JRadioButton("Yellow", false);
                menuBar.add(YellowButton);
                JButton ResetButton = new JButton("Reset");
                menuBar.add(ResetButton);
               
                setJMenuBar(menuBar);
               
        buttonPanel = new JPanel(new GridLayout(6, 8));
        addButton();
        getContentPane().add(buttonPanel);
        }
      
        private void addButton()
        {
                IntegerPair pair = new IntegerPair();
                int [] array = pair.GeneratePairedIntegerArray();
                for (int count = 0; count < array.length; count++)
                {
                        JButton button = new JButton();
                        button.setActionCommand(Integer.toString(array[count]));
                        button.addActionListener(new ActionListener()
                        {
                                public void actionPerformed(ActionEvent e)
                                {
                                    if (cardnum == 2)
                                    {
                                            if(firstButton != secondButton){
                                            if(comparecard()){
                                                    firstButton.setEnabled(false);
                                                    secondButton.setEnabled(false);
                                            } else {
                                                    firstButton.setText("");
                                                    secondButton.setText("");
                                            }
                                            } else {
                                                    secondButton = null;
                                                    cardnum--;
                                            }
                                    }
                                       
                                        String flipcard = e.getActionCommand();
                                        cardnum++;
                                        JButton b = (JButton) e.getSource();
                                        setcard (flipcard, cardnum, b);
                                        b.setText(flipcard);
                                }
                        });
                        buttonPanel.add(button);
                }
        }
      
        private void setcard(String card, int cardnum, JButton button)
        {
                if (cardnum == 1)
                {
                        first = card;
                        firstButton = button;
                        System.out.println("First card is: " + first + "\n");
                }
               
                else if (cardnum == 2)
                {
                        second = card;
                        secondButton = button;
                        System.out.println("Second card is: " + second + "\n");
                }
        }
      
        public String getfirstCard()
        {
                return first;
        }
      
        public String getsecondCard()
        {
                return second;
        }
      
        private boolean comparecard()
        {
                String a = getfirstCard();
                String b = getsecondCard();
                int firstcards = Integer.parseInt(a);
                int secondcards = Integer.parseInt(b);
                if (firstcards == secondcards)
                {
                        System.out.println("get pair!!");
                        cardnum = 0;
                        return true;
                }
               
                else
                {
                        cardnum = 0;
                        System.out.println("sorry!!");
                        return false;
                }
        }
}

        class IntegerPair
        {
                public int MAX_VALUE = 20;
                public int ARRAY_SIZE = 48;
               
                private int [] sampleArray;
               
                public IntegerPair()
                {
                        sampleArray = InitializeArray(MAX_VALUE);
                }
               
                public int [] GeneratePairedIntegerArray()
                {
                        int [] intArray = InitializeArray(ARRAY_SIZE);
                        int [] indexArray = InitializeArray(ARRAY_SIZE);
                       
                        int currentIndexSize = indexArray.length;        //10
                        int iterationNum = intArray.length / 2;                //5
                        for (int i = 0; i < iterationNum; i++)
                        {
                                int sampleIndex = (int)(Math.random() * sampleArray.length);
                                //random 0-99 number
                              
                                int index = (int)(Math.random() * currentIndexSize);
                                intArray[indexArray[index]] = sampleArray[sampleIndex];
                                indexArray[index] = indexArray[--currentIndexSize];
                              
                                index = (int)(Math.random() * currentIndexSize);
                                intArray[indexArray[index]] = sampleArray[sampleIndex];
                                indexArray[index] = indexArray[--currentIndexSize];
                        }
                       
                        return intArray;
                }
               
                private int [] InitializeArray(int size)
                {
                        int [] array = new int [size];
                       
                        for (int i = 0; i < size; i++)
                        {
                                array[i] = i;
                        }
                       
                        return array;
                }      
        }



CoolQQ :
thanks for helping
呢個只係 draft黎
未係 final copy,
所以 structure 上係會亂少少.

都係果句, 做好basic既functions 後,
先再執好佢.

anyways, o係你post出黎之前
我已經做到 button disable lu~
暫時係得番 Reset, timer, 仲有 Number of clicks 要理...

Again... 我知係好亂....
我都話係draft lor...
唔駛講兩次掛.... 好hurt....

頂部

mickeyGoUp     Rank: 7Rank: 7Rank: 7
版主
性別 男
UID 5

精華 0
帖子 35511
積分 5235   詳情

閱讀權限 150
註冊 2006-3-24
來自 美國滴滴尼
狀態 離線

 
 
 
 
發表於 2007-4-22 12:03 PM  資料  個人空間  短訊  加為好友 
嘩! lhy 真好野!  幫您執過晒個 class 添!!    我呢 d 唔熟 AWT/Swing 既﹐都係企響側邊幫頭幫尾算勒。  

QUOTE:
1. 如果我想 click 一張卡 (button),  唔可以再click多次 (再click會出pop up), 點整??     <--- 整到一click出個數字Lu~~
2. 兩個button 數字唔match, 想enable 番兩個button 同埋 hide 番個數字, 又係點呢?
3. 搵到一對 match 之後, disable 晒兩個button  <--program 點知係邊兩個button click過?

1. 點解唔索性 disable 左第一個 button 呢?  到開第二張唔 match 時先 enable 返佢 (hide 埋 d 數字).
2. 請睇 lhy's codes 中 if(comparecard()){...} else {....}
3. 如果我無估錯﹐lhy's codes 中果一行 JButton b = (JButton) e.getSource(); 就可以取得被 click 既 button 既 reference 勒。


lhy﹐ 係唔係應該將 if  if (cardnum == 2) 調落去呢?  否則要 click 第三張 card 先會入到去 (cardnum == 2) 喎?

                        button.addActionListener(new ActionListener()
                        {
                                public void actionPerformed(ActionEvent e)
                                {
                                        String flipcard = e.getActionCommand();
                                        cardnum++;
                                        JButton b = (JButton) e.getSource();
                                        setcard (flipcard, cardnum, b);
                                        b.setText(flipcard);

                                    if (cardnum == 2)
                                    {
                                            if(firstButton != secondButton){
                                            if(comparecard()){
                                                    firstButton.setEnabled(false);
                                                    secondButton.setEnabled(false);
                                            } else {
                                                    firstButton.setText("");
                                                    secondButton.setText("");
                                            }
                                            } else {
                                                    secondButton = null;
                                                    cardnum--;
                                            }
                                    }
                                }
                        });
                        buttonPanel.add(button);
                }


頂部

  lhy1     Rank: 2Rank: 2
藍之初
性別 保密
UID 30112

精華 0
帖子 3
積分 100   詳情

閱讀權限 30
註冊 2007-4-22
來自
狀態 離線

 
 
 
 
發表於 2007-4-22 09:53 PM  資料  個人空間  短訊  加為好友 
Yes you can put the if comparecard() back down. But if I look at the sample of his teacher it works this way. So I have move it up there for him. Also the compare function is a good example of bad structure. Instead of setting class var's to remember what the first card is and what is the second card you should give them as parameter to the compare function. This way if you want to change the game in the future to open up 3 each turn you can easily do it in the compareCard() function. no need to add another third card etc...

Sorry to sound so critical and rude. But again the structure of a application is really the most important part. It is fatal if the structure is wrong in the later stage.

mickeyGoUp: I lost my password on my account. I dont remember what my "hidden question" is. So I cannot retrieve my password. Can you help me with that?

CoolQQ :
首先都好多謝你既教導同意見 :)
我本身唔係CS, 所以好少寫program
只係閒科要take~~

我對 programming 有興趣架 :)
會再學下點去做好d logic 上既野~

而家務求想有份野做到work~ 哈哈

Reset 方面好困擾... 仲未諗到點解決到.

Timer 都仲未試... 聽日就要交lu

頂部

  CoolQQ     Rank: 3
青出藍
性別 保密
UID 301

精華 0
帖子 247
積分 474   詳情

閱讀權限 40
註冊 2006-6-28
來自
狀態 離線

 
 
 
 
發表於 2007-4-22 09:55 PM  資料  個人空間  短訊  加為好友 
如果o係contentpane 入面, 想 remove 已存在既 button,
有方法remove再重新做過一set 新的buttons 嗎?? (Reset the game)

我試過 getContentPane.remove(buttonPanel)
跟住再add 番, 都唔得既?
要click一click個frame既框框, 佢先真正咁reset到...
好奇怪.

頂部

mickeyGoUp     Rank: 7Rank: 7Rank: 7
版主
性別 男
UID 5

精華 0
帖子 35511
積分 5235   詳情

閱讀權限 150
註冊 2006-3-24
來自 美國滴滴尼
狀態 離線

 
 
 
 
發表於 2007-4-23 12:41 AM  資料  個人空間  短訊  加為好友 


QUOTE:
原帖由 lhy1 於 2007-4-22 08:53 AM 發表
Yes you can put the if comparecard() back down. But if I look at the sample of his teacher it works this way. So I have move it up there for him. Also the compare function is a good example of bad structure. Instead of setting class var's to remember what the first card is and what is the second card you should give them as parameter to the compare function. This way if you want to change the game in the future to open up 3 each turn you can easily do it in the compareCard() function. no need to add another third card etc...

Sorry to sound so critical and rude. But again the structure of a application is really the most important part. It is fatal if the structure is wrong in the later stage.

mickeyGoUp: I lost my password on my account. I dont remember what my "hidden question" is. So I cannot retrieve my password. Can you help me with that?

I totally agreed with you about how important the design (or structure) of an application is.  I am a big fan of object reuse and easy maintenance.    

At the same time, I think we should also adjust our standard and try not to expect too much from a college class homework assignment.    I remember when I was taking a C cource, I was totally lost in my first assignment.  And I learned the good programming styles later when I started to write more codes as the same time to learn from others.

Sorry I did not have time to read the teacher's example.  Don't you think the if (cardnum == 2) was at the top, it won't be true until the third card is clicked?  Because the increment cardnum++ is after the if (cardnum == 2), when the second card was clicked, cardnum was still 1 at that point.  It won't work the way it is supposed to unless we changed it to (cardnum == 1) or move it down to the bottom.

I have pm-ed you about your old account.  

頂部

mickeyGoUp     Rank: 7Rank: 7Rank: 7
版主
性別 男
UID 5

精華 0
帖子 35511
積分 5235   詳情

閱讀權限 150
註冊 2006-3-24
來自 美國滴滴尼
狀態 離線

 
 
 
 
發表於 2007-4-23 12:53 AM  資料  個人空間  短訊  加為好友 


QUOTE:
原帖由 CoolQQ 於 2007-4-22 08:55 AM 發表
如果o係contentpane 入面, 想 remove 已存在既 button,
有方法remove再重新做過一set 新的buttons 嗎?? (Reset the game)

我試過 getContentPane.remove(buttonPanel)
跟住再add 番, 都唔得既?
要click一c ...

remove 之後您有無再做 buttonPanel = new JPanel(new GridLayout(6, 8)); 先 addButton(); 呢?

同埋可以試下在 addButton()  之後 call:

getContentPane().validate();

因為我真係唔係好熟 Swing﹐暫時又未有時間幫您 research﹐所以我都係估下﹐如果講錯就勞煩其他高手更正。

CoolQQ :
唔該晒大家~~
米奇, 有呀, 我有grid多一次~~

我試下你講既:
getContentPane().validate(); 先~~
一陣再回~~~

========================
WAAAAAAAAAAAA
米奇, work啦,
真係差呢一句, 好勁呀~~~
唔該晒呀!!!!!




萬分感激你地兩位勁人 :) 真係幫左我好多 hehe
thanks thanks~~

頂部


快速美言











一群熱心會員對本主題作出以下的回覆:

CoolQQ: RMK added - PID 103427 [ view ]
CoolQQ: RMK added - PID 103427 [ view ]
CoolQQ: RMK added - PID 103592 [ view ]
CoolQQ: RMK added - PID 103657 [ view ]
CoolQQ: RMK added - PID 103657 [ view ]



最後回覆日期: 2007-4-23 01:56 AM
  編輯帖子
快速美言
           


當前時區 GMT+8, 現在時間是 2024-5-7 05:17 PM

    Powered by Discuz!  © 2001-2007 Comsenz Inc.   
Processed in 0.978728 second(s), 8 queries

清除 Cookies - 聯繫我們 - LIPS Corner 新天藍 - Archiver