以下のプログラムでは、手がランダムに出されるのですが、これを少し変えて
パー、グー、チョキ、パー、グー、チョキ・・・
のように規則性があるように手を出させるためにはどうしたら良いですか。
** ジャンケン の通常のコンピュータ プレイヤ */
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;
}
}
}
public abstract class Player {
protected String name; // プレイヤの名前
protected int matches; // matches回勝負(matches回先に勝った方が勝ち)
protected int wins; // プレイヤの勝利数
protected int losts; // プレイヤの負け数
/** コンストラクタ, プレイヤの名前を指定する */
public Player(String name) {
this.name = name; // プレイヤの名前
wins = losts = 0; // 勝敗を初期化
}
/** プレイヤの名前を返す */
public String getName() {
return name;
}
/** 何回勝負か設定するメソッド
*/
public void setMatches(int matches) {
this.matches = matches;
}
/**
* 勝ったことを知らされるメソッド
*/
public void youWon() {
wins++;
}
/**
* 負けたことを知らされるメソッド
*/
public void youLost() {
losts++;
}
/** match回目の勝負のnum回目のプレイヤの手を出す
* 抽象メソッドなので,サブクラスでオーバーライドする
*/
public abstract Hand showHand(int match, int num);
}