用java实现简单五子棋人人对战

用java实现简单五子棋人人对战,对于初学者还是比较好玩的。
接下来 看下我写的五子棋程序
我们将它分为三个类
1.主窗体类
2.鼠标事件处理器
3.判断一方是否胜利
比较简单,望大家多多指正

[img]http://dl.iteye.com/upload/attachment/0079/5078/190bac46-f7bc-3e78-a4a4-90810408e2e3.png[/img]



1.主窗体类
/**************************************************************************************************************************************/
package cn.lantian.Fivezq;

import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JFrame;

public class WuzqFrame extends JFrame {
/**
* 五子棋
*
* @兰天
*/
//入口主函数
public static void main(String[] args) {
WuzqFrame wzq = new WuzqFrame();
wzq.init();
}

public void init() {
chushi();// 初始化棋盘
//基本设置
this.setTitle("五子棋");
this.setSize(520, 535);
this.setDefaultCloseOperation(3);
//设置居中
this.setLocationRelativeTo(null);
//用户不可调整大小
this.setResizable(false);
this.setVisible(true);
//获取窗体的画布
Graphics g = this.getGraphics();
//定义鼠标监听类
wzqListener wzql = new wzqListener(g,this);
//添加鼠标监听
this.addMouseListener(wzql);
}
//对窗体进行重绘
public void paint(Graphics g) {
//调用父类的方法,super
super.paint(g);
//画棋盘
for (int i = 0; i < n + 1; i++) {
g.drawLine(x + i * size, y, x + i * size, y + n * size);
}
for (int i = 0; i < n + 1; i++) {
g.drawLine(x, y + i * size, x + n * size, y + i * size);
}
for (int i=0;i<n;i++)
for (int j=0;j<n;j++)
if (s[i][j]==1){
g.fillOval(x +i* WuzqFrame.size-WuzqFrame.size/2 , y + j*WuzqFrame.size-WuzqFrame.size/2 ,
WuzqFrame.qsize, WuzqFrame.qsize);
}else if (s[i][j]==-1){
//将颜色改为白色
g.setColor(Color.WHITE);
g.fillOval(x +i* WuzqFrame.size-WuzqFrame.size/2 , y + j*WuzqFrame.size-WuzqFrame.size/2 ,
WuzqFrame.qsize, WuzqFrame.qsize);
//定义黑色为初始色
g.setColor(Color.BLACK);
}


}

//定义起始直线坐标
public static int x = 30, y = 50;
//棋子的多少及格子大小
public static int n = 15, size = 30, qsize = 30;
//定义一个数组来存储棋子
public static int[][] s = new int[n][n];
//初始方法,数组全赋为0
public void chushi() {
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
s[i][j] = 0;
}
}
/**************************************************************************************************************************************/


2.鼠标事件处理器
/**************************************************************************************************************************************/
package cn.lantian.Fivezq;


import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
//棋盘鼠标监听器
public class wzqListener implements MouseListener {
private Graphics g;
//用来确定下什么棋
public static int t = 1;
private WuzqFrame wzq;
//构造器,将画布传过来
public wzqListener(Graphics g,WuzqFrame wzq) {
this.g = g;
this.wzq = wzq;
}

public void mousePressed(MouseEvent e) {
//获得要下的棋子的行和列
int x1=Math.round((float) (e.getX() - WuzqFrame.x) / WuzqFrame.size);
int y1=Math.round((float) (e.getY() - WuzqFrame.y) / WuzqFrame.size);
//判断行列是否出界,及是否放了棋子
if(x1>=0&&x1<WuzqFrame.n&&y1>=0&&y1<WuzqFrame.n&&WuzqFrame.s[x1][y1]==0){
//棋子放在棋盘上后,数组做相应标示
WuzqFrame.s[x1][y1] = t;
//重绘棋盘
wzq.repaint();
//调用QiziPd类中的方法,来判断输赢
QiziPd.pd(t, x1, y1,g,wzq);
//改变棋子颜色
t = -t;
}
}

@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

}

}
/**************************************************************************************************************************************/


3.判断一方是否胜利
/**************************************************************************************************************************************/
package cn.lantian.Fivezq;

import java.awt.Graphics;

import javax.swing.JOptionPane;

//判断胜负
public class QiziPd {
// 将一些重要参数的值传过来
public static int[][] s = WuzqFrame.s;
private static int n = WuzqFrame.n;
// 构建两个移动数组(暂且这么说)
private static int[] a = { -1, -1, 0, 1, 1, 1, 0, -1 };
private static int[] b = { 0, 1, 1, 1, 0, -1, -1, -1 };

// 判断方法,z为棋子颜色的标志,x y 为下的棋子的行与列
public static void pd(int z, int x, int y, Graphics g, WuzqFrame wzq) {
// 四个方向横、竖、左斜、右斜
for (int i = 0; i < 4; i++) {
// 记录在一条直线上的棋子数(不包括下的棋子)
int temp = 0;
// 里面以棋子为界,又分两个方向
for (int j = 0; j < 2; j++) {
int x1 = x, y1 = y;
// 判断同一直线上的棋子数
while (temp < 4) {
// 算出那条直线上的棋子的坐标
x1 = x1 + a[i + 4 * j];
y1 = y1 + b[i + 4 * j];
// 如果在棋盘内,继续循环,否则退出循环
if (x1 < 0 || x1 >= n || y1 < 0 || y1 >= n)
break;
else {
// 如果这条直线上有自己颜色的棋子,则temp++,否则退出
if (s[x1][y1] == z) {
temp++;
} else
break;
}
}
// 如果连着的棋子数为4(没有包括自己,总数是五)
if (temp == 4) {
// 弹出谁赢了的提示框
if (z == 1) {
JOptionPane.showMessageDialog(null, "黑棋赢了,再来一盘吧!");
} else {
JOptionPane.showMessageDialog(null, "白棋赢了,再来一盘吧!");
}
//初始数组
wzq.chushi();
//重绘棋盘
wzq.repaint();
//先下的 棋子为黑子,t改为白子,后面的语句会改过来
wzqListener.t=-1;
break;
}
}
}

}

}
/**************************************************************************************************************************************/
  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单Java五子棋人人对战的示例: ```java import java.util.Scanner; public class Gomoku { private static final int SIZE = 15; private static final int EMPTY = 0; private static final int BLACK = 1; private static final int WHITE = 2; private int[][] board; private int currentPlayer; public Gomoku() { board = new int[SIZE][SIZE]; currentPlayer = BLACK; } public void play() { Scanner scanner = new Scanner(System.in); while (true) { System.out.println("当前玩家: " + (currentPlayer == BLACK ? "黑棋" : "白棋")); System.out.print("请输入要下棋的位置(x y): "); int x = scanner.nextInt(); int y = scanner.nextInt(); if (isValidMove(x, y)) { makeMove(x, y); printBoard(); if (isWinningMove(x, y)) { System.out.println("恭喜玩家 " + (currentPlayer == BLACK ? "黑棋" : "白棋") + " 获胜!"); break; } currentPlayer = (currentPlayer == BLACK ? WHITE : BLACK); } else { System.out.println("无效的移动,请重新输入!"); } } scanner.close(); } private boolean isValidMove(int x, int y) { return x >= 0 && x < SIZE && y >= 0 && y < SIZE && board[x][y] == EMPTY; } private void makeMove(int x, int y) { board[x][y] = currentPlayer; } private boolean isWinningMove(int x, int y) { int count = 1; // 检查水平方向 for (int i = x - 1; i >= 0 && board[i][y] == currentPlayer; i--) { count++; } for (int i = x + 1; i < SIZE && board[i][y] == currentPlayer; i++) { count++; } if (count >= 5) { return true; } // 检查垂直方向 count = 1; for (int i = y - 1; i >= 0 && board[x][i] == currentPlayer; i--) { count++; } for (int i = y + 1; i < SIZE && board[x][i] == currentPlayer; i++) { count++; } if (count >= 5) { return true; } // 检查左上到右下的对角线 count = 1; for (int i = x - 1, j = y - 1; i >= 0 && j >= 0 && board[i][j] == currentPlayer; i--, j--) { count++; } for (int i = x + 1, j = y + 1; i < SIZE && j < SIZE && board[i][j] == currentPlayer; i++, j++) { count++; } if (count >= 5) { return true; } // 检查右上到左下的对角线 count = 1; for (int i = x - 1, j = y + 1; i >= 0 && j < SIZE && board[i][j] == currentPlayer; i--, j++) { count++; } for (int i = x + 1, j = y - 1; i < SIZE && j >= 0 && board[i][j] == currentPlayer; i++, j--) { count++; } if (count >= 5) { return true; } return false; } private void printBoard() { for (int i = 0; i < SIZE; i++) { for (int j = 0; j < SIZE; j++) { if (board[i][j] == EMPTY) { System.out.print("- "); } else if (board[i][j] == BLACK) { System.out.print("● "); } else if (board[i][j] == WHITE) { System.out.print("○ "); } } System.out.println(); } } } public class Main { public static void main(String[] args) { Gomoku game = new Gomoku(); game.play(); } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值