hooyantsing's Blog

第29次课程

字数统计: 1.5k阅读时长: 8 min
2019/07/06

源辰74班

第29次课程

07.06.2019

**内容    **

** Java_面向对象部分复习[蒋斌]**

1.[游戏]大富翁_续

Class Game

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package com.hooy.homework.dfw;

import java.util.Random;
import java.util.Scanner;

public class Game {
    private String[] playName = new String[2];
    private Map map;
    private int posplay1;
    private int posplay2;
    private String[] goAndStop = new String[2];
    private Scanner sc = new Scanner(System.in);
    private Random r = new Random();
    
    public void init() {
        map = new Map();
        posplay1 = 0;
        posplay2 = 0;
        goAndStop[0] = "on";
        goAndStop[1] = "on";
    }
    
    public void start() {
        init();
        System.out.println("========================================");
        System.out.println("||                                    ||");
        System.out.println("||               大  富  翁                                       ||");
        System.out.println("||                                    ||");
        System.out.println("========================================");
        System.out.println("\n");
        System.out.println("XXXXXXXXXXXXXXXX 双人对战   XXXXXXXXXXXXXXXX");
        System.out.println("\n");
        System.out.println("请选择角色:1.钱夫人  2.大老千  3.阿依公主  4.贝哥");
        System.out.println("请选择一号玩家角色:");
        int role1 = sc.nextInt();
        int role2 = 0;
        do {
            System.out.println("请选择二号玩家角色:");
            role2 = sc.nextInt();
        }while(role1==role2);
        sc.nextLine();
        setRole(1,role1);
        setRole(2,role2);
        System.out.println("****************************************");
        System.out.println("               Game Start               ");
        System.out.println("****************************************");
        System.out.println(playName[0]+",您的代号:A");
        System.out.println(playName[1]+",您的代号:B");
        System.out.println("图例:¤幸运转盘  ★地雷位置  ■暂停位置  〓时空隧道  ∷普通");
        map.showMap(posplay1,posplay2);
        //游戏
        play();
    }

    private void play() {
        while(true) {
            System.out.println();
            if("on".equals(goAndStop[0])) {
                int step = throwshifter(1);
                System.out.println("\n骰子数"+step);
                posplay1 += step;
                if(posplay1<100) {
                    //调用游戏规则方法
                    posplay1 = gameRule(1,posplay1);
                }else {
                    System.out.println("恭喜:"+playName[0]+",奔跑第一名!");
                    break;
                }
                
            }else {
                System.out.println("停止一次");
                goAndStop[0] = "on";
            }
            System.out.println("您当前位置:"+posplay1);
            map.showMap(posplay1,posplay2);
            System.out.println();
            if("on".equals(goAndStop[1])) {
                int step = throwshifter(2);
                System.out.println("\n骰子数"+step);
                posplay2 += step;
                if(posplay2<100) {
                    //调用游戏规则方法
                    posplay2 = gameRule(2,posplay2);
                }else {
                    System.out.println("恭喜:"+playName[1]+",奔跑第一名!");
                    break;
                }
            }else {
                System.out.println("停止一次");
                goAndStop[1] = "on";
            }
            System.out.println("您当前位置:"+posplay2);
            map.showMap(posplay1,posplay2);
        }
        
    }

    private int throwshifter(int i) {
        System.out.print(playName[i-1]+"请按任意键投掷骰子");
        sc.nextLine();
        return r.nextInt(6)+1;
    }

    private void setRole(int no, int role) {
        switch(role) {
        case 1:
            playName[no-1] = "钱夫人";
            break;
        case 2:
            playName[no-1] = "大老千";
            break;
        case 3:
            playName[no-1] = "阿依公主";
            break;
        case 4:
            playName[no-1] = "贝哥";
            break;
        default:
            System.out.println("请输入正确选项");
        }
        
    }
    
