java简单五子棋源代码

package com.renyou;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class FiveChess extends JFrame {
 ImageIcon icon = new ImageIcon("images//start.PNG"); // 相对路径
 ImageIcon icon5 = new ImageIcon("images//help1.jpg");
 ImageIcon icon6 = new ImageIcon("images//board.png");
 ImageIcon icon7 = new ImageIcon("images//function.png");
 ImageIcon icon8 = new ImageIcon("images//regret.png");
 ImageIcon icon9 = new ImageIcon("images//rstart.png");
 Graphics g = null;
 int count = 0;// 悔棋步
 Color color;

 // ArrayList list=new ArrayList();
 MyContainer list = new MyContainer();
 boolean flag_renren = false;
 boolean flag_renji = false;
 boolean flag_regret = false;
 boolean flag_color = false;
 boolean flag = true;
 int[][] chese = new int[12][12];

 public void start() {// 开始界面

  JFrame f1 = new JFrame();
  f1.setTitle("五子棋小游戏");
  f1.setSize(380, 500);
  f1.setResizable(false);
  f1.setLocationRelativeTo(null);
  JPanel co = new JPanel() {
   protected void paintComponent(Graphics g) {

    super.paintComponent(g);
    g.drawImage(icon.getImage(), 0, 0, null);
   }
  };

  co.setLayout(null);
  f1.add(co);

  MouseAdapter mouse_listener = new MouseAdapter() {
   // 鼠标按下
   public void mousePressed(MouseEvent e) {
    if (e.getX() >= 247 && e.getX() <= 428 && e.getY() >= 345 && e.getY() <= 385) // 人机
    {
    }

    if (e.getX() >= 95 && e.getX() <= 280 && e.getY() >= 280 && e.getY() <= 335) // 人人
    {
     showUI();
     flag_renren = true;
     f1.setVisible(false);
    }

    if (e.getX() >= 95 && e.getX() <= 280 && e.getY() >= 345 && e.getY() <= 390) // 帮助
    {
     JFrame f = new JFrame();
     f.setTitle("游戏帮助");
     f.setLayout(new FlowLayout());
     f.setResizable(false);
     f.setSize(420, 280);
     f.setLocation(0, 200);
     f.setVisible(true);
     JButton btn = new JButton();
     btn.setIcon(icon5);
     btn.setPreferredSize(new Dimension(420, 280));
     f.add(btn);
    }

    if (e.getX() >= 95 && e.getX() <= 280 && e.getY() >= 410 && e.getY() <= 455) // 退出
    {
     f1.setVisible(false);
    }
   }
  };
  f1.addMouseListener(mouse_listener);
  f1.setVisible(true);

 }

 public void showUI() {// 游戏主界面

  setTitle("五子棋小游戏");
  setSize(557, 670);

  setResizable(false);// 不可全屏

  setLocationRelativeTo(null);// 居中
  setLayout(new FlowLayout());// 流线型

  setDefaultCloseOperation(3);// 设置任务管理器默认关闭操作//1,2,3

  JPanel board_panel = new JPanel() {

   public void paint(Graphics g) {// 画棋盘
    super.paint(g);
    g.drawImage(icon6.getImage(), 0, 0, null);

    for (int i = 0; i < 13; i++)// 画棋盘线
    {
     g.drawLine(38, 38 + 40 * i, 518, 38 + 40 * i);// 横线
    }
    for (int i = 0; i < 13; i++) {
     g.drawLine(38 + 40 * i, 38, 38 + 40 * i, 518);// 纵线
    }
    if (flag_regret == true) {
     for (int i = 0; i < list.getLength(); i++) {
      // 取出每个棋子对象
      Chess chess = (Chess) list.get(i);
      // 画棋子
      chess.drawChese(g);
     }
     flag_regret = false;
    }
   }

  };
  board_panel.setPreferredSize(new Dimension(557, 554));

  JPanel function_panel = new JPanel() {
   @Override
   protected void paintComponent(Graphics g) {
    // TODO Auto-generated method stub
    super.paintComponent(g);
    g.drawImage(icon7.getImage(), 0, 0, null);
   }
  };

  function_panel.setPreferredSize(new Dimension(557, 100));
  function_panel.setLayout(new FlowLayout(1, 100, 20));

  add(board_panel);
  add(function_panel);

  JButton regret = new JButton();
  regret.setPreferredSize(new Dimension(75, 25));
  regret.setIcon(icon8);

  JButton rstart = new JButton();
  rstart.setPreferredSize(new Dimension(71, 28));
  rstart.setIcon(icon9);

  function_panel.add(regret);
  function_panel.add(rstart);

  rstart.addActionListener(new ActionListener() {// 重新开始
   public void actionPerformed(ActionEvent e) {

    for (int i = 0; i < 11; i++)// 棋盘清空
     for (int j = 0; j < 11; j++)
      chese[i][j] = 0;

    repaint();

    for (int i = 0; i < 50; i++)
     list.delete(); // 删除所有元素
   }
  });

  regret.addActionListener(new ActionListener() {// 悔棋
   public void actionPerformed(ActionEvent e) {

    count++;
    System.out.println(count);
    flag_regret = true;

    Chess chess = (Chess) list.get(list.getLength() - 1);

    chese[chess.i()][chess.j()] = 0;// 悔过的棋置0

    list.delete(); // 删除最后的元素
    // 刷新棋盘
    board_panel.repaint();

   }
  });

  setVisible(true);

  g = board_panel.getGraphics();
  MouseListener mouse_listener = new MouseListener() {

   public void mouseEntered(MouseEvent e) {
    System.out.println("进入");
   } // 进入

   public void mouseExited(MouseEvent e) {
    System.out.println("离开");
   } // 离开

   public void mousePressed(MouseEvent e) {
    System.out.println("按压");
   } // 按压

   public void mouseReleased(MouseEvent e) {
    System.out.println("释放");
   }// 释放

   // 单击
   public void mouseClicked(MouseEvent e) {

    if (flag_renren) {// 点击了人人按钮
     // 获取鼠标单击时的坐标

     int x = e.getX();
     int y = e.getY();
     // 得到所有交叉点的通式
     for (int i = 0; i < 10; i++) {
      for (int j = 0; j < 10; j++) {
       int x0 = 30 + 40 * j;
       int y0 = 30 + 40 * i;
       // 判断 当前鼠标点击的坐标是否在误差范围内
       if (x < x0 + 18 && x > x0 - 18 && y < y0 + 18 && y > y0 - 18) {

        if (chese[i][j] == 0) {// 判断 该处是否有棋子

         if (flag_color == false) {// 黑棋子

          color = Color.black;
          chese[i][j] = 1;
          flag_color = true;
         }

         else if (flag_color == true) {// 白棋子

          color = Color.white;
          chese[i][j] = 2;
          flag_color = false;
         }

         if (count % 2 != 0) // 悔了奇数步棋,保持颜色不变
          if (color == Color.white) {
           color = Color.black;
           chese[i][j] = 1;
          } else {
           color = Color.white;
           chese[i][j] = 2;
          }

         // 下棋
         Chess chess = new Chess(x0, y0, color);
         // 将棋子保存到容器中
         list.add(chess);
         // 画棋子
         chess.drawChese(g);
         renji(i, j);
         win(i, j);

        }
       }
      }
     }
    }

   }
  };

  // 给棋盘面板添加鼠标监听器
  board_panel.addMouseListener(mouse_listener);
 }

 public void renji(int x, int y) {

 }

 // 判断输赢的方法 hang 代表当前棋子所在的行 lie代表当前棋子所在的列
 public void win(int hang, int lie) {
  // 计数器

  // 判断行
  int count = 1;// 统计相同棋子的个数
  int m = 1;// 控制列值的改变
  // 水平向左
  while (lie - m >= 0 && chese[hang][lie] == chese[hang][lie - m]) {
   m++;
   count++;
  }
  m = 1;
  // 水平向右
  while (lie + m <= 10 && chese[hang][lie] == chese[hang][lie + m]) {
   m++;
   count++;
  }

  // 判断列

  int count1 = 1;
  m = 1;

  // 水平向上
  while (hang - m >= 0 && chese[hang][lie] == chese[hang - m][lie]) {
   m++;
   count1++;
  }
  m = 1;

  while (hang + m <= 10 && chese[hang][lie] == chese[hang + m][lie]) {
   m++;
   count1++;
  }

  // 判断右斜
  int count2 = 1;
  m = 1;
  while (lie + m <= 10 && hang - m >= 0 && chese[hang][lie] == chese[hang - m][lie + m]) {
   m++;
   count2++;
  }
  m = 1;
  //
  while (lie - m >= 0 && hang + m <= 10 && chese[hang][lie] == chese[hang + m][lie - m]) {
   m++;
   count2++;
  }
  // 判断左斜
  int count3 = 1;
  m = 1;
  while (lie - m >= 0 && hang - m >= 0 && chese[hang][lie] == chese[hang - m][lie - m]) {
   m++;
   count3++;
  }
  m = 1;
  //
  while (lie + m <= 10 && hang + m <= 10 && chese[hang][lie] == chese[hang + m][lie + m]) {
   m++;
   count3++;
  }

  // 判断结果
  if (chese[hang][lie] == 1 && (count >= 5 || count1 >= 5 || count2 >= 5 || count3 >= 5)) {// 黑棋胜利
   // 提示用户
   JOptionPane.showMessageDialog(null, "黑棋胜利!");

   for (int i = 0; i <= 10; i++)
    for (int j = 0; j <= 10; j++)
     chese[i][j] = 0;
   repaint();
   for (int i = 0; i < 50; i++)
    list.delete(); // 删除所有元素
   count = count1 = count2 = count3 = 1;
  }
  if (chese[hang][lie] == 2 && (count >= 5 || count1 >= 5 || count2 >= 5 || count3 >= 5)) {// 白棋胜利
   // 提示用户
   JOptionPane.showMessageDialog(null, "白棋胜利!");

   for (int i = 0; i <= 10; i++)
    for (int j = 0; j <= 10; j++)
     chese[i][j] = 0;

   repaint();
   for (int i = 0; i < 50; i++)
    list.delete(); // 删除所有元素
   count = count1 = count2 = count3 = 1;

  }
 }

}










package com.renyou;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.ImageIcon;

public class Chess {
 ImageIcon icon3=new ImageIcon("images//b1.png"); //相对路径
 ImageIcon icon4=new ImageIcon("images//h1.png"); //相对路径
int x,y;
Color color;
int i,j;
Chess (int x,int y ,Color color){
 this.x=x;
 this.y=y;
 this.color=color;
}
public void drawChese(Graphics g){
 
 if(color==Color.white)
 g.drawImage(icon3.getImage(),x-15,y-15,null);
 
 else
  g.drawImage(icon4.getImage(),x-15,y-15,null);
 
}
public int i(){
 
 i=(y-15)/40;
 return i;
 
}
public int j(){
 
 j=(x-15)/40;
 
 return j;
 
}
}





package com.renyou;

/**
 * 自定义容器
 *
 *
 */
public class MyContainer {
 //原始数组   初始大小为0
 Chess[] srcArray = new Chess[0];
 
 //在末尾添加元素的方法
 public void add(Chess element){
  //创建新数组  长度比srcArray大1
  Chess[] newArray = new Chess[srcArray.length+1];
  //将srcArray中的元素拷贝到newArray中
  for(int i=0;i    newArray[i] = srcArray[i];
  }
  //将element添加到newArray末尾
  newArray[srcArray.length] = element;
  //数组交换
  srcArray = newArray;
 }
 
 
 
 //获取指定下标的元素的方法
 public Chess get(int index){
  return srcArray[index];
 }
 
 //获取容器大小的方法
 public int getLength(){
  return srcArray.length;
 }
 
 //删除末尾元素的方法
 public void delete(){
  Chess[] newArray = new Chess[srcArray.length-1];
  
  for(int i=0;i    
   newArray[i] = srcArray[i];
  
  //数组交换
  srcArray = newArray;
 }
 
}


package com.renyou;

public class Test {
 public static void main(String[] args) {
  new FiveChess().start();

 }
}


以上就是五子棋源代码



java简单五子棋源代码_第1张图片 java简单五子棋源代码_第2张图片 java简单五子棋源代码_第3张图片

以上是我的资源,都是网上搜的,再处理一下。



本人是java初学者,编的程序可能有很多的不足之处,还望大家多多指教,目前还没有写人机算法,以后会逐渐跟上,目前的代码值得一提的是我用的容器是自定义容器,
因为悔棋的时候要用到容器,刚开始用到的是ArrayList容器,并调用里面的add(),remove()等方法。后来用自定义容器实现了悔棋功能。





你可能感兴趣的:(java)