标题:我自己用JAVA编的一个五子棋游戏(有简单的AI)!
只看楼主
hange2007
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2007-6-26
 问题点数:0 回复次数:4 
我自己用JAVA编的一个五子棋游戏(有简单的AI)!

import java.awt.*;
import java.util.*;
import javax.swing.*;
import java.awt.geom.*;
import java.awt.event.*;

class PaintPanel extends JPanel{

JDialog labHeiQi = new JDialog(new JFrame() ,"胜利",false);

JLabel jbm1=new JLabel("★★★★★★★★★★★★★★★★★");
JLabel jb1=new JLabel("★★★★★恭喜,你赢了!!★★★★★");

JDialog labHongQi = new JDialog(new JFrame(),"失败",false);

JLabel jbm2=new JLabel("★★★★★★★★★★★★★★★★");
JLabel jb2=new JLabel("★★★你输了,请继续努力!★★★");

public int [][]map;
public int[]cp;
public int isSuccess=0;

ArrayList Items = new ArrayList();

public PaintPanel() {
setLayout(new BorderLayout());
ButtonPanel buttonPanel = new ButtonPanel();
add(buttonPanel, BorderLayout.SOUTH);
addMouseListener(new MouseHandler());
map=new int [19][19];
for(int i=0;i<19;i++){
for(int j=0;j<19;j++){
map[i][j]=-1;
}
}
cp=new int [2];
cp[0]=-1;
cp[1]=-1;
}
public void lab(int k){

labHeiQi.setLayout(new BorderLayout());
labHeiQi.add("Center",jb1);
labHeiQi.add("North",jbm1);
labHeiQi.pack();

labHongQi.setLayout(new BorderLayout());
labHongQi.add("Center",jb2);
labHongQi.add("North",jbm2);
labHongQi.pack();

if(k==1){

labHeiQi.setVisible(true);
}
else{

if(k==2)
labHongQi.setVisible(true);
}
}
public void paintComponent(Graphics g) {
int startX = 50;
int startY = 50;
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
for(int i=0; i<19; i++) {
g2.draw(new Line2D.Double(startX, startY+20*i, startX+360, startY+20*i));
g2.draw(new Line2D.Double(startX+20*i, startY, startX+20*i, startY+360));
}
for(int i=0; i<Items.size(); i++) {
int k=(i+2)%2;
if(k==0){
g2.setColor(Color.BLACK);
if(isSuccess==1){
lab(1);
}
}
else{
g2.setColor(Color.RED);
if(isSuccess==2){
lab(2);
}
}
g2.fill((Ellipse2D)Items.get(i));
}
}
void myRepaint() {
repaint();
}
void reset(){
isSuccess=0;
for(int i=0;i<19;i++){
for(int j=0;j<19;j++){
map[i][j]=-1;
}
}
}

class MouseHandler extends MouseAdapter {

public void mousePressed(MouseEvent event) {
int x = event.getX();
int y = event.getY();
x=(x-50)/20;
y=(y-50)/20;
if(chessExist(x,y)&&isSuccess==0){
paintItem(x, y);
isSuccess=Success(x,y,1);
map[x][y]=1;
repaint();
if(isSuccess==0){
computer();
int q=cp[0];
int r=cp[1];
paintItem(q,r);
isSuccess=Success(q,r,2);
map[q][r]=2;
repaint();
}
}
}
public void paintItem(int x, int y) {
x=x*20+50;
y=y*20+50;
if(x >30 && x <430 && y >30 && y <430) {
int X = x / 20;
int Y = y / 20;
int centerX = X*20 + 10;
int centerY = Y*20 + 10;
Ellipse2D ellipse = new Ellipse2D.Double();
ellipse.setFrameFromCenter(centerX, centerY, centerX+8, centerY+8);
Items.add(ellipse);
}
}
public void computer(){ //计算机走棋
int maxblack,maxHong,maxtemp,max=0;
for(int i=1;i<18;i++){
for(int j=1;j<18;j++){
if(chessExist(i,j)){//判断是否可以下子
maxblack=CheckMax(i,j,1);
maxHong=CheckMax(i,j,2);
maxtemp=Math.max(maxblack,maxHong);
if(maxtemp>max){
max=maxtemp;
cp[0]=i;
cp[1]=j;
}
}
}
}
}
public int CheckMax(int x,int y,int person){//判断某一点四个方向上棋子的最大值
int maxnum=0;
int up,down,left,right;
left=right=x;
up=down=y;

do{
up++;
}
while(up>=0&&map[x][up]==person);

do{
down--;
}
while(down<19&&map[x][down]==person);

if((up-down)>maxnum){
maxnum=up-down;
}

do{
right++;
}
while(right<19&&map[align=right][y]==person);

do{
left--;
}
while(left>=0&&map[align=left][y]==person);

if((right-left)>maxnum){
maxnum=right-left;
}

left=right=x;
down=up=y;
do{
right++;
down++;
}
while(right<19&&down<19&&map[align=right][down]==person);

do{
left--;
up--;
}
while(left>=0&&up>=0&&map[align=left][up]==person);

if((right-left)>maxnum){
maxnum=right-left;
}

left=right=x;
down=up=y;
do{
right++;
down--;
}
while(right<19&&down>=0&&map[align=right][down]==person);

do{
left--;
up++;
}
while(left>=0&&up<19&&map[align=left][up]==person);

if((right-left)>maxnum){
maxnum=right-left;
}
return maxnum;
}


public int Success(int x,int y,int person){ //判断胜负
int up,down,left,right;
left=right=x;
up=down=y;
do{
up++;
}
while(up>=0&&map[x][up]==person);

do{
down--;
}
while(down<19&&map[x][down]==person);

if((up-down)>5){
return person;
}
else{
do{
right++;
}
while(right<19&&map[align=right][y]==person);

do{
left--;
}
while(left>=0&&map[align=left][y]==person);

if((right-left)>5){
return person;
}
else{
left=right=x;
down=up=y;
do{
right++;
down++;
}
while(right<19&&down<19&&map[align=right][down]==person);

do{
left--;
up--;
}
while(left>=0&&up>=0&&map[align=left][up]==person);

if((right-left)>5){
return person;
}
else{
left=right=x;
down=up=y;
do{
right++;
down--;
}
while(right<19&&down>=0&&map[align=right][down]==person);

do{
left--;
up++;
}
while(left>=0&&up<19&&map[align=left][up]==person);

if((right-left)>5){
return person;
}
else{
return 0;
}
}
}
}
}
public boolean chessExist(int i,int j){//判断该格是否有棋子
if(map[i][j]==-1)
return true;
else
return false;
}
}
class ButtonPanel extends JPanel{
public ButtonPanel() {
JButton reset = new JButton("新游戏");
add(reset);
JButton quit = new JButton("退出");
add(quit);

ResetEvent listenerR = new ResetEvent();
reset.addMouseListener(listenerR);

QuitEvent listenerQ = new QuitEvent();
quit.addMouseListener(listenerQ);
}
class QuitEvent extends MouseAdapter {//离开游戏
public void mouseClicked(MouseEvent event) {
System.exit(1);
}
}
class ResetEvent extends MouseAdapter { //重新开始游戏
public void mouseClicked(MouseEvent event) {
Items.clear();
myRepaint();
reset();
}
}
}
}
class GameFrame extends JFrame {
public GameFrame() {
setTitle("五子棋");
setSize(500, 600);
setResizable(false);
PaintPanel panel = new PaintPanel();
getContentPane().add(panel);
}
}
public class Game {
public static void main(String[] args) {
GameFrame frame = new GameFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}

由于棋子的情况采用数组存储,坐标都经过了处理,这样也导致棋盘的最外一圈不能下棋,电脑还可以!大家有兴趣的可以看看,这个是我数据结构做的课程设计!

搜索更多相关主题的帖子: JAVA 五子棋 import JLabel 
2007-07-12 23:48
想你的天空
Rank: 2
等 级:新手上路
威 望:5
帖 子:610
专家分:0
注 册:2004-12-30
得分:0 
搂住辛苦的劳动, 还是要顶的

2007-07-13 00:35
神vLinux飘飄
Rank: 1
等 级:新手上路
威 望:1
帖 子:309
专家分:0
注 册:2007-1-27
得分:0 

顶一下,转到j2se区吧


泛出微微的蓝色的光,像有生命般涌动着,闪烁着,平滑而优美,达到了机械和美学结合的最高境界
2007-07-13 08:17
Gramary
Rank: 4
等 级:贵宾
威 望:13
帖 子:769
专家分:0
注 册:2007-7-9
得分:0 
楼主``厉害`
不过``我帮你测了一下`
好多的异常啊` 加油``

学编程不能急,要慢慢来。
2007-07-14 19:12
dxq530610286
Rank: 2
等 级:论坛游民
帖 子:54
专家分:10
注 册:2007-10-1
得分:0 
能不能注释一下吗
2008-03-21 14:32



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-154979-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019409 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved