じゃんけんプログラムを規則性のあるパターンに置き換えたい
Q&A
Closed
以下の3つのプログラムをいじって、Jhonの手を
グー、パー、チョキ、グー、グー、パー、チョキ、グー、…
と規則性のある手を出したいのですが、どうやってプログラムしたらいいのかが全くわかりません。
(ちなみに、3回先に勝利したほうが勝ちとしています)
①
public class NormalComputerPlayer extends Player {
protected Hand goo, choki, par;
protected java.util.Random random; // 手を決めるときに使う乱数
/** コンストラクタ, プレイヤの名前を指定する */
public NormalComputerPlayer(String name) {
super(name);
// 手を設定する
goo = new Goo();
choki = new Choki();
par = new Par();
random = new java.util.Random();
}
/** match回目の勝負のnum回目のプレイヤの手を出す
* Player の showHand をオーバーライド
*/
public Hand showHand(int match, int num) {
int hand = random.nextInt(3);
if (hand == 0) {
return goo;
} else if (hand == 1) {
return choki;
} else {
return par;
}
}
}
②
import javax.swing.*;
public class HumanPlayer extends Player {
protected Hand hands[]; // プレイヤの手
/** コンストラクタ */
public HumanPlayer(String name) {
super(name);
// 手を設定する
hands = new Hand[3];
hands[0] = new Goo();
hands[1] = new Choki();
hands[2] = new Par();
}
/** match回目の勝負のnum回目のプレイヤの手を選ぶ
* Player の showHand をオーバーライド
*/
public Hand showHand(int match, int num) {
// ダイアログを表示して手を選ばせる
// selectedは選んだ手の,配列handsの要素番号になっている
int selected =
JOptionPane.showOptionDialog(null, "Select Your Hand",
"Select Your Hand",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE,
null, hands, null);
Hand hand = hands[selected];
return hand;
}
}
③
/** ジャンケンを開始するためのクラス */
public class Janken {
public static void main(String args[]) {
try {
Judge judge = new Judge(); // 審判の作成
// プレイヤの作成と審判への登録
Player jhon = new NormalComputerPlayer("Jhon");
judge.registPlayer(jhon);
Player paul = new NormalComputerPlayer("Paul");
judge.registPlayer(paul);
// 3回勝負でジャンケンの開始
judge.startJankens(3);
}
catch (JankenException e) {
System.err.println("Error: " + e.getMessage());
}
}
}
①のプログラムをいじって、規則性のあるものにするのかな?と思い、とりあえず①’を作ってみたのですが、余計にわからなくなりました
/** ジャンケン の通常のコンピュータ プレイヤ */
public class NormalComputerPlayer extends Player {
protected Hand goo, choki, par;
protected java.util.Random random; // 手を決めるときに使う乱数
/** コンストラクタ, プレイヤの名前を指定する */
public NormalComputerPlayer(String name) {
super(name);
// 手を設定する
goo = new Goo();
choki = new Choki();
par = new Par();
}
/** match回目の勝負のnum回目のプレイヤの手を出す
* Player の showHand をオーバーライド
*/
public Hand showHand(int match, int num) {
int hand ,i;
if (hand == 0) {
return goo;
} else if (hand == 1) {
return par;
} else {
return choki;
}
for(i=0;i<100;i++){
hand=i%3;
}
}
}
どうすれば期待しているものが出力されるのでしょうか。
よろしくお願いします
ちなみに、今のままでは、ランダムになっているので、Johnの手は以下のようになります。
$ java Janken
Match 1: Jan Ken Pon!
Match 1-1:
Jhon's hand is Goo
Paul's hand is Goo
Aiko de Sho
Match 1-2:
Jhon's hand is Goo
Paul's hand is Par
Match 1: Paul won.
Match 2: Jan Ken Pon!
Match 2-1:
Jhon's hand is Par
Paul's hand is Goo
Match 2: Jhon won.
Match 3: Jan Ken Pon!
Match 3-1:
Jhon's hand is Par
Paul's hand is Goo
Match 3: Jhon won.
Match 4: Jan Ken Pon!
Match 4-1:
Jhon's hand is Goo
Paul's hand is Par
Match 4: Paul won.
Match 5: Jan Ken Pon!
Match 5-1:
Jhon's hand is Goo
Paul's hand is Choki
Match 5: Jhon won.
Jhon is a champion.
Jhon: 3 wins
Paul: 2 wins