package djr.chess.pad;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import javax.swing.JTextField;
public class FIRPad extends Panel implements MouseListener, ActionListener
{
// 鼠标是否能使用
public boolean isMouseEnabled = false;
// 是否胜利
public boolean isWinned = false;
// 是否在下棋中
public boolean isGaming = false;
// 棋子的x轴坐标位
public int chessX_POS = -1;
// 棋子的y轴坐标位
public int chessY_POS = -1;
// 棋子的颜色
public int chessColor = 1;
// 黑棋x轴坐标位数组
public int chessBlack_XPOS[] = new int[200];
// 黑棋y轴坐标位数组
public int chessBlack_YPOS[] = new int[200];
// 白棋x轴坐标位数组
public int chessWhite_XPOS[] = new int[200];
// 白棋y轴坐标位数组
public int chessWhite_YPOS[] = new int[200];
// 黑棋数量
public int chessBlackCount = 0;
// 白棋数量
public int chessWhiteCount = 0;
// 黑棋获胜次数
public int chessBlackVicTimes = 0;
// 白棋获胜次数
public int chessWhiteVicTimes = 0;
// 套接口
public Socket chessSocket;
public DataInputStream inputData;
public DataOutputStream outputData;
public String chessSelfName = null;
public String chessPeerName = null;
public String host = null;
public int port = 4331;
public TextField statusText = new TextField("请连接服务器!");
public FIRThread firThread = new FIRThread(this);
public FIRPad()
{
setSize(440, 440);
setLayout(null);
setBackground(Color.LIGHT_GRAY);
addMouseListener(this);
add(statusText);
statusText.setBounds(new Rectangle(40, 5, 360, 24));
statusText.setEditable(false);
}
// 连接到主机
public boolean connectServer(String ServerIP, int ServerPort) throws Exception
{
try
{
// 取得主机端口
chessSocket = new Socket(ServerIP, ServerPort);
// 取得输入流
inputData = new DataInputStream(chessSocket.getInputStream());
// 取得输出流
outputData = new DataOutputStream(chessSocket.getOutputStream());
firThread.start();
return true;
}
catch (IOException ex)
{
statusText.setText("连接失败! \n");
}
return false;
}
// 设定胜利时的棋盘状态
public void setVicStatus(int vicChessColor)
{
// 清空棋盘
this.removeAll();
// 将黑棋的位置设置到零点
for (int i = 0; i <= chessBlackCount; i++)
{
chessBlack_XPOS[i] = 0;
chessBlack_YPOS[i] = 0;
}
// 将白棋的位置设置到零点
for (int i = 0; i <= chessWhiteCount; i++)
{
chessWhite_XPOS[i] = 0;
chessWhite_YPOS[i] = 0;
}
// 清空棋盘上的黑棋数
chessBlackCount = 0;
// 清空棋盘上的白棋数
chessWhiteCount = 0;
add(statusText);
statusText.setBounds(40, 5, 360, 24);
if (vicChessColor == 1)
{ // 黑棋胜
chessBlackVicTimes++;
statusText.setText("黑方胜,黑:白 " + chessBlackVicTimes + ":" + chessWhiteVicTimes
+ ",游戏重启,等待白方...");
}
else if (vicChessColor == -1)
{ // 白棋胜
chessWhiteVicTimes++;
statusText.setText("白方胜,黑:白 " + chessBlackVicTimes + ":" + chessWhiteVicTimes
+ ",游戏重启,等待黑方...");
}
}
// 取得指定棋子的位置
public void setLocation(int xPos, int yPos, int chessColor)
{
if (chessColor == 1)
{ // 棋子为黑棋时
chessBlack_XPOS[chessBlackCount] = xPos * 20;
chessBlack_YPOS[chessBlackCount] = yPos * 20;
chessBlackCount++;
}
else if (chessColor == -1)
{ // 棋子为白棋时
chessWhite_XPOS[chessWhiteCount] = xPos * 20;
chessWhite_YPOS[chessWhiteCount] = yPos * 20;
chessWhiteCount++;
}
}
// 判断当前状态是否为胜利状态
public boolean checkVicStatus(int xPos, int yPos, int chessColor)
{
int chessLinkedCount = 1; // 连接棋子数
int chessLinkedCompare = 1; // 用于比较是否要继续遍历一个棋子的相邻网格
int chessToCompareIndex = 0; // 要比较的棋子在数组中的索引位置
int closeGrid = 1; // 相邻网格的位置
if (chessColor == 1)
{ // 黑棋时
chessLinkedCount = 1; // 将该棋子自身算入的话,初始连接数为1
//以下每对for循环语句为一组,因为下期的位置能位于中间而非两端
for (closeGrid = 1; closeGrid <= 4; closeGrid++)
{ // 遍历相邻4个网格
for (chessToCompareIndex = 0; chessToCompareIndex <= chessBlackCount; chessToCompareIndex++)
{ // 遍历棋盘上所有黑棋子
if (((xPos + closeGrid) * 20 == chessBlack_XPOS[chessToCompareIndex])
&& ((yPos * 20) == chessBlack_YPOS[chessToCompareIndex]))
{ // 判断当前下的棋子的右边4个棋子是否都为黑棋
chessLinkedCount = chessLinkedCount + 1; // 连接数加1
if (chessLinkedCount == 5)
{ // 五子相连时,胜利
return true;
}
}
}
if (chessLinkedCount == (chessLinkedCompare + 1)) {
chessLinkedCompare++;
}
else {// 若中间有一个棋子非黑棋,则会进入此分支,此时无需再遍历
break;
}
}
for (closeGrid = 1; closeGrid <= 4; closeGrid++)
{
for (chessToCompareIndex = 0; chessToCompareIndex <= chessBlackCount; chessToCompareIndex++)
{
if (((xPos - closeGrid) * 20 == chessBlack_XPOS[chessToCompareIndex])
&& (yPos * 20 == chessBlack_YPOS[chessToCompareIndex]))
{ // 判断当前下的棋子的左边4个棋子是否都为黑棋
chessLinkedCount++;
if (chessLinkedCount == 5)
{
return true;
}
}
}
if (chessLinkedCount == (chessLinkedCompare + 1)) {
chessLinkedCompare++;
}
else {
break;
}
}
// 进入新的一组for循环时要将连接数等重置
chessLinkedCount = 1;
chessLinkedCompare = 1;
for (closeGrid = 1; closeGrid <= 4; closeGrid++)
{
for (chessToCompareIndex = 0; chessToCompareIndex <= chessBlackCount; chessToCompareIndex++)
{
if ((xPos * 20 == chessBlack_XPOS[chessToCompareIndex])
&& ((yPos + closeGrid) * 20 == chessBlack_YPOS[chessToCompareIndex]))
{ // 判断当前下的棋子的上边4个棋子是否都为黑棋
chessLinkedCount++;
if (chessLinkedCount == 5)
{
return true;
}
}
}
if (chessLinkedCount == (chessLinkedCompare + 1)) {
chessLinkedCompare++;
}
else {
break;
}
}
for (closeGrid = 1; closeGrid <= 4; closeGrid++)
{
for (chessToCompareIndex = 0; chessToCompareIndex <= chessBlackCount; chessToCompareIndex++)
{
if ((xPos * 20 == chessBlack_XPOS[chessToCompareIndex])
&& ((yPos - closeGrid) * 20 == chessBlack_YPOS[chessToCompareIndex]))
{ // 判断当前下的棋子的下边4个棋子是否都为黑棋
chessLinkedCount++;
if (chessLinkedCount == 5)
{
return true;
}
}
}
if (chessLinkedCount == (chessLinkedCompare + 1)) {
chessLinkedCompare++;
}
else {
break;
}
}
chessLinkedCount = 1;
chessLinkedCompare = 1;
for (closeGrid = 1; closeGrid <= 4; closeGrid++)
{
for (chessToCompareIndex = 0; chessToCompareIndex <= chessBlackCount; chessToCompareIndex++)
{
if (((xPos - closeGrid) * 20 == chessBlack_XPOS[chessToCompareIndex])
&& ((yPos + closeGrid) * 20 == chessBlack_YPOS[chessToCompareIndex]))
{ // 判断当前下的棋子的左上方向4个棋子是否都为黑棋
chessLinkedCount++;
if (chessLinkedCount == 5)
{
return true;
}
}
}
if (chessLinkedCount == (chessLinkedCompare + 1)) {
chessLinkedCompare++;
}
else {
break;
}
}
for (closeGrid = 1; closeGrid <= 4; closeGrid++)
{
for (chessToCompareIndex = 0; chessToCompareIndex <=
评论0
最新资源