下面的代码实现了一个非常简单的联网五子棋程序,写给学生的一个例子,很多功能都还没实现,用的也是民间规则,姑且一看吧!
棋盘类:
package com.accp;
import java.awt.Color;
import java.awt.Graphics;
/**
* 棋盘
* @author 骆昊
*
*/
public class Board {
private int[][] board = new int[15][15]; // 用二维数组代表15*15的棋盘
/**
* 绘制棋盘
* @param g 画笔
*/
public void draw(Graphics g) {
for(int i = 0; i < board.length; i++) {
for(int j = 0; j < board[i].length; j++) {
if(board[i][j] != 0) { // 不是空格
if(board[i][j] == 1) { // 黑子
g.setColor(Color.BLACK);
}
else { // 白子
g.setColor(Color.WHITE);
}
g.fillOval(35 * (j + 1), 35 * (i + 1), 30, 30);
if(board[i][j] == 2) { // 给白子加上边框
g.setColor(Color.BLACK);
g.drawOval(35 * (j + 1), 35 * (i + 1), 30, 30);
}
}
}
}
}
/**
* 向棋盘中放置棋子
* @param row 放置棋子的行
* @param col 放置棋子的列
* @param isBlack 黑棋还是白棋
* @return 返回true落子成功, 返回false落子失败(已经有棋子)
*/
public boolean addPiece(int row, int col, boolean isBlack) {
if(board[row][col] == 0) { // 没有棋子的地方才能落子
board[row][col] = isBlack? 1 : 2; // 1代表黑子2代表白子
return true;
}
return false;
}
/**
* 判断胜负
* @param row 落子的行
* @param col 落子的列
* @param isBlack 是黑子还是白子
* @return 获胜返回true否则返回false
*/
public boolean isWin(int row, int col, boolean isBlack) {
return checkH(row, col, isBlack) ||
checkV(row, col, isBlack) ||
checkX1(row, col, isBlack) ||
checkX2(row, col, isBlack);
}
// 判断从右上到左下的斜线上是否连成5颗棋子
private boolean checkX2(int row, int col, boolean isBlack) {
int counter = 1;
int currentRow = row;
int currentCol = col;
int v = isBlack? 1 : 2;
while(currentRow > 0 && currentCol < 14 &&
board[--currentRow][++currentCol] == v) {
counter++;
}
currentRow = row;
currentCol = col;
while(currentRow < 14 && currentCol > 0 &&
board[++currentRow][--currentCol] == v) {
counter++;
}
return counter >= 5;
}
// 判断从左上到右下的斜线上是否连成5颗棋子
private boolean checkX1(int row, int col, boolean isBlack) {
int counter = 1;
int currentRow = row;
int currentCol = col;
int v = isBlack? 1 : 2;
while(currentRow > 0 && currentCol > 0 &&
board[--currentRow][--currentCol] == v) {
counter++;
}
currentRow = row;
currentCol = col;
while(currentCol < 14 && currentRow < 14 &&
board[++currentRow][++currentCol] == v) {
counter++;
}
return counter >= 5;
}
// 判断竖着方向上是否连成5颗棋子
private boolean checkV(int row, int col, boolean isBlack) {
int counter = 1;
int currentRow = row;
int v = isBlack? 1 : 2;
while(currentRow > 0 && board[--currentRow][col] == v) {
counter++;
}
currentRow = row;
while(currentRow < 14 && board[++currentRow][col] == v) {
counter++;
}
return counter >= 5;
}
// 判断横着方向上是否连成5颗棋子
private boolean checkH(int row, int col, boolean isBlack) {
int counter = 1;
int currentCol = col;
int v = isBlack? 1 : 2;
while(currentCol > 0 && board[row][--currentCol] == v) {
counter++;
}
currentCol = col;
while(currentCol < 14 && board[row][++currentCol] == v) {
counter++;
}
return counter >= 5;
}
}
绘制棋盘的面板类:
package com.accp;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
@SuppressWarnings("serial")
/**
* 绘制棋盘的面板
* @author 骆昊
*
*/
public class MyPanel extends JPanel {
private Board b = null;
public MyPanel(Board b) {
this.b = b;
}
@Override
public void paint(Graphics g) {
g.setColor(new Color(165, 185, 75));
g.fillRect(35, 35, 525, 525); // 绘制背景
g.setColor(Color.BLACK);
for (int i = 0; i < 15; i++) { // 绘制棋盘网格
g.drawLine(50, 50 + i * 35, 540, 50 + i * 35);
g.drawLine(50 + i * 35, 50, 50 + i * 35, 540);
}
g.fillOval(290, 290, 10, 10); // 绘制天元
b.draw(g); // 绘制棋盘
}
}
游戏窗体类:
package com.accp;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class MyFrame extends JFrame implements ActionListener {
private boolean isBlack = true; // 是否轮到黑棋
private Board b = new Board(); // 棋盘对象
private boolean isWin = false; // 是否获得胜利
private boolean isStart = false; // 是否联机成功开始游戏
private boolean isYourTurn = false; // 是否轮到自己走子
private DataInputStream din = null;
private DataOutputStream dout = null;
private class MouseHandler extends MouseAdapter {
@Override
public void mousePressed(MouseEvent e) {
if (isStart && isYourTurn && !isWin) { // 游戏已经开始并且轮到自己且未分出胜负则可落子
int x = e.getX();
int y = e.getY();
if (x >= 50 && x <= 540 && y >= 0 && y <= 540) { // 棋盘范围以内
// 通过鼠标坐标计算出点击棋盘的行和列
int row = Math.round((y - 50) / 35f);
int col = Math.round((x - 50) / 35f);
if (b.addPiece(row, col, isBlack)) { // 落子成功
repaint();
try { // 向对方棋盘发送刚才落子的位置(行、列)以及黑子还是白子
dout.writeInt(row);
dout.writeInt(col);
dout.writeBoolean(isBlack);
} catch (IOException ex) {
ex.printStackTrace();
}
isWin = b.isWin(row, col, isBlack); // 判断是否获胜
if (!isWin) {
isYourTurn = !isYourTurn; // 尚未分出胜负则交换走棋方
} else {
JOptionPane.showMessageDialog(null, isBlack ? "黑棋胜"
: "白棋胜");
isWin = true;
repaint();
}
}
}
}
}
}
private JButton createGameButton, joinGameButton;
private JPanel boardPanel; // 绘制棋盘的面板
public MyFrame() {
this.setTitle("五子棋");
this.setSize(700, 600);
this.setResizable(false);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
boardPanel = new MyPanel(b);
boardPanel.setBounds(0, 0, 600, 600);
this.add(boardPanel);
boardPanel.addMouseListener(new MouseHandler()); // 向面板中添加鼠标监听器
this.setLayout(null);
createGameButton = new JButton("创建游戏");
createGameButton.setBounds(580, 100, 100, 35);
createGameButton.addActionListener(this);
joinGameButton = new JButton("加入游戏");
joinGameButton.setBounds(580, 160, 100, 35);
joinGameButton.addActionListener(this);
this.add(createGameButton);
this.add(joinGameButton);
// 创建一个线程监听对方走棋的消息
new Thread(new Runnable() {
public void run() {
while (true) { // 循环监听对方发送的走棋消息
while (isStart && !isYourTurn) {
try {
// 读取对方落子的行列以及是黑子还是白子
int row = din.readInt();
int col = din.readInt();
boolean isBlack = din.readBoolean();
b.addPiece(row, col, isBlack); // 向棋盘添加对方走的棋子
repaint();
if(b.isWin(row, col, isBlack)) {
JOptionPane.showMessageDialog(null, isBlack ? "黑棋胜"
: "白棋胜");
isWin = true;
repaint();
}
isYourTurn = !isYourTurn; // 收到对方棋子后交换走棋方
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
}).start();
}
public static void main(String[] args) {
new MyFrame().setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if (command.equals("创建游戏")) {
// 创建一个线程启动服务器套接字
new Thread(new Runnable() {
@Override
public void run() {
try {
ServerSocket server = new ServerSocket(5566);
Socket client = server.accept();
din = new DataInputStream(client.getInputStream());
dout = new DataOutputStream(client.getOutputStream());
isStart = true;
isBlack = true;
isYourTurn = true;
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
joinGameButton.setEnabled(false);
} else if (command.equals("加入游戏")) {
String ip = JOptionPane.showInputDialog(this, "请输入游戏主机IP地址或机器名: ");
if(ip != null && !ip.equals("")) {
try {
Socket client = new Socket(ip, 5566);
din = new DataInputStream(client.getInputStream());
dout = new DataOutputStream(client.getOutputStream());
isStart = true;
isYourTurn = false;
isBlack = false;
} catch (Exception ex) {
ex.printStackTrace();
}
repaint();
createGameButton.setEnabled(false);
}
}
}
}
下面是运行的效果: