⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 maingame.java

📁 QQ游戏大厅
💻 JAVA
📖 第 1 页 / 共 2 页
字号:

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)
					{
						e.printStackTrace();
					}
					reset();	
				}
			}			
		}
	}

	/*
		初始化游戏
	*/
	public void reset()
	{
		//-------- 初始化棋盘 --------//
		for (int i=0; i<chess.length; i++ )
		{
			for (int j=0; j<chess[i].length; j++ )
			{
				chess[i][j] = 0;
				curChess[i][j] = 0;
			}
		}
		step = 0;
		cursor = 0;
		sec = 0;
		dSec = 0;
		min = 0;
		dMin = 1;
		secR = 0;
		dSecR = 0;
		minR = 0;
		dMinR = 1;
		timerLeft.stop();
		timerRight.stop();
		select[preSX][preSY] = 0;
		playChess = false;
		isBegin = false;
		isReady = false;
		isOtherReady = false;
		mp.repaint();    //调用repaint()方法重画棋盘
	}

	/*
		处理下棋事件
	*/
	public void playChess()
	{		
		mp.addMouseListener(new MouseAdapter()
		{
			public void mouseClicked(MouseEvent e)
			{
				try
				{
					//开始按钮的事件
					if (!isBegin && !isReady && startButton.contains(e.getX() , e.getY()))
					{
						ps.println(MyProtocol.GAME_READY + tableNumber + ":" + way + ";" + 
									infoSave.getUserName() + MyProtocol.GAME_READY);
						isReady = true;
						if (isOtherReady)
						{
							isBegin = true;
						}
					}
					//求和按钮的事件
					else if (isBegin && playChess && peaceButton.contains(e.getX() , e.getY()))
					{
						int result = JOptionPane.showConfirmDialog(mp, "是否向对手求和?", "求和", 
																	JOptionPane.YES_NO_OPTION );
						if (result == 0)
						{	
							playChess = false;
							select[preSX][preSY] = 0;
							ps.println(MyProtocol.PEACE + tableNumber + ":" + way + MyProtocol.PEACE);
							timerLeft.stop();
							timerRight.stop();
						}			
					}
					//认输按钮的事件
					else if (isBegin && playChess && lostButton.contains(e.getX() , e.getY()))
					{
						int result = JOptionPane.showConfirmDialog(mp, "是否要认输了?", "认输", 
																	JOptionPane.YES_NO_OPTION );
						if (result == 0)
						{
							if (way.equals(MyProtocol.LEFT_TABLE))
							{
								winCountR++;
							}
							else
							{
								winCountL++;
							}
							reset();
							ps.println(MyProtocol.LOST + tableNumber + ":" + way + MyProtocol.LOST);
						}		
					}
					//下棋事件
					if (playChess)
					{
						X = (int)(e.getX()-97)/(535/15) ;    //截取鼠标点击的坐标
						Y = (int)(e.getY()-91)/(536/15) ;

						if (X >= 0 && Y >= 0 && X <= 14 && Y <= 14 && chess[X][Y] != 1 && chess[X][Y] != 2)
						{
							if (way.equals(MyProtocol.LEFT_TABLE))    //左边玩家下黑棋
							{
								curChess[X][Y] = 1;
								chess[X][Y] = 1;    //黑棋下棋
								//因为是左手边的玩家先下棋所以要判断是否第一步
								if (step != 0)
								{
									curChess[otherX][otherY] = 0;	 //把对方下的棋的标志消除掉
								}
								timerLeft.stop();
								timerRight.start();

								
							}
							else if (way.equals(MyProtocol.RIGHT_TABLE))    //右边玩家下白棋
							{
								curChess[otherX][otherY] = 0;		//把对方下的棋的标志消除掉
								curChess[X][Y] = 2;
								chess[X][Y] = 2 ;	//白棋下棋
								timerRight.stop();
								timerLeft.start();
							}
							select[X][Y] = 0;
							mp.repaint();	//重画棋盘
							playChess = false;
							ps.println(MyProtocol.X_Y + tableNumber + ":" + way + ";" + X + "," + Y +
										MyProtocol.X_Y);
						}
						preX = X;
						preY = Y;
						step++;
						win();
					}
				}
				catch (Exception ee)
				{
					ee.printStackTrace();
				}
			}
		});
		/*
			鼠标移动时的事件
		*/
		mp.addMouseMotionListener(new MouseMotionAdapter()
		{
			public void mouseMoved(MouseEvent e)
			{
				//System.out.println("X: " + e.getX());
				//System.out.println("Y: " + e.getY());
				if (playChess)
				{
					posX = (int)(e.getX()-97)/(535/15);    //截取鼠标点击的坐标
					posY = (int)(e.getY()-91)/(536/15);
					if (posX >= 0 && posY >= 0 && posX <= 14 && posY <= 14 )    //防止越界
					{
						setCursor();
						select[preSX][preSY] = 0; 
						select[posX][posY] = 1;    //为1的时候画selected
						preSX = posX;
						preSY = posY;
						mp.repaint();
					}
					else if (cursor == 0)
					{
						select[preSX][preSY] = 0;
						setDefaultCursor();
					}
				}
				if (!isBegin && !isReady && startButton.contains(e.getX() , e.getY()))
				{
					setCursor();
					startOver = true;
					cursor = 1;
					mp.repaint();
				}
				else if (isBegin  && playChess && peaceButton.contains(e.getX() , e.getY()))
				{
					setCursor();
					peaceOver = true;
					cursor = 1;
					mp.repaint();
				}
				else if (isBegin  && playChess && lostButton.contains(e.getX() , e.getY()))
				{
					setCursor();
					lostOver = true;
					cursor = 1;
					mp.repaint();
				}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -