• 第十三章 指导学习:人机猜拳


    一、难点突破

    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(" 请出拳: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()

        

    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("----------------欢 迎 进 入 游 戏 世 界---------------- ");
    22. System.out.println(" ******************");
    23. System.out.println (" ** 猜拳, 开始 **");
    24. System.out.println (" ******************");
    25. System.out.println(" 出拳规则: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("----------------欢 迎 进 入 游 戏 世 界---------------- ");
    23. System.out.println(" ******************");
    24. System.out.println (" ** 猜拳, 开始 **");
    25. System.out.println (" ******************");
    26. System.out.println(" 出拳规则: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(" 要开始吗?(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("结果:和局,真衰! "); //平局
    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("结果说:^_^,你输了,真笨! "); //计算机赢
    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 代码

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

    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 代码

    • 实现对战结果显示

    1. /**
    2. * 游戏类
    3. * 阶段6:实现对战结果显示
    4. */
    5. public class Game4 {
    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("----------------欢 迎 进 入 游 戏 世 界---------------- ");
    23. System.out.println(" ******************");
    24. System.out.println (" ** 猜拳, 开始 **");
    25. System.out.println (" ******************");
    26. System.out.println(" 出拳规则: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. System.out.print(" 要开始吗?(y/n) ");
    40. String con = input.next();
    41. int perFist; //用户出的拳
    42. int compFist; //计算机出的拳
    43. while(con.equals("y")){
    44. /*出拳*/
    45. perFist = person.showFist();
    46. compFist = computer.showFist();
    47. /*裁决*/
    48. if((perFist == 1 && compFist == 1) || (perFist == 2 && compFist == 2) || (perFist == 3 && compFist == 3)){
    49. System.out.println("结果:和局,真衰!嘿嘿,等着瞧吧 ! "); //平局
    50. }else if((perFist == 1 && compFist == 3) || (perFist == 2 && compFist == 1) || (perFist == 3 && compFist == 2)){
    51. System.out.println("结果: 恭喜, 你赢了!"); //用户赢
    52. person.score++;
    53. }else{
    54. System.out.println("结果说:^_^,你输了,真笨! "); //计算机赢
    55. computer.score++;
    56. }
    57. count++;
    58. System.out.print(" 是否开始下一轮(y/n): ");
    59. con = input.next();
    60. }
    61. /*显示结果*/
    62. showResult();
    63. }
    64. /**
    65. * 显示比赛结果
    66. */
    67. public void showResult(){
    68. /*显示最后结果*/
    69. System.out.println("---------------------------------------------------");
    70. System.out.println(computer.name + " VS " + person.name);
    71. System.out.println("对战次数:"+ count);
    72. int result = calcResult();
    73. if(result == 1){
    74. System.out.println("结果:打成平手,下次再和你一分高下!");
    75. }else if(result == 2){
    76. System.out.println("结果:恭喜恭喜!"); //用户获胜
    77. }else{
    78. System.out.println("结果:呵呵,笨笨,下次加油啊!"); //计算机获胜
    79. }
    80. System.out.println("---------------------------------------------------");
    81. }
    82. /**
    83. * 计算比赛结果
    84. * @return 1:战平;2:用户赢;3:电脑赢
    85. */
    86. public int calcResult(){
    87. if(person.score == computer.score){
    88. return 1; // 战平
    89. }else if(person.score > computer.score){
    90. return 2; // 用户赢
    91. }else{
    92. return 3; // 电脑赢
    93. }
    94. }
    95. }
    • 测试
    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(" ******************");
    19. System.out.println (" ** 猜拳, 开始 **");
    20. System.out.println (" ******************");
    21. System.out.println(" 出拳规则: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+" 对战 ");
    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("结果:和局,真衰!嘿嘿,等着瞧吧 ! "); //平局
    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("结果说:^_^,你输了,真笨! "); //计算机赢
    58. computer.score++;
    59. }
    60. count++;
    61. System.out.print(" 是否开始下一轮(y/n): ");
    62. start = input.next();
    63. }
    64. /*显示结果*/
    65. showResult();
    66. // 扩展功能3:循环游戏,直到退出系统
    67. System.out.print(" 要开始下一局吗?(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(" 姓名 得分");
    84. System.out.println(person.name+" "+person.score);
    85. System.out.println(computer.name+" "+computer.score+" ");
    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. }

    三、总结

    • 类和对象的关系是抽象和具体的关系
    • 使用类的步骤如下
      • (1)定义类:使用关键字class。
      • (2)创建类的对象:使用关键字new。
      • (3)使用类的属性和方法:使用“.”操作符。
    • 定义类的方法包括三个部分
      • (1)方法的名称 
      • (2)方法返回值的类型 
      • (3)方法的主体
    • 类的方法调用,使用如下两种形式。
      • (1)同一个类中的方法,直接使用方法名
      • (2)不同类的方法,先创建对象,再使用“对象名.方法名”
    • 在Java中,有成员变量和局部变量,它们的作用域各不相同

    关注我们


    捐赠我们

        良师益友工作室一直在致力于帮助编程爱好更加快速方便地学习编程,如果您对我们的成果表示认同并且觉得对你有所帮助,欢迎您对我们捐赠^_^。

        






  • 相关阅读:
    mysql 常用命令
    MAC 升级到10.10(OS X Yosemite)下apache+php的配置问题
    php 第1讲 html介绍 html运行原理①
    php 开山篇
    Sublime Text 2
    java 抽象类和接口整理
    petri 网理论与研究(第一节140915)
    程序员应该读的书
    python logging模块小记
    常见设计模式,单例模式(双重检测锁方式)示例
  • 原文地址:https://www.cnblogs.com/imentors/p/4729909.html
Copyright © 2020-2023  润新知