求一个五子棋游戏的java程序

(1) 可以人机对战。
(2) 可以双人对战
(3) 可以设置单步超时(例如超时者视为放弃当歩操作的权利,或者程序随机替游戏者下一子)

import java.awt.*;
import java.awt.event.*;
class ChessPad extends Panel implements MouseListener,ActionListener {
int x = -1, y = -1, chessColor = 1;
Button button = new Button("重新开局");
TextField text_1 = new TextField("请下黑子棋"),
text_2 = new TextField();
ChessPad() {
setSize(440,440);
setLayout(null); setBackground(Color.orange);
addMouseListener(this);
add(button); button.setBounds(10,5,60,26);
button.addActionListener(this);
add(text_1);
text_1.setBounds(90,5,90,24);
add(text_2);
text_2.setBounds(290,5,90,24);
text_1.setEditable(false);
text_2.setEditable(false);
}

public void paint(Graphics g) {
for(int i = 40; i <= 380;i = i+20) {
g.drawLine(40,i,400,i);
}
g.drawLine(40,400,400,400);
for(int j = 40; j <= 380; j = j+20 ) {
g.drawLine(j,40,j,400);
}
g.drawLine(400,40,400,400);
g.fillOval(97,97,6,6);
g.fillOval(337,97,6,6);
g.fillOval(97,337,6,6);
g.fillOval(337,337,6,6);
g.fillOval(217,217,6,6);
}

public void mousePressed(MouseEvent e) {
if(e.getModifiers()==InputEvent.BUTTON1_MASK) {
x = (int)e.getX();
y = (int)e.getY();
ChessPoint_black chesspoint_black = new ChessPoint_black(this);
ChessPoint_white chesspoint_white = new ChessPoint_white(this);
int a = (x+10)/20,
b = (y+10)/20;
if( x/20<2 || y/20<2 || x/20>19 || y/20>19) {}
else {
if(chessColor==1) {
this.add(chesspoint_black);
chesspoint_black.setBounds(a*20-10,b*20-10,20,20);
chessColor=chessColor*(-1);
text_2.setText("请白棋下子");
text_1.setText("");
}
else if(chessColor==-1) {
this.add(chesspoint_white);
chesspoint_white.setBounds(a*20-10,b*20-10,20,20);
chessColor = chessColor*(-1);
text_1.setText("请黑棋下子");
text_2.setText("");
}
}
}
}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mouseClicked(MouseEvent e) {}
public void actionPerformed(ActionEvent e) {
this.removeAll(); chessColor = 1;
add(button); button.setBounds(10,5,60,26);
add(text_1); text_1.setBounds(90,5,90,24);
text_2.setText(""); text_1.setText("请黑棋下子");
add(text_2); text_2.setBounds(290,5,90,24);
}
}

class ChessPoint_black extends Canvas implements MouseListener {
ChessPad chesspad = null;
ChessPoint_black(ChessPad p) {
setSize(20,20); chesspad = p;
addMouseListener(this);
}
public void paint(Graphics g) {
g.setColor(Color.black);
g.fillOval(0,0,20,20);
}
public void mousePressed(MouseEvent e) {
if(e.getModifiers() == InputEvent.BUTTON3_MASK) {
chesspad.remove(this);
chesspad.chessColor=1;
chesspad.text_2.setText("");
chesspad.text_1.setText("请黑棋下子");
}
}
public void mouseReleased(MouseEvent e){}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mouseClicked(MouseEvent e) {
if(e.getClickCount()>=2) chesspad.remove(this);
}
}
class ChessPoint_white extends Canvas implements MouseListener {
ChessPad chesspad = null;
ChessPoint_white(ChessPad p) {
setSize(20,20);addMouseListener(this);
chesspad = p;
}
public void paint(Graphics g) {
g.setColor(Color.white);
g.fillOval(0,0,20,20);
}
public void mousePressed(MouseEvent e) {
if(e.getModifiers()==InputEvent.BUTTON3_MASK) {
chesspad.remove(this);
chesspad.chessColor=-1;
chesspad.text_2.setText("请白棋下子");
chesspad.text_1.setText("");
}
}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mouseClicked(MouseEvent e) {
if(e.getClickCount()>=2) chesspad.remove(this);
}
}

public class Chess extends Frame {
ChessPad chesspad = new ChessPad();
Chess() {
setVisible(true);
setLayout(null);
Label label = new Label("单击左键下棋子,双击吃棋子,用右键单击棋子悔棋",Label.CENTER);
add(label);
label.setBounds(70,55,440,26);
label.setBackground(Color.orange);
add(chesspad);
chesspad.setBounds(70,90,440,440);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
} );
pack(); setSize(600,550);
}

public static void main(String args[]) {
Chess chess = new Chess();
}
}
温馨提示:内容为网友见解,仅供参考
第1个回答  2009-12-31

这个要好几千行代码呢。

我曾经用 swing 写过一个简化的版本,有禁手,可以单机双人、人机,就是没有单步超时。实话说人机功能不是非常完善,电脑表现不稳定。

要的话100分源程序发给你。

本回答被网友采纳
第2个回答  2009-12-31
多少顿宵夜啊。。。

求五子棋代码(要java写的),有界面
import java.awt.Color;public class WuZhiqi extends Applet implements ActionListener, MouseListener,MouseMotionListener, ItemListener { int color = 0;\/\/ 旗子的颜色标识 0:白子 1:黑子 boolean isStart = false;\/\/ 游戏开始标志 int bodyArray[][] = new int[16][16]; \/\/ 设置棋盘棋子状...

java五子棋
chessclient.setTitle("Java五子棋客户端 "+"用户名:"+chessclient.chessClientName); } else if(recMessage.equals("\/reject")) { try { chessclient.chesspad.statusText.setText("不能加入游戏"); chessclient.controlpad.cancelGameButton.setEnabled(false); chessclient.controlpad.joinGameButton.setEnabled...

找一java游戏小程序,要交作业的
g.setColor(Color.BLACK);g.fillOval((i + 1) * 30 - 8, (j + 1) * 30 - 8, 16, 16);}

求五子棋Java代码旁边就注释 让我看的清楚明白一些,要详细的解释和思路...
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 intGa...

java程序设计,五子棋判断输赢
int Map[MAX_LINE][MAX_ROW];int CheckWin() \/\/返回0表示都没赢,返回1表示白棋赢,返回2表示黑棋赢 { int i,j,k;int tmp;\/* 判断所有横行 *\/ for(i=0;i<MAX_LINE;i++){ for(j=0;j<MAX_ROW-5;j++){ tmp=Map[i][j];if( tmp != 0){ for(k=1;k<5;k++)if(Map[i...

关于java五子棋的代码
\/\/棋子枚举类public enum Chessman{ \/\/枚举类第一行必须列出所有实例,这两个表示创建两个棋子视力,它们的棋子成员变量分别是●和○ BLACK_CHESS("●"), WHITE_CHESS("○"); \/\/成员变量,决定这个棋子的类型 private String chessman; \/\/构造器,枚举类的构造器只能提供给自己的实例使用,外部不能...

java 五子棋的源代码 以及类图 用例图 时序图
readLine())!=null){ String[] posStrArr=inputstr.split(",");int xpos=Integer.parseInt(posStrArr[0]);int ypos=Integer.parseInt(posStrArr[1]);gb.board[xpos-1][ypos-1]="●";gb.printboard();System.out.println("请输入您下棋的坐标,应为X,Y的格式:");} } } ...

求一个简单的JAVA五子棋代码!! 网上复制的别来了!
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...

下了个JAVA五子棋代码不会看 求注释
public void paint(Graphics g) \/\/重绘和applet程序装载的时候会调用这个绘制的过程 { draw_qipan(g); \/\/画棋盘 } public void set_Qizi(int x,int y) \/\/落子 { if (intGame_Start==0) \/\/判断游戏未开始 { return; \/\/走棋无效,返回 } if (intGame_Body[x\/20][y\/20]!=0) \/\/...

跪求JAVA五子棋源代码
很sb的电脑五子棋:import java.io.*;import java.util.*;public class Gobang { \/\/ 定义一个二维数组来充当棋盘 private String[][] board;\/\/ 定义棋盘的大小 private static int BOARD_SIZE = 15;public void initBoard() { \/\/ 初始化棋盘数组 board = new String[BOARD_SIZE][BOARD_SIZE]...

相似回答
大家正在搜