Java Homemade man-machine games —————————— scissors, stone, cloth

Source: Internet
Author: User

 Packagecom.hello.test;ImportJava.util.Scanner; Public classTestgame { Public Static voidMain (string[] args) {Player P=NewPlayer ();//instantiate a Player objectGame G=NewGame (P);//instantiating a Game objectg.startgame ();//Calling Methods    }}//Game Classclassgame{PrivatePlayer p;//Set player         PublicGame (Player p) { This. P =p; }        //make the rules of the game     Public intrule (string s1, string s2) {if(S1.equals (S2))//first, put the computer in the same situation.        {            return0 ; }        if(S1.equals ("scissors")))        {            if(S2.equals ("cloth"))            {                return1 ; }            if(S2.equals ("Stone"))            {                return-1 ; }        }        if(S1.equals ("Stone"))        {            if(S2.equals ("cloth"))            {                return-1 ; }            if(S2.equals ("scissors")))            {                return1 ; }        }        if(S1.equals ("cloth"))        {            if(S2.equals ("scissors")))            {                return-1 ; }            if(S2.equals ("Stone"))            {                return1 ; }        }        return0; }         Public BooleanPanduan (String s) {if("Scissors". Equals (s) | | " Cloth ". Equals (s) | |" Stone ". Equals (s) | |" Esc. Equals (s)) {            return true ; }        Else         {            return false ; }    }        //Game Start Method     Public voidStartgame () {System.out.println ("Start the game"); System.out.println ("Please enter your game nickname:"); Scanner s=NewScanner (system.in); String SG=S.nextline ();                P.setname (SG); P.setscore (100); System.out.println ("Congratulations on your successful registration, your initial credit is 100"); System.out.println (P.getname ()+ "Enter the game");  while(true) {string[] array=NewString[] {"Scissors", "stone", "cloth"} ; System.out.println ("Please enter the scissors, stone or cloth, and end Please enter ESC"); Scanner SC=NewScanner (system.in);//provide input windowString S1=Sc.nextline (); if(!Panduan (S1)) {System.out.println ("Input wrong, re-enter!" "); Continue ; }                        if(S1.equals ("ESC"))            {                 Break ; }                        intA = (int) (Math.random ());//Random 0,1,2 as an indexString S2=Array[a]; intit =rule (S1,S2); if(it>0) {System.out.println ("You are out of the" +s1+ "computer Out is" +S2); System.out.println ("Congratulations" +p.getname () + "won, reward 10 points"); P.setscore (P.getscore ()+10); }            Else if(it<0) {System.out.println ("You are out of the" +s1+ "computer Out is" +S2); System.out.println ("Sorry" +p.getname () + "you lost, minus 10 points for"); P.setscore (P.getscore ()-10); }            Else{System.out.println ("You are out of the" +s1+ "computer Out is" +S2); System.out.println ("It's flat.");            P.setscore (P.getscore ());        } System.out.println (P); } System.out.println (P.getname ()+ "exited the game, the current points are:" +P.getscore ()); }    }//Player Classclassplayer{PrivateString name;//player Name        Private intScore;//Player Points//setting, getting     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     Public intGetscore () {returnscore; }     Public voidSetScore (intscore) {         This. score =score; } @Override PublicString toString () {return"Player:" + name + ", Current points:" +score; }    }

Java Homemade man-machine games —————————— scissors, stone, cloth

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.