    public int gameRule(int player,int posplay) {
        //幸运转盘
        if(map.getMap()[posplay]==1) {
            System.out.println("幸运转盘");
            map.showMap(posplay1,posplay2);
            int result = 0;
            do {
                System.out.println("\n1.交换 2.炸弹");
                result = sc.nextInt();
                sc.nextLine();
                switch(result) {
                case 1:
                    if(player==1) {
                        int temp = posplay1;
                        posplay1 = posplay2;
                        posplay2 = temp;
                        return posplay1;
                    }else {
                        int temp = posplay1;
                        posplay1 = posplay2;
                        posplay2 = temp;
                        return posplay2;
                    }
                case 2:
                    //老师案例(存在隐含bug,不建议使用)
                    /*if(player==1 && posplay2<6) {
                        posplay2 = 0;
                    }else {
                        posplay2 -= 6;
                    }
                    if(player==2 && posplay1<6){
                        posplay1 = 0;
                    }else {
                        posplay1 -= 6;
                    }*/
                    //老师案例(修复版)
                    if(player==1) {
                        if(posplay2<6) {
                            posplay2 = 0;
                        }else {
                            posplay2 -= 6;
                        }
                    }
                    if(player==2) {
                        if(posplay1<6) {
                            posplay1 = 0;
                        }else {
                            posplay1 -= 6;
                        }
                    }
                    //自主案例(修复老师案例中隐含bug,推荐)
                    /*if(player==1 && posplay2<6) {
                        posplay2 = 0;
                    }
                    if(player==1 && posplay2>=6){
                        posplay2 -= 6;
                    }
                    if(player==2 && posplay1<6){
                        posplay1 = 0;
                    }
                    if(player==2 && posplay1>=6){
                        posplay1 -= 6;
                    }*/
                    return posplay;
                default:
                    System.out.println("没有这个选项");
                }
            }while(result<1 || result>2);
            //报错
            return -1000;
        }
        //地雷位置
        if(map.getMap()[posplay]==2) {
            System.out.println("地雷位置");
            if(posplay<6) {
                return 0;
            }
            return posplay-6;
        }
        //暂停
        if(map.getMap()[posplay]==3) {
            System.out.println("暂停位置");
            goAndStop[player-1] = "off";
            return posplay;
        }
        //时空隧道
        if(map.getMap()[posplay]==4) {
            System.out.println("时空隧道");
            return posplay+10;
        }
        return posplay;
    }
}

Class Map

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package com.hooy.homework.dfw;
public class Map {
    private int[] map = new int[100];
    private int[] lucyTurn =  {6,23,40,55,69,83};           //幸运转盘
    private int[] landMine =  {5,13,17,33,38,50,64,80,94};  //地雷位置
    private int[] pause = {9,27,60,93};                     //暂停位置
    private int[] timeTunnel =  {20,25,45,63,72,88,90};       //时空隧道
    
    //关卡:1.幸运轮盘 2.地雷 3.暂停 4.时空隧道 0.普通
    
    public void createMap() {
       for(int i=0;i<lucyTurn.length;i++) {
           map[lucyTurn[i]] = 1;
       }
       for(int i=0;i<landMine.length;i++) {
           map[landMine[i]] = 2;
       }
       for(int i=0;i<pause.length;i++) {
           map[pause[i]] = 3;
       }
       for(int i=0;i<timeTunnel.length;i++)  {
           map[timeTunnel[i]] = 4;
       }
    }
    //横右向
    public void showLine(int start,int  end,int posplay1,int posplay2) {
       for(int i=start;i<end;i++) {
           getGraph(i,map[i],posplay1,posplay2);
       }
    }
    //横左向
       public void showLineR(int start,int  end,int posplay1,int posplay2) {
           for(int i=start;i>end;i--) {
               getGraph(i,map[i],posplay1,posplay2);
           }
       }
    //右竖
    public void showRLine(int start,int  end,int posplay1,int posplay2) {
       for(int i=start;i<end;i++) {
           System.out.println();
           for(int j=1;j<=4;j++) {
               System.out.print("\t");
           }   
           getGraph(i,map[i],posplay1,posplay2);
       }
       System.out.println();
    }
    //左竖
    public void showLLine(int start,int  end,int posplay1,int posplay2) {
       for(int i=start;i<end;i++) {
           System.out.println();
           getGraph(i,map[i],posplay1,posplay2);
       }
       System.out.println();
    }
    private void getGraph(int i,int  choice,int posplay1,int posplay2) {
       if(i==posplay1 && i==posplay2) {
           System.out.print("@");
       }else if(i==posplay1){
           System.out.print("A");
       }else if(i==posplay2) {
           System.out.print("B");
       }else {
           switch(choice) {
           case 1:
               System.out.print("¤");
               break;
           case 2:
               System.out.print("★");
               break;
           case 3:
               System.out.print("■");
               break;
           case 4:
               System.out.print("〓");
               break;
           default:
               System.out.print("∷");
           }
       }
    }
    
    public void showMap(int posplay1,int  posplay2) {
       createMap();
       showLine(0,31,posplay1,posplay2);
       showRLine(31,35,posplay1,posplay2);
       showLineR(65,34,posplay1,posplay2);
       showLLine(66,69,posplay1,posplay2);
       showLine(69,100,posplay1,posplay2);
    }
    public int[] getMap() {
       return map;
    }
}

Main Test

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package com.hooy.homework.dfw;
public class Test {
    public static void main(String[] args) {
       /*Map maps = new Map();
       maps.createMap();
       maps.showLine(0,31);
       maps.showRLine(31,35);
       maps.showLine(35,66);
       maps.showLLine(66,69);
       maps.showLine(69,100);*/
       Game g = new Game();
       g.start();
    }
}

最终效果图:

f4020a48aba7fc9bd024ea9808c9efaf.png

3f3d3c823b105a44b4686e509cc853a9.png

CATALOG