我们分为四个类来进行实现.
我在这里创建了一个GameDevelop包,将所有的类都放在里面.
1.棋子类
package GameDevelop;
import java.awt.*;
public class Chess {
//棋盘的行
private int row;
//棋盘的列
private int col;
//定义颜色变量
private Color color;
//线与线之间的距离为30px,最后也就是格子的大小.
public static final int DIAMETER=30;
//构造方法(有参数)
public Chess(int row, int col,Color color){
this.row=row;
this.col=col;
this.color=color;
}
public Color getColor() {
return color; }
public void setColor(Color color) {
this.color = color; }
public int getRow() {
return row;
}
public void setRow(int row) {
this.row = row;
}
public int getCol() {
return col;
}
public void setCol(int col) {
this.col = col;
}
}
2.画板类
package GameDevelop;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class DrawPanel extends JPanel implements MouseListener {
//边距
public static final int MARGIN=30;
//网格边距
public static final int GRID_SPAN=35;
//
public static final int ROW=15;
public static final int COL=15;
private int x_index=0;
private int y_index=0;
private boolean isBlack=true;
private Chess[] chessArray=new Chess[(COL+1)*(ROW+1)];
private int chessCount=0;
private boolean gameOver=false;
public DrawPanel(){
super();
this.setBackground(Color.LIGHT_GRAY);
this.addMouseListener(this);
}
public void paint(Graphics g){
super.paint(g);
//画棋盘,横线
for (int i = 0; i <=ROW; i++) {
g.drawLine(MARGIN,MARGIN+i*GRID_SPAN,MARGIN+COL*GRID_SPAN,MARGIN+i*GRID_SPAN);
}
//竖线
for(int j=0;j<=COL;j++){
g.drawLine(MARGIN+j*GRID_SPAN,MARGIN,MARGIN+j*GRID_SPAN,MARGIN+ROW*GRID_SPAN);
}
//画棋子
for (int i=0;i<chessCount;i++){
int xp=chessArray[i].getRow()*GRID_SPAN+MARGIN;
int yp=chessArray[i].getCol()*GRID_SPAN+MARGIN;
g.setColor(chessArray[i].getColor());
g.fillOval(xp-Chess.DIAMETER/2,yp-Chess.DIAMETER/2,Chess.DIAMETER,Chess.DIAMETER);
if (i==chessCount-1){
g.setColor(Color.blue);
g.drawRect(xp-Chess.DIAMETER/2,yp-Chess.DIAMETER/2,Chess.DIAMETER,Chess.DIAMETER);
}
}
}
//把矩形设置成完美的大小,将当前组件的大小设置为最佳的,自动调用
public Dimension getPreferredSize() {
return new Dimension(MARGIN*2+GRID_SPAN*ROW,MARGIN*2+GRID_SPAN*COL);
}
public void mouseClicked(MouseEvent e) {
}
//鼠标按下方法,当鼠标按下时响应的操作.
public void mousePressed(MouseEvent e) {
x_index=(e.getX()-MARGIN+GRID_SPAN/2)/GRID_SPAN;
y_index=(e.getY()-MARGIN+GRID_SPAN/2)/GRID_SPAN;
System.out.println("x="+x_index+" y="+y_index);
//判断是不是一个有用的棋子
//游戏结束不能下棋子
if(gameOver){
return;
}
//棋盘外不能下棋子
if(x_index<0 || x_index>COL || y_index<0 || y_index>ROW){
return;
}
//位置有棋子不能下棋
if(isChess(x_index,y_index)){
return;
}
Chess ch=new Chess(x_index,y_index,isBlack?Color.BLACK:Color.WHITE);
chessArray[chessCount++]=ch;
System.out.println("棋子的个数:"+chessCount);
this.repaint();
//判断赢棋
if(isWin()){
String msg=String.format("恭喜你,%s赢了!",isBlack?"黑棋":"白棋");
JOptionPane.showMessageDialog(this,msg);
gameOver=true;
}
isBlack=!isBlack;
}
//但是因为要实现接口,所以要写出来这些方法,但是可以不用重写这些方法.
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
//判断是哪一个颜色的棋子
public boolean isChess(int x,int y){
for (Chess c:chessArray){
if(c!=null && c.getRow()==x && c.getCol()==y){
return true;
}
}
return false;
}
//得到棋盘上的棋子
public Chess getChess(int x,int y,Color color){
for(Chess c:chessArray){
if (c!=null && c.getRow()==x && c.getCol()==y && c.getColor()==color){
return c;
}
}
return null;
}
//判断是否赢棋
public boolean isWin(){
return search1()||search2()||search3()||search4();
}
public boolean search1(){
int count=1;
//左下-右上
for (int x=x_index+1,y=y_index-1; x<=COL && y>=0; x++,y--){
Color c=isBlack?Color.BLACK:Color.WHITE;
if(getChess(x,y,c)!=null){
count++;
}else{
break;
}
}
//右上-左下
for (int x=x_index-1,y=y_index+1; x>=0 && y<=ROW; x--,y++){
Color c=isBlack?Color.BLACK:Color.WHITE;
if(getChess(x,y,c)!=null){
count++;
}else{
break;
}
}
if(count>=5){
return true;
}else{
count=1;
}
return false;
}
//水平方向
public boolean search2(){
int count=1;
//右->左
for (int x=x_index-1; x >=0 ; x--) {
Color c=isBlack?Color.BLACK:Color.WHITE;
if (getChess(x,y_index,c)!=null){
count++;
}
else{
break;
}
}
//左->右
for (int x=x_index+1; x<=COL ; x++) {
Color c=isBlack?Color.BLACK:Color.WHITE;
if (getChess(x,y_index,c)!=null){
count++;
}
else{
break;
}
}
if(count>=5){
return true;
}else{
count=1;
}
return false;
}
public boolean search3(){
int count=1;
//左上-右下
for (int x=x_index+1,y=y_index+1; x<=COL && y<=ROW; x++,y++){
Color c=isBlack?Color.BLACK:Color.WHITE;
if(getChess(x,y,c)!=null){
count++;
}else{
break;
}
}
//右下-左上
for (int x=x_index-1,y=y_index-1; x>=0 && y>=0; x--,y--){
Color c=isBlack?Color.BLACK:Color.WHITE;
if(getChess(x,y,c)!=null){
count++;
}else{
break;
}
}
if(count>=5){
return true;
}else{
count=1;
}
return false;
}
//垂直方向
public boolean search4(){
int count=1;
//上->下
for (int y=y_index+1; y<=ROW ; y++) {
Color c=isBlack?Color.BLACK:Color.WHITE;
if (getChess(x_index,y,c)!=null){
count++;
}
else{
break;
}
}
//下->上
for (int y=y_index-1; y>=0 ; y--) {
Color c=isBlack?Color.BLACK:Color.WHITE;
if (getChess(x_index,y,c)!=null){
count++;
}
else{
break;
}
}
if(count>=5){
return true;
}else{
count=1;
}
return false;
}
public void reStartGame(){
//清除棋子
for (int i = 0; i < chessArray.length; i++) {
chessArray[i]=null;
}
//恢复游戏相关的变量
isBlack=true;
gameOver=false;
chessCount=0;
//重画棋盘
this.repaint();
}
public void goBack(){
if(chessCount==0){
return;
}
chessArray[chessCount-1]=null;
chessCount--;
if(chessCount>0){
x_index=chessArray[chessCount-1].getRow();
y_index=chessArray[chessCount-1].getCol();
}
isBlack=!isBlack;
this.repaint();
}
}
3.游戏设置类
package GameDevelop;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class GameSet extends JFrame {
JPanel jp=new JPanel();
private JButton b1,b2,b3;
private DrawPanel dp;
private JMenuBar menubar;
private JMenuItem item1,item2,item3;
private myListener listener;
public GameSet() {
}
public void init() {
this.setTitle("五子棋");
listener=new myListener();
//创建菜单栏
JMenu menu = new JMenu("游戏菜单");
item1 = new JMenuItem("重新开始游戏");
item1.addActionListener(listener);
item2 = new JMenuItem("悔棋");
item2.addActionListener(listener);
item3 = new JMenuItem("退出");
item1.addActionListener(listener);
b1=new JButton("重新开始游戏!");
b1.addActionListener(listener);
b2=new JButton("悔棋!");
b2.addActionListener(listener);
b3=new JButton("退出!");
b3.addActionListener(listener);
//设置窗口菜单栏
menubar=new JMenuBar();
this.setJMenuBar(menubar);
menubar.add(menu);
menu.add(item1);
menu.add(item2);
menu.add(item3);
jp.add(b1);
jp.add(b2);
jp.add(b3);
dp=new DrawPanel();
this.setLayout(new BorderLayout());
this.add(jp,BorderLayout.NORTH);
this.add(dp,BorderLayout.CENTER);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.setLocation(400, 15);
pack();
this.setVisible(true);
}
private class myListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
if (e.getSource()==b1||e.getSource()==item1){
dp.reStartGame();
}
if (e.getSource()==b2||e.getSource()==item2){
dp.goBack();
}
if (e.getSource()==b3||e.getSource()==item3){
System.exit(0);
}
}
}
}
4.主方法类.
package GameDevelop;
public class GameApp {
public static void main(String[] args) {
new GameSet().init();
}
}
最后直接在主方法类中运行程序就行.
5.程序运行部分截图.