Guessing number games (Java)

Source: Internet
Author: User
Tags gettext

Game rules:
         Usually played by two people, one side out of numbers, the other guessing. The number of people to think of a good one without repeating the number of 4 digits, can not let the guessing people know. Guess the person can start to guess. Each guess a number, the number of people to give a few a a few B according to this figure, where a before the number indicates that the number of correct position is also the number of correct numbers, and B before the number of the number is correct and the number of wrong position.         If the correct answer is 5234, and guess the person guess 5346, then is 1A2B, which has a 5 of the position of the pair, recorded as 1 A, and 3 and 4 of the two number pairs, and the position is not correct, so the record is 2B, together is 1a2b.

Screenshots of the game:




Run.java:

Package Xjj.java.guessnumber2;public class Run {public static void main (string[] args) {jguessgame g=new jguessgame (); g.s Tr=guessnumb.getnumber ();//Get a random four-digit number}}



Guessnumb.java:

Package Xjj.java.guessnumber2;public class Guessnumb {public static String GetNumber () {//randomly generates four-digit  char[] Ch=new Char [4];for (int i=0;i<ch.length;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= "\tguess\ TResult "; int count=1; String str; JTextField TfD; JTextArea tar; JButton btn;public Jguessgame () {super ("Guess Game!"); /Set the title This.setdefaultcloseoperation (exit_on_close) with the construction method of the JFrame class;//Set the fork-off function this.setresizable (false);// Control frame can change size dimension Dim=this.gettoolkit (). Getscreensize ();//Get screen resolution this.setbounds (DIM.WIDTH/3, DIM.HEIGHT/5, DIM.WIDTH/3, 2*DIM.HEIGHT/3);//Set frame size and position this.setbackground (Color.lightgray);//Set Frame background color This.getcontentpane (). SetBackground (Color.lightgray); This.getcontentpane (). setlayout (New FlowLayout ());//Set Layout type JPanel p=new JPanel ();//Add panel P.setbackground ( Color.lightgray);p. Add (New JLabel ("Input:")) Btn=new JButton ("OK");//Set button Tfd=new JTextField (20);//Set edit box P.add (TFD) ;//Add buttons and edit boxes to the Panel p.add (BTN); This.getcontentpane (). Add (P);//Adds panel tar=new JTextArea (20,20) to the frame;// Add Text field tar.setbackground (Color.lightgray); This.getcontentpane (). Add (tar); tar.seteditable (false);// Set the text field as non-editable btn.addactionlistener (this);//Monitor button addmymenu ();//Add menu this.setvisible (TRUE);//Display frame}private void Addmymenu () {//Todojmenubar MenuBar =new JMenuBar ();//New menu bar This.setjmenubar (menuBar);//Add menu bar String menustrs[]={" Game "," Help "}; Jmenu[] Menu =new jmenu[menustrs.length];//new menu for (int i=0;i<menustrs.length;i++) {menu[i]=new jmenu (MenuStrs[i]) ; Menubar.add (Menu[i]);} JMenuItem Menuitemview = new JMenuItem ("Play");//New menu item JMenuItem menuitemexit = new JMenuItem ("Exit"); JMenuItem-menuitemnew = new JMenuItem ("New Game"); JMenuItem menuitempase = new JMenuItem ("pause");//jmenuitem Menuitembook = new JmenuitEM ("leaderboard"); 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);//Monitor the menu item 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 {///Listen input is stored with a character that is not a number int x = Integer.parseint ( Tfd.gettext ());} catch (NumberFormatException E1) {Joptionpane.showmessagedialog (this, "Please enter a four-digit number!!!"); Tfd.settext (""); return;} if (Tfd.gettext (). Length ()!=4) {//Listener input is four number of joptionpane.showmessagedialog (this, "Please enter a four-digit number!!!"); Tfd.settext (""); return;} String Strresult=result.getresult (Tfd.gettext (), str);//Get result string=string+ "\ n" +count+ "\ T" +tfd.gettext ()+ "\ T" +strresult;//results are processed and output to Text field Tar.settext (string); Tfd.settext (""); if (Strresult.charat (0) = = ' 4 ' && Strresult.charat (2) = = ' 4 ') {//Guess right, Game over System.out.println ("congratulation"); Joptionpane.showmessagedialog (This, "Congratulation! Long Live little JJ! "); Tfd.seteditable (false);} if (count==20) {//steps are exhausted, the game ends with Joptionpane.showmessagedialog (this, "Game over! You Fail! "); Tfd.seteditable (false);//cannot continue editing}count++ the text box;} if (E.getsource () instanceof JMenuItem &&e.getactioncommand (). Equalsignorecase ("Exit")) {system.exit (0);// Answer}if (E.getsource () instanceof JMenuItem &&e.getactioncommand () to the opt-out item in the Press menu. Equalsignorecase ("new")) { String= "\tguess\tresult";//reply to the new game item in the Press menu 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, "click on ' OK ' to continue the game!!! ");} if (E.getsource () instanceof JMenuItem &&e.getactioncommand (). Equalsignorecase ("View")) {JOPtionpane.showmessagedialog (This, "1, enter a four-digit \n2, according to the display of a few b for the next input (a front number indicates the number of correct position, and the number of the front of B indicates the number is correct and the number of position is wrong) \n3, The game ends until the 4a4b is displayed. \n4, 20 times did not get the correct results, the game is over, you lost! ");}}}

Result.java:

Package Xjj.java.guessnumber2;public class Result {public static string GetResult (String str1,string str2) {// Compare the guessed with the original answer, get the hint int a=0,b=0;for (int i=0;i<str1.length (); i++) {//the same position and number of the same number  if (Str1.charat (i) = = Str2.charat (i)) {b++;}} for (int i=0;i<str1.length (), i++) {for (int j=0;j<str2.length (); j + +) {//number of the same number if (Str1.charat (i) ==str2.charat (j)) {A++;break;}}} System.out.println (A + "" +b), return a + "a" +b+ "B";//Return result}}




The first use of Java to do graphical interface, there are many deficiencies, let me slowly improve ha!








Guessing number games (Java)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.