import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
/**
* @author yeeku.H.lee kongyeeku@163.com
* @version 1.0
* <br>Copyright (C), 2005-2008, yeeku.H.Lee
* <br>This program is protected by copyright laws.
* <br>Program Name:
* <br>Date:
*/
public class MainGame extends JPanel
{
private InfoSave infoSave;
private MyPanel mp;
private int tableNumber;
private String name;
private String sex;
private String way;
private String otherWay;
private String size = "1";
private BufferedImage whiteDisk;
private BufferedImage blackDisk;
private BufferedImage curBlack;
private BufferedImage curWhite;
private BufferedImage seatEmpty;
private BufferedImage maleSeatFull;
private BufferedImage femaleSeatFull;
private BufferedImage gameImage;
private BufferedImage ready;
private BufferedImage start;
private BufferedImage play;
private BufferedImage peace;
private BufferedImage lost;
private PrintStream ps;
private Rectangle leftName = new Rectangle(13, 348, 66, 125);
private Rectangle rightName = new Rectangle(645, 348, 66, 125);
private Image board; //棋盘图片
private Image white; //白棋图片
private Image black; //黑棋图片
private Image selected;
private int posX;
private int posY;
private int X;
private int Y;
private int preX; //前一步的坐标
private int preY; //前一步的坐标
private int preSX; //前一步的移动坐标
private int preSY; //前一步的移动坐标
private int otherX; //对方的坐标
private int otherY; //对方的坐标
private int step; //步数
private int count; //count 是计数器,为5的时候表胜利
private int cursor; //光标标志
private int winCountL; //胜利次数
private int winCountR; //胜利次数
private boolean startOver = false;
private boolean peaceOver = false;
private boolean lostOver = false;
private final int CHESS_SIZE = 15; //CHESS_SIZE 是棋盘大小
private int[][] chess = new int[CHESS_SIZE][CHESS_SIZE];
private int[][] select = new int[CHESS_SIZE][CHESS_SIZE];
private int[][] curChess = new int[CHESS_SIZE][CHESS_SIZE]; //自己或者对手上一步下的棋
private boolean playChess = false; //可否下棋
private boolean isBegin = false; //游戏开始了吗
private boolean isReady = false; //准备好了吗
private boolean isOtherReady = false; //对方准备好了吗
private Rectangle startButton = new Rectangle(334, 656 , 60 , 31);
private Rectangle peaceButton = new Rectangle(269, 653 , 42 , 35);
private Rectangle lostButton = new Rectangle(413, 659 , 45 , 18);
private int sec = 0;
private int dSec = 0;
private int min = 0;
private int dMin = 1;
private int secR = 0;
private int dSecR = 0;
private int minR = 0;
private int dMinR = 1;
private Timer timerLeft;
private Timer timerRight;
public MainGame(InfoSave infoSave , String way , int tableNumber)
{
try
{
this.tableNumber = tableNumber;
this.infoSave = infoSave;
this.way = way;
mp = new MyPanel();
ps = new PrintStream(infoSave.getSocket().getOutputStream());
gameImage = ImageIO.read(new File("fiveStone\\room2.bmp"));
ready = ImageIO.read(new File("fiveStone\\ready.jpg"));
start = ImageIO.read(new File("fiveStone\\start.jpg"));
play = ImageIO.read(new File("fiveStone\\play.jpg"));
peace = ImageIO.read(new File("fiveStone\\peace.jpg"));
lost = ImageIO.read(new File("fiveStone\\lost.jpg"));
whiteDisk = ImageIO.read(new File("fiveStone\\whiteDisk.gif"));
blackDisk = ImageIO.read(new File("fiveStone\\blackDisk.gif"));
curBlack = ImageIO.read(new File("fiveStone\\curBlack.gif"));
curWhite = ImageIO.read(new File("fiveStone\\curWhite.gif"));
maleSeatFull = ImageIO.read(new File("fiveStone\\maleSeatFull.jpg"));
femaleSeatFull = ImageIO.read(new File("fiveStone\\femaleSeatFull.jpg"));
seatEmpty = ImageIO.read(new File("fiveStone\\seatEmpty.jpg"));
white = ImageIO.read(new File("fiveStone\\white.gif"));
black = ImageIO.read(new File("fiveStone\\black.gif"));
selected = ImageIO.read(new File("fiveStone\\selected.gif"));
this.setMinimumSize(new Dimension(723 , 702));
this.setPreferredSize(new Dimension(gameImage.getWidth(null), gameImage.getHeight(null)));
mp.setPreferredSize(new Dimension(gameImage.getWidth(null), gameImage.getHeight(null)));
this.add(mp);
timer();
playChess();
}
catch (Exception e)
{
e.printStackTrace();
}
}
/*
向右横扫描4格,判断是否有5个相连的棋
*/
private void acrossScan(int i , int j)
{
//如果遇到有棋的地方就开始扫描
if (chess[i][j] == 1 || chess[i][j] == 2)
{
if ((j+4) < chess[i].length) //判断棋的右方是否足够有4个连续位置
{
count = 1;
for (int k=1;k<=4 ;k++ )
{
if (chess[i][j+k] == chess[i][j])
{
count++; //如果相连的地方有相同的棋就做标记
}
else count = 0;
}
}
}
}
/*
向下竖扫描4格,判断是否有5个相连的棋
*/
private void erectScan(int i , int j)
{
//如果遇到有棋的地方就开始扫描
if (chess[i][j] == 1 || chess[i][j] == 2)
{
if ((i+4) < chess.length) //判断棋的下方是否足够有4个连续位置
{
count = 1;
for (int k=1;k<=4 ;k++ )
{
if (chess[i+k][j] == chess[i][j])
{
count++; //如果相连的地方有相同的棋就做标记
}
else count = 0;
}
}
}
}
/*
向右上横扫描4格,判断是否有5个相连的棋
*/
private void topRightScan(int i , int j)
{
//如果遇到有棋的地方就开始扫描
if (chess[i][j] == 1 || chess[i][j] == 2)
{
if ((i-4) >= 0 && (j+4) < chess[i].length) //判断棋的右斜上方是否足够有4个连续位置
{
count = 1;
for (int k=1;k<=4 ;k++ )
{
if (chess[i-k][j+k] == chess[i][j])
{
count++; //如果相连的地方有相同的棋就做标记
}
else count = 0;
}
}
}
}
/*
向右下横扫描4格,判断是否有5个相连的棋
*/
private void belowRightScan(int i , int j)
{
//如果遇到有棋的地方就开始扫描
if (chess[i][j] == 1 || chess[i][j] == 2)
{
if ((i+4) < chess.length && (j+4) < chess[i].length) //判断棋的右斜下方是否足够有4个连续位置
{
count = 1;
for (int k=1;k<=4 ;k++ )
{
if (chess[i+k][j+k] == chess[i][j])
{
count++; //如果相连的地方有相同的棋就做标记
}
else count = 0;
}
}
}
}
//逐一调用4种扫描方法判断胜负
public void win()
{
for (int i = 0; i < chess.length ; i++ )
{
for (int j = 0 ; j < chess[i].length ; j++ )
{
acrossScan(i,j);
if (count!=5)
{
erectScan(i,j);
}
if (count!=5)
{
topRightScan(i,j);
}
if (count!=5)
{
belowRightScan(i,j);
}
if (count==5)
{
count=0;
//判断是黑棋还是白棋赢
if (chess[i][j] == 1)
{
timerLeft.stop();
timerRight.stop();
JOptionPane.showMessageDialog(mp , "黑棋胜利!!" , "胜利" ,
JOptionPane.INFORMATION_MESSAGE);
winCountL++;
}
else if (chess[i][j] == 2)
{
timerLeft.stop();
timerRight.stop();
JOptionPane.showMessageDialog(mp , "白棋胜利!!" , "胜利" ,
JOptionPane.INFORMATION_MESSAGE);
winCountR++;
}
try
{
PrintStream ps = new PrintStream(infoSave.getSocket().getOutputStream());
ps.println(MyProtocol.WIN + this.tableNumber + MyProtocol.WIN);
}
catch (Exception e)
{
评论2
最新资源