以前用C#做的,现在改为java版的了。源码拿出来给大家分享。
反正,自我感觉呢,代码写的很清楚,比较适合初学者学习。
好了废话不多说,上代码。
貌似,复制了即可运行。
/*
* GuessNumForm.java
*
* Created on __DATE__, __TIME__
*/
package com.neusoft.rd.elf;
import javax.swing.JOptionPane;
/**
*
* @author __USER__
*/
public class GuessNumForm extends javax.swing.JFrame {
/** Creates new form GuessNumForm */
public GuessNumForm() {
initComponents();
initMoreComponents();
}
/***********************************
* 这块区域是填写自己定义的窗口属性值;
* *********************************/
private void initMoreComponents() {
this.setTitle("猜数小游戏");
}
/***********************************
* 以上区域是填写自己定义的窗口属性值;
* *********************************/
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
//GEN-BEGIN:initComponents
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
jButton3 = new javax.swing.JButton();
jButtonYes = new javax.swing.JButton();
jButtonNo = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jTextArea1.setColumns(20);
jTextArea1.setEditable(false);
jTextArea1.setRows(5);
jScrollPane1.setViewportView(jTextArea1);
jButton3.setText("\u91cd\u6765");
jButton3.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jButton3MouseClicked(evt);
}
});
jButtonYes.setText("\u6709");
jButtonYes.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jButtonYesMouseClicked(evt);
}
});
jButtonNo.setText("\u6ca1");
jButtonNo.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jButtonNoMouseClicked(evt);
}
});
jLabel1.setForeground(new java.awt.Color(0, 51, 255));
jLabel1.setText("\u73a9\u6cd5\u8bf4\u660e");
jLabel1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseEntered(java.awt.event.MouseEvent evt) {
jLabel1MouseEntered(evt);
}
public void mouseExited(java.awt.event.MouseEvent evt) {
jLabel1MouseExited(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(layout
.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(
layout.createSequentialGroup()
.addGap(64, 64, 64)
.addComponent(jScrollPane1,
javax.swing.GroupLayout.PREFERRED_SIZE,
190,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addGroup(
layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(
javax.swing.GroupLayout.Alignment.TRAILING,
layout.createSequentialGroup()
.addComponent(
jLabel1,
javax.swing.GroupLayout.DEFAULT_SIZE,
51,
Short.MAX_VALUE)
.addGap(18, 18,
18))
.addGroup(
layout.createSequentialGroup()
.addGroup(
layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(
jButtonNo,
javax.swing.GroupLayout.DEFAULT_SIZE,
59,
Short.MAX_VALUE)
.addComponent(
jButton3,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE)
.addComponent(
jButtonYes,
javax.swing.GroupLayout.DEFAULT_SIZE,
59,
Short.MAX_VALUE))
.addContainerGap()))));
layout.setVerticalGroup(layout
.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(
layout.createSequentialGroup()
.addGap(36, 36, 36)
.addGroup(
layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(
jScrollPane1,
javax.swing.GroupLayout.PREFERRED_SIZE,
137,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(
layout.createSequentialGroup()
.addComponent(
jButtonYes)
.addPreferredGap(
javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(
jButtonNo)
.addPreferredGap(
javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(
jButton3)))
.addPreferredGap(
javax.swing.LayoutStyle.ComponentPlacement.RELATED,
14, Short.MAX_VALUE)
.addComponent(jLabel1).addContainerGap()));
pack();
}// </editor-fold>
//GEN-END:initComponents
private void jLabel1MouseExited(java.awt.event.MouseEvent evt) {
this.jTextArea1.setText(this.CurrentText);
}
private void jLabel1MouseEntered(java.awt.event.MouseEvent evt) {
this.CurrentText=this.jTextArea1.getText();
this.jTextArea1
.setText("请从1-127之间选择一个数字,然后看看列出的数字中有没有您选的这个,并点击对应的按钮,然后我就知道您选的是几了.Happy Time!");
}
private void jButton3MouseClicked(java.awt.event.MouseEvent evt) {
this.ResetArgs();
this.jTextArea1.setText(this.generateText(ClickCount));
}
private void jButtonNoMouseClicked(java.awt.event.MouseEvent evt) {
if (this.ClickCount == 7) {
if (!hasGivenResult) {
this.guessedString = "1" + this.guessedString;
GiveResult();
hasGivenResult = true;
}
}
if (this.ClickCount < 7) {
this.guessedString = "1" + this.guessedString;
this.ClickCount++;
this.generatedString = this.generateText(this.ClickCount);
this.jTextArea1.setText(this.generatedString);
}
}
private void jButtonYesMouseClicked(java.awt.event.MouseEvent evt) {
if (this.ClickCount == 7) {
if (!hasGivenResult) {
this.guessedString = "0" + this.guessedString;
GiveResult();
hasGivenResult = true;
}
}
if (this.ClickCount < 7) {
this.guessedString = "0" + this.guessedString;
this.ClickCount++;
this.generatedString = this.generateText(ClickCount);
this.jTextArea1.setText(this.generatedString);
}
}
/**************************
* ******自定义函数区域******
**************************/
//清空所有的变量
private void ResetArgs() {
ClickCount = 1;
generatedString = "";
guessedString = "";
guessedInt = -1;
joking = false;
hasGivenResult = false;
subStr = "";//截取最后的串的前7位判断是不是玩家在开玩笑!
}
//第七次点击鼠标时计算用户所猜数字并显示
public void GiveResult() {
if (this.guessedString.length() >= 7) {
this.subStr = this.guessedString.substring(0, 7);
if ("0000000".equals(this.subStr) || "1111111".equals(this.subStr)) {
this.joking = true;
this.jTextArea1.setText("不跟不遵守游戏规则的人玩!");
JOptionPane.showMessageDialog(this, "讨厌,不跟你玩了!");
}
}
if (!joking) {
this.guessedInt = this.BinaryStringtoInt(this.guessedString);
this.jTextArea1.setText("您猜的数字是:" + this.guessedInt);
}
}
//传入二进制的字符串,转成十进制整形。0110111--
public int BinaryStringtoInt(String s) {
int oneBit = -1;
int allBit = 0;
int len = s.length();
for (int i = 0; i < len; i++) {
oneBit = Integer.parseInt(s.charAt(i) + "");
allBit += oneBit << (len - 1 - i);
}
return allBit;
}
//生成文本域中的数字字符串的方法
public String generateText(int times) {
String returnIntString = "";
String binary = "";
int value = -1;
for (int i = 1; i < 128; i++) {
binary = Integer.toBinaryString(i);
int k = 8 - binary.length();
for (int j = 1; j < k; j++) {//补充为7位二进制 0110111
binary = "0" + binary;
}
if ('0' == binary.charAt(7 - times)) {//把每个符合条件的串,拼成十进制的输出串
value = BinaryStringtoInt(binary);
returnIntString += value + ",";
}
}
return returnIntString.substring(0, returnIntString.length() - 1);//去掉最后一个逗号
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
GuessNumForm gNForm = new GuessNumForm();
gNForm.setVisible(true);
gNForm.jTextArea1.setLineWrap(true);
gNForm.jTextArea1.setText(gNForm.generateText(1));
}
/***********************************
*********自定义变量区域***********
* *********************************/
private int ClickCount = 1;
private String generatedString = "";
private String guessedString = "";
private int guessedInt = -1;
//程序健壮性参数
private boolean joking = false;
private boolean hasGivenResult = false;
private String subStr = "";//截取最后的串的前7位判断是不是玩家在开玩笑!
private String CurrentText="";
//GEN-BEGIN:variables
// Variables declaration - do not modify
private javax.swing.JButton jButton3;
private javax.swing.JButton jButtonNo;
private javax.swing.JButton jButtonYes;
private javax.swing.JLabel jLabel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea jTextArea1;
// End of variables declaration//GEN-END:variables
}