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

📄 chesswzq.java

📁 自己做的五子棋代码
💻 JAVA
字号:
/**
 * @(#)chessWZQ.java
 *
 *
 * @author 
 * @version 1.00 2008/11/2
 */
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
import java.io.*;
/*
 *为什么12,13,14列的没有结束?
 *棋盘的宽度和高度的坐标范围:30————450;
 *每个方格是边长为30的正方形;
 */
public class chessWZQ extends JPanel implements MouseListener
{
    Mywuziqi wuziqi;
    int numblackchess=0;//记录黑色的颜色的棋子的个数。
    int numwhitechess=0;//记录白棋的个数。
    public int timenum=0;
    private int pointx=0;//记录点击时鼠标的坐标。
    private int pointy=0;//
	private int peoplecolor;
	public int chessNUM=15;
	public int[][] chessBoard;
    String line="a        b        c        d        e        f       g        h         i         j        k        l        m        n        o";
    char[] rownum1=
    {
    	'1','2','3','4','5','6','7','8','9'
    };
    char[] rownum2=
    {
    	'1','0','1','1','1','2','1','3','1','4','1','5'
    };
    public chessWZQ()  
    {
    	chessBoard=new int[chessNUM][chessNUM];
    	this.addMouseListener(this);
    	for(int i=0;i<chessNUM;i++)
    	  for(int j=0;j<chessNUM;j++)
    		chessBoard[i][j]=0;
    } 
    public void paint(Graphics g)
    {
    	super.paint(g);
    	g.setColor(Color.BLUE);
    	g.drawString(line,25,15);
    	for(int i=0;i<9;i++)
    	{
    		g.drawChars(rownum1,i,1,10,35+30*i);
    	}
    	for(int i=9,j=0;i<15;i++,j+=2)
    		g.drawChars(rownum2,j,2,10,35+i*30);
    	for(int i=0;i<15;i++)
    	{
    		g.drawLine(30,30+i*30,450,30+i*30);
    		g.drawLine(30+i*30,30,30+i*30,450);
    	}
    	for(int i=0;i<chessNUM;i++)
    	{
    		for(int j=0;j<chessNUM;j++)
    		{
    			if(chessBoard[i][j]==1)
    			{
    				g.setColor(Color.BLACK);
    				g.fillOval(i*30+15,j*30+15,28,28);
    			}
    			if(chessBoard[i][j]==2)
    			{
    				g.setColor(Color.RED);
    				g.fillOval(i*30+15,j*30+15,28,28);
    			}
    		}
    	}	
    }
    public void mouseExited(MouseEvent event)
    {
    }
    public void mouseEntered(MouseEvent event)
    {
    }
    public void mouseReleased(MouseEvent event)
    {
    }
    public void mousePressed(MouseEvent event)
    {
    	int[][] fivechess=new int[5][5];
    	pointx=event.getX();
    	pointy=event.getY();
    	int i=pointx/31;
    	int j=pointy/31;
    	int clicknum=0;
    	if(chessBoard[i][j]==1||chessBoard[i][j]==2)
    		return;
    	timenum+=1;
        int time=timenum%2;
        if(time==1)
    	   chessBoard[i][j]=2;
    	else
    	   chessBoard[i][j]=1;
    	repaint();
      for(int p=0;p<11;p++)
    	{
    		for(int q=0;q<11;q++)
    		{
    			for(int m=p;m<p+5;m++)
    			{
    				for(int n=q;n<q+5;n++)
    				{
    					fivechess[m-p][n-q]=chessBoard[m][n];
    				}
    			}
    	       chessOK(fivechess);
    		}
    	}
    	
    }
    public void mouseClicked(MouseEvent event)
    {
    }
    public void chessOK(int[][] fivechess)
    {
    	int blackcountrcrr=0;
    	int whitecountrcrc=0;
    	int blackcountrc=0;
    	int whitecountrc=0;
    	int blackcountr=0;
    	int blackcountc=0;
    	int whitecountr=0;
    	int whitecountc=0;
    	if(fivechess[0][0]==0&&fivechess[0][4]==0)
    		return;
    	else
    	{
    		for(int i=0;i<5;i++)
    		{
    			if(fivechess[0][i]==1)
    				blackcountc++;
    			if(fivechess[i][0]==1)
    				blackcountr++;
    			if(fivechess[i][0]==2)
    				whitecountr++;
    			if(fivechess[0][i]==2)
    				whitecountc++;
    			if(fivechess[i][i]==1)
    				blackcountrc++;
    			if(fivechess[i][i]==2)
    				whitecountrc++;
    			if(fivechess[i][4-i]==1)
    				blackcountrcrr++;
    		    if(fivechess[i][4-i]==2)
    		    	whitecountrcrc++;
    		}
    		if(whitecountr==5||whitecountc==5||whitecountrc==5||whitecountrcrc==5)
    		{
    			showwhitewin();
    		}
    		if(blackcountr==5||blackcountc==5||blackcountrc==5||blackcountrcrr==5)
    		{
    			showblackwin();
    		}
    	}
    }
    public void showblackwin()
    {
    	JOptionPane.showConfirmDialog(this,"黑方胜利!","游戏结束",JOptionPane.YES_OPTION,JOptionPane.PLAIN_MESSAGE);
    }
    public void showwhitewin()
    {
    	JOptionPane.showConfirmDialog(this,"红方胜利!","游戏结束",JOptionPane.YES_OPTION,JOptionPane.PLAIN_MESSAGE);
    }
}

⌨️ 快捷键说明

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