zoukankan      html  css  js  c++  java
  • 人机猜拳.

    /**
     * 计算机类
     */
    public class Computer {
           String name = "电脑"; // 名字
           int score = 0;;       // 积分
           
           /**
            * 出拳
            * @return 出拳结果:1.剪刀 2.石头 3.布
            */
           public int showFist(){
               // 产生随机数
               int show = (int)(Math.random()*10)%3 + 1;  //产生随机数,表示电脑出拳
               
               // 输出出拳结果并返回
               switch(show){
                  case 1:
                      System.out.println(name+"出拳: 剪刀");
                      break;
                  case 2:
                      System.out.println(name+"出拳: 石头");
                      break;
                  case 3: 
                      System.out.println(name+"出拳: 布");
                      break;
               }
               return show;
           }
    }
    /**
     * 用户类
     */
    public class Person { 
          String name = "匿名";  // 名字
          int score = 0;         // 积分  
          
          /**
           * 出拳
           * @return 出拳结果:1.剪刀 2.石头 3.布
           */
          public int showFist(){
              // 接收用户的选择
              Scanner input = new Scanner(System.in);
              System.out.print("
    请出拳:1.剪刀 2.石头 3.布 (输入相应数字) :");
              int show = input.nextInt();
              
              // 输出出拳结果,并返回
              switch(show){
                  case 1: 
                      System.out.println("你出拳: 剪刀");
                      break;
                  case 2:
                      System.out.println("你出拳: 石头");
                      break;
                  case 3: 
                      System.out.println("你出拳: 布");
                      break;
              }
              return show;
          }
    }
    /**
     * 游戏类
     */
    public class Game {
        Person person;       //甲方
        Computer computer;   //乙方
        int count;           //对战次数
        
        /**
         * 初始化
         */
        public void initial(){
            person = new Person();
            computer = new Computer();
            count = 0;
        }
            
        /**
         * 开始游戏
         */
        public void startGame() {
            
            System.out.println("----------------欢 迎 进 入 游 戏 世 界----------------");
            System.out.println("
    		******************");
            System.out.println  ("		**  猜拳, 开始    **");
            System.out.println  ("		******************");
            
            System.out.println("
    出拳规则:1.剪刀 2.石头 3.布");
            
            Scanner input = new Scanner(System.in);
            String exit = "n";  // 退出系统
            
            do{
                initial();  // 初始化
                /*选择对方角色*/
                System.out.print("请选择对方角色(1:刘备 2:孙权 3:曹操): ");
                
                int role = input.nextInt();
                if(role == 1){
                    computer.name = "刘备";
                }else if(role == 2){
                    computer.name = "孙权";
                }else if(role == 3){
                    computer.name = "曹操";
                }   
                
                // 扩展功能1:输入用户姓名
                /*输入用户姓名*/
                System.out.print("请输入你的姓名:");
                person.name = input.next();
                
                System.out.println(person.name+"  VS  "+computer.name+"  对战
    ");
                // 扩展功能1结束
                
                System.out.print("要开始吗?(y/n) ");
                String start = input.next();  // 开始每一局游戏
                
                
                int perFist;   //用户出的拳
                int compFist;  //计算机出的拳
                
                while(start.equals("y")){
                    /*出拳*/
                    perFist = person.showFist();
                    compFist = computer.showFist();
                    /*裁决*/
                    if((perFist == 1 && compFist == 1) || (perFist == 2 && compFist == 2) || (perFist == 3 && compFist == 3)){
                        System.out.println("结果:和局,真衰!嘿嘿,等着瞧吧 !
    ");  //平局
                    }else if((perFist == 1 && compFist == 3) || (perFist == 2  && compFist == 1) || (perFist == 3 && compFist == 2)){
                        System.out.println("结果: 恭喜, 你赢了!");  //用户赢
                        person.score++;
                    }else{
                        System.out.println("结果说:^_^,你输了,真笨!
    ");  //计算机赢
                        computer.score++;
                    }
                    count++;
                    System.out.print("
    是否开始下一轮(y/n):  ");
                    start = input.next();    
                }
                /*显示结果*/
                showResult();            
                
                // 扩展功能3:循环游戏,直到退出系统
                System.out.print("
    要开始下一局吗?(y/n):");
                exit = input.next();            
                System.out.println();
                // 扩展功能3结束
            }while(!exit.equals("n"));    
            
            System.out.println("系统退出!");
        }
        
        /**
         * 显示比赛结果
         */
        public void showResult(){
            /*显示对战次数*/
            System.out.println("---------------------------------------------------");
            System.out.println(computer.name + "  VS  " + person.name);
            System.out.println("对战次数:"+ count);
            
            // 扩展功能2:显示最终的得分
            System.out.println("
    姓名	得分");        
            System.out.println(person.name+"	"+person.score);
            System.out.println(computer.name+"	"+computer.score+"
    ");
            // 扩展功能2结束
            
            /*显示对战结果*/
            int result = calcResult();
            if(result == 1){
                System.out.println("结果:打成平手,下次再和你一分高下!");
            }else if(result == 2){
                System.out.println("结果:恭喜恭喜!");   //用户获胜
            }else{ 
                System.out.println("结果:呵呵,笨笨,下次加油啊!");   //计算机获胜
            }
            System.out.println("---------------------------------------------------");
        }
        
        /**
         * 计算比赛结果
         * @return 1:战平;2:用户赢;3:电脑赢
         */
        public int calcResult(){
            if(person.score == computer.score){
                  return 1; // 战平
            }else if(person.score > computer.score){
                  return 2; // 用户赢
            }else{
                  return 3; // 电脑赢
            }
            
        }
    }
    /**
     * 人机互动版猜拳游戏 
     * 程序入口
     */
    public class Test {
        public static void main(String[] args) {
            Game game = new Game();
            game.startGame();
        }
    }


     

                                                                                                                                                                                                                                                                                  完!

  • 相关阅读:
    数据结构(1)
    数据库知识(2)
    Leetcode每日一题(1)
    数据库知识(1)
    Redis之MISCONF Redis is configured to save RDB snapshots, but is currently not able to persist………………
    mstsc远程连接本地的虚拟机步骤
    Spring Scurity入门--遇到的坑-01
    idea环境连接Oracle数据库步骤
    虚拟机oracle: ORA-12514,TNS:listener does not currently know of SID given in connect descriptor错误解决
    多模块Maven工程 install时 出现Compilation failure: Compilation failure: …………ProductServiceImpl.java:[3,26] 程序包com.ssm.dao不存在 的错误解决办法
  • 原文地址:https://www.cnblogs.com/matianpeng/p/7358637.html
Copyright © 2011-2022 走看看