第十三章 指導學習:人機猜拳


一、難點突破

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()

    

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

  • 實現循環對戰
      
      
      
              
  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("----------------歡 迎 進 入 游 戲 世 界----------------\n");
  24. System.out.println("\n\t\t******************");
  25. System.out.println ("\t\t** 猜拳, 開始 **");
  26. System.out.println ("\t\t******************");
  27. System.out.println("\n\n出拳規則: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("\n要開始嗎?(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("結果:和局,真衰!嘿嘿,等着瞧吧 !\n"); //平局
  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("結果說:^_^,你輸了,真笨!\n"); //計算機贏
  57. computer.score++;
  58. }
  59. count++;
  60. System.out.print("\n是否開始下一輪(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("----------------歡 迎 進 入 游 戲 世 界----------------\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. System.out.print("\n要開始嗎?(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("結果:和局,真衰!嘿嘿,等着瞧吧 !\n"); //平局
  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("結果說:^_^,你輸了,真笨!\n"); //計算機贏
  55. computer.score++;
  56. }
  57. count++;
  58. System.out.print("\n是否開始下一輪(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("\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. }

三、總結

  • 類和對象的關系是抽象和具體的關系
  • 使用類的步驟如下
    • (1)定義類:使用關鍵字class。
    • (2)創建類的對象:使用關鍵字new。
    • (3)使用類的屬性和方法:使用“.”操作符。
  • 定義類的方法包括三個部分
    • (1)方法的名稱 
    • (2)方法返回值的類型 
    • (3)方法的主體
  • 類的方法調用,使用如下兩種形式。
    • (1)同一個類中的方法,直接使用方法名
    • (2)不同類的方法,先創建對象,再使用“對象名.方法名”
  • 在Java中,有成員變量和局部變量,它們的作用域各不相同

關注我們


捐贈我們

    良師益友工作室一直在致力於幫助編程愛好更加快速方便地學習編程,如果您對我們的成果表示認同並且覺得對你有所幫助,歡迎您對我們捐贈^_^。

    







免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM