package packplane;
/*
* Flight类
* 完成棋盘的绘制,以及相关游戏规则的算法实现
*/
import javax.swing.JPanel; // 面板
import java.awt.Point; // 点
import java.awt.Graphics; // 图形
import javax.swing.ImageIcon; // 图标
import java.awt.Image; //
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton; // 按钮
import java.awt.Color;
public class Flight extends JPanel
{
// 定义相关变量
private int width = 680; // 图片的宽
private int height = 680; // 图片的高
private int length1 = 52; // 主干路线的长度
private int length2 = 6; // 各类颜色的棋子的分路径长度
private int planeNum = 4; // 玩家飞机的架数
private int size = width/17; // 将棋盘划分成17*17的方格 ,size为方格的大小
private Point primaryRoad[] = new Point [length1]; // 定义主干路线的点列坐标矩阵
private byte numOfPlaneOnRoad [] = new byte [length1]; // 记录主干路线上每一点上飞机的数量
/*
* 由于不同玩家的飞机不可能占据同一位置,对每点上飞机的数量最出如下约定
* 0——没有飞机
* 1——有红色飞机
* 2——有黄色飞机
* 3——有绿色飞机
* 4——有蓝色飞机
*/
private Point redRoad[] = new Point [length2]; // 定义红色棋子的分路线的点列坐标矩阵
private Point yellowRoad[] = new Point [length2]; // 定义黄色棋子的分路线的点列坐标矩阵
private Point greenRoad[] = new Point [length2]; // 定义绿色棋子的分路线的点列坐标矩阵
private Point blueRoad[] = new Point [length2]; // 定义蓝色棋子的分路线的点列坐标矩阵
private Point redAirport[] = new Point [planeNum]; // 红棋飞机场位置
private Point yellowAirport[] = new Point [planeNum]; // 黄棋飞机场位置
private Point greenAirport[] = new Point [planeNum]; // 绿棋飞机场位置
private Point blueAirport[] = new Point [planeNum] ; // 蓝棋飞机场位置
/*
* 飞机停放位置索引:-1 表示停留在飞机场 0 表示准备出发 1 ——……表示飞机正在飞行
*/
private int redPlane[] = new int [planeNum]; // 红棋飞机停放位置索引
private int yellowPlane[] = new int [planeNum]; // 黄棋飞机停放位置索引
private int greenPlane [] = new int [planeNum]; // 绿棋飞机停放位置索引
private int bluePlane [] = new int [planeNum]; // 蓝棋飞机停放位置索引
private ImageIcon board; // 棋盘图片
private ImageIcon red; // 红飞机
private ImageIcon yellow; // 黄飞机
private ImageIcon green; // 绿飞机
private ImageIcon blue; // 蓝飞机
private Image imR;
private Image imY;
private Image imG;
private Image imB;
private JButton b = new JButton("掷骰子");
private byte count; // 投掷骰子的点数
private int planeIndex = 0; // 飞机索引
private GAMESTATE state; // 游戏进行状态
// 构造方法
public Flight()
{
red = new ImageIcon("red.gif");
yellow = new ImageIcon("yellow.gif");
green = new ImageIcon("green.gif");
blue = new ImageIcon("blue.gif");
imR = red.getImage();
imY = yellow.getImage();
imG = green.getImage();
imB = blue.getImage();
mb_initBoard(); // 调用初始化变量方法
setLayout(null);
add(b);
b.setBounds(300,0,80,30);// 添加按钮
b.addActionListener( new ActionListener() // 按钮动作监听器
{
public void actionPerformed( ActionEvent e )
{
count = (byte)(10*Math.random()%6+1);
if( state == GAMESTATE.REDCAST )
{
// 如果掷出的点数不是5或6 而且当前玩家没有起飞的飞机,则视为玩家投掷失败
if( !mb_isAllowToFly()&&!( count ==5|| count == 6 ))
{
state=GAMESTATE.YELLOWCAST;
}
else
{
state = GAMESTATE.REDGO;
}
}
else if( state == GAMESTATE.YELLOWCAST )
{
// 如果掷出的点数不是5或6 而且当前玩家没有起飞的飞机,则视为玩家投掷失败
if( !mb_isAllowToFly()&& !( count ==5|| count == 6 ))
{
state = GAMESTATE.GREENCAST;
}
else
{
state = GAMESTATE.YELLOWGO;
}
}
else if( state == GAMESTATE.GREENCAST)
{
// 如果掷出的点数不是5或6 而且当前玩家没有起飞的飞机,则视为玩家投掷失败
if( !mb_isAllowToFly()&&!( count ==5|| count == 6 ))
{
state = GAMESTATE.BLUECAST;
}
else
{
state = GAMESTATE.GREENGO;
}
}
else if( state == GAMESTATE.BLUECAST)
{
// 如果掷出的点数不是5或6 而且当前玩家没有起飞的飞机,则视为玩家投掷失败
if( !mb_isAllowToFly()&&!( count ==5|| count == 6 ))
{
state = GAMESTATE.REDCAST;
}
else
{
state = GAMESTATE.BLUEGO;
}
}
repaint();
}
}
);
addMouseListener( new MouseListener()
{
public void mouseClicked( MouseEvent e ) // 点击鼠标
{
} // 方法结束
public void mouseEntered( MouseEvent e ) // 鼠标进入组件
{
} // 方法结束
public void mouseExited( MouseEvent e ) // 光标退出组件
{
} // 方法结束
public void mousePressed( MouseEvent e ) // 鼠标按下
{
if(mb_searchPlane(e.getX(),e.getY())==true )
{
if( state == GAMESTATE.REDGO)
{
if( redPlane[planeIndex]!=-1 && redPlane[planeIndex]!=56)
{
redPlane[planeIndex] += count;
}
else if( redPlane[planeIndex]==-1)
{
redPlane[planeIndex]=0;
}
mb_fly();
if( redPlane[planeIndex] > 56 )
{
redPlane[planeIndex] = 56 - (redPlane[planeIndex]-56) ;
}
// 如果当前位置部位空且不是红色飞机 则发生“撞机”
if( redPlane[planeIndex]-1>=0&&numOfPlaneOnRoad[(redPlane[planeIndex]-1)%52]!=0
&& numOfPlaneOnRoad[redPlane[planeIndex]-1] !=1 )
{
mb_crash(redPlane[planeIndex]-1); // 调用撞机方法
}
if(redPlane[planeIndex]-1>=0)
numOfPlaneOnRoad[redPlane[planeIndex]-1]=1; // 该位置有红色飞机
repaint();
state = GAMESTATE.YELLOWCAST;
// 掷出6点奖励一次机会
if( count == 6 )
{
state = GAMESTATE.REDCAST;
}
}
else if( state == GAMESTATE.YELLOWGO )
{
if( yellowPlane[planeIndex]!=-1 && yellowPlane[planeIndex]!=56)
{
yellowPlane[planeIndex] += count;
}
else if( yellowPlane[planeIndex]==-1)
{
yellowPlane[planeIndex]=0;
}
mb_fly();
if( yellowPlane[planeIndex] > 56 )
{
yellowPlane[planeIndex] = 56 - (yellowPlane[planeIndex]-56) ;
}
// 如果当前位置部位空且不是黄色飞机 则发生“撞机”
if( numOfPlaneOnRoad[(yellowPlane[planeIndex]+12)%51]!=0
&& numOfPlaneOnRoad[(yellowPlane[planeIndex]+12)%51] !=2 )
{
mb_crash((yellowPlane[planeIndex]+12)%51); // 调用撞机方法
}
numOfPlaneOnRoad[(yellowPlane[planeIndex]+12)%51]=2; // 该位置有红色飞机
repaint();
state = GAMESTATE.GREENCAST;
// 掷出6点奖励一次机会
if( count == 6 )
{
state = GAMESTATE.YELLOWCAST;
}
}
else if( state == GAMESTATE.GREENGO)
{
if( greenPlane[planeIndex]!=-1 && greenPlane[planeI