猜拳小游戏案例面向对象

* Computer 电脑类

* 属性 :角色名,得分。

* 方法:出拳的方法

 1 /**
 2  * Computer 电脑类
 3  * 属性 :角色名,得分。
 4  * 方法:出拳的方法
 5  * @author lisi
 6  * @version 版本 1.0
 7  * 
 8  */
 9 public class Computer {
10 //    属性 昵称,得分
11     String computer_name = "电脑";// null
12     int computer_score = 0;// 0
13     
14     //出拳方法  int --- 参数无参数。实例方法,对象方法 不被static修饰。
15     /**
16      * Computer 出拳方法
17      * @return 电脑出的拳:1 剪刀 2 石头 3 布 
18      */
19     public int computerShowFist() {
20         int show = (int)(Math.random()*3)+1;
21         switch (show) {
22         case 1:
23             System.out.println(computer_name + "出了剪刀");
24             
25             break;
26         case 2:
27             System.out.println(computer_name + "出了石头");
28             break;
29         case 3:
30             System.out.println(computer_name + "出了布");
31             break;
32 
33         
34         }
35         return show;
36         
37     }
38     
39     
40 
41 }
电脑类
1 public class ComputerTest {
2     public static void main(String[] args) {
3         Computer computer = new Computer();
4         int show = computer.computerShowFist();
5         System.out.println(show);
6         
7     }
8 
9 }
电脑类测试类
 1 import java.util.Scanner;
 2 
 3 /**
 4  * Person 类
 5  * 名字
 6  * 分数
 7  * 出去的功能
 8  * @author lisi
 9  *
10  */
11 public class Person {
12     String person_name = "匿名";
13     int person_score = 0;
14     
15     
16     // 人出拳
17     /**
18      * 人出拳的方法
19      * @return  人出的拳头;1 剪刀 2 石头 3 布
20      */
21     public int personShowFist() {
22         Scanner sc = new Scanner(System.in);
23         System.out.println("请出拳:1 剪刀 2 石头 3 布 ");
24         int show = sc.nextInt();
25         switch (show) {
26         case 1:
27             System.out.println(person_name + "出了剪刀");
28             
29             break;
30         case 2:
31             System.out.println(person_name + "出了石头");
32             break;
33         case 3:
34             System.out.println(person_name + "出了布");
35             break;
36 
37         
38         }
39         
40         return show;
41     }
42 
43 }
人类
  1 import java.util.Scanner;
  2 
  3 /**
  4  * 游戏类
  5  * 操作 管理 上面定义的Person类和Computer类,
  6  * 调用各自出拳的方法,去做比较,输赢。
  7  * @author 
  8  *
  9  */
 10 public class Game {
 11     
 12     // 把 Person 和 Computer,作为自己的属性值。
 13     Person person;
 14     Computer computer;// 类作为另外一个类的成员。
 15     // 引用数据类型默认值:--- null- --- 空指针
 16 //    定义属性count表示对战的次数
 17     int count = 0;
 18     
 19     
 20     public void inital() {
 21         if(person == null) {
 22             person = new Person();
 23         }
 24         if(computer == null) {
 25             computer = new Computer();
 26         }
 27     }
 28     
 29     
 30     // 开始游戏的方法:
 31     public void startGame() {
 32         Scanner sc = new Scanner(System.in);
 33         System.out.println("-----------欢迎加入猜拳游戏--------------");
 34         System.out.println();
 35         System.out.println("************************************");
 36         System.out.println("出拳的规则 : 1 剪刀 2 石头 3 布");
 37         System.out.println("**************猜拳开始********************");
 38         System.out.println("****************************************");
 39         System.out.println();
 40         
 41         // 调用初始化方法
 42         inital();
 43         // 加入角色名
 44         System.out.println("选择角色:1 曹操 2 吕布 3 孙权");
 45         int role = sc.nextInt();
 46         
 47         switch (role) {
 48         case 1:
 49             computer.computer_name = "曹操";
 50             break;
 51         case 2:
 52             computer.computer_name = "吕布";
 53             break;
 54         case 3:
 55             computer.computer_name = "孙权";
 56             break;
 57         }
 58         System.out.println("请输入你的名字:");
 59         person.person_name = sc.next();
 60         
 61         System.out.println(person.person_name + "   PK   " + computer.computer_name +" 对战");
 62         System.out.println("是否开始游戏:y / n");
 63         String answer = sc.next();
 64         
 65         while("y".equals(answer)) {
 66             
 67         
 68             // 调用各种的出拳:
 69             int personFist = person.personShowFist();
 70             int computerFist = computer.computerShowFist();
 71             
 72             // 根据出的拳比较输赢 1 剪刀 2 石头 3 布
 73             
 74             if (personFist == 1 && computerFist == 3 || personFist == 2 && computerFist == 1||personFist == 3 && computerFist == 2) {
 75                 System.out.println(person.person_name + "赢");
 76                 // 添加人的分数
 77                 person.person_score += 1;
 78             }else if (personFist == computerFist) {
 79                 System.out.println("平局");
 80             }else {
 81                 System.out.println(computer.computer_name + "赢");
 82                 computer.computer_score += 1;
 83             }
 84             // 记录对战的次数:
 85             count ++;
 86             
 87             System.out.println("是否开始下一局:y / n");
 88             answer = sc.next();
 89         }
 90         
 91         
 92         // 循环结束,结束游戏
 93         System.out.println("退出游戏");
 94         // 调用展示结果的方法
 95         showResult();
 96     }
 97     
 98     public void showResult() {
 99         System.out.println("--------------------");
100         System.out.println(person.person_name + "   PK   " + computer.computer_name );
101         System.out.println("对战的次数:" + count);
102         
103         
104         System.out.println(person.person_name + "的得分:"+ person.person_score);
105         System.out.println(computer.computer_name + "得分"+computer.computer_score);
106         
107         if (person.person_score > computer.computer_score) {
108             System.out.println(person.person_name+ "好厉害");
109         }else if (person.person_score == computer.computer_score) {
110             System.out.println("最终是平局");
111         }else {
112             System.out.println("布要气馁,下次接着来");
113         }
114         
115         
116         
117     }
118     
119     
120     
121     
122     
123     
124 
125 }
Game类
public class GameTest {
    public static void main(String[] args) {
        Game game = new Game();
        
        game.startGame();
    }

}
GameTest类

 

posted @ 2023-02-24 10:48  I_PENCIL  阅读(124)  评论(0编辑  收藏  举报