Java实现经典猜拳游戏

Java实现经典猜拳游戏

猜拳游戏:
玩家与电脑进行猜拳游戏,玩家行为采用输入方式,电脑行为采用随机形式
玩家可以输入数字选择电脑玩家进行游戏:1:刘备 2:孙权 3:曹操
玩家出拳规则,输入数字:1:剪刀 2:石头 3:布

 /*
猜拳游戏:
      玩家与电脑进行猜拳游戏,玩家行为采用输入方式,电脑行为采用随机形式
      玩家可以输入数字选择电脑玩家进行游戏:1:刘备 2:孙权 3:曹操
      玩家出拳规则,输入数字:1:剪刀 2:石头 3:布
*/

import java.util.Scanner;


public class FingerguessingGame {

  static String AiPlayer = "";  //电脑名字
  static String UsPlayer = "";  //玩家名字
  static int AiScore = 0;       //电脑得分
  static int UsScore = 0;       //玩家得分
  static int count = 0;         //游戏次数

  public static void main(String[] args) {
      Scanner sc = new Scanner(System.in);

      //游戏界面
      System.out.println("--------------------欢迎进入游戏世界--------------------");
      System.out.println();
      System.out.println("\t\t" + "******************************");
      System.out.println("\t\t" + "********** 猜拳,开始 **********");
      System.out.println("\t\t" + "******************************");
      System.out.println();

      InterFace();

      //询问是否开始游戏
      System.out.print("要开始吗?(y/n)");
      String judge = sc.nextLine();

      if (judge.equals("y")) {
          fingerguessingGame();
          while (true) {
              //询问是否开始下一轮游戏
              System.out.print("是否开始下一轮?(y/n)");
              String Judge = sc.nextLine();
              System.out.println("------------------------------------------------------");
              if (Judge.equals("y")) {
                  fingerguseeingGame();
              } else {
                  showScore();
                  break;
              }

          }
      } else {
          System.out.println("退出游戏!");
      }
  }

  //选择电脑玩家,输入自己的名字
  public static void InterFace() {
      Scanner sc = new Scanner(System.in);
      System.out.println("出拳规则:1.剪刀 2.石头 3.布");
      System.out.print("请选择对方角色(1:刘备 2:孙权 3:曹操):");
      int number = sc.nextInt();    //输入数字选择电脑玩家

      switch (number) {
          case 1:
              AiPlayer = "刘备";
              break;
          case 2:
              AiPlayer = "孙权";
              break;
          case 3:
              AiPlayer = "曹操";
              break;
          default:
      }
      sc.nextLine();      //清空前一次输入
      System.out.print("请输入你的名字:");
      UsPlayer = sc.nextLine();     //输入玩家名字
      System.out.println(UsPlayer + "  VS  " + AiPlayer);
      System.out.println();
  }


  //游戏主体
  public static void fingerguessingGame() {
      Scanner sc = new Scanner(System.in);
      System.out.println();
      System.out.print("请出拳:1.剪刀 2.石头 3.布(请输入相应数字)");
      int guess = sc.nextInt();    //输入数字选择剪刀、石头、布

      switch (guess) {
          case 1:
              System.out.println("你出拳:剪刀");
              break;
          case 2:
              System.out.println("你出拳:石头");
              break;
          case 3:
              System.out.println("你出拳:布");
              break;
          default :
      }

      int number = (int) (Math.random() * 10 % 3);    //生成电脑玩家的剪刀、石头、布

      switch (number) {
          case 1:
              System.out.println(AiPlayer + "出拳:剪刀");
              break;
          case 2:
              System.out.println(AiPlayer + "出拳:石头");
              break;
          case 3:
              System.out.println(AiPlayer + "出拳:布");
          default :
      }

          //判断输赢
          if (guess == 1 && number == 3 || guess == 2 && number == 1 || guess == 3 && number == 1) {
              System.out.println("结果:恭喜,你赢了!");
              UsScore++;
          } else if (guess == number) {
              System.out.println("结果:平手了!");
          } else {
              System.out.println("结果:下次加油!");
              AiScore++;
          }

          System.out.println();
      }

  //显示得分情况
  public static void showScore() {
      System.out.println(AiPlayer + "  VS  " + UsPlayer);
      System.out.println("对战次数:" + count);
      System.out.println();
      System.out.println("姓名" + "\t\t" + "得分");
      System.out.println(UsPlayer + "\t\t" + UsScore);
      System.out.println(AiPlayer + "\t\t" + AiScore);

      if (UsScore > AiScore) {
          System.out.println("结果:恭喜恭喜!");
      } else if (UsScore == AiScore) {
          System.out.println("结果:平局!");
      } else {
          System.out.println("结果:很遗憾,你输了!");
      }
      System.out.println();
      System.out.println("-------------------------------------------------------------------------");
  }
}

在这里插入图片描述

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值