JAVA编写一个简单的跳棋游戏

Python033

JAVA编写一个简单的跳棋游戏,第1张

import java.awt.*

import javax.swing.JFrame

public class JumpChess extends JFrame{

public JumpChess(){

setSize(800,800) setVisible(true)

setBackground(Color.pink)

}

public void paint(Graphics g){ g.setColor(Color.gray) int x=290 int y=140 int x1=330

for(int row=1row<13row++){ g.drawLine(x,y,x1,y)

x=x-20

x1=x1+20

y=y+30

System.out.println() }

int x2=110 int y2=230 int x3=90 int y3=260

for(int row=1row<13row++){ g.drawLine(x2,y2,x3,y3) x2=x2+40 x3=x3+20 y3=y3+30

System.out.println()

}

int x4=510 int y4=230 int x5=530 int y5=260

for(int row=1row<13row++){ g.drawLine(x4,y4,x5,y5) x4=x4-40 x5=x5-20 y5=y5+30

System.out.println()

}

int x6=510

先导入三个按钮图片

正常显示的图片"Begin1.jpg"

鼠标移动到按钮上时显示的图片"Begin2.jpg"

按下鼠标时显示的图片"Begin1.jpg"

final ImageLoader imageBegin1 = new ImageLoader(sShell.getDisplay(), "Begin1.jpg")

final ImageLoader imageBegin2 = new ImageLoader(sShell.getDisplay(), "Begin2.jpg")

final ImageLoader imageBegin3 = new ImageLoader(sShell.getDisplay(),"Begin1.jpg")

创建按钮

lblBegin = new Label(parent, SWT.NO_BACKGROUND)

lblBegin.setImage(imageBegin1.getImage())

lblBegin.setBounds(70, 40, 75, 38)

为按钮各事件写入代码

lblBegin.addMouseTrackListener(new MouseTrackAdapter() {

public void mouseEnter(MouseEvent e) { //鼠标移动到按钮上方

lblBegin.setImage(imageBegin2.getImage())

}

public void mouseExit(MouseEvent e) { //鼠标从按钮上方移开

lblBegin.setImage(imageBegin1.getImage())

}

})

lblBegin.addMouseListener(new MouseAdapter() {

public void mouseDown(MouseEvent e) {

if (e.button == 1) {//按下鼠标左键

lblBegin.setImage(imageBegin3.getImage()) //在这里写入单击事件代码

}

}

public void mouseUp(MouseEvent e) {

if (e.button == 1) {//释放鼠标左键

lblBegin.setImage(imageBegin2.getImage())

}

}

})

如图所示,当X坐标为1时,Y的坐标只能为5,当X坐标为2时,Y的坐标可以5或6。于是我们建立一个数组:

final static private int[][] pos = {

{5,5}, //X坐标为1,Y的上限是5,下限是5

{5,6}, //X坐标为2,Y的上限是5,下限是6

{5,7}, //X坐标为3,Y的上限是5,下限是7

{5,8}, //X坐标为4,Y的上限是5,下限是8

{1,13}, //X坐标为5,Y的上限是1,下限是13

{2,13}, //6

{3,13}, //7

{4,13}, //8

{5,13}, //9

{5,14}, //10

{5,15}, //11

{5,16}, //12

{5,17}, //13

{10,13}, //14

{11,13}, //15

{12,13}, //16

{13,13}, //17

}

在Position类中IsLegalPosition函数可以确定一个坐标是否合法

public static boolean IsLegalPosition(int x, int y) {

if ((x <1) || (x >17)) {

return false

}

if ((y <pos[x - 1][0]) || (y >pos[x - 1][1])) {

return false

}

return true

}

3. 棋盘类(ChessBoard)中棋子和坐标的索引关系

棋盘中所有Chess集合

private Chess[] chesses = null//所有棋子对象都保存这个数组当中

下面函数可以根据索引号返回棋子对象

public Chess getChess(int index) {

return chesses[index]

}

棋子和坐标的对应关系

private Position[] chessesPosition = null//所有棋子坐标都保存在这个数组当中

下面函数可以根据棋子对象或棋子索引号返回坐标

public Position getPosition(Chess chess) {

return chessesPosition[chess.getindex()]

}

public Position getPosition(int index) {

return chessesPosition[index]

}

坐标和棋子的对应关系

private Chess[][] chessesIndex = new Chess[17][17]//数组保存了17*17个棋子对象指针

下面函数可以根据棋子坐标返回该位置上的棋子,如果没有棋子返回Null

public Chess getChess(Position position) {

if (position == null){

return null

}

return chessesIndex[position.getx() - 1][position.gety() - 1]

}

这个其实和五子棋 象棋的寻址区别不大,都是最佳算法

其实主要看搜索深度的,我以前自己做过五子棋和象棋的java版本

有很多算法实现 你用alpha beta或者置换表都可以

验证做好的是最忧搜索+置换表+历史启发,现在不写那些了 代码不好找 你去网上搜索五子棋人工智能就应该有