/*--------------------------------------------- Not100ゲーム アプレット版 (300×300) ------------------------------------------------ */ import java.applet.*; import java.awt.*; import java.awt.event.*; public class Applet_Not100_game extends Applet implements ActionListener { Button btn; // リセット用ボタン TextField txt; // テキストフィールド Font font; // メッセージ描画用フォント Label lb; int start_FLG; //スタート用フラグ(はじめはpaintしない) int num_human; //人間の「手」 int num_comp; //コンピュータの「手」 int turn; //誰の番か int YOU=1; //人間の番 int COMP=2; //コンピュータの番 int now; // 現在の値 int max=20; // ゲームの終了値 /* -------------- 初期設定 --------------- */ public void init(){ btn=new Button("Not20 ゲームスタート"); this.add(btn); btn.addActionListener(this); lb =new Label("あなたの手を入れてください(1〜3)"); lb.setBackground(Color.yellow); this.add(lb); txt=new TextField(10); this.add(txt); txt.addActionListener(this); font=new Font(null,Font.PLAIN,24); resetGame(); } /* ------------ ゲームスタート ------------- */ public void resetGame(){ start_FLG=0; now=0; turn=YOU; //あなたが先攻 } /* -------------- 描画 --------------- */ public void paint(Graphics g){ if ( start_FLG != 0 ) { //初回だけpaintをスキップする if (now=max) { g.setColor(Color.magenta); // g.drawString("debug turn="+turn,50,300); { if (turn==YOU) g.drawString("あなたの勝ちです! ",50,230); else g.drawString("残念!負けました ",50,230); } } } } } /* ---------------- アクションリスナー ---------------- */ public void actionPerformed(ActionEvent ev){ Object src=ev.getSource(); if( src==txt ) { try{ num_human=Integer.parseInt(txt.getText()); start_FLG=1; } catch(NumberFormatException e) { } txt.setText(""); } if ( src==btn ) { resetGame(); } repaint(); } }