简单五子棋
棋盘棋子
public void paint(Graphics g) {
super.paint(g); //super 相当于是指向当前对象的父类
//画棋盘
for(int i=0;i<15;i++) {
g.setColor(Color.blue);
g.drawLine(30, 150+i*30, 450, 150+i*30);
g.drawLine(30+i*30, 150, 30+i*30, 570);
}
//画棋子
for(int i=0;i<15;i++) //进行了一次遍历,遍历 chess[][]!=0.画下棋子
for(int j=0;j<15;j++) {
if(allChess[i][j]==1) {
int ovalX=i*30+20;
int ovalY=j*30+140;
g.setColor(Color.black);
g.fillOval(ovalX,ovalY , 20, 20);
g.drawOval(ovalX,ovalY , 20, 20);
}
if(allChess[i][j]==2) {
int ovalX=i*30+20;
int ovalY=j*30+140;
g.setColor(Color.white);
g.fillOval(ovalX,ovalY , 20, 20);
g.drawOval(ovalX,ovalY , 20, 20);
}
}
}
鼠标监听
public void mousePressed(MouseEvent e) {
if(canPlay) {
x=e.getX();
y=e.getY();
if(x>=20 && x<=460 && y>=160 && y<=580) {//判断是否超出棋盘范围
x=(int)(x/30.0-0.5);
y=(int)((y-120)/30.0-0.5);//因为要下在相交处,四舍五入确定精度
if(allChess[x][y]==0) {
if(isblack) {
allChess[x][y]=1;
isblack=false;//判定是下哪个棋子
}
else {
allChess[x][y]=2;
isblack=true;
}
this.repaint();//重绘
if(isWin()) {
if(allChess[x][y]==1) JOptionPane.showMessageDialog(this, "黑方获胜");
else JOptionPane.showMessageDialog(this, "白方获胜");
this.canPlay=false;//不能下了
}
}
}
}
}
判定
public boolean isWin() {
boolean flag=false;
int count=1;
//四种判定
//1
count=checkCount(0, 1, allChess[x][y]);
if(count==5) flag=true;
//2
count=checkCount(1, 0, allChess[x][y]);
if(count==5) flag=true;
//3
count=checkCount(1, 1, allChess[x][y]);
if(count==5) flag=true;
//4
count=checkCount(1, -1, allChess[x][y]);
if(count==5) flag=true;
return flag;
}
public int checkCount(int xChange,int yChange,int color) {
int count=1;
int tX=xChange;
int tY=yChange;
while(x+xChange>=0&&x+xChange<=15&&y+yChange>=0&&
y+yChange<=15&& color==allChess[x+xChange][y+yChange]) //防止数组越界
{
count++;
if(xChange!=0) xChange++;
if(yChange!=0) {
if(yChange>0) yChange++;
else yChange--;
} //半边的判断
}
xChange=tX;
yChange=tY;//之所以不用传过来的数据又定义了一个,是防止自加自减出现错误
while(x-xChange>=0&& x-xChange<15&&y-yChange>=0&&
y-yChange<15&&color==allChess[x-xChange][y-yChange])
{
count++;
if(xChange!=0) xChange++;
if(yChange!=0) {
if(yChange>0) yChange++;
else yChange--;
} //半边的判断
}
return count;
}
这是一个极简版的五子棋,不能人人,无悔棋,认输等按钮,只能下棋,以后或许会改进的,接下来上源码
package chess;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
//界面
public class MyChess extends JFrame implements MouseListener{
/**
*
*/
private static final long serialVersionUID = 1L;
int [][] allChess=new int [15][15]; //把棋子放进数组中
int x,y; //用getX()获得 鼠标点击的坐标
boolean canPlay=true; //判断是否能下
boolean isblack=true; //判断是否应该改下黑棋子
int i,j;
public MyChess(){
this.setTitle("五子棋");
this.setSize(600,600);
this.setLocation(300, 300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false); //设置窗口不可改变
this.getContentPane().setBackground(Color.orange); //设置窗口的背景颜色
this.setVisible(true);
this.repaint();
this.addMouseListener(this);
}
public void paint(Graphics g) {
super.paint(g); //super 相当于是指向当前对象的父类
//画棋盘
for(int i=0;i<15;i++) {
g.setColor(Color.blue);
g.drawLine(30, 150+i*30, 450, 150+i*30);
g.drawLine(30+i*30, 150, 30+i*30, 570);
}
//画棋子
for(int i=0;i<15;i++) //进行了一次遍历,遍历 chess[][]!=0.画下棋子
for(int j=0;j<15;j++) {
if(allChess[i][j]==1) {
int ovalX=i*30+20;
int ovalY=j*30+140;
g.setColor(Color.black);
g.fillOval(ovalX,ovalY , 20, 20);
g.drawOval(ovalX,ovalY , 20, 20);
}
if(allChess[i][j]==2) {
int ovalX=i*30+20;
int ovalY=j*30+140;
g.setColor(Color.white);
g.fillOval(ovalX,ovalY , 20, 20);
g.drawOval(ovalX,ovalY , 20, 20);
}
}
}
public void mousePressed(MouseEvent e) {
if(canPlay) {
x=e.getX();
y=e.getY();
if(x>=20 && x<=460 && y>=160 && y<=580) {//判断是否超出棋盘范围
x=(int)(x/30.0-0.5);
y=(int)((y-120)/30.0-0.5);//因为要下在相交处,四舍五入确定精度
if(allChess[x][y]==0) {
if(isblack) {
allChess[x][y]=1;
isblack=false;//判定是下哪个棋子
}
else {
allChess[x][y]=2;
isblack=true;
}
this.repaint();//重绘
if(isWin()) {
if(allChess[x][y]==1) JOptionPane.showMessageDialog(this, "黑方获胜");
else JOptionPane.showMessageDialog(this, "白方获胜");
this.canPlay=false;//不能下了
}
}
}
}
}
public boolean isWin() {
boolean flag=false;
int count=1;
//四种判定
//1
count=checkCount(0, 1, allChess[x][y]);
if(count==5) flag=true;
//2
count=checkCount(1, 0, allChess[x][y]);
if(count==5) flag=true;
//3
count=checkCount(1, 1, allChess[x][y]);
if(count==5) flag=true;
//4
count=checkCount(1, -1, allChess[x][y]);
if(count==5) flag=true;
return flag;
}
public int checkCount(int xChange,int yChange,int color) {
int count=1;
int tX=xChange;
int tY=yChange;
while(x+xChange>=0&&x+xChange<=15&&y+yChange>=0&&
y+yChange<=15&& color==allChess[x+xChange][y+yChange]) //防止数组越界
{
count++;
if(xChange!=0) xChange++;
if(yChange!=0) {
if(yChange>0) yChange++;
else yChange--;
} //半边的判断
}
xChange=tX;
yChange=tY;//之所以不用传过来的数据又定义了一个,是防止自加自减出现错误
while(x-xChange>=0&& x-xChange<15&&y-yChange>=0&&
y-yChange<15&&color==allChess[x-xChange][y-yChange])
{
count++;
if(xChange!=0) xChange++;
if(yChange!=0) {
if(yChange>0) yChange++;
else yChange--;
} //半边的判断
}
return count;
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
public static void main(String[] args) {
new MyChess();
}
}