石头剪刀布练习

Java练习-石头剪刀布游戏

package zhuzhile.softwere.niit;
import java.util.Scanner;

public class TestMyGame {

    public static void main(String[] args) {
    Game myGame=new Game();
    myGame.startGame();
    }

}


public class Game {
    Human normalMan;
    Computer  computerMacine;
    public Game() {
    normalMan=new Human();
    computerMacine=new Computer();
    }

    public Game(String age,String name,String size,String config)
    {
        normalMan=new Human(age,name);
        computerMacine=new Computer(size,config);
    }

    public  void startGame()
    {
        Scanner in=new Scanner(System.in);
        String choice="yes";
        while(!choice.equals("no"))
        {
            meanu();
            int ChoiceOfComputer=computerMacine.showMove();
            int ChoiceOfHuman=normalMan.showMove();
            System.out.println("computer choice is "+ChoiceOfComputer );
            System.out.println("The ResultOfgame is "+resultOfGame(ChoiceOfComputer, ChoiceOfHuman));
            System.out.println("是否继续?(yes or no)");
            choice=in.next();
        }

    }

    public  void meanu()
    {
        System.out.println("**********0.石头**********");
        System.out.println("**********1.剪刀**********");
        System.out.println("**********2.布     **********");
    }

    public String resultOfGame(int a,int b)
    {
        if(a-b==0)
        {
            return "that having a tie";
        }else{
            if(a-b==-1||a-b==2)
            {
                return "that computer is winner";
            }else{
                return "that human is winner";
            }
        }
    }

}




public class Human {
   private String age;
   private String name;
   public Human(String age,String name)
   {
       age=this.age;
       name=this.name;
   }
   public Human()
   {
       setAge();
       setName();
   }
   public void setAge()
   {
       System.out.println("请输入你的年龄:");
       Scanner in=new Scanner(System.in);
       age=in.next();
       while(!Tool.checkType(age))
       {
           System.out.println("请重新输入你的年龄:");
           age=in.next();
       }

   }


   public void setName()
   {
       System.out.println("请输入你的名字:");
       Scanner in=new Scanner(System.in);
       name=in.next();
   }

   public int showMove()
   {
       String choiceOfMove;
       Scanner in=new Scanner(System.in);
       System.out.println("请输入你的选择:");
       choiceOfMove=in.next();

       while(!Tool.checkChoiceType(choiceOfMove)||!Tool.checkChoice(choiceOfMove))
       {
           System.out.println("请重新输入你的选择:");
           choiceOfMove=in.next();
       }

       System.out.println("Human's choice is "+choiceOfMove);
       return Tool.StringToInt(choiceOfMove);
   }
}



public class Computer {
    String size;
    String config;
    public Computer() {
        setSize();
        setConfig();

    }
    public Computer(String size,String config)
    {
        this.size=size;
        this.config=config;
    }

    public void setSize()
    {
        System.out.println("请输入机器的型号:");
        Scanner in=new Scanner(System.in);
        size=in.next();
    }

    public void setConfig()
    {
        System.out.println("请输入机器的相关配置:");
        Scanner in=new Scanner(System.in);
        config=in.next();
    }


    public int showMove()
    {
       int choiceOfMove;
       choiceOfMove=(int)(Math.random()*10)%3;
       return choiceOfMove;
    }

}




public class Tool {

public static boolean checkChoice(String chocie)
{
   return chocie.length()==1?true:false; 
}

 public static int StringToInt(String chocie)
 {
   return Integer.parseInt(chocie);
 }



 public static boolean checkType(String chocie)
 {
     try {
        int num=Integer.valueOf(chocie);
        return true;
    } catch (Exception e) {
        return false;
    }
 }

 public static boolean checkChoiceType(String chocie)
 {
     try {
        int num=Integer.valueOf(chocie); 
        if(num>2||num<0)
        {
            return false;
        }
        return true;
    } catch (Exception e) {
        return false;
    }
 }

}

总结:这里有五个类,一个是人类类(Human class),还有一个是电脑类(Computer Class),还有一个游戏类(Game Class),还有一个游戏测试类(TestMyGame Class)这个是主类,最后我加了一个工具类(Tool Class),为了不出现输入类型不对而导致出错的bug。


猜你喜欢

转载自blog.csdn.net/weixin_40867255/article/details/82497992