人机猜拳

一、难点突破

1 知识梳理

 

二、综合练习

1 阶段1:练习——分析业务,创建用户类 

1.1 需求说明

  • 分析业务,抽象出类、类的特征和行为

  • 创建用户类

    

1.2 分析

1.3 代码

  • 创建用户:定义用户类Person,定义类的属性(name、score)和类的方法(showFirst())
     
  1. /**
  1. *
  1. * @author wangshaohua
  1. *
  1. */
  1. public class Person {
  1. String name = "匿名"; // 名字
  1. int score = 0; // 积分
  1. /**
  1. * 出拳
  1. *
  1. * @return 出拳结果:1.剪刀 2.石头 3.布
  1. */
  1. public int showFist() {
  1. // 接收用户的选择
  1. Scanner input = new Scanner(System.in);
  1. System.out.print("\n请出拳:1.剪刀 2.石头 3.布 (输入相应数字) :");
  1. int show = input.nextInt();
  1. // 输出出拳结果,并返回
  1. switch (show) {
  1. case 1:
  1. System.out.println("你出拳: 剪刀");
  1. break;
  1. case 2:
  1. System.out.println("你出拳: 石头");
  1. break;
  1. case 3:
  1. System.out.println("你出拳: 布");
  1. break;
  1. }
  1. return show;
  1. }
  1. }
  • 测试用户出拳
  1. /**
  2. * 人机互动版猜拳游戏
  3. * 阶段1:测试用户出拳
  4. *
  5. */
  6. public class TestPerson {
  7. public static void main(String[] args) {
  8. Person person = new Person();
  9. System.out.println(person.showFist());
  10. }
  11. }

    2 阶段2:练习——创建计算机类

    2.1 需求说明

            创建计算机类Computer。实现计算机出拳
            

    2.2 分析

    产生一个1~3的随机数,模拟计算机的出拳结果,例如,产生2,显示“电脑出拳:石头”

    2.3 代码

     

    • 计算机类

     

     

    1. /**
    2. * 计算机类
    3. * 阶段2完成
    4. */
    5. public class Computer {
    6. String name = "电脑"; // 名字
    7. int score = 0;; // 积分
    8. /**
    9. * 出拳
    10. * @return 出拳结果:1.剪刀 2.石头 3.布
    11. */
    12. public int showFist(){
    13. // 产生随机数
    14. int show = (int)(Math.random()*10)%3 + 1; //产生随机数,表示电脑出拳
    15. // 输出出拳结果并返回
    16. switch(show){
    17. case 1:
    18. System.out.println(name+"出拳: 剪刀");
    19. break;
    20. case 2:
    21. System.out.println(name+"出拳: 石头");
    22. break;
    23. case 3:
    24. System.out.println(name+"出拳: 布");
    25. break;
    26. }
    27. return show;
    28. }
    29. }
    • 测试计算机类

     

     

    1. /**
    2. * 人机互动版猜拳游戏
    3. * 阶段2:测试电脑出拳
    4. */
    5. public class TestComputer {
    6. public static void main(String[] args) {
    7. Computer computer = new Computer();
    8. System.out.println(computer.showFist());
    9. }
    10. }

    3 阶段3 练习——创建游戏类,选择对战对手

     

    3.1 需求说明

     

    • 创建游戏类Game
    • 编写游戏类的初始化方法initial()
    • 编写游戏类的开始游戏方法startGame()编写游戏类的开始游戏方法startGame()

       

          

      3.2 分析

      3.3 代码

      • 游戏类

       

      1. /**
      2. * 游戏类
      3. */
      4. public class Game1 {
      5. Person person; //甲方
      6. Computer computer; //乙方
      7. int count; //对战次数
      8. /**
      9. * 初始化
      10. */
      11. public void initial(){
      12. person = new Person();
      13. computer = new Computer();
      14. count = 0;
      15. }
      16. /**
      17. * 开始游戏
      18. */
      19. public void startGame() {
      20. initial();
      21. System.out.println("----------------欢 迎 进 入 游 戏 世 界----------------\n");
      22. System.out.println("\n\t\t******************");
      23. System.out.println ("\t\t** 猜拳, 开始 **");
      24. System.out.println ("\t\t******************");
      25. System.out.println("\n\n出拳规则:1.剪刀 2.石头 3.布");
      26. /*选择对方角色*/
      27. System.out.print("请选择对方角色(1:刘备 2:孙权 3:曹操): ");
      28. Scanner input = new Scanner(System.in);
      29. int role = input.nextInt();
      30. if(role == 1){
      31. computer.name = "刘备";
      32. }else if(role == 2){
      33. computer.name = "孙权";
      34. }else if(role == 3){
      35. computer.name = "曹操";
      36. }
      37. System.out.print("你选择了"+computer.name+"对战");
      38. }
      39. }
      • 测试开始游戏:选择对战角色
      1. /**
      2. * 人机互动版猜拳游戏
      3. * 阶段3:测试开始游戏:选择对战角色
      4. */
      5. public class TestGame1 {
      6. public static void main(String[] args) {
      7. Game1 game = new Game1();
      8. game.startGame();
      9. }
      10. }

      4 阶段4:练习——实现一局对战

      4.1 需求说明分别调用用户类和计算机类的出拳方法showFist(),接受返回值并比较,给出胜负结果

                  

       

      4.2 分析
      4.3 代码

      • 实现一局对战

       

      1. /**
      2. * 游戏类
      3. * 阶段4:实现一局对战
      4. */
      5. public class Game2 {
      6. Person person; //甲方
      7. Computer computer; //乙方
      8. int count; //对战次数
      9. /**
      10. * 初始化
      11. */
      12. public void initial(){
      13. person = new Person();
      14. computer = new Computer();
      15. count = 0;
      16. }
      17. /**
      18. * 开始游戏
      19. */
      20. public void startGame() {
      21. initial(); // 初始化
      22. System.out.println("----------------欢 迎 进 入 游 戏 世 界----------------\n");
      23. System.out.println("\n\t\t******************");
      24. System.out.println ("\t\t** 猜拳, 开始 **");
      25. System.out.println ("\t\t******************");
      26. System.out.println("\n\n出拳规则:1.剪刀 2.石头 3.布");
      27. /*选择对方角色*/
      28. System.out.print("请选择对方角色(1:刘备 2:孙权 3:曹操): ");
      29. Scanner input = new Scanner(System.in);
      30. int role = input.nextInt();
      31. if(role == 1){
      32. computer.name = "刘备";
      33. }else if(role == 2){
      34. computer.name = "孙权";
      35. }else if(role == 3){
      36. computer.name = "曹操";
      37. }
      38. System.out.println("你选择了 "+computer.name+"对战");
      39. /*开始游戏*/
      40. System.out.print("\n要开始吗?(y/n) ");
      41. String con = input.next();
      42. int perFist; //用户出的拳
      43. int compFist; //计算机出的拳
      44. if(con.equals("y")){
      45. /*出拳*/
      46. perFist = person.showFist();
      47. compFist = computer.showFist();
      48. /*裁决*/
      49. if((perFist == 1 && compFist == 1) || (perFist == 2 && compFist == 2) || (perFist == 3 && compFist == 3)){
      50. System.out.println("结果:和局,真衰!\n"); //平局
      51. }else if((perFist == 1 && compFist == 3) || (perFist == 2 && compFist == 1) || (perFist == 3 && compFist == 2)){
      52. System.out.println("结果: 恭喜, 你赢了!"); //用户赢
      53. }else{
      54. System.out.println("结果说:^_^,你输了,真笨!\n"); //计算机赢
      55. }
      56. }
      57. }
      58. }
      • 测试开始游戏:实现1局对战

       

       

      1. /**
      2. * 人机互动版猜拳游戏
      3. * 阶段4:测试开始游戏:实现1局对战
      4. */
      5. public class TestGame2 {
      6. public static void main(String[] args) {
      7. Game2 game = new Game2();
      8. game.startGame();
      9. }
      10. }

      5 阶段5:练习——实现循环对战,并累计得分 

       

      5.1 需求说明

       

          实现循环对战,

      并且累加赢家的得分

       

       

          

       

      5.2 分析
      5.3 代码

       

      • 实现循环对战/**
        • * 游戏类
        • * 阶段5:实现循环对战
        • *
        • */
        • public class Game3 {
        • Person person; //甲方
        • Computer computer; //乙方
        • int count; //对战次数
        • /**
        • * 初始化
        • */
        • public void initial(){
        • person = new Person();
        • computer = new Computer();
        • count = 0;
        • }
        • /**
        • * 开始游戏
        • */
        • public void startGame() {
        • initial(); // 初始化
        • System.out.println("----------------欢 迎 进 入 游 戏 世 界----------------\n");
        • System.out.println("\n\t\t******************");
        • System.out.println ("\t\t** 猜拳, 开始 **");
        • System.out.println ("\t\t******************");
        • System.out.println("\n\n出拳规则:1.剪刀 2.石头 3.布");
        • /*选择对方角色*/
        • System.out.print("请选择对方角色(1:刘备 2:孙权 3:曹操): ");
        • Scanner input = new Scanner(System.in);
        • int role = input.nextInt();
        • if(role == 1){
        • computer.name = "刘备";
        • }else if(role == 2){
        • computer.name = "孙权";
        • }else if(role == 3){
        • computer.name = "曹操";
        • }
        • System.out.println("你选择了 "+computer.name+"对战");
        • /*开始游戏*/
        • System.out.print("\n要开始吗?(y/n) ");
        • String con = input.next();
        • int perFist; //用户出的拳
        • int compFist; //计算机出的拳
        • while(con.equals("y")){
        • /*出拳*/
        • perFist = person.showFist();
        • compFist = computer.showFist();
        • /*裁决*/
        • if((perFist == 1 && compFist == 1) || (perFist == 2 && compFist == 2) || (perFist == 3 && compFist == 3)){
        • System.out.println("结果:和局,真衰!嘿嘿,等着瞧吧 !\n"); //平局
        • }else if((perFist == 1 && compFist == 3) || (perFist == 2 && compFist == 1) || (perFist == 3 && compFist == 2)){
        • System.out.println("结果: 恭喜, 你赢了!"); //用户赢
        • person.score++;
        • }else{
        • System.out.println("结果说:^_^,你输了,真笨!\n"); //计算机赢
        • computer.score++;
        • }
        • count++;
        • System.out.print("\n是否开始下一轮(y/n): ");
        • con = input.next();
        • }
        • }
        • }
        • 测试循环对战

         

         

        1. public class TestGame3 {
        2. public static void main(String[] args) {
        3. Game3 game = new Game3();
        4. game.startGame();
        5. }
        6. }

        6 阶段6:练习——显示对战结果

         

        6.1 需求说明

                游戏结束后,显示对战结果

                

        6.2 分析

         

            编写showResult( )方法,比较二者的得分情况,给出对战结果

         

        6.3 代码

         

        • 实现对战结果显示/**
          • * 游戏类
          • * 阶段6:实现对战结果显示
          • */
          • public class Game4 {
          • Person person; //甲方
          • Computer computer; //乙方
          • int count; //对战次数
          • /**
          • * 初始化
          • */
          • public void initial(){
          • person = new Person();
          • computer = new Computer();
          • count = 0;
          • }
          • /**
          • * 开始游戏
          • */
          • public void startGame() {
          • initial(); // 初始化
          • System.out.println("----------------欢 迎 进 入 游 戏 世 界----------------\n");
          • System.out.println("\n\t\t******************");
          • System.out.println ("\t\t** 猜拳, 开始 **");
          • System.out.println ("\t\t******************");
          • System.out.println("\n\n出拳规则:1.剪刀 2.石头 3.布");
          • /*选择对方角色*/
          • System.out.print("请选择对方角色(1:刘备 2:孙权 3:曹操): ");
          • Scanner input = new Scanner(System.in);
          • int role = input.nextInt();
          • if(role == 1){
          • computer.name = "刘备";
          • }else if(role == 2){
          • computer.name = "孙权";
          • }else if(role == 3){
          • computer.name = "曹操";
          • }
          • System.out.println("你选择了 "+computer.name+"对战");
          • System.out.print("\n要开始吗?(y/n) ");
          • String con = input.next();
          • int perFist; //用户出的拳
          • int compFist; //计算机出的拳
          • while(con.equals("y")){
          • /*出拳*/
          • perFist = person.showFist();
          • compFist = computer.showFist();
          • /*裁决*/
          • if((perFist == 1 && compFist == 1) || (perFist == 2 && compFist == 2) || (perFist == 3 && compFist == 3)){
          • System.out.println("结果:和局,真衰!嘿嘿,等着瞧吧 !\n"); //平局
          • }else if((perFist == 1 && compFist == 3) || (perFist == 2 && compFist == 1) || (perFist == 3 && compFist == 2)){
          • System.out.println("结果: 恭喜, 你赢了!"); //用户赢
          • person.score++;
          • }else{
          • System.out.println("结果说:^_^,你输了,真笨!\n"); //计算机赢
          • computer.score++;
          • }
          • count++;
          • System.out.print("\n是否开始下一轮(y/n): ");
          • con = input.next();
          • }
          • /*显示结果*/
          • showResult();
          • }
          • /**
          • * 显示比赛结果
          • */
          • public void showResult(){
          • /*显示最后结果*/
          • System.out.println("---------------------------------------------------");
          • System.out.println(computer.name + " VS " + person.name);
          • System.out.println("对战次数:"+ count);
          • 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; // 电脑赢
          • }
          • }
          • }
          • 测试
          1. public class TestGame4 {
          2. /**
          3. * 人机互动版猜拳游戏
          4. */
          5. public static void main(String[] args) {
          6. Game4 game = new Game4();
          7. game.initial();
          8. game.startGame();
          9. }
          10. }

          7 阶段7:练习——完善游戏类的startGame()

           

          7.1 需求说明输入并保存用户姓名,游戏结束后显示双方的各自得分

              

          7.2 分析
          7.3 代码

           

          • 功能扩展

           

           

          1. public class Game {
          2. Person person; //甲方
          3. Computer computer; //乙方
          4. int count; //对战次数
          5. /**
          6. * 初始化
          7. */
          8. public void initial(){
          9. person = new Person();
          10. computer = new Computer();
          11. count = 0;
          12. }
          13. /**
          14. * 开始游戏
          15. */
          16. public void startGame() {
          17. System.out.println("----------------欢 迎 进 入 游 戏 世 界----------------");
          18. System.out.println("\n\t\t******************");
          19. System.out.println ("\t\t** 猜拳, 开始 **");
          20. System.out.println ("\t\t******************");
          21. System.out.println("\n出拳规则:1.剪刀 2.石头 3.布");
          22. Scanner input = new Scanner(System.in);
          23. String exit = "n"; // 退出系统
          24. do{
          25. initial(); // 初始化
          26. /*选择对方角色*/
          27. System.out.print("请选择对方角色(1:刘备 2:孙权 3:曹操): ");
          28. int role = input.nextInt();
          29. if(role == 1){
          30. computer.name = "刘备";
          31. }else if(role == 2){
          32. computer.name = "孙权";
          33. }else if(role == 3){
          34. computer.name = "曹操";
          35. }
          36. // 扩展功能1:输入用户姓名
          37. /*输入用户姓名*/
          38. System.out.print("请输入你的姓名:");
          39. person.name = input.next();
          40. System.out.println(person.name+" VS "+computer.name+" 对战\n");
          41. // 扩展功能1结束
          42. System.out.print("要开始吗?(y/n) ");
          43. String start = input.next(); // 开始每一局游戏
          44. int perFist; //用户出的拳
          45. int compFist; //计算机出的拳
          46. while(start.equals("y")){
          47. /*出拳*/
          48. perFist = person.showFist();
          49. compFist = computer.showFist();
          50. /*裁决*/
          51. if((perFist == 1 && compFist == 1) || (perFist == 2 && compFist == 2) || (perFist == 3 && compFist == 3)){
          52. System.out.println("结果:和局,真衰!嘿嘿,等着瞧吧 !\n"); //平局
          53. }else if((perFist == 1 && compFist == 3) || (perFist == 2 && compFist == 1) || (perFist == 3 && compFist == 2)){
          54. System.out.println("结果: 恭喜, 你赢了!"); //用户赢
          55. person.score++;
          56. }else{
          57. System.out.println("结果说:^_^,你输了,真笨!\n"); //计算机赢
          58. computer.score++;
          59. }
          60. count++;
          61. System.out.print("\n是否开始下一轮(y/n): ");
          62. start = input.next();
          63. }
          64. /*显示结果*/
          65. showResult();
          66. // 扩展功能3:循环游戏,直到退出系统
          67. System.out.print("\n要开始下一局吗?(y/n):");
          68. exit = input.next();
          69. System.out.println();
          70. // 扩展功能3结束
          71. }while(!exit.equals("n"));
          72. System.out.println("系统退出!");
          73. }
          74. /**
          75. * 显示比赛结果
          76. */
          77. public void showResult(){
          78. /*显示对战次数*/
          79. System.out.println("---------------------------------------------------");
          80. System.out.println(computer.name + " VS " + person.name);
          81. System.out.println("对战次数:"+ count);
          82. // 扩展功能2:显示最终的得分
          83. System.out.println("\n姓名\t得分");
          84. System.out.println(person.name+"\t"+person.score);
          85. System.out.println(computer.name+"\t"+computer.score+"\n");
          86. // 扩展功能2结束
          87. /*显示对战结果*/
          88. int result = calcResult();
          89. if(result == 1){
          90. System.out.println("结果:打成平手,下次再和你一分高下!");
          91. }else if(result == 2){
          92. System.out.println("结果:恭喜恭喜!"); //用户获胜
          93. }else{
          94. System.out.println("结果:呵呵,笨笨,下次加油啊!"); //计算机获胜
          95. }
          96. System.out.println("---------------------------------------------------");
          97. }
          98. /**
          99. * 计算比赛结果
          100. * @return 1:战平;2:用户赢;3:电脑赢
          101. */
          102. public int calcResult(){
          103. if(person.score == computer.score){
          104. return 1; // 战平
          105. }else if(person.score > computer.score){
          106. return 2; // 用户赢
          107. }else{
          108. return 3; // 电脑赢
          109. }
          110. }
          111. }
          测试

           

           

            1. /**
            2. * 人机互动版猜拳游戏
            3. * 程序入口
            4. */
            5. public class StartGuess {
            6. public static void main(String[] args) {
            7. Game game = new Game();
            8. game.startGame();
            9. }
            10. }

转载于:https://www.cnblogs.com/li654321/p/11466077.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值