以下のコードい対する私の理解の確認と疑問点への回答をお願いします。
理解確認❶(実行しスタートボタンのみ押した場合の一番は初めの変化)
ArrayListのslotに入っている画像とintの配列変数iCountは以下のように変化する
slot(インデックス0)slot.jpg1→slot3.jpg
slot(インデックス1)slot.jpg3→slot5.jpg
slot(インデックス2)slot.jpg5→slot7.jpg
iCount{1,3,5}→iCount{2,4,6}
理解確認❷(❶のあとストップボタンを押さなかった場合)
slotの画像、iCount共に1づつ増えていく。
自分でも確認しましたが、理解はあっておりますでしょうか。
疑問点❶
switch文の中の
case THREE:
timer.stop();
if (iCount[0] == iCount[1] && iCount[1] == iCount[2]) {
if (iCount[0] == MAX - 1)
lbel.setText("やった!!最高得点だ");
else
lbel.setText("やった!!おめでとう");
} else
ですがなぜif (iCount[0] == MAX - 1)をするのでしょうか。
7が三つ揃えばlbel.setText("やった!!最高得点だ");表示されますが、なぜ配列の1番目が6であれば良いのでしょうか。
Java
1package week11; 2 3import java.awt.event.ActionEvent; 4import java.awt.event.ActionListener; 5import java.util.ArrayList; 6 7import javax.swing.ImageIcon; 8import javax.swing.JButton; 9import javax.swing.JFrame; 10import javax.swing.JLabel; 11import javax.swing.JPanel; 12import javax.swing.Timer; 13 14enum Stop { 15 ONE, TWO, THREE 16} 17 18public class ThreeSlot extends JFrame implements ActionListener { 19 20 private static final long serialVersionUID = 1L; 21 22 final int MAX = 7; 23 Stop iStopCount = Stop.ONE; 24 int[] iCount = { 1, 3, 5 }; 25 ArrayList<ImageIcon> SlotImage = new ArrayList<ImageIcon>(); 26 ArrayList<JLabel> slot = new ArrayList<JLabel>(); 27 Timer timer; 28 JButton[] bt; 29 JLabel lbel = new JLabel("スタートで始まるよ", JLabel.CENTER); 30 31 JPanel init_Button(String... args) { 32 JPanel p = new JPanel(); 33 bt = new JButton[args.length]; 34 for (int i = 0; i < args.length; i++) { 35 bt[i] = new JButton(args[i]); 36 bt[i].setEnabled(false); 37 bt[i].addActionListener(this); 38 p.add(bt[i]); 39 } 40 return p; 41 } 42 43 ThreeSlot(String title) { 44 super(title); 45 timer = new Timer(300, this); 46 timer.setActionCommand("timer"); 47 setBounds(200, 200, 230, 150); 48 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 49 add("North", lbel); 50 for (int k = 1; k <= MAX; k++) 51 SlotImage.add(new ImageIcon("src/" + "Slot" + k + ".jpg")); 52 slot.add(new JLabel(SlotImage.get(0))); 53 slot.add(new JLabel(SlotImage.get(2))); 54 slot.add(new JLabel(SlotImage.get(4))); 55 JPanel p = new JPanel(); 56 for (JLabel l : slot) 57 p.add(l); 58 add("Center", p); 59 add("South", init_Button("スタート", "ストップ")); 60 bt[0].setEnabled(true); 61 setVisible(true); 62 } 63 64 public static void main(String[] args) { 65 new ThreeSlot("スロットマシン"); 66 } 67 68 public void actionPerformed(ActionEvent e) { 69 String cmd = e.getActionCommand(); 70 if (cmd.equals("timer")) { 71 int i = 0; 72 if (iStopCount == Stop.TWO) 73 i = 1; 74 else if (iStopCount == Stop.THREE) 75 i = 2; 76 for (; i < iCount.length; i++) { 77 if (++iCount[i] == MAX) 78 iCount[i] = 0; 79 slot.get(i).setIcon(SlotImage.get(iCount[i])); 80 } 81 } 82 if (e.getSource() == bt[0]) { 83 timer.start(); 84 lbel.setText("ストップで数字を揃えよう"); 85 bt[0].setEnabled(false); 86 bt[1].setEnabled(true); 87 } else if (e.getSource() == bt[1]) { 88 switch (iStopCount) { 89 case ONE: 90 lbel.setText("2つ目も" + (iCount[0] + 1) + "で止めよう"); 91 iStopCount = Stop.TWO; 92 break; 93 case TWO: 94 lbel.setText("ストップで全部止まるよ"); 95 if (iCount[0] == iCount[1]) 96 lbel.setText("リーチだ"); 97 iStopCount = Stop.THREE; 98 break; 99 case THREE: 100 timer.stop(); 101 if (iCount[0] == iCount[1] && iCount[1] == iCount[2]) { 102 if (iCount[0] == MAX - 1) 103 lbel.setText("やった!!最高得点だ"); 104 else 105 lbel.setText("やった!!おめでとう"); 106 } else 107 lbel.setText("残念でした"); 108 iStopCount = Stop.ONE; 109 bt[0].setEnabled(true); 110 bt[1].setEnabled(false); 111 } 112 } 113 } 114}
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/01/16 09:19 編集
2019/01/16 09:00
2019/01/16 09:29
2019/01/16 09:35