Skip to content

Commit 0d8bbf2

Browse files
committed
添加猜数字小游戏
1 parent fb6b91d commit 0d8bbf2

9 files changed

Lines changed: 564 additions & 2 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ xJavaFxTool是使用javaFx开发的实用小工具集,利用业余时间把工
103103
50. FileSearchTool:文件搜索工具(使用[lucene](https://lucene.apache.org/)搜索引擎)
104104
51. Mp3ConvertTool:Mp3转换工具(目前支持网易云音乐.ncm、QQ音乐.qmc转换为mp3格式)(使用[jaudiotagger](http://www.jthink.net/jaudiotagger/)工具)
105105
52. SealBuilderTool:印章生成工具
106+
53. BullsAndCowsGame:猜数字小游戏
106107

107108
传输工具目前支持功能如下:
108109

README_EN.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ Support plug-in development, the plug-in jar package can be automatically loaded
103103
50. FileSearchTool: File Search Tool (using the [lucene](https://lucene.apache.org/) search engine)
104104
51. Mp3ConvertTool: Mp3 conversion tool (currently supports NetEase cloud music.ncm, QQ music.qmc converted to mp3 format) (using [jaudiotagger](http://www.jthink.net/jaudiotagger/) tool)
105105
52. SealBuilderTool: Stamp Generation Tool
106+
53. BullsAndCowsGame: A number guessing game
106107

107108
##### The transfer tools currently support the following features:
108109
###### Receiver:
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
package com.xwintop.xJavaFxTool.controller.games;
2+
3+
import com.xwintop.xJavaFxTool.services.games.BullsAndCowsGameService;
4+
import com.xwintop.xJavaFxTool.view.games.BullsAndCowsGameView;
5+
import javafx.application.Platform;
6+
import javafx.event.ActionEvent;
7+
import javafx.event.Event;
8+
import javafx.fxml.FXML;
9+
import javafx.geometry.Pos;
10+
import javafx.scene.control.Button;
11+
import javafx.scene.control.Label;
12+
import javafx.scene.control.TextField;
13+
import javafx.scene.layout.HBox;
14+
import javafx.scene.paint.Color;
15+
import javafx.scene.text.Font;
16+
import lombok.Getter;
17+
import lombok.Setter;
18+
import lombok.extern.slf4j.Slf4j;
19+
import org.apache.commons.lang3.StringUtils;
20+
21+
import java.net.URL;
22+
import java.util.ResourceBundle;
23+
24+
/**
25+
* @ClassName: BullsAndCowsGameController
26+
* @Description: 猜数字小游戏
27+
* @author: xufeng
28+
* @date: 2019/8/24 0024 15:05
29+
*/
30+
31+
@Getter
32+
@Setter
33+
@Slf4j
34+
public class BullsAndCowsGameController extends BullsAndCowsGameView {
35+
private BullsAndCowsGameService bullsAndCowsGameService = new BullsAndCowsGameService(this);
36+
37+
private TextField[] inputNumberTextFields = null;
38+
private TextField inputNumberTextField;
39+
40+
private HBox[] answerHboxs = new HBox[20];
41+
42+
@Override
43+
public void initialize(URL location, ResourceBundle resources) {
44+
initView();
45+
initEvent();
46+
initService();
47+
}
48+
49+
private void initView() {
50+
for (int i = 0; i < 20; i++) {
51+
Label[] labels = new Label[7];
52+
labels[0] = new Label((i + 1) + ":");
53+
labels[0].setPrefWidth(50);
54+
labels[0].setAlignment(Pos.CENTER_RIGHT);
55+
labels[1] = new Label(" ");
56+
labels[1].setTextFill(Color.RED);
57+
labels[1].setAlignment(Pos.CENTER);
58+
labels[1].setPrefWidth(60);
59+
labels[2] = new Label("数字对");
60+
labels[3] = new Label(" ");
61+
labels[3].setAlignment(Pos.CENTER);
62+
labels[3].setTextFill(Color.RED);
63+
labels[3].setPrefWidth(20);
64+
labels[4] = new Label("个, 位置对");
65+
labels[5] = new Label(" ");
66+
labels[5].setAlignment(Pos.CENTER);
67+
labels[5].setTextFill(Color.RED);
68+
labels[5].setPrefWidth(20);
69+
labels[6] = new Label("位");
70+
for (Label label : labels) {
71+
label.setFont(Font.font(20));
72+
}
73+
answerHboxs[i] = new HBox();
74+
answerHboxs[i].getChildren().addAll(labels);
75+
if (i < 10) {
76+
answerVBox1.getChildren().add(answerHboxs[i]);
77+
} else {
78+
answerVBox2.getChildren().add(answerHboxs[i]);
79+
}
80+
}
81+
}
82+
83+
private void initEvent() {
84+
inputNumberTextFields = new TextField[]{inputNumberTextField1, inputNumberTextField2, inputNumberTextField3, inputNumberTextField4};
85+
for (TextField numberTextField : inputNumberTextFields) {
86+
numberTextField.textProperty().addListener((observable, oldValue, newValue) -> {
87+
if (StringUtils.isEmpty(newValue)) {
88+
return;
89+
}
90+
try {
91+
int number = Integer.parseInt(newValue);
92+
if (number > 9 || number < 0) {
93+
numberTextField.setText(oldValue);
94+
}
95+
} catch (Exception e) {
96+
log.warn("输入非数字:" + newValue);
97+
numberTextField.setText(oldValue);
98+
}
99+
for (int i = 0; i < 4; i++) {
100+
if (inputNumberTextFields[i] == numberTextField) {
101+
final int index = (i == 3 ? 0 : i + 1);
102+
Platform.runLater(() -> {
103+
inputNumberTextFields[index].requestFocus();
104+
});
105+
break;
106+
}
107+
}
108+
});
109+
numberTextField.focusedProperty().addListener((observable, oldValue, newValue) -> {
110+
if (newValue) {
111+
inputNumberTextField = numberTextField;
112+
}
113+
});
114+
}
115+
Platform.runLater(() -> {
116+
inputNumberTextField1.requestFocus();
117+
});
118+
}
119+
120+
private void initService() {
121+
bullsAndCowsGameService.initAnswerNumber();
122+
bullsAndCowsGameService.initRecordData();
123+
}
124+
125+
@FXML
126+
private void inputNumberOnAction(ActionEvent event) {
127+
Button button = (Button) event.getSource();
128+
if ("清除".equals(button.getText())) {
129+
for (TextField inputNumberTextField : inputNumberTextFields) {
130+
inputNumberTextField.setText(null);
131+
}
132+
} else if ("确定".equals(button.getText())) {
133+
bullsAndCowsGameService.enterInputNumber();
134+
} else if ("重玩".equals(button.getText())) {
135+
bullsAndCowsGameService.initAnswerNumber();
136+
button.setText("确定");
137+
rightAnswersLabel.setVisible(false);
138+
} else {
139+
inputNumberTextField.setText(button.getText());
140+
}
141+
}
142+
143+
/**
144+
* 父控件被移除前调用
145+
*/
146+
public void onCloseRequest(Event event) {
147+
System.out.println("移除所以线程");
148+
if (bullsAndCowsGameService.getTimer() != null) {
149+
bullsAndCowsGameService.getTimer().cancel();
150+
}
151+
}
152+
}
Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
package com.xwintop.xJavaFxTool.services.games;
2+
3+
import com.xwintop.xJavaFxTool.controller.games.BullsAndCowsGameController;
4+
import com.xwintop.xJavaFxTool.utils.ConfigureUtil;
5+
import com.xwintop.xcore.util.javafx.AlertUtil;
6+
import javafx.application.Platform;
7+
import javafx.scene.control.Label;
8+
import javafx.scene.control.TextField;
9+
import javafx.scene.layout.HBox;
10+
import lombok.Getter;
11+
import lombok.Setter;
12+
import lombok.extern.slf4j.Slf4j;
13+
import org.apache.commons.configuration.PropertiesConfiguration;
14+
import org.apache.commons.io.FileUtils;
15+
import org.apache.commons.lang3.StringUtils;
16+
17+
import java.io.File;
18+
import java.util.Random;
19+
import java.util.Timer;
20+
import java.util.TimerTask;
21+
22+
/**
23+
* @ClassName: BullsAndCowsGameService
24+
* @Description: 猜数字小游戏
25+
* @author: xufeng
26+
* @date: 2019/8/24 0024 15:06
27+
*/
28+
29+
@Getter
30+
@Setter
31+
@Slf4j
32+
public class BullsAndCowsGameService {
33+
private BullsAndCowsGameController bullsAndCowsGameController;
34+
private int daojishiTime = 0;
35+
private int[] answerNumbers = new int[4];
36+
private int enterAnswerNumber = 0;
37+
private Timer timer;
38+
39+
private int recordNumber = 0;
40+
private int recordTime = 0;
41+
42+
public BullsAndCowsGameService(BullsAndCowsGameController bullsAndCowsGameController) {
43+
this.bullsAndCowsGameController = bullsAndCowsGameController;
44+
}
45+
46+
public void initRecordData() {
47+
try {
48+
PropertiesConfiguration xmlConfigure = new PropertiesConfiguration(ConfigureUtil.getConfigureFile("BullsAndCowsGameConfigure.properties"));
49+
recordNumber = xmlConfigure.getInt("recordNumber", 0);
50+
recordTime = xmlConfigure.getInt("recordTime", 0);
51+
this.setRecordData();
52+
} catch (Exception e) {
53+
log.error("加载配置失败!");
54+
}
55+
}
56+
57+
public void initAnswerNumber() {
58+
daojishiTime = 0;
59+
enterAnswerNumber = 0;
60+
for (int i = 0; i < answerNumbers.length; i++) {
61+
Random ran = new Random();
62+
int a = ran.nextInt(10);
63+
if (i - 1 != -1) {
64+
for (int j = 0; j < i; j++) {
65+
if (a == answerNumbers[j]) {
66+
i--;
67+
break;
68+
} else {
69+
answerNumbers[i] = a;
70+
}
71+
}
72+
} else {
73+
answerNumbers[i] = a;
74+
}
75+
}
76+
timer = new Timer();
77+
timer.schedule(new TimerTask() {
78+
@Override
79+
public void run() {
80+
daojishiTime++;
81+
int hours = daojishiTime / 60 / 60;
82+
int minutes = daojishiTime / 60;
83+
int seconds = daojishiTime % 60;
84+
Platform.runLater(() -> {
85+
bullsAndCowsGameController.getDaojishiTimeLabel().setText("计时: " + hours + " 小时 " + minutes + " 分 " + seconds + " 秒");
86+
});
87+
}
88+
}, 0, 1000);
89+
Platform.runLater(() -> {
90+
for (int i = 0; i < 20; i++) {
91+
HBox answerHbox = bullsAndCowsGameController.getAnswerHboxs()[i];
92+
((Label) answerHbox.getChildren().get(1)).setText("");
93+
((Label) answerHbox.getChildren().get(3)).setText("");
94+
((Label) answerHbox.getChildren().get(5)).setText("");
95+
}
96+
});
97+
}
98+
99+
public void enterInputNumber() {
100+
String inputNumberString = "";
101+
int[] inputNumsArray = new int[4];
102+
for (int i = 0; i < 4; i++) {
103+
TextField inputNumberTextField = bullsAndCowsGameController.getInputNumberTextFields()[i];
104+
if (StringUtils.isEmpty(inputNumberTextField.getText())) {
105+
AlertUtil.showInfoAlert("请输入四个0-9的数字!");
106+
return;
107+
}
108+
inputNumsArray[i] = Integer.parseInt(inputNumberTextField.getText());
109+
inputNumberString += inputNumsArray[i];
110+
}
111+
int aA = 0;
112+
int bB = 0;
113+
for (int i = 0; i < answerNumbers.length; i++) {
114+
if (inputNumsArray[i] == answerNumbers[i]) {
115+
aA += 1;
116+
}
117+
for (int j = 0; j < 4; j++) {
118+
if (inputNumsArray[j] == answerNumbers[i]) {
119+
bB += 1;
120+
break;
121+
}
122+
}
123+
}
124+
HBox answerHbox = bullsAndCowsGameController.getAnswerHboxs()[enterAnswerNumber];
125+
((Label) answerHbox.getChildren().get(1)).setText(inputNumberString);
126+
((Label) answerHbox.getChildren().get(5)).setText("" + aA);
127+
((Label) answerHbox.getChildren().get(3)).setText("" + bB);
128+
enterAnswerNumber++;
129+
if (aA == 4) {
130+
timer.cancel();
131+
timer = null;
132+
AlertUtil.showInfoAlert("恭喜你,答对了!答案是:" + inputNumberString);
133+
bullsAndCowsGameController.getRightAnswersLabel().setText("正确答案:" + inputNumberString);
134+
bullsAndCowsGameController.getEnterButton().setText("重玩");
135+
bullsAndCowsGameController.getRightAnswersLabel().setVisible(true);
136+
if (recordNumber == 0 || enterAnswerNumber < recordNumber) {
137+
recordNumber = enterAnswerNumber;
138+
}
139+
if (daojishiTime == 0 || recordTime < daojishiTime) {
140+
recordTime = daojishiTime;
141+
}
142+
setRecordData();
143+
}
144+
if (enterAnswerNumber == 10) {
145+
AlertUtil.showInfoAlert("您还有10次机会!");
146+
}
147+
if (enterAnswerNumber >= 20) {
148+
timer.cancel();
149+
timer = null;
150+
AlertUtil.showInfoAlert("挑战失败,点击\"重玩\"继续游戏!");
151+
bullsAndCowsGameController.getRightAnswersLabel().setText("正确答案:" + answerNumbers[0] + answerNumbers[1] + answerNumbers[2] + answerNumbers[3]);
152+
bullsAndCowsGameController.getEnterButton().setText("重玩");
153+
bullsAndCowsGameController.getRightAnswersLabel().setVisible(true);
154+
}
155+
System.out.println("答案是:" + answerNumbers[0] + answerNumbers[1] + answerNumbers[2] + answerNumbers[3]);
156+
}
157+
158+
private void setRecordData() {
159+
int hours = recordTime / 60 / 60;
160+
int minutes = recordTime / 60;
161+
int seconds = recordTime % 60;
162+
Platform.runLater(() -> {
163+
bullsAndCowsGameController.getRecordNumberLabel().setText("最少次数:第 " + recordNumber + " 次完成");
164+
bullsAndCowsGameController.getRecordTimeLabel().setText("最短时间: " + hours + " 小时 " + minutes + " 分 " + seconds + " 秒");
165+
});
166+
try {
167+
File file = ConfigureUtil.getConfigureFile("BullsAndCowsGameConfigure.properties");
168+
FileUtils.touch(file);
169+
PropertiesConfiguration xmlConfigure = new PropertiesConfiguration(ConfigureUtil.getConfigureFile("BullsAndCowsGameConfigure.properties"));
170+
xmlConfigure.setProperty("recordNumber", recordNumber);
171+
xmlConfigure.setProperty("recordTime", recordTime);
172+
xmlConfigure.save();
173+
} catch (Exception e) {
174+
log.error("保存配置失败!", e);
175+
}
176+
}
177+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.xwintop.xJavaFxTool.view.games;
2+
3+
import javafx.fxml.FXML;
4+
import javafx.fxml.Initializable;
5+
import javafx.scene.control.Button;
6+
import javafx.scene.control.Label;
7+
import javafx.scene.control.TextField;
8+
import javafx.scene.layout.VBox;
9+
import lombok.Getter;
10+
import lombok.Setter;
11+
12+
/**
13+
* @ClassName: BullsAndCowsGameView
14+
* @Description: 猜数字小游戏
15+
* @author: xufeng
16+
* @date: 2019/8/24 0024 15:06
17+
*/
18+
19+
@Getter
20+
@Setter
21+
public abstract class BullsAndCowsGameView implements Initializable {
22+
@FXML
23+
protected Label daojishiTimeLabel;
24+
@FXML
25+
protected TextField inputNumberTextField1;
26+
@FXML
27+
protected TextField inputNumberTextField2;
28+
@FXML
29+
protected TextField inputNumberTextField3;
30+
@FXML
31+
protected TextField inputNumberTextField4;
32+
@FXML
33+
protected VBox answerVBox1;
34+
@FXML
35+
protected VBox answerVBox2;
36+
@FXML
37+
protected Label recordNumberLabel;
38+
@FXML
39+
protected Label recordTimeLabel;
40+
@FXML
41+
protected Button enterButton;
42+
@FXML
43+
protected Label rightAnswersLabel;
44+
45+
}

0 commit comments

Comments
 (0)