4歳の子供とのテレビのチャネル争いが絶えない。笑
休日は、特にね。そんな時に、閃いた!!!
最近、保育園で、「ひらがな」と「カタカナ」を勉強したとの事なので、ジャンケンゲームを作ってあげれば、それで一日中、遊ぶのではないか?
「Youtubeを見るよりも、文字の勉強になるのでは?」と都合よくも考えて、試作しました。
参考サイトはこれで、私が下部にアップしたコードは、このサイトからの「ほぼコピー」です。
https://camp.trainocate.co.jp/magazine/java-game/
ただ、上記サイトは、パッケージ概念の説明が主目的なので、パッケージを使っているが、私は、それを単一コードに書き換えました。(それだけ。猿でもできる。。。。)
「アンパンマンじゃんけんゲーム」と命名しました。
こんな感じです。
グー、チョキ、パーを子供が選びます。
引き分けなら、もう一回です。
結構、子供は夢中になってやっております。
因みに、「5回連続でコンピューターに勝ったら、テレビ視聴権を譲る!」と子供と約束をしておりますので、10分くらいは、テレビを見れます!
意外と使える事に気がついた。
<下記がコードです。>
とりあえず、動きますが、UIをもうちょっとブラッシュアップする予定。
import javax.swing.*;
import java.util.Random;
/**
* ゲームのメイン画面
*/
class MainWindow {
/**
* ゲームを表示するフレーム
*/
private final JFrame frame;
/**
* メッセージを表示するラベル
*/
private final JLabel messageLabel;
/**
* グー のボタン
*/
private final JButton rockButton;
/**
* チョキ のボタン
*/
private final JButton scissorsButton;
/**
* パー のボタン
*/
private final JButton paperButton;
/**
* プレイ状況のステータス
*/
private Status playState;
/**
* 相手が出した手
*/
private Hands opponentHand;
/**
* コンストラクタ
*/
// public MainWindow() {
MainWindow() {
// 画面生成
this.frame = new JFrame("アンパンマン じゃんけんゲーム!");
this.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 画面サイズを指定
this.frame.setBounds(200, 200, 600, 400);
var pane = this.frame.getContentPane();
// このcanvasに対して、ボタンやラベルを配置していく
var canvas = new JPanel();
// 自由レイアウトに変更する
canvas.setLayout(null);
// ラベル
this.messageLabel = new JLabel("じゃーんけーん・・・");
this.messageLabel.setBounds(20, 20, 400, 30);
canvas.add(this.messageLabel);
// --------------
// ボタンを作成する
// --------------
// グー
this.rockButton = new JButton(Hands.Rock.getDisplay());
this.rockButton.setBounds(100, 100, 100, 40);
this.rockButton.addActionListener((e) -> this.selectHand(Hands.Rock));
canvas.add(this.rockButton);
// チョキ
this.scissorsButton = new JButton(Hands.Scissors.getDisplay());
scissorsButton.setBounds(250, 100, 100, 40);
this.scissorsButton.addActionListener((e) -> this.selectHand(Hands.Scissors));
canvas.add(scissorsButton);
// パー
this.paperButton = new JButton(Hands.Paper.getDisplay());
paperButton.setBounds(400, 100, 100, 40);
this.paperButton.addActionListener((e) -> this.selectHand(Hands.Paper));
canvas.add(paperButton);
// 画面にCanvasを追加
pane.add(canvas);
}
/**
* 画面表示
*/
void show() {
this.init();
this.frame.setVisible(true);
}
/**
* ゲームの初期化
*/
void init() {
// 相手の手をリセットし、待ち状態にする
this.opponentHand = Hands.getRandomHand();
this.playState = Status.Wait;
}
/**
* 自分の手を選んだ時の処理
*
* @param selected 選択した手
*/
void selectHand(Hands selected) {
// 入力待ちでなければ以降の処理はしない
if (this.playState != Status.Wait) {
return;
}
// 勝ち負けの判定
switch ((selected.getNumber() - opponentHand.getNumber() + 3) % 3) {
case 0:
// 引き分けなので継続
this.messageLabel.setText("アンパンマンもおなじだよ。あーいこーだよ。もういっかい!");
// 手を出しなおす
this.init();
break;
case 1:
// 負け
this.messageLabel.setText(String.format("アンパンマンは「%s」なのであなたの「まけ」です。Lost !!!!", this.opponentHand.getDisplay()));
// ゲーム終了
this.playState = Status.Done;
break;
case 2:
this.messageLabel.setText(String.format("アンパンマンは「%s」なのであなたの「かち」です。WINNER !!!", this.opponentHand.getDisplay()));
// ゲーム終了
this.playState = Status.Done;
break;
}
}
}
/**
* ジャンケンの手
*/
enum Hands{
/** グー */
Rock("グー", 0),
/** チョキ */
Scissors("チョキ", 1),
/** パー */
Paper("パー", 2);
/** 表示文字 */
private final String display;
/** 内部番号 */
private final int number;
/**
* コンストラクタ
*
* @param display 表示名
* @param number 番号
*/
Hands(String display, int number) {
this.display = display;
this.number = number;
}
/**
* ランダムな手を生成
*
* @return ランダムに生成した手
*/
static Hands getRandomHand() {
Random rand = new Random();
return Hands.values()[rand.nextInt(3)];
}
/**
* 表示名を取得
*
* @return 表示名
*/
String getDisplay() {
return this.display;
}
/**
* 番号を取得
*
* @return 番号
*/
int getNumber() {
return this.number;
}
}
/**
* ゲームのプレイ状況
*/
enum Status {
/** 待ち状態 */
Wait,
/** 結果発表中 */
Done
}
public class Main {
public static void main(String[] args) {
// ゲームWindowの生成と表示
MainWindow window = new MainWindow();
window.show();
}
}