基于java的五子棋:
创新互联于2013年创立,先为克东等服务建站,克东等地企业,进行企业商务咨询服务。为克东企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。
主要功能代码如下:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Main extends JFrame implements ActionListener{
private static final long serialVersionUID = 1L;
JButton[][] point=new JButton[40][40];
int[][] state=new int[40][40];
int[][] value=new int[40][40];
int[] place=new int[2];
public Main(){
this.setTitle("五子棋");
this.setBounds(100,10,1000,1000);
this.setLayout(new GridLayout(30,30));
int i,j;
for(i=0;i=39;i++){
for(j=0;j=39;j++){
猛蠢 state[i][j]=0;
缓知乎 value[i][j]=0;
}
}
for(i=5;i=34;i++){
for (j=5;j=34;j++){
point[i][j]=new JButton("");
this.add(point[i][j]);
point[i][j].addActionListener(this);
}
}
this.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
int i,j;
for(i=5;i=34;i++){
for(j=5;j=34;j++){
if(e.getSource()==point[i][j]){
point[i][j].setBackground(Color.RED);
state[i][j]=1;
扰悉 point[i][j].setEnabled(false);
value[i][j]=0;
}
}
}
public static void main(String[] args)throws NullPointerException{
new Main();
}
else if(((state[i][j]==0)(state[i][j+1]==1)(state[i][j+2]==0))||
((state[i][j]==0)(state[i][j-1]==1)(state[i][j-2]==0))||
((state[i][j]==0)(state[i+1][j]==1)(state[i+2][j]==0))||
((state[i][j]==0)(state[i-1][j]==1)(state[i-2][j]==0))|| //空红空
((state[i][j]==0)(state[i+1][j+1]==1)(state[i+2][j+2]==0))||
((state[i][j]==0)(state[i+1][j-1]==1)(state[i+2][j-2]==0))||
((state[i][j]==0)(state[i-1][j+1]==1)(state[i-2][j+2]==0))||
((state[i][j]==0)(state[i-1][j-1]==1)(state[i-2][j-2]==0))||
((state[i][j]==0)(state[i][j+1]==1)(state[i][j+2]==1)(state[i][j+3]==2))||
((state[i][j]==0)(state[i][j-1]==1)(state[i][j-2]==1)(state[i][j-3]==2))||
((state[i][j]==0)(state[i+1][j]==1)(state[i+2][j]==1)(state[i+3][j]==2))||
((state[i][j]==0)(state[i-1][j]==1)(state[i-2][j]==1)(state[i-3][j]==2))|| //空红红蓝型
((state[i][j]==0)(state[i+1][j+1]==1)(state[i+2][j+2]==1)(state[i+3][j+3]==2))||
((state[i][j]==0)(state[i+1][j-1]==1)(state[i+2][j-2]==1)(state[i+3][j-3]==2))||
((state[i][j]==0)(state[i-1][j+1]==1)(state[i-2][j+2]==1)(state[i-3][j+3]==2))||
((state[i][j]==0)(state[i-1][j-1]==1)(state[i-2][j-2]==1)(state[i-3][j-3]==2))){
value[i][j]=2;
return value[i][j];
}
else if(((state[i][j]==0)(state[i][j+1]==2)(state[i][j+2]==0))||
((state[i][j]==0)(state[i][j-1]==2)(state[i][j-2]==0))||
((state[i][j]==0)(state[i+1][j]==2)(state[i+2][j]==0))||
((state[i][j]==0)(state[i-1][j]==2)(state[i-2][j]==0))||
((state[i][j]==0)(state[i+1][j+1]==2)(state[i+2][j+2]==0))||
((state[i][j]==0)(state[i+1][j-1]==2)(state[i+2][j-2]==0))||
((state[i][j]==0)(state[i-1][j+1]==2)(state[i-2][j+2]==0))||
((state[i][j]==0)(state[i-1][j-1]==2)(state[i-2][j-2]==0))||
((state[i][j]==0)(state[i][j+1]==2)(state[i][j+2]==2)(state[i][j+3]==1))||
((state[i][j]==0)(state[i][j-1]==2)(state[i][j-2]==2)(state[i][j-3]==1))||
((state[i][j]==0)(state[i+1][j]==2)(state[i+2][j]==2)(state[i+3][j]==1))||
((state[i][j]==0)(state[i-1][j]==2)(state[i-2][j]==2)(state[i-3][j]==1))||
((state[i][j]==0)(state[i+1][j+1]==2)(state[i+2][j+2]==2)(state[i+3][j+3]==1))||
((state[i][j]==0)(state[i+1][j-1]==2)(state[i+2][j-2]==2)(state[i+3][j-3]==1))||
((state[i][j]==0)(state[i-1][j+1]==2)(state[i-2][j+2]==2)(state[i-3][j+3]==1))||
((state[i][j]==0)(state[i-1][j-1]==2)(state[i-2][j-2]==2)(state[i-3][j-3]==1))){
value[i][j]=1;
return value[i][j];
}
else{
value[i][j]=0;
return value[i][j];
}
}
public int[] largestValue(int[][] value){
int max,i,j,l,k;
l=0;k=0;
max=0;
for(i=5;i34;i++){
for(j=5;j34;j++){
if(maxvalue[i][j]){
max=value[i][j];
place[0]=i;
place[1]=j;
}
}
}
return place;
}
}
运行结果如下图所示:
楼主要是觉得看的不舒服可以逗银拷到记事本里看~ import java.applet.*; import java.awt.*; import java.awt.event.*; import java.applet.Applet; import java.awt.Color; //这一段import就不说了,下面要用到的就import进来 public class wuziqi extends Applet implements ActionListener,MouseListener,MouseMotionListener,ItemListener //继承Applet表明是个applet,后面的接口必须要实现每个接口的所有方法 { int color_Qizi=0;//旗子的颜色标识 0:白子 1:黑子 int intGame_Start=0;//游戏开始标志 0未开始 1游戏中 int intGame_Body[][]=new int[16][16]; //设置棋盘棋子状态 0 无子 1 白子 2 黑子 Button b1=new Button("游戏开始"); Button b2=new Button("重置游戏"); //两个按钮 Label lblWin=new Label(" "); //这个label用来显示最后输赢信息的,先留空 Checkbox ckbHB[]=new Checkbox[2]; //用来表明选择黑气或白棋先走的checkbox CheckboxGroup ckgHB=new CheckboxGroup(); //两个checkbox必须放在同一个checkboxgroup里才能做到单选 public void init() //初始化,差指乱堆砌界面 { setLayout(null); //不设布局管理器 addMouseListener(this); //将本类作为鼠标事件的接口响应鼠标动作 add(b1); //将事先定义好的第一个按钮添加入界面 b1.setBounds(330,50,80,30); //设置第一个按钮左上角的位置和大小 b1.addActionListener(this); //将本类作为按钮事件的接口响应按钮动作 add(b2); //将事先定义好的第二个按钮添加进去 b2.setBounds(330,90,80,30); /设置第二个按钮左上角的位置和大小 b2.addActionListener(this); //将本类作为按钮事件的接口响应按钮动作 ckbHB[0]=new Checkbox("白子先",ckgHB,false); //new一个checkbox ckbHB[0].setBounds(320,20,60,30); //设置左上角位置和大小 ckbHB[1]=new Checkbox("黑子先",ckgHB,false); //new第二虚档个checkbox ckbHB[1].setBounds(380,20,60,30); //设置左上角位置和大小 add(ckbHB[0]); //将第一个checkbox加入界面 add(ckbHB[1]); //将第二个checkbox加入界面 ckbHB[0].addItemListener(this); //将本类作为其事件接口来响应选中动作 ckbHB[1].addItemListener(this); //将本类作为其事件接口来响应选中动作 add(lblWin); //将标签加入界面 lblWin.setBounds(330,130,80,30); //设置标签的左上角位置和大小 Game_start_csh(); //调用游戏初始化 } public void itemStateChanged(ItemEvent e) //ItemListener接口中的方法,必须要有 { if (ckbHB[0].getState()) //选择黑子先还是白子先 { color_Qizi=0; //白棋先 } else { color_Qizi=1; //黑棋先 } } public void actionPerformed(ActionEvent e) //ActionListener接口中的方法,也是必须的 { Graphics g=getGraphics(); //这句话貌似可以去掉,g是用来画图或者画界面的 if (e.getSource()==b1) //如果动作的来源是第一个按钮 { Game_start(); //游戏开始 } else //否则 { Game_re(); //游戏重新开始 } } public void mousePressed(MouseEvent e){} //MouseListener接口中的方法,用不到所以留个空,但一定要有 public void mouseClicked(MouseEvent e) //鼠标单击时 { Graphics g=getGraphics(); //获得画笔 int x1,y1; x1=e.getX(); //单击处的x坐标 y1=e.getY(); //单击处的y坐标 if (e.getX()20 || e.getX()300 || e.getY()20 || e.getY()300) //在棋盘范围之外 { return; //则这是不能走棋的,直接返回 } //下面这两个if和两个赋值的作用是将x和y坐标根据舍入原则修改成棋盘上格子的坐标 if (x1%2010) { x1+=20; } if(y1%2010) { y1+=20; } x1=x1/20*20; y1=y1/20*20; set_Qizi(x1,y1); //在棋盘上画上一个棋子 } public void mouseEntered(MouseEvent e){} //MouseListener接口中的方法,用不到所以留个空,但一定要有 public void mouseExited(MouseEvent e){} //MouseListener接口中的方法,用不到所以留个空,但一定要有 public void mouseReleased(MouseEvent e){} //MouseListener接口中的方法,用不到所以留个空,但一定要有 public void mouseDragged(MouseEvent e){} //MouseListener接口中的方法,用不到所以留个空,但一定要有 public void mouseMoved(MouseEvent e){} //MouseListener接口中的方法,用不到所以留个空,但一定要有 public void paint(Graphics g) //重绘和applet程序装载的时候会调用这个绘制的过程 { draw_qipan(g); //画棋盘 }
package com.test;
import java.util.Scanner;
public class TestMain{
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
char[] name = { '空', '黑', '白' };
int count = 0;// 计数器,用来计算该谁下棋了
char[][] qi = new char[16][16];// 把棋盘情况放进二维数组中
while (true) {
int row = 0;// 初始化从控制台接收到的row和col 的index为0
int col = 0;
if (count == 0) {
print(qi, name[count], row, col);
count++;
}
if (count == 1) {
System.out.println("请黑(@)下棋(i j):");
row = console.nextInt(16)%16;// 从控制台接收到的row的index
col = console.nextInt(16)%16;// 从控制台接收到的col的index
String condition = print(qi, name[count], row, col);
if (condition.length() == 1) {// 下棋正确
count++;
}
if (condition.length() == 2) {// 下棋出现错误,要下的余肢位置已经下过棋子
System.out.println("此位置已经有棋子,黑棋重新再下");
}
if (condition.length() == 3) {// 黑棋子赢
System.out.println("黑棋很给力,白棋是浮云,黑棋赢了");
break;
}
if (condition.length() == 4) {
System.out.println("白棋子赢");
break;
}
}
if (count == 2) {
System.out.println("请白(*)下棋(i j):");
row = console.nextInt(16);
col = console.nextInt(16);
String condition = print(qi, name[count], row, col);
if (condition.length() == 1) {// 打印兆罩棋盘
count--;
}
if (condition.length() == 2) {// 下棋出现错误,要下的位置已经下过棋子
System.out.println("此位置已经有棋子,白棋重新再下");
}
if (condition.length() == 3) {
System.out.println("黑棋子赢");
break;
}
if (condition.length() == 4) {
System.out.println("白棋很给力,黑棋是浮云,白棋赢了");
break;
}
}
}
}
/**
* @param qi是装棋盘情况
* @param name轮到谁下棋了
* row 行号 col 列号 return true-下棋正确,false-下棋错误,重新下
*/
public static String print(char[][] qi, char name, int row, int col) {
String b = "无";//族毁闹 初始化下棋是正确的
String str = "0123456789111111";
// 打印下第一行
System.out.print(" ");
for (int i = 0; i str.length(); i++) {
System.out.print(str.charAt(i));
}
System.out.println();
for (int i = 0; i qi.length; i++) {
System.out.print(str.charAt(i));
for (int j = 0; j qi[i].length; j++) {
if (name == '空') { // 还没开始下棋时,棋盘默认显示是'.'
qi[i][j] = '.';
}
if ((name == '黑') (i == row) (j == col)) {
if (qi[i][j] != '.') {// 下的位置已经有棋子了,返回false
b = "有棋";
} else {
qi[i][j] = '@';// 黑方下棋后的位置显示是'@'
if (win(qi, '@', row, col)) {
b = "黑棋子";
}
}
}
if ((name == '白') (i == row) (j == col)) {
if (qi[i][j] != '.') {
b = "有棋";
} else {
qi[i][j] = '*';// 黑方下棋后的位置显示是'*'
if (win(qi, '*', row, col)) {
b = "白棋子赢";
}
}
}
System.out.print(qi[i][j]);
}
System.out.println();
}
return b;
}
public static boolean win(char[][] qi, char ch, int row, int col) {
boolean b = false;
boolean ns = n2s(qi, ch, row, col);
boolean we = w2e(qi, ch, row, col);
boolean wnes = wn2es(qi, ch, row, col);
boolean enws = en2ws(qi, ch, row, col);
b = ns || we || wnes || enws;
return b;
}
/* 从北到南方向 */
public static boolean n2s(char[][] qi, char ch, int row, int col) {
boolean win = false;
int north = 0;// begin
int south = 0;// end
while (qi[row][col] == ch) {
row--;
if (row 0) {
break;
}
}
row++;// row此时要++,不然等到下面的while(..)中的qi[row][col]永不会==ch
north = row;
while (qi[row][col] == ch) {
row++;
if (row 15) {
break;
}
}
south = row;
if ((south - north) = 5) {
win = true;
}
return win;
}
/* 从西到东方向 */
public static boolean w2e(char[][] qi, char ch, int row, int col) {
boolean win = false;
int west = 0;// begin
int east = 0;// end
while (qi[row][col] == ch) {
col--;
if (col 0) {
break;
}
}
col++;// col此时要++,不然等到下面的while(..)中的qi[row][col]永不会==ch
west = col;
while (qi[row][col] == ch) {
col++;
if (col 15) {
break;
}
}
east = col;
if ((east - west) = 5) {
win = true;
}
return win;
}
/* 从西北到东南方向 */
public static boolean wn2es(char[][] qi, char ch, int row, int col) {
boolean win = false;
int wn = 0;// begin
int es = 0;// end
while (qi[row][col] == ch) {
row--;
col--;
if (row 0 || col 0) {
break;
}
}
row++;// row和col此时要++,不然等到下面的while(..)中的qi[row][col]永不会==ch
col++;
wn = row;
while (qi[row][col] == ch) {
row++;
col++;
if (row 15 || col 15) {
break;
}
}
es = row;
if ((es - wn) = 5) {
win = true;
}
return win;
}
/* 从东北到西南方向 */
public static boolean en2ws(char[][] qi, char ch, int row, int col) {
boolean win = false;
int en = 0;// begin
int ws = 0;// end
while (qi[row][col] == ch) {
row--;
col--;
if (row 0 || col 0) {
break;
}
}
row++;// row和col此时要++,不然等到下面的while(..)中的qi[row][col]永不会==ch
col++;
en = row;
while (qi[row][col] == ch) {
row++;
col++;
if (row 15 || col 15) {
break;
}
}
ws = row;
if ((ws - en) = 5) {
win = true;
}
return win;
}
}
以下是现写的 实现了两人对战 自己复制后运行陵培源把 没什么难度 类名 Games
import java.util.Scanner;
public class Games {
private String board[][];
private static int SIZE = 17;
private static String roles = "A玩家";
//初始化数组
public void initBoard() {
board = new String[SIZE][SIZE];
for (int i = 0; i SIZE; i++) {
for (int j = 0; j SIZE; j++) {
// if(i==0){
// String str = "";
// str += j+" ";
// board[i][j]= str;
// }else if(i!=0j==0){
// String str = "";
// str += i+" ";
// board[i][j]= str;
// }else{
board[i][j] = "╋";
// }
尺态 }
}
}
//输出棋盘
public void printBoard() {
for (int i = 0; i SIZE; i++) {
for (int j = 0; j SIZE; j++) {
System.out.print(board[i][j]);
}
System.out.println();
}
}
//判断所下棋子位置是否合理
public boolean isOk(int x, int y) {
boolean isRight = true;
if (x = 16 || x 1 || y = 16 | y 1) {
//System.out.println("输入错误,请从新输入");
isRight = false;
}
if (board[x][y].equals("●") || board[x][y].equals("○")) {
isRight = false;
}
return isRight;
}
//判断谁赢了
public void whoWin(Games wz) {
// 从数组挨个查找找到某个类型的棋子就从该棋子位置向右,向下,斜向右下 各查找5连续的位置看是否为5个相同的
int xlabel;// 记录第一次找到某个棋子的x坐标
int ylabel;// 记录第一次找到某个棋子的y坐标
// ●○╋
// 判断人是否赢了
for (int i = 0; i SIZE; i++) {
for (int j = 0; j SIZE; j++) {
if (board[i][j].equals("○")) {
xlabel = i;
ylabel = j;
// 横向找 x坐标不变 y坐标以此加1连成字符串
String heng = "";
if (i + 5 SIZE j + 5 SIZE) {
for (int k = j; k j + 5; k++) {
heng += board[i][k];
}
if (heng.equals("○○○○○")) {
System.out.println(roles+"赢了!您中圆输了!");
System.exit(0);
}
// 向下判断y不变 x逐增5 连成字符串
String xia = "";
for (int l = j; l i + 5; l++) {
xia += board[l][j];
// System.out.println(xia);
}
if (xia.equals("○○○○○")) {
System.out.println(roles+"赢了!您输了!");
System.exit(0);
}
// 斜向右下判断
String youxia = "";
for (int a = 1; a = 5; a++) {
youxia += board[xlabel++][ylabel++];
}
if (youxia.equals("○○○○○")) {
System.out.println(roles+"赢了!您输了!");
System.exit(0);
}
}
}
}
}
// 判断电脑是否赢了
for (int i = 0; i SIZE; i++) {
for (int j = 0; j SIZE; j++) {
if (board[i][j].equals("●")) {
xlabel = i;
ylabel = j;
// 横向找 x坐标不变 y坐标以此加1连成字符串
String heng = "";
if (j + 5 SIZE i + 5 SIZE) {
for (int k = j; k j + 5; k++) {
heng += board[i][k];
}
if (heng.equals("●●●●●")) {
System.out.println(roles+"赢输了!您输了!");
System.exit(0);
}
// 向下判断y不变 x逐增5 连成字符串
String xia = "";
for (int l = i; l i + 5; l++) {
xia += board[l][ylabel];
// System.out.println(xia);
}
if (xia.equals("●●●●●")) {
System.out.println(roles+"赢了!您输了!");
System.exit(0);
}
// 斜向右下判断
String youxia = "";
for (int a = 1; a = 5; a++) {
youxia += board[xlabel++][ylabel++];
}
if (youxia.equals("●●●●●")) {
System.out.println(roles+"赢了!您输了!");
System.exit(0);
}
}
}
}
}
}
public static void main(String[] args) {
Games wz = new Games();
Scanner sc = new Scanner(System.in);
wz.initBoard();
wz.printBoard();
while (true) {
System.out.print("请"+roles+"输入X,Y坐标,必须在0-15范围内,xy以空格隔开,输入16 16结束程序");
int x = sc.nextInt();
int y = sc.nextInt();
if (x == SIZE y == SIZE) {
System.out.println("程序结束");
System.exit(0);
}
if (x SIZE || x 0 || y SIZE | y 0) {
System.out.println("输入错误,请从新输入");
continue;
}
//如果roles是A玩家 就让A玩家下棋,否则就让B玩家下棋。
if (wz.board[x][y].equals("╋")roles.equals("A玩家")) {
wz.board[x][y] = "○";
wz.printBoard();
//判断输赢
wz.whoWin(wz);
}else if(wz.board[x][y].equals("╋")roles.equals("B玩家")){
wz.board[x][y] = "●";
wz.printBoard();
//判断输赢
wz.whoWin(wz);
} else {
System.out.println("此处已经有棋子,从新输入");
continue;
}
if(roles.equals("A玩家")){
roles = "B玩家";
}else if(roles.equals("B玩家")){
roles = "A玩家";
}
}
}
}