弱雞小富翁
阿新 • • 發佈:2017-12-02
ice show action 簡單的 desktop class markdown cati bound
簡單的棋盤
課程的理解
在這幾次的使用類,構建類間的關系實踐中,已經懂得一部分類的概念,也能構建相對規範實用的類。類圖的畫法也基本上可以分辨得出來,但是有幾種自己實踐中沒用到的,印象不深,還得翻一翻書才能分辨。使用類可以使得整個代碼的結構清晰,哪一部分的功能調用哪一個類,這樣在出現問題的時候,能夠精準快速地找到問題和改正問題。當然不僅如此,還有要添加新功能,還是修改一些方法,直接在類中修改,而不需要每個傳遞到的地方都要修改,很大地提高了效率。在開始作業前,我考慮了Map類需要有哪些方法什麽變量,進而將Map類分割成由Zone類組成,再將兩者關系結構考慮清楚,還有遊戲者Role的實現。
設計實現
如圖所示的各類間關系,MapGui調用了Dice類,Role類,Map類,Map類調用Zone類。各類間所帶方法和參數如圖所示。
代碼說明
1.Dice.java
//骰子類的實現 public class Dice { private int faceValue; //定義骰子面值 public Dice(){ faceValue = (int)(Math.random() * 6) + 1; //獲得一 個1-6的隨機數 } public void roll(){ //搖骰子 faceValue = (int)(Math.random() * 6) + 1; } public void unnormalroll(){ //搖不出4的骰子 while(faceValue==4){ faceValue = (int)(Math.random() * 6) + 1; } } public int getValue(){ //獲得骰子數值 return faceValue; } }
2.Role.java
public class Role { private String name; private int money = 0; private int location = 0; public Role(String name,int money) { this.name = name; this.money = money; } public String getName() { return this.name; } public void changMoney(int money) { this.money += money; } public int getMoney() { return this.money; } public void move(int i,int limit) { this.location = this.location +i; if(this.location > limit-1 ) { this.location = this.location-limit+1; } } public int getlocation() { return this.location; } }
3.Zone.java
public class Zone {
private int price = 0;
private int fee = 0;
private String owner = null;
public Zone(int price) {
this.price = price;
this.fee = (int)(price * 0.1);
}
public String showOwner() {
return this.owner;
}
public int showPrice() {
return this.price;
}
public int showFee() {
return this.fee;
}
public void changOwner(String owner) {
this.owner = owner;
}
}
4.Map.java
public class Map {
private int num;
private Zone zone[];
public Map(int num){
this.num = num;
zone = new Zone[num];
}
public void modul1() {
int i;
zone[0] = new Zone(0);
for(i=1;i<num;i++) {
switch(i%5) {
case 0: zone[i] = new Zone(1000);break;
case 1: zone[i] = new Zone(2000);break;
case 2: zone[i] = new Zone(3000);break;
case 3: zone[i] = new Zone(4000);break;
case 4: zone[i] = new Zone(5000);break;
}
}
}
public int zoneValue(int i) {
return zone[i].showPrice();
}
public int zoneFee(int i) {
return zone[i].showFee();
}
public String zoneOwner(int i) {
return zone[i].showOwner();
}
public void zoneOwe(int i,String owner) {
zone[i].changOwner(owner);
}
}
5.MapGui.java
import java.awt.Color;
import java.awt.EventQueue;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTable;
import java.awt.Font;
import javax.swing.SwingConstants;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class MapGui {
private JFrame frame;
private int[] setting = {4,50000,20};
private Role[] role;
public String name[]= {"丁丁","迪西","拉拉","小波"};
public static int i=1,round=1;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MapGui window = new MapGui();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public MapGui() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
JLabel[] jLabel = new JLabel[26];
Map map = new Map(26);
Dice dice = new Dice();
JLabel lblNewLabel_2 = new JLabel("\u7B5B\u5B50\u56FE\u7247");
ImageIcon[] image = new ImageIcon[7];
image[0] = new ImageIcon("D:\\Desktop\\dice\\0.gif");
for(int j=1;j<7;j++)
image[j] = new ImageIcon("D:\\Desktop\\dice\\"+j+".png");
map.modul1();
frame = new JFrame();
frame.setTitle("弱雞小富翁");
frame.setResizable(false);
frame.setBounds(100, 100, 1284, 720);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JPanel panel = new JPanel();
panel.setBounds(0, 0, 1280, 720);
frame.getContentPane().add(panel);
panel.setLayout(null);
for(i=0;i<26;i++) {
jLabel[i] = new JLabel();
jLabel[i].setOpaque(true);
jLabel[i].setBackground(Color.WHITE);
jLabel[i].setFont(new Font("宋體", Font.BOLD, 12));
if(i != 0) {
jLabel[i].setText("<html><body><center>區域"+i+"<br>" + "地價為:" +map.zoneValue(i)+"<br>如果有人擁有<br>您將付"+map.zoneFee(i)+"元</center></body></html>");
}
if(i == 0) {
jLabel[i].setText(" 起點");
jLabel[i].setBounds(1065, 514, 87, 84);
}else if(i < 9) {
jLabel[i].setBounds(1065-97*i, 514, 87, 84);
}else if(i < 13) {
jLabel[i].setBounds(289, 420-94*(i-9), 87, 84);
}else if(i < 22) {
jLabel[i].setBounds(289+97*(i-13), 44, 87, 84);
}else{
jLabel[i].setBounds(1065, 138+94*(i-22), 87, 84);
}
panel.add(jLabel[i]);
}
JPanel panel_1 = new JPanel();
panel_1.setBounds(31, 115, 224, 501);
panel.add(panel_1);
panel_1.setLayout(null);
JLabel lblNewLabel_1 = new JLabel(" 個人信息面板");
lblNewLabel_1.setFont(new Font("宋體", Font.BOLD, 24));
lblNewLabel_1.setBounds(10, 10, 204, 49);
panel_1.add(lblNewLabel_1);
JLabel Label_name = new JLabel("\u4EBA\u7269\u540D\u79F0\uFF1A");
Label_name.setFont(new Font("宋體", Font.BOLD, 15));
Label_name.setBounds(10, 69, 197, 30);
panel_1.add(Label_name);
JLabel Label_money = new JLabel("\u4EBA\u7269\u91D1\u94B1\uFF1A");
Label_money.setFont(new Font("宋體", Font.BOLD, 15));
Label_money.setBounds(10, 108, 197, 30);
panel_1.add(Label_money);
JLabel Label_location = new JLabel("\u4EBA\u7269\u4F4D\u7F6E\uFF1A");
Label_location.setFont(new Font("宋體", Font.BOLD, 15));
Label_location.setBounds(10, 146, 197, 30);
panel_1.add(Label_location);
JLabel Label_info = new JLabel("回合: - 第 號投擲");
Label_info.setFont(new Font("宋體", Font.BOLD, 15));
Label_info.setBounds(10, 209, 204, 30);
panel_1.add(Label_info);
JButton useDice = new JButton("使用篩子");
useDice.setVisible(false);
i=1;
useDice.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
int j=0;
dice.roll();
dice.getValue();
lblNewLabel_2.setIcon(image[dice.getValue()]);
role[i-1].move(dice.getValue(),26);
if(map.zoneOwner(role[i-1].getlocation()) == null || "".equals(map.zoneOwner(role[i-1].getlocation()))) {
role[i-1].changMoney(-1*map.zoneValue(role[i-1].getlocation()));
map.zoneOwe(role[i-1].getlocation(),role[i-1].getName());
}else if(map.zoneOwner(role[i-1].getlocation()) != role[i-1].getName()) {
role[i-1].changMoney(-1*map.zoneFee(role[i-1].getlocation()));
j++;
}else {
role[i-1].changMoney(j*map.zoneFee(role[i-1].getlocation()));
j=0;
}
Label_name.setText("人物名稱:"+ role[i-1].getName());
Label_money.setText("人物金錢:"+role[i-1].getMoney());
Label_location.setText("人物位置:"+role[i-1].getlocation()+"區域");
Label_info.setText("回合:"+round+" - 第"+i+"號投擲");
if(i < setting[0]) {
i++;
} else {
i=1;
round++;
if(round > setting[2]) {
int[] rank = {0,1,2,3};
int[] store = {role[0].getMoney(),role[1].getMoney(),role[2].getMoney(),role[3].getMoney()};
for(int z1 = 0;z1 < 3;z1++) {
for(int z2 = z1;z2 < 4;z2++) {
if(store[z1]<store[z2]) {
int t =store[z1];
store[z1]=store[z2];
store[z2]=t;
t=rank[z1];
rank[z1]=rank[z2];
rank[z2]=t;
}
}
}
lblNewLabel_1.setText(" 遊戲結束");
Label_name.setText("No.1:"+role[rank[0]].getName()+role[rank[0]].getMoney());
Label_money.setText("No.2:"+role[rank[1]].getName()+role[rank[1]].getMoney());
Label_location.setText("No.3:"+role[rank[2]].getName()+role[rank[2]].getMoney());
Label_info.setText("No.4:"+role[rank[3]].getName()+role[rank[3]].getMoney());
round = 1;
useDice.setVisible(false);
lblNewLabel_2.setVisible(false);
}
}
}
});
useDice.setBounds(67, 385, 93, 39);
panel_1.add(useDice);
lblNewLabel_2.setIcon(new ImageIcon("D:\\Desktop\\dice\\0.gif"));
lblNewLabel_2.setBounds(57, 249, 105, 107);
panel_1.add(lblNewLabel_2);
JButton button = new JButton("開始/重置");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
useDice.setVisible(true);
lblNewLabel_1.setText(" 個人信息面板");
lblNewLabel_2.setIcon(new ImageIcon("D:\\Desktop\\dice\\0.gif"));
lblNewLabel_2.setVisible(true);
Label_name.setText("人物名稱:");
Label_money.setText("人物金錢:");
Label_location.setText("人物位置:");
Label_info.setText("回合: - 第 號投擲");
role = new Role[setting[0]];
for(int i=0;i<setting[0];i++) {
role[i]=new Role(name[i],setting[1]);
}
}
});
button.setBounds(10, 434, 93, 39);
panel_1.add(button);
JButton button_1 = new JButton("\u9000\u51FA\u6E38\u620F");
button_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
button_1.setBounds(121, 434, 93, 39);
panel_1.add(button_1);
JLabel lblNewLabel = new JLabel("New label");
lblNewLabel.setIcon(new ImageIcon("D:\\Desktop\\dice\\background.jpg"));
lblNewLabel.setBounds(0, -23, 1280, 720);
panel.add(lblNewLabel);
}
}
測試運行
由MapGui設置setting數組來決定遊戲的初始參數(在這裏,遊戲人數4,名稱分別為丁丁,迪西,拉拉,小波,初始金幣為50000,回合數為20)
小結
個人在GUI界面方面有所缺漏,難以獨立完成,通過參照子琦大佬的模式,模仿其中運行的方式,從而適配自己的結構,也改進了其中一些方式,縮減一半代碼量。基本上還是在圖形界面消耗的時間最多。
弱雞小富翁