A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 冯佩 中级黑马   /  2013-1-19 01:57  /  2006 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

编写一个关于石头,剪刀,布的猜拳小游戏,力求代码简单优化。

评分

参与人数 1技术分 +1 收起 理由
Rancho_Gump + 1 赞一个!

查看全部评分

3 个回复

倒序浏览
本帖最后由 唐晓 于 2013-1-19 09:01 编辑

/*电脑随机出的石头剪子布*/
public class Computer {
                private String fist;
        public String getFist() {
                return fist;
        }

        public void setFist(String fist) {
                this.fist = fist;
        }
  
    //1 2 3   剪刀 石头 布
    public int ShowFist()
    {
        int result = -1;
        Random r=new Random();
      
                result= (int)(Math.random()*2+1);
               

        switch (result)
        {
            case 1:
                fist = "剪刀";
                break;
            case 2:
                fist = "石头";
                break;
            case 3:
                fist = "布";
                break;
        }

        return result;
    }
}


/*你自己的出拳*/
public class Player {
               private String fist;
              public String getFist() {
                return fist;
        }
        public void setFist(String fist) {
                this.fist = fist;
        }
    public int ShowFist(String fist)
    {
        this.fist = fist;
        int result = -1;
        if (fist == "剪刀")
        {
            result = 1;
        }
        else if(fist == "石头")
        {
            result = 2;
        }
        else if(fist == "布")
        {
            result = 3;
        }
        return result;
    }
}


/*判断输赢*/
public class Choose {
         public String Check(int player, int pc)
     {
         String result = "";
         int tmp = player - pc;
         if (tmp == 0)
         {
             result = "平手";
         }
         else if (tmp == 1 || tmp == -2)
         {
             result = "YOU WIN!";
         }
         else
         {
             result = "YOU LOST!";
         }
         return result;
     }
}


主程序运行:异常没处理,这里全部都抛出去了。
public static void main(String[] args) throws Exception{
                // TODO Auto-generated method stub
                System.out.println("请输入石头剪子或者布:");
                //创建输入流
                BufferedReader br = new BufferedReader(new InputStreamReader(System.in));  
                String line = null;  
                //读取一行数据
                   line=br.readLine();
                   Check(line);
        }
         private static void Check(String fist)
     {
         Player player = new Player();
         player.ShowFist(fist);
         Computer pc = new Computer();
         pc.ShowFist();
         Choose j = new Choose();
         //判断输赢
       System.out.println("你出的是:"+player.getFist()+"----电脑出的是:"+pc.getFist()+"-----"+j.Check(player.ShowFist(fist), pc.ShowFist()));
         
     }

评分

参与人数 1技术分 +1 收起 理由
Rancho_Gump + 1 赞一个!

查看全部评分

回复 使用道具 举报
分析这个游戏包含三个类,玩家类,电脑类,游戏类
玩家类出1-3为石头,布剪刀,而电脑则用1-3随机数
的产生来确定出什么,游戏类将两个对象的结果进行
对比,在控制台输出结果即可
回复 使用道具 举报

  1. /**
  2. * 猜拳游戏
  3. * 1.通过输入数字,来出拳,也可随机出拳
  4. * 2.没有提供程序退出命令
  5. * */
  6. import java.util.Random;
  7. import java.util.Scanner;



  8. public class fingerGuessinGame {
  9.        
  10.         private static int number = punch.values().length ;  //获得枚举punch的length();
  11.         private Scanner input  = new Scanner(System.in);     
  12.        
  13.         public static void main(String[] args) {
  14.                 fingerGuessinGame f = new fingerGuessinGame();
  15.                 System.out.println("1:石头,2:剪刀,3:布,其他数字均为石头,0为随机");
  16.                 while (true)
  17.                         f.punch();
  18.         }
  19.        
  20.         private void punch()
  21.         {
  22.                   System.out.print("请输入:     ");
  23.                   
  24.                   int i = input.nextInt();                                   //获得输入的数字
  25.                   punch p = getRandom(i);                                        //从输入的数字得到出拳
  26.                   System.out.print(p.getName()+"  k  ");
  27.                   
  28.                   punch  random = getRandom(0);                   //电脑出拳
  29.                   System.out.print(random.getName());
  30.                   
  31.                   
  32.                   if( p.win.equals(random.toString()))
  33.                           System.out.println("    这局你赢");
  34.                   else if(p.lose.equals(random.toString()))
  35.                           System.out.println("    这局你输");
  36.                   else
  37.                           System.out.println("    平局");
  38.         }
  39.        
  40.         public static punch getRandom(int i){
  41.                 if(i==0)                                                                 //如果i为0,就随机出拳
  42.                         i = new Random().nextInt(number);
  43.                 switch ((int) i){
  44.                 case 1:
  45.                 return punch.stone;
  46.                 case 2:
  47.                 return punch.scissors;
  48.                 case 3:
  49.                 return punch.cloth;
  50.                 default: return punch.stone;
  51.                 }
  52.         }
  53.        
  54. }

  55. enum punch
  56. {
  57.         stone("石头","scissors","cloth"),
  58.         scissors("剪刀","cloth","stone"),
  59.         cloth("布","stone","scissors");

  60.         private String name;            
  61.          String win;                                        //出拳赢
  62.          String lose;                                        //出拳输
  63.        
  64.         public String getName()
  65.         {
  66.                 return name;
  67.         }
  68.        
  69.        
  70.         private punch(String name,String win,String lose)
  71.         {
  72.                 this.name = name;
  73.                 this.win = win;
  74.                 this.lose = lose;
  75.         }
  76. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
Rancho_Gump + 1 赞一个!

查看全部评分

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马