简单的棋盘
课程的理解
在这几次的使用类,构建类间的关系实践中,已经懂得一部分类的概念,也能构建相对规范实用的类。类图的画法也基本上可以分辨得出来,但是有几种自己实践中没用到的,印象不深,还得翻一翻书才能分辨。使用类可以使得整个代码的结构清晰,哪一部分的功能调用哪一个类,这样在出现问题的时候,能够精准快速地找到问题和改正问题。当然不仅如此,还有要添加新功能,还是修改一些方法,直接在类中修改,而不需要每个传递到的地方都要修改,很大地提高了效率。在开始作业前,我考虑了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界面方面有所缺漏,难以独立完成,通过参照子琦大佬的模式,模仿其中运行的方式,从而适配自己的结构,也改进了其中一些方式,缩减一半代码量。基本上还是在图形界面消耗的时间最多。
时间: 2024-11-05 14:59:26