界面
源碼
挑戰(zhàn)模式可設(shè)置猜測次數(shù)融撞。
package top.warmj.guessnumber;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
public class Client extends Frame implements ActionListener {
private TextArea ta_result;
private TextField tf_input;
private Button b_define;
private Boolean isChallengeMode = false; // 挑戰(zhàn)模式
private Boolean isGeneralMode = false; // 普通模式
private Boolean isEnd = false; // 游戲結(jié)束
private int randNum; // 隨機數(shù)
private int userNum; // 用戶回答數(shù)
private int count = -1; // 挑戰(zhàn)次數(shù)
private int beginNum = 1; // 開始范圍數(shù)
private int endNum = 100; // 結(jié)束范圍數(shù)
private int temp; // 臨時保存總次數(shù)
private KeyListener keyEnter;
public static void main(String[] args) {
new Client("Guess Number —— WarMj");
}
public Client(String name) {
super(name);
init();
}
public Client() {
init();
}
// 初始化
public void init() {
this.setSize(500, 385);
this.setResizable(false);
this.setVisible(true);
this.setLocation(700, 200);
centerPanel();
northPanel();
event();
tf_input.requestFocus(); //輸入框獲取焦點
this.setIconImage(Toolkit.getDefaultToolkit().createImage("icon.png"));
}
//用戶輸入模式選擇
public void selectMode() {
if (Integer.parseInt(tf_input.getText()) == 1) {
isChallengeMode = true; //切換到挑戰(zhàn)模式
ta_result.append("進入挑戰(zhàn)模式\n");
ta_result.append("請輸入挑戰(zhàn)次數(shù):\n");
tf_input.setText("");
} else if (Integer.parseInt(tf_input.getText()) == 0) {
isGeneralMode = true; //切換到普通模式
count = 0;
ta_result.append("進入普通模式\n");
ta_result.append("\n請輸入你要猜的數(shù)字:(" + beginNum + "~" + endNum + ")\n");
tf_input.setText("");
}
}
// 普通模式
public void generalMode() {
if (!tf_input.getText().equals("")) {
// 獲取用戶數(shù)字
userNum = Integer.parseInt(tf_input.getText());
// 計數(shù)加一
count++;
if (userNum > randNum) {
// 如果大了
if (userNum < endNum) {
endNum = userNum;
}
ta_result.append("你猜大了\n");
} else if (userNum < randNum) {
// 如果小了
if (userNum > beginNum) {
beginNum = userNum;
}
ta_result.append("你猜小了\n");
} else {
// 如果答案正確
ta_result.append("\n答案是" + randNum + "恭喜你虎囚,挑戰(zhàn)成功湾揽!花了" + count + "次泼疑,就猜中了\n");
isEnd = true;
ta_result.append("是否重來游戲链快? 是 —— 1 否 —— 0\n");
tf_input.setText("");
return;
}
ta_result.append("\r\n請輸入你要猜的數(shù)字:(" + beginNum + "~" + endNum + ")\n");
}
// 清空輸入框
tf_input.setText("");
}
// 挑戰(zhàn)模式
public void challengeMode() {
if (count < 0) {
//第一次進入挑戰(zhàn)模式腊嗡,設(shè)置挑戰(zhàn)次數(shù)
count = Integer.parseInt(tf_input.getText());
temp = count; // 臨時保存總次數(shù)
ta_result.append("挑戰(zhàn)次數(shù)為" + count + "次\n");
ta_result.append("請輸入你要猜的數(shù)字:(" + beginNum + "~" + endNum + ")\n");
tf_input.setText("");
} else {
if (count > 0) {
// 如果還有挑戰(zhàn)次數(shù)
if (!tf_input.getText().equals("")) {
// 獲取用戶數(shù)字
userNum = Integer.parseInt(tf_input.getText());
// 挑戰(zhàn)次數(shù)減一
count--;
if (userNum > randNum) {
// 如果大了
if (userNum < endNum) {
endNum = userNum;
}
ta_result.append("你猜大了\n");
ta_result.append("---你還有" + count + "次機會---\n");
} else if (userNum < randNum) {
// 如果小了
if (userNum > beginNum) {
beginNum = userNum;
}
ta_result.append("你猜小了\n");
ta_result.append("---你還有" + count + "次機會---\n");
} else {
// 如果答案正確
ta_result.append("\n答案是" + randNum + "恭喜你,挑戰(zhàn)成功捅彻!花了" + (temp - count) + "次,就猜中了\n");
isEnd = true;
ta_result.append("是否重來游戲鞍陨? 是 —— 1 否 —— 0\n");
tf_input.setText("");
return;
}
}
// 清空輸入框
tf_input.setText("");
if (count == 0) {
ta_result.append("挑戰(zhàn)失敗\n");
isEnd = true;
ta_result.append("是否重來游戲步淹? 是 —— 1 否 —— 0\n");
tf_input.setText("");
} else {
ta_result.append("請輸入你要猜的數(shù)字:(" + beginNum + "~" + endNum + ")\n");
}
}
}
}
// 事件
public void event() {
// 關(guān)閉窗口
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
// 按鈕添加監(jiān)聽事件
b_define.addActionListener(this);
//創(chuàng)建鍵盤回車事件
keyEnter = new KeyAdapter() {
public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
handleButton();
}
}
};
// 輸入框添加回車事件
tf_input.addKeyListener(keyEnter);
// 生成隨機數(shù)
randNum = new Random().nextInt(99) + 1;
}
//確認按鈕功能
public void handleButton() {
if (!isChallengeMode && !isGeneralMode && !tf_input.getText().equals("") && !isEnd) {
// 如果是游戲選擇模式
selectMode();
} else if (isGeneralMode && !isEnd) {
//普通游戲模式
generalMode();
} else if (isChallengeMode && !isEnd) {
//挑戰(zhàn)游戲模式
challengeMode();
} else if (isEnd) {
if (Integer.parseInt(tf_input.getText()) == 1) {
// 如果選擇重新開始游戲
initValue(); // 初始化各類值
} else {
ta_result.append("\r\n---游戲結(jié)束---");
b_define.setEnabled(false);
tf_input.setEditable(false);
tf_input.removeKeyListener(keyEnter);
}
}
}
// 初始化各類值
public void initValue() {
count = -1;
beginNum = 1;
endNum = 100;
isChallengeMode = false;
isGeneralMode = false;
isEnd = false;
randNum = new Random().nextInt(99) + 1;
ta_result.append("請選擇游戲模式:\n" + "挑戰(zhàn)模式 —— 1 普通模式 —— 0\n");
tf_input.setText("");
}
//點擊動作監(jiān)聽
public void actionPerformed(ActionEvent e) {
// 當確定按鈕被點擊
if (e.getActionCommand().equals("確定")) {
handleButton();
}
}
// 北布局,結(jié)果顯示框
public void northPanel() {
Panel north = new Panel();
north.setLayout(new BorderLayout());
ta_result = new TextArea("請選擇游戲模式:\n" + "挑戰(zhàn)模式 —— 1 普通模式 —— 0\n", 0, 0, TextArea.SCROLLBARS_NONE);
ta_result.setEditable(false);
ta_result.setFont(new Font("宋體", Font.PLAIN, 24));
ta_result.setBackground(Color.white);
north.add(ta_result, BorderLayout.CENTER);
this.add(north, BorderLayout.NORTH);
}
// 中間布局湾戳,輸入框和確認按鈕
public void centerPanel() {
Panel center = new Panel();
tf_input = new TextField();
tf_input.setFont(new Font("宋體", Font.PLAIN, 24));
tf_input.setColumns(5);
center.add(tf_input);
b_define = new Button("確定");
b_define.setFont(new Font("宋體", Font.PLAIN, 24));
center.add(b_define);
this.add(center, BorderLayout.CENTER);
}
}
icon.png