Xiao 2 years ago
parent 2d5689e045
commit c24e062fa8

@ -12,26 +12,41 @@ import java.util.Random;
public class BrandUtil {
public static Random random = new Random();
public static String[] brandName= {
public static String[] brandName1= {
"刷子","剪刀","叉子","手套",
"木桩","棉花","毛线","水桶",
"火堆","牛奶","玉米","白菜",
"稻草","萝卜","铃铛","青草",
};
public static String getBrandName(){
int randomIndex = random.nextInt(brandName.length);
return brandName[randomIndex];
public static String[] brandName2={
"羊1","羊2","羊3","羊4",
"羊5","羊6","羊7","羊8",
"羊9","羊10","羊11","羊12",
"羊13","羊14","羊15","羊16",
};
public static String getBrandName(Integer gameMode){
int randomIndex = random.nextInt(brandName1.length);
String retname = "";
switch (gameMode){
case 1:
retname = brandName1[randomIndex];
break;
case 2:
retname = brandName2[randomIndex];
break;
}
return retname;
}
//创建随机牌的数组
public static Brand[] buildBrands(Integer capacity){
public static Brand[] buildBrands(Integer capacity,Integer gamemode){
//创建牌数组
Brand[] brands = new Brand[capacity];
//赋值
for (int i = 0; i < brands.length; i+=3) {
String randomBrandName = getBrandName();
String randomBrandName = getBrandName(gamemode);
Brand brand1 = new Brand(randomBrandName);
Brand brand2 = new Brand(randomBrandName);
Brand brand3 = new Brand(randomBrandName);

@ -7,13 +7,13 @@ import com.sheep.model.Map;
public class LayerUtil {
public static Layer LayerBuild(Integer rowNum,Integer colNum,Map map){
public static Layer LayerBuild(Integer rowNum,Integer colNum,Map map,Integer gameMode){
//创建一个二维图层
Layer layer = new Layer(rowNum, colNum);
//创建牌数组
Brand[] brands = BrandUtil.buildBrands(layer.getCapacity());
Brand[] brands = BrandUtil.buildBrands(layer.getCapacity(),gameMode);
//将打乱的牌放入图层
int flag = 0;

@ -8,14 +8,16 @@ import com.sheep.model.Cell;
import com.sheep.model.Layer;
import com.sheep.model.Map;
import javax.swing.*;
public class MapUtil {
public static Map MapBuild(Integer floorHeight) {
public static Map MapBuild(Integer floorHeight,Integer gameMode) {
Map map = new Map();
map.setFoolHeight(floorHeight);
Layer layer1 = LayerUtil.LayerBuild(6, 6,map);
Layer layer2 = LayerUtil.LayerBuild(6, 6,map);
Layer layer3 = LayerUtil.LayerBuild(6, 6,map);
Layer layer1 = LayerUtil.LayerBuild(6, 6,map,gameMode);
Layer layer2 = LayerUtil.LayerBuild(6, 6,map,gameMode);
Layer layer3 = LayerUtil.LayerBuild(6, 6,map,gameMode);
layer1.setMap(map);
layer2.setMap(map);
@ -65,6 +67,7 @@ public class MapUtil {
public static void compareAll(Map m){
Map map = m;
boolean flag = true;
//i=0最顶层不需要比较
for (int i = 1; i < map.getList().size(); i++) {
Layer layer = map.getList().get(i);
@ -74,6 +77,7 @@ public class MapUtil {
for(int col = 0;col <cells[row].length;col++){
Cell cell = cells[row][col];
if(cell.getState()==1){
flag = false;
Brand brand = cell.getBrand();
boolean result = MapUtil.compare(brand,layer.getParent());
@ -82,5 +86,9 @@ public class MapUtil {
}
}
}
if(flag){
JOptionPane.showMessageDialog(null,"游戏成功");
System.exit(0);
}
}
}

@ -14,9 +14,11 @@ import javax.swing.*;
import java.util.List;
public class Game extends JFrame {
private Map map = MapUtil.MapBuild(3);
private Map map;
public Game(Integer gameMode) {
map = MapUtil.MapBuild(3,gameMode);
public Game() {
init();//初始化窗口
//渲染图层

@ -0,0 +1,7 @@
package com.sheep.view;
public class Main {
public static void main(String[] args) {
Start s = new Start();
}
}

@ -1,16 +1,24 @@
package com.sheep.view;
import com.sheep.model.Brand;
import com.sheep.util.MapUtil;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
/*
*/
public class Start extends JFrame {
Integer gameMode;
JLabel backgroundLabel = new JLabel(new ImageIcon("imgs/背景1.png"));
public Start() {
gameMode = 1;
init();//初始化窗口
//添加背景及按钮
@ -34,19 +42,64 @@ public class Start extends JFrame {
}
private void addBackgound(){
JLabel gameMode1 = new JLabel(new ImageIcon("imgs/刷子.png"));
gameMode1.setBounds(100,550,50,50);
this.add(gameMode1);
JLabel gameMode2 = new JLabel(new ImageIcon("imgs/羊1.png"));
gameMode2.setBounds(200,550,50,50);
this.add(gameMode2);
JLabel gameMode3 = new JLabel(new ImageIcon("imgs/羊1.png"));
gameMode3.setBounds(300,550,50,50);
this.add(gameMode3);
JButton startButton = new JButton(new ImageIcon("imgs/开始.png"));
startButton.setBounds(125,400,200,50);
this.add(startButton);
JLabel backgroungLabel = new JLabel(new ImageIcon("imgs/背景.png"));
backgroungLabel.setBounds(0,0,450,800);
this.add(backgroungLabel);
backgroundLabel.setBounds(0,0,450,800);
this.add(backgroundLabel);
//游戏模式1监听事件
gameMode1.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
gameMode = 1;
backgroundLabel.setIcon(new ImageIcon("imgs/背景1.png"));
backgroundLabel.repaint();
}
});
//游戏模式2监听事件
gameMode2.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
gameMode = 2;
backgroundLabel.setIcon(new ImageIcon("imgs/背景2.png"));
backgroundLabel.repaint();
}
});
//游戏模式3监听事件
gameMode3.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
gameMode = 2;
backgroundLabel.setIcon(new ImageIcon("imgs/背景3.png"));
backgroundLabel.repaint();
}
});
//按钮添加监听事件
startButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new Game();
new Game(gameMode);
Start.super.dispose();
}
});
@ -72,7 +125,4 @@ public class Start extends JFrame {
}).start();
}
public static void main(String[] args) {
new Start();
}
}

Loading…
Cancel
Save