hooyantsing's Blog

第28次课程

字数统计: 938阅读时长: 5 min
2019/07/05

源辰74班

第28次课程

07.05.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
package com.hooy.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\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);
        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(posplay1<99 && posplay2<99) {
            if("on".equals(goAndStop[0])) {
                int step = throwshifter(1);
                System.out.println("\n骰子数"+step);
                System.out.println("您当前位置:"+posplay1);
            }else {
                System.out.println("停止一次");
                goAndStop[0] = "on";
            }
        }
        
    }

    private int throwshifter(int i) {
        System.out.println(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("请输入正确选项");
        }
        
    }
}

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
package com.hooy.dfw;
public class Map {
    int[] map = new int[100];
    int[] lucyTurn = {6,23,40,55,69,83};         //幸运转盘
    int[] landMine =  {5,13,17,33,38,50,64,80,94};  //地雷位置
    int[] pause = {9,27,60,90};                     //暂停位置
    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 showRLine(int start,int  end,int posplay1,int posplay2) {
       for(int i=start;i<end;i++) {
           System.out.println();
           for(int j=1;j<=84;j++) {
               System.out.print(" ");
           }
           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);
       showLine(35,66,posplay1,posplay2);
       showLLine(66,69,posplay1,posplay2);
       showLine(69,100,posplay1,posplay2);
    }
}

Main Test

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package com.hooy.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();
    }
}
CATALOG