java 数字游戏的方法_java实现猜数字游戏
本文實例為大家分享了java實現猜數字游戲的具體代碼,供大家參考,具體內容如下
游戲規則:
通常由兩個人玩,一方出數字,一方猜。出數字的人要想好一個沒有重復數字的4位數,不能讓猜的人知道。猜的人就可以開始猜。每猜一個數字,出數者就要根據這個數字給出幾A幾B,其中A前面的數字表示數字正確位置也正確的數的個數,而B前的數字表示數字正確而位置不對的數的個數。
如正確答案為 5234,而猜的人猜 5346,則是 1A2B,其中有一個5的位置對了,記為1A,而3和4這兩個數字對了,而位置沒對,因此記為 2B,合起來就是 1A2B。
游戲截屏:
Run.java:
package xjj.java.GuessNumber2;
public class Run {
public static void main(String[] args) {
JGuessGame g=new JGuessGame();
g.str=GuessNumb.getNumber();//得到隨機的四位數
}
}
GuessNumb.java:
package xjj.java.GuessNumber2;
public class GuessNumb {
public static String getNumber(){//隨機產生四位數
char[] ch=new char[4];
for(int i=0;i
ch[i]=(char) ((int)(Math.random()*10)+"0");
}
//System.out.println(ch);
String str=new String(ch);
System.out.println(str);
return str;
}
}
JGuessGame.java:
package xjj.java.GuessNumber2;
import javax.swing.*;
import java.awt.Button;
import java.awt.Color;
import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.JobAttributes;
import java.awt.Label;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseListener;
public class JGuessGame extends JFrame implements ActionListener{
String string="GuessResult";
int count=1;
String str;
JTextField tfd;
JTextArea tar;
JButton btn;
public JGuessGame(){
super("Guess Game !");//用JFrame類的構造方法設置標題
this.setDefaultCloseOperation(EXIT_ON_CLOSE);//設置叉關閉功能
this.setResizable(false);//控制框架能否改變大小
Dimension dim=this.getToolkit().getScreenSize();//獲取屏幕分辨率
this.setBounds(dim.width/3, dim.height/5, dim.width/3, 2*dim.height/3);//設置框架大小與位置
this.setBackground(Color.lightGray);//設置框架背景顏色
this.getContentPane().setBackground(Color.lightGray);
this.getContentPane().setLayout(new FlowLayout());//設置布局類型
JPanel p=new JPanel();//添加面板
p.setBackground(Color.lightGray);
p.add(new JLabel("Input : "));
btn=new JButton("確定");//設置按鈕
tfd=new JTextField(20);//設置編輯框
p.add(tfd);//向面板添加按鈕和編輯框
p.add(btn);
this.getContentPane().add(p);//向框架添加面板
tar=new JTextArea(20,20);//添加文本域
tar.setBackground(Color.lightGray);
this.getContentPane().add(tar);
tar.setEditable(false);//設置文本域為不可編輯
btn.addActionListener(this);//監聽按鈕
addMyMenu();//添加菜單
this.setVisible(true);//顯示框架
}
private void addMyMenu() {
// TODO
JMenuBar menuBar =new JMenuBar();//新建菜單欄
this.setJMenuBar(menuBar);//添加菜單欄
String menuStrs[]={"Game","Help"};
JMenu[] menu =new JMenu[menuStrs.length];//新建菜單
for(int i=0;i
menu[i]=new JMenu(menuStrs[i]);
menuBar.add(menu[i]);
}
JMenuItem menuItemView = new JMenuItem("玩法");//新建菜單項
JMenuItem menuItemExit = new JMenuItem("退出");
JMenuItem menuItemNew = new JMenuItem("新游戲");
JMenuItem menuItemPase = new JMenuItem("暫停");
//JMenuItem menuItemBook = new JMenuItem("排行榜");
menu[0].add(menuItemNew) ;
menu[0].add(menuItemPase) ;
//menu[0].add(menuItemBook) ;
menu[0].addSeparator();
menu[1].add(menuItemView);
menuItemView.setActionCommand("View");
menuItemPase.setActionCommand("Pase");
menuItemNew.setActionCommand("New");
menuItemExit.setActionCommand("Exit");
menu[0].add(menuItemExit) ;
menuItemView.addActionListener(this);//對菜單項進行監聽
menuItemPase.addActionListener(this);
menuItemNew.addActionListener(this);
menuItemExit.addActionListener(this);
}
public String getTextField(){
return tfd.getText();
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==btn){
try {//監聽輸入 里是否存儲不是數字的字符
int x = Integer.parseInt(tfd.getText());
} catch (NumberFormatException e1) {
JOptionPane.showMessageDialog(this, "請輸入一個四位數 ! ! !");
tfd.setText("");
return ;
}
if(tfd.getText().length()!=4){//監聽輸入的是否為四為數的數
JOptionPane.showMessageDialog(this, "請輸入一個四位數 ! ! !");
tfd.setText("");
return ;
}
String strresult=Result.getResult(tfd.getText(), str);//得到結果
string=string+"
"+count+""+tfd.getText()+""+strresult;//將結果處理,并輸出到文本域
tar.setText(string);
tfd.setText("");
if(strresult.charAt(0)=="4"&&strresult.charAt(2)=="4"){//猜對,游戲結束
System.out.println("congratulation");
JOptionPane.showMessageDialog(this, "congratulation ! 小JJ萬歲 !");
tfd.setEditable(false);
}
if(count==20){//步數耗盡,游戲結束
JOptionPane.showMessageDialog(this, "Game Over ! You Fail !");
tfd.setEditable(false);//不能對文本框繼續編輯
}
count++;
}
if(e.getSource() instanceof JMenuItem &&e.getActionCommand().equalsIgnoreCase("exit")){
System.exit(0);//對按下菜單中的退出項做出應答
}
if(e.getSource() instanceof JMenuItem &&e.getActionCommand().equalsIgnoreCase("new")){
string="GuessResult";//對按下菜單中的新游戲項做出應答
tfd.setEditable(true);
tar.setText("");
tfd.setText("");
count=1;
this.str=GuessNumb.getNumber();
}
if(e.getSource() instanceof JMenuItem &&e.getActionCommand().equalsIgnoreCase("pase")){
JOptionPane.showMessageDialog(this, "點擊‘確定"繼續游戲 !!!");
}
if(e.getSource() instanceof JMenuItem &&e.getActionCommand().equalsIgnoreCase("view")){
JOptionPane.showMessageDialog(this, "1、輸入一個四位數
2、根據顯示的幾A幾B進行下一次輸入(A前面數字表示位置正確的數的個數,而B前面的數字表示數字正確而位置不對的數的個數)
3、直到顯示4A4B時,游戲結束。
4、20次內沒得到正確結果,游戲也結束,你輸了!");
}
}
}
Result.java:
package xjj.java.GuessNumber2;
public class Result {
public static String getResult(String str1,String str2) {//將猜的與原答案進行比較,得到提示
int a=0,b=0;
for(int i=0;i
if(str1.charAt(i)==str2.charAt(i)){
b++;
}
}
for(int i=0;i
for(int j=0;j
if(str1.charAt(i)==str2.charAt(j)){
a++;
break;
}
}
}
System.out.println(a+" "+b);
return a+"A"+b+"B";//返回結果
}
}
初次用java做圖形界面,還有很多不足的地方,容我慢慢改進哈!
更多有趣的經典小游戲實現專題,也分享給大家:
C++經典小游戲匯總
python經典小游戲匯總
python俄羅斯方塊游戲集合
JavaScript經典游戲 玩不停
java經典小游戲匯總
javascript經典小游戲匯總
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持云海天教程。
原文鏈接:https://blog.csdn.net/u011479875/article/details/45559241
總結
以上是生活随笔為你收集整理的java 数字游戏的方法_java实现猜数字游戏的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 弹性地基梁板实用计算_YJK软件前处理之
- 下一篇: Redhat下小企鹅输入法的安装