我的五子棋(一)
五子棋代码什么的,网上都有一堆了,不过既然是自己写的,都觉得好好的,然后我将会分四节跟大家讲解我是怎么用java做五子棋的,包括其中所用的方法和类:
这一节先上源代码哈:
全局变量:文件:Config.java;
import java.util.ArrayList;
import java.util.List;
public
class Config {
public static int X0 = 50;//初始X坐标
public static int Y0 = 50;//初始Y坐标
public static int size = 35;//格子长宽
public static int size_H = 15;//行数
public static int size_L = 15;//列数
public static int count = 0;//所下步数;
public static int regret = 0;
public static List<Integer> listx = new ArrayList<Integer>();
public static List<Integer> listy = new ArrayList<Integer>();
//public static List listcount = new ArrayList();
}
主类代码:主类:Draw.java
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionListener;
import java.awt.event.MouseListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.
JSeparator;
public class Draw extends JFrame {//新建界面Draw类,继承JFrame,它拥有JFrame
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Draw a = new Draw();
a.Getguit();
a.repaint();//刷新界面
}
public void Getguit()
{
this.setTitle("欢乐五子棋");
this.setSize(740,600);
this.set
DefaultClose
Operation(3);//关闭窗体就退出程序
this.setLocationRelativeTo(null);//窗体显示在中央
//
ImageIcon backgroundImage = new ImageIcon("picture/backlove.jpg");
JLabel Imagebg = new JLabel(backgroundImage);
Imagebg.setBounds(0, 0, backgroundImage.getIconWidth(),backgroundImage.getIconHeight());
this.getLayeredPane().add(Imagebg,new Integer(Integer.MIN_VALUE));//添加窗体的LayeredPane面板到最底层
JPanel contentPanel = (JPanel)this.getContentPane();//获取窗体的第一层面板
contentPanel.setOpaque(false);//设置第一层面板为透明
//
JPanel eastPanel = new JPanel();//初始化面板(窗口东部)
JPanel eastPanelup = new JPanel();//初始化面板(窗口东部上部)
JPanel eastPanelmin = new JPanel();//初始化面板中部
eastPanelup.setPreferredSize(new Dimension(70,90));//(窗口东部上部留空部分)
eastPanelmin.setPreferredSize(new Dimension(100,100));//(窗口东部中部留空中间)
eastPanel.setLayout(new BorderLayout());//设置布局方式——边框布局
eastPanelmin.setLayout(new FlowLayout(FlowLayout.CENTER,10,38));
JButton jbtopen = new JButton("开始新游戏");//设置按钮1
JButton jbthui = new JButton("悔棋");//按钮2
JButton jbtvsplayer = new JButton("玩家对战");//按钮3
JButton jbtvscomputer = new JButton("人机对战");//按钮4
eastPanelmin.add(jbtopen);//放置按钮1
eastPanelmin.add(jbthui);//放置按钮2
eastPanelmin.add(jbtvsplayer);//放置按钮3
eastPanelmin.add(jbtvscomputer);//放置按钮4
eastPanel.add(eastPanelup,BorderLayout.NORTH);//放置面板eastPanelup至eastPanel北部
eastPanel.add(eastPanelmin,BorderLayout.CENTER);//放置面板eastPanelmin至eastPanel北部
this.add(eastPanel,BorderLayout.EAST);//放置面板eastPanel至this
//设置菜单栏
JMenuBar jme = new JMenuBar();
JMenu menuV = new JMenu("查看(V)");//实例化菜单栏对象
JMenuItem jmiT = new JMenuItem("帮助");
JMenuItem JmiS = new JMenuItem("作者信息");
menuV.add(jmiT);
JSeparator jse = new JSeparator();
menuV.add(jse);
menuV.add(JmiS);
jme.add(menuV);
//
this.setVisible(true);//窗体可见
Graphics g = this.getGraphics();//窗体可见后获取画布对象
MouseListener dl = new DrawListener(g,this);//实例化
事件处理类的对象
this.addMouseListener(dl);//
ActionListener jbtopen2 = new OpenListener(this);//
jbtopen.addActionListener(jbtopen2);//给按钮1加监视器
ActionListener jbthui2 = new RegretListener(this);//
jbthui.addActionListener(jbthui2);//给按钮2加监视器
}
public void paint(Graphics g){
//调用父类的
重绘方法
super.paint(g);
//绘制棋盘
//绘制15条
//横线
for(int i = 0;i<=Config.size_H;i++){
g.drawLine(Config.X0,Config.Y0+i*Config.size,575,Config.Y0+i*Config.size);
}
//竖线
for(int i = 0;i<=Config.size_L;i++){
g.drawLine(Config.X0+i*Config.size,Config.Y0,Config.X0+i*Config.size,575);
}
//重绘棋盘上的棋子
for(int i = 6;i<Config.size_H+12;i++){
for(int j = 6;j<Config.size_L+12;j++){
if(DrawListener.reclocation[i][j]==1){
g.setColor(Color.WHITE);
g.fillOval((Config.size*(j+1-6)),(Config.size*(i+1-6)), Config.size-5, Config.size-5);
}
if(DrawListener.reclocation[i][j]==-1){
g.setColor(Color.BLACK);
g.fillOval((Config.size*(j+1-6)),(Config.size*(i+1-6)), Config.size-5, Config.size-5);
}
}
}
}
}
鼠标监视器:(监视棋盘上的动作)文件:DrawListener.java
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import com.sun.xml.internal.bind.v2.schemagen.xmlschema.List;
public class DrawListener implements MouseListener {
private int x2, y2;//坐标
private Graphics g;//
画图类
private Draw a;
public int state = 1;//控制下棋的顺序
private int k2,m2;//将棋子显示在格子中央
public static int reclocation[][] = new int[Config.size_L+12][Config.size_H+12];//元素默认值为零
private int input = 9;//避免下在重复的位置而导致覆盖
DrawListener(){
}
DrawListener(Graphics g,Draw a) {
this.g = g;
this.a = a;
}
@Override
public void mouseClicked(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
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
x2 = e.getX();
y2 = e.getY();
k2 = (x2-Config.X0)/Config.size;//计算格子在第几行
if((x2-Config.X0)%Config.size>(Config.size/2))
{
k2 = k2+1;
}
m2 = (y2-Config.Y0)/Config.size;//计算格子在第几列
if((y2-Config.X0)%Config.size>(Config.size/2))
{
m2 = m2+1;
}
int x3 = (Config.size*(k2+1));//棋子坐标x
int y3 = (Config.size*(m2+1));//棋子坐标y
if(state == 1){//黑子下
this.judgment();//判断下在此位置是否合法
g.setColor(Color.BLACK);
System.out.println("黑棋state: "+state);
}if(state == -1){//白子下
this.judgment();//判断下在此位置是否合法
g.setColor(Color.WHITE);
System.out.println("白棋state: "+state);
}
if(input == 9){//如果合法;在界面中绘出该点
System.out.println("改变state前: "+state);
System.out.println("state改变后: "+state);
System.out.println("++++++++++++++++++++++++++++++");
this.changeState();
g.fillOval(x3,y3, Config.size-5, Config.size-5);
reclocation[m2+6][k2+6] = state;//在此位置做标记,表示此位置已经有子
}
if(Config.regret==1){
reclocation[m2+6][k2+6]=0;
a.repaint();
Config.regret=0;
}
this.judgmentwin();
}//方法封装
public void judgment(){//判断下在此位置是否合法
if(reclocation[m2+6][k2+6]==1||reclocation[m2+6][k2+6]==-1||m2>15||k2>15){//如果有子
System.out.println("下错位置咯");
input = 10;//判断为非法,无法下在此位置
}else{
input = 9;
Config.count = Config.listx.size()+1;
Config.listx.add(k2+6);
Config.listy.add(m2+6);
}
}//judgment函数结束
public void judgmentwin(){
int i;
int win = 0;//计数器水平方向
int win2 = 0;//计数器竖直方向
int win3 = 0;//计数器45度
int win4 = 0;//计数器135度
for(i = 0;i<=5;i++){
for(int j = i-5;j<i;j++){
win=win+reclocation[m2+6][k2+6+j];//检查水平方向
win2=win2+reclocation[m2+6+j][k2+6];//检查竖直方向
win3=win3+reclocation[m2+6+j][k2+6+j];//检查45°方向
win4=win4+reclocation[m2+6-j][k2+6+j];//检查135°方向
}//内部
循环结束
if(win>=5||win2>=5||win3>=5||win4>=5){
JOptionPane.showMessageDialog(null,"恭喜,白棋胜利了");
break;
}else if(win<=-5||win2<=-5||win3<=-5||win4<=-5){
JOptionPane.showMessageDialog(null,"恭喜,黑棋胜利了");break;
}else{//计数器全归零,重新计算
win = 0;
win2 = 0;
win3 = 0;
win4 = 0;
}
}
}//方法
public int getState(){//获取步骤
return state;
}
public void changeState(){//获取步骤
state = state*(-1);
System.out.println("使用悔棋后state: "+state);
}
}//类的封装
再来一局:文件:OpenListener.java
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class OpenListener implements ActionListener {
private Draw a;
public OpenListener(Draw a){
this.a = a;
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
for(int i = 6;i<Config.size_H+12;i++){
System.out.println();
for(int j = 6;j<Config.size_L+12;j++){
DrawListener.reclocation[i][j] = 0;//重置所有棋子
System.out.print(" "+DrawListener.reclocation[i][j]);//
}//内循环结束;
}//外循环结束
a.repaint();//刷新界面
Config.count = 0;
Config.listx.clear();
Config.listy.clear();
}
}
悔棋操作:文件:RegretListener.java
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;
public class RegretListener implements ActionListener {
private Draw a;
public RegretListener(Draw a){
this.a = a;
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
DrawListener b = new DrawListener();
if(Config.count>=1){//如果步数大于1,可执行悔棋操作
int x = Config.listx.get(Config.count-1);//获取要悔棋的点的坐标x
int y = Config.listy.get(Config.count-1);//获取要悔棋的点的坐标y
DrawListener.reclocation[y][x]=0;//
System.out.println("reclocation[x][y]"+" "+y+" "+x+" :"+DrawListener.reclocation[y][x]);//
Config.count--;//界面棋子数-1
Config.regret=1;
}//
else{//如果棋盘上没有棋子
JOptionPane.showMessageDialog(null,"已经是最后一步了");//
}//
a.repaint();//刷新
}
}