Java跳棋算法问题这个其实和五子棋 象棋的寻址区别不大,都是最佳算法
其实主要看搜索深度的,我以前自己做过五子棋和象棋的java版本
有很多算法实现 你用alpha beta或者置换表都可以
验证做好的是最忧搜索 置换表 历史启发,现在不写那些了 代码不好找 你去网上搜索五子棋人工智能就应该有
急求俄罗斯方块等小游戏的源代码俄罗斯方块——java源代码提供
import java.awt.*;
import java.awt.event.*;
//俄罗斯方块类
public class ERS_Block extends Frame{
public static boolean isPlay=false;
public static int level=1,score=0;
public static TextField scoreField,levelField;
public static MyTimer timer;
GameCanvas gameScr;
public static void main(String[] argus){
ERS_Block ers = new ERS_Block("俄罗斯方块游戏 V1.0 Author:Vincent");
WindowListener win_listener = new WinListener();
ers.addWindowListener(win_listener);
}
//俄罗斯方块类的构造方法
ERS_Block(String title){
super(title);
setSize(600,480);
setLayout(new GridLayout(1,2));
gameScr = new GameCanvas();
gameScr.addKeyListener(gameScr);
timer = new MyTimer(gameScr);
timer.setDaemon(true);
timer.start();
timer.suspend();
add(gameScr);
Panel rightScr = new Panel();
rightScr.setLayout(new GridLayout(2,1,0,30));
rightScr.setSize(120,500);
add(rightScr);
//右边信息窗体的布局
MyPanel infoScr = new MyPanel();
infoScr.setLayout(new GridLayout(4,1,0,5));
infoScr.setSize(120,300);
rightScr.add(infoScr);
//定义标签和初始值
Label scorep = new Label("分数:",Label.LEFT);
Label levelp = new Label("级数:",Label.LEFT);
scoreField = new TextField(8);
levelField = new TextField(8);
scoreField.setEditable(false);
levelField.setEditable(false);
infoScr.add(scorep);
infoScr.add(scoreField);
infoScr.add(levelp);
infoScr.add(levelField);
scorep.setSize(new Dimension(20,60));
scoreField.setSize(new Dimension(20,60));
levelp.setSize(new Dimension(20,60));
levelField.setSize(new Dimension(20,60));
scoreField.setText("0");
levelField.setText("1");
//右边控制按钮窗体的布局
MyPanel controlScr = new MyPanel();
controlScr.setLayout(new GridLayout(5,1,0,5));
rightScr.add(controlScr);
//定义按钮play
Button play_b = new Button("开始游戏");
play_b.setSize(new Dimension(50,200));
play_b.addActionListener(new Command(Command.button_play,gameScr));
//定义按钮Level UP
Button level_up_b = new Button("提高级数");
level_up_b.setSize(new Dimension(50,200));
level_up_b.addActionListener(new Command(Command.button_levelup,gameScr));
//定义按钮Level Down
Button level_down_b =new Button("降低级数");
level_down_b.setSize(new Dimension(50,200));
level_down_b.addActionListener(new Command(Command.button_leveldown,gameScr));
//定义按钮Level Pause
Button pause_b =new Button("游戏暂停");
pause_b.setSize(new Dimension(50,200));
pause_b.addActionListener(new Command(Command.button_pause,gameScr));
//定义按钮Quit
Button quit_b = new Button("退出游戏");
quit_b.setSize(new Dimension(50,200));
quit_b.addActionListener(new Command(Command.button_quit,gameScr));
controlScr.add(play_b);
controlScr.add(level_up_b);
controlScr.add(level_down_b);
controlScr.add(pause_b);
controlScr.add(quit_b);
setVisible(true);
gameScr.requestFocus();
}
}
//重写MyPanel类 , 使Panel的四周留空间
class MyPanel extends Panel{
public Insets getInsets(){
return new Insets(30,50,30,50);
}
}
//游戏画布类
class GameCanvas extends Canvas implements KeyListener{
final int unitSize = 30; //小方块边长
int rowNum; //正方格的行数
int columnNum; //正方格的列数
int maxAllowRowNum; //允许有多少行未削
int blockInitRow; //新出现块的起始行坐标
int blockInitCol; //新出现块的起始列坐标
int [][] scrArr; //屏幕数组
Block b; //对方快的引用
//画布类的构造方法
GameCanvas(){
rowNum = 15;
columnNum = 10;
maxAllowRowNum = rowNum - 2;
b = new Block(this);
blockInitRow = rowNum - 1;
blockInitCol = columnNum/2 - 2;
scrArr = new int [32][32];
}
//初始化屏幕,并将屏幕数组清零的方法
void initScr(){
for(int i=0;irowNum;i)
for (int j=0; jcolumnNum;j)
scrArr[j]=0;
b.reset();
repaint();
}
//重新刷新画布方法
public void paint(Graphics g){
for(int i = 0; irowNum; i)
for(int j = 0; jcolumnNum; j)
drawUnit(i,j,scrArr[j]);
}
//画方块的方法
public void drawUnit(int row,int col,int type){
scrArr[row][col] = type;
Graphics g = getGraphics();
tch(type){ //表示画方快的方法
case 0: g.setColor(Color.black);break; //以背景为颜色画
case 1: g.setColor(Color.blue);break; //画正在下落的方块
case 2: g.setColor(Color.magenta);break; //画已经落下的方法
}
g.fill3DRect(col*unitSize,getSize().height-(row 1)*unitSize,unitSize,unitSize,true);
g.dispose();
}
public Block getBlock(){
return b; //返回block实例的引用
}
//返回屏幕数组中(row,col)位置的属性值
public int getScrArrXY(int row,int col){
if (row0 || row = rowNum || col0 || col = columnNum)
return(-1);
else
return(scrArr[row][col]);
}
//返回新块的初始行坐标方法
public int getInitRow(){
return(blockInitRow); //返回新块的初始行坐标
}
//返回新块的初始列坐标方法
public int getInitCol(){
return(blockInitCol); //返回新块的初始列坐标
}
//满行删除方法
void deleteFullLine(){
int full_line_num = 0;
int k = 0;
for (int i=0;irowNum;i){
boolean isfull = true;
L1:for(int j=0;jcolumnNum;j)
if(scrArr[j] == 0){
k;
isfull = false;
break L1;
}
if(isfull) full_line_num;
if(k!=0k-1!=i!isfull)
for(int j = 0; jcolumnNum; j){
if (scrArr[j] == 0)
drawUnit(k-1,j,0);
else
【java编写跳棋源代码 java编写跳棋源代码怎么写】drawUnit(k-1,j,2);
scrArr[k-1][j] = scrArr[j];
}
}
for(int i = k-1 ;irowNum; i){
for(int j = 0; jcolumnNum; j){
drawUnit(i,j,0);
scrArr[j]=0;
}
}
ERS_Block.score= full_line_num;
ERS_Block.scoreField.setText("" ERS_Block.score);
}
//判断游戏是否结束方法
boolean isGameEnd(){
for (int col = 0 ; col columnNum; col){
if(scrArr[maxAllowRowNum][col] !=0)
return true;
}
return false;
}
public void keyTyped(KeyEvent e){
}
public void keyReleased(KeyEvent e){
}
//处理键盘输入的方法
public void keyPressed(KeyEvent e){
if(!ERS_Block.isPlay)
return;
tch(e.getKeyCode()){
case KeyEvent.VK_DOWN:b.fallDown();break;
case KeyEvent.VK_LEFT:b.leftMove();break;
case KeyEvent.VK_RIGHT:b.rightMove();break;
case KeyEvent.VK_SPACE:b.leftTurn();break;
}
}
}
//处理控制类
class Command implements ActionListener{
static final int button_play = 1; //给按钮分配编号
static final int button_levelup = 2;
static final int button_leveldown = 3;
static final int button_quit = 4;
static final int button_pause = 5;
static boolean pause_resume = true;
int curButton; //当前按钮
GameCanvas scr;
//控制按钮类的构造方法
Command(int button,GameCanvas scr){
curButton = button;
this.scr=scr;
}
//按钮执行方法
public void actionPerformed (ActionEvent e){
tch(curButton){
case button_play:if(!ERS_Block.isPlay){
scr.initScr();
ERS_Block.isPlay = true;
ERS_Block.score = 0;
ERS_Block.scoreField.setText("0");
ERS_Block.timer.resume();
}
scr.requestFocus();
break;
case button_levelup:if(ERS_Block.level10){
ERS_Block.level;
ERS_Block.levelField.setText("" ERS_Block.level);
ERS_Block.score = 0;
ERS_Block.scoreField.setText("" ERS_Block.score);
}
scr.requestFocus();
break;
case button_leveldown:if(ERS_Block.level1){
ERS_Block.level--;
ERS_Block.levelField.setText("" ERS_Block.level);
ERS_Block.score = 0;
ERS_Block.scoreField.setText("" ERS_Block.score);
}
scr.requestFocus();
break;
case button_pause:if(pause_resume){
ERS_Block.timer.suspend();
pause_resume = false;
}else{
ERS_Block.timer.resume();
pause_resume = true;
}
scr.requestFocus();
break;
case button_quit:System.exit(0);
}
}
}
//方块类
class Block {
static int[][] pattern = {
{0x0f00,0x4444,0x0f00,0x4444},//用十六进至表示,本行表示长条四种状态
{0x04e0,0x0464,0x00e4,0x04c4},
{0x4620,0x6c00,0x4620,0x6c00},
{0x2640,0xc600,0x2640,0xc600},
{0x6220,0x1700,0x2230,0x0740},
{0x6440,0x0e20,0x44c0,0x8e00},
{0x0660,0x0660,0x0660,0x0660}
};
int blockType; //块的模式号(0-6)
int turnState; //块的翻转状态(0-3)
int blockState; //快的下落状态
int row,col; //块在画布上的坐标
GameCanvas scr;
//块类的构造方法
Block(GameCanvas scr){
this.scr = scr;
blockType = (int)(Math.random() * 1000)%7;
turnState = (int)(Math.random() * 1000)%4;
blockState = 1;
row = scr.getInitRow();
col = scr.getInitCol();
}
//重新初始化块,并显示新块
public void reset(){
blockType = (int)(Math.random() * 1000)%7;
turnState = (int)(Math.random() * 1000)%4;
blockState = 1;
row = scr.getInitRow();
col = scr.getInitCol();
dispBlock(1);
}
//实现“块”翻转的方法
public void leftTurn(){
if(assertValid(blockType,(turnState1)%4,row,col)){
dispBlock(0);
turnState = (turnState1)%4;
dispBlock(1);
}
}
//实现“块”的左移的方法
public void leftMove(){
if(assertValid(blockType,turnState,row,col-1)){
dispBlock(0);
col--;
dispBlock(1);
}
}
//实现块的右移
public void rightMove(){
if(assertValid(blockType,turnState,row,col 1)){
dispBlock(0);
col;
dispBlock(1);
}
}
//实现块落下的操作的方法
public boolean fallDown(){
if(blockState == 2)
return(false);
if(assertValid(blockType,turnState,row-1,col)){
dispBlock(0);
row--;
dispBlock(1);
return(true);
}else{
blockState = 2;
dispBlock(2);
return(false);
}
}
//判断是否正确的方法
boolean assertValid(int t,int s,int row,int col){
int k = 0x8000;
for(int i = 0; i4; i){
for(int j = 0; j4; j){
if((int)(pattern[t][s]k) != 0){
int temp = scr.getScrArrXY(row-i,col j);
if (temp0||temp==2)
return false;
}
k = k1;
}
}
return true;
}
//同步显示的方法
public synchronized void dispBlock(int s){
int k = 0x8000;
for (int i = 0; i4; i){
for(int j = 0; j4; j){
if(((int)pattern[blockType][turnState]k) != 0){
scr.drawUnit(row-i,col j,s);
}
k=k1;
}
}
}
}
//定时线程
class MyTimer extends Thread{
GameCanvas scr;
public MyTimer(GameCanvas scr){
this.scr = scr;
}
public void run(){
while(true){
try{
sleep((10-ERS_Block.level1)*100);
}
catch(InterruptedException e){}
if(!scr.getBlock().fallDown()){
scr.deleteFullLine();
if(scr.isGameEnd()){
ERS_Block.isPlay = false;
suspend();
}else
scr.getBlock().reset();
}
}
}
}
class WinListener extends WindowAdapter{
public void windowClosing (WindowEvent l){
System.exit(0);
}
}
Java 跳棋的程序 急~~先导入三个按钮图片?
正常显示的图片"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());
}
}
});
如图所示java编写跳棋源代码,当X坐标为1时java编写跳棋源代码 , Y的坐标只能为5,当X坐标为2时,Y的坐标可以5或6 。于是java编写跳棋源代码我们建立一个数组java编写跳棋源代码:
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 ((x1) || (x17)) {
return false;
}
if ((ypos[x - 1][0]) || (ypos[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];
}
求跳棋游戏VB代码VB跳棋代码:
窗体代码:
Dim ChessBoard(-2 To 10, -2 To 10) As Byte ''棋盘(8竖*8棋)
Dim x(10) As Integer, y(10) As Integer''搜索的每种走法
Dim x1(10) As Integer, y1(10) As Integer''搜索的每种走法的可吃子坐标
Dim BestLocate As CHESSER
Dim CurrentPlayer As Byte''当前玩家
Dim CurrentStep As Integer''当前步
Dim 人机模式 As Boolean
Dim cSel As Byte''玩家选择了哪个棋子
Dim tTemp As Boolean
Const MAXDOWNPOINT = 7
Rem 如果Cer为1(黑方),则返回2(红方),否则返加1(黑方)
Public Function NextCer(ByVal Cer As Byte) As Byte
NextCer = 1
If Cer = 1 Then NextCer = 2
End Function
Rem 棋盘
Private Sub Initial()
Dim i As Integer, j As Integer
For i = 1 To 8: For j = 1 To 8: ChessBoard(i, j) = 0: Next j: Next i
ChessBoard(1, 2) = 201
ChessBoard(1, 4) = 201
ChessBoard(1, 6) = 201
ChessBoard(1, 8) = 201
ChessBoard(2, 1) = 201
ChessBoard(2, 3) = 201
ChessBoard(2, 5) = 201
ChessBoard(2, 7) = 201
ChessBoard(3, 2) = 201
ChessBoard(3, 4) = 201
ChessBoard(3, 6) = 201
ChessBoard(3, 8) = 201
ChessBoard(6, 1) = 101
ChessBoard(6, 3) = 101
ChessBoard(6, 5) = 101
ChessBoard(6, 7) = 101
ChessBoard(7, 2) = 101
ChessBoard(7, 4) = 101
ChessBoard(7, 6) = 101
ChessBoard(7, 8) = 101
ChessBoard(8, 1) = 101
ChessBoard(8, 3) = 101
ChessBoard(8, 5) = 101
ChessBoard(8, 7) = 101
End Sub
Rem 反显示(将屏幕显示的内容存入ChessBoard数组)
Private Sub ReDisplay()
Dim i As Integer, j As Integer, k As Integer
k = 0
For i = 1 To 8
For j = 1 To 8
If cbText(k).Text = "" Then ChessBoard(i, j) = 0
If cbText(k).Text = "101" Then ChessBoard(i, j) = 101
If cbText(k).Text = "201" Then ChessBoard(i, j) = 201
If cbText(k).Text = "102" Then ChessBoard(i, j) = 102
If cbText(k).Text = "202" Then ChessBoard(i, j) = 202
k = k1
Next j
Next i
End Sub
Rem 显示(将ChessBoard数组的内容显示到屏幕后)
Private Sub Display()
Dim i As Integer, j As Integer, k As Integer
k = 0
For i = 1 To 8
For j = 1 To 8
If ChessBoard(i, j) = 0 Then
cbText(k).Text = ""
Else
cbText(k).Text = ChessBoard(i, j)
End If
k = k1
Next j
Next i
Call 胜负判断
End Sub
Rem 胜负判断
Private Sub 胜负判断()
Dim i As Integer, j As Integer
Dim a As Integer, b As Integer
a = 0: b = 0
For i = 1 To 8
For j = 1 To 8
If Int(ChessBoard(i, j) / 100) = 1 Then a = a1''计算玩家的棋子数
If Int(ChessBoard(i, j) / 100) = 2 Then b = b1''计算电脑的棋子数
Next j
Next i
If a = 0 Then Call MsgBox("我赢了!", vbOKOnly32, "提示:"): Exit Sub
If b = 0 Then Call MsgBox("我认输了!", vbOKOnly32, "提示:"): Exit Sub
End Sub
Rem 返回估值
Private Function CurrentValue(Cer As Byte) As Integer
Dim i As Integer, j As Integer
CurrentValue = https://www.04ip.com/post/0
For i = 1 To 8
For j = 1 To 8
If Int(ChessBoard(i, j) / 100) = Cer Then _
CurrentValue = https://www.04ip.com/post/CurrentValueChessBoard(i, j) Mod 100 * 100100''是我方的棋子,棋子为1加100分,棋子为2加200分
If Int(ChessBoard(i, j) / 100) = NextCer(Cer) Then _
CurrentValue = https://www.04ip.com/post/CurrentValue - (ChessBoard(i, j) Mod 100 * 100100)''对方的棋子,棋子为1减100分,棋子为2减200分
Next j
Next i
End Function
Rem 如果Cer方i,j的棋子还可以吃子则返回True
Private Function IsLine(Cer As Byte, i As Byte, j As Byte) As Boolean
Dim x As Byte, y As Byte, x1 As Byte, y1 As Byte
IsLine = False
''开始搜索棋盘
''如果是Cer方的棋子
If Int(ChessBoard(i, j) / 100) = Cer Then
''吃子式走法1:即如果基本走法的位置有对方的棋子则可以跳吃(走法限制:Cer为1或棋子为加强棋才可走)
If Int(ChessBoard(i - 1, j - 1) / 100) = NextCer(Cer) And (Cer = 1 Or ChessBoard(i, j) Mod 100 = 2) Then
x = (i - 1) - 1''目标坐标
y = (j - 1) - 1
x1 = i - 1''吃子坐标
y1 = j - 1
If x0 And y0 And x9 And y9 And ChessBoard(x, y) = 0 Then IsLine = True'有可吃子,返回True
End If
''吃子式走法2
If Int(ChessBoard(i - 1, j1) / 100) = NextCer(Cer) And (Cer = 1 Or ChessBoard(i, j) Mod 100 = 2) Then
x = (i - 1) - 1
y = (j1)1
x1 = i - 1
y1 = j1
If x0 And y0 And x9 And y9 And ChessBoard(x, y) = 0 Then IsLine = True'有可吃子 , 返回True
End If
''吃子式走法3
If Int(ChessBoard(i1, j - 1) / 100) = NextCer(Cer) And (Cer = 2 Or ChessBoard(i, j) Mod 100 = 2) Then
x = (i1)1
y = (j - 1) - 1
x1 = i1
y1 = j - 1
If x0 And y0 And x9 And y9 And ChessBoard(x, y) = 0 Then IsLine = True'有可吃子,返回True
End If
''吃子式走法4
If Int(ChessBoard(i1, j1) / 100) = NextCer(Cer) And (Cer = 2 Or ChessBoard(i, j) Mod 100 = 2) Then
x = (i1)1
y = (j1)1
x1 = i1
y1 = j1
If x0 And y0 And x9 And y9 And ChessBoard(x, y) = 0 Then IsLine = True'有可吃子,返回True
End If
End If
End Function
Rem 如果Cer方的棋子还可以吃子则返回True
Private Function IsLine2(Cer As Byte) As Boolean
Dim x As Byte, y As Byte, x1 As Byte, y1 As Byte
Dim i As Integer, j As Integer
IsLine2 = False
''开始搜索棋盘
For i = 1 To 8
For j = 1 To 8
''如果是Cer方的棋子
If Int(ChessBoard(i, j) / 100) = Cer Then
''吃子式走法1:即如果基本走法的位置有对方的棋子则可以跳吃(走法限制:Cer为1或棋子为加强棋才可走)
If Int(ChessBoard(i - 1, j - 1) / 100) = NextCer(Cer) And (Cer = 1 Or ChessBoard(i, j) Mod 100 = 2) Then
x = (i - 1) - 1''目标坐标
y = (j - 1) - 1
x1 = i - 1''吃子坐标
y1 = j - 1
If x0 And y0 And x9 And y9 And ChessBoard(x, y) = 0 Then IsLine2 = True'有可吃子,返回True
End If
''吃子式走法2
If Int(ChessBoard(i - 1, j1) / 100) = NextCer(Cer) And (Cer = 1 Or ChessBoard(i, j) Mod 100 = 2) Then
x = (i - 1) - 1
y = (j1)1
x1 = i - 1
y1 = j1
If x0 And y0 And x9 And y9 And ChessBoard(x, y) = 0 Then IsLine2 = True'有可吃子,返回True
End If
''吃子式走法3
If Int(ChessBoard(i1, j - 1) / 100) = NextCer(Cer) And (Cer = 2 Or ChessBoard(i, j) Mod 100 = 2) Then
x = (i1)1
y = (j - 1) - 1
x1 = i1
y1 = j - 1
If x0 And y0 And x9 And y9 And ChessBoard(x, y) = 0 Then IsLine2 = True'有可吃子,返回True
End If
''吃子式走法4
If Int(ChessBoard(i1, j1) / 100) = NextCer(Cer) And (Cer = 2 Or ChessBoard(i, j) Mod 100 = 2) Then
x = (i1)1
y = (j1)1
x1 = i1
y1 = j1
If x0 And y0 And x9 And y9 And ChessBoard(x, y) = 0 Then IsLine2 = True'有可吃子,返回True
End If
End If
Next j
Next i
End Function
Rem 搜索程序
Private Function Search(Cer As Byte, Steps As Integer, IsTop As Boolean, UpMax As Integer)
Dim a As Integer, b As Integer, b1 As Integer, b2 As Integer, i As Integer, j As Integer, k As Integer, l As Integer, v As Integer
Dim MaxValue As Integer
Dim Sc(40) As CHESSER
Dim IsEat(7) As Boolean''搜索到的7种走法有没有吃子
Dim EAT As Boolean''有没有吃子
If IsTop Then
List1.Clear
For i = 0 To 40: Sc(i).Allow = False: Next i ';默认情况下所有走法皆不允许,如果所有值均为False则皆允许
End If
EAT = False
For i = 0 To 7: IsEat(7) = False: Next i''默认情况所有搜索到的走法都没有吃子
Steps = Steps - 1
If Steps1 And IsLine2(Cer) = False Then
''如果我方无子可吃时才返回估值
Search = -CurrentValue(Cer)''返回估值
Exit Function
End If
k = 0
''开始搜索棋盘
For i = 1 To 8
For j = 1 To 8
''如果是Cer方的棋子
If Int(ChessBoard(i, j) / 100) = Cer Then
For i1 = 1 To MAXDOWNPOINT: x(i1) = 0: x1(i1) = 0: Next ''x记载所有走法,清空x
''列出所有走法
''基本走法:上左、上右、下左、下右
x(0) = i - 1: y(0) = j - 1
x(1) = i - 1: y(1) = j1
x(2) = i1: y(2) = j - 1
x(3) = i1: y(3) = j1
''棋子表示方法:白棋101(普通)、102 (过底的威力棋)
''红棋201(普通)、202 (过底的威力棋)
''下一句解释:如果是白棋(101、102) , 不允许后退(删除x(2)、x(3))
If Cer = 1 And ChessBoard(i, j) Mod 1002 Then x(2) = -2: x(3) = -2
''下一句解释:如果是红棋(201、202) , 不允许后退(删除x(0)、x(1))
If Cer = 2 And ChessBoard(i, j) Mod 1002 Then x(0) = -2: x(1) = -2
''吃子式走法1:即如果基本走法的位置有对方的棋子则可以跳吃(走法限制:Cer为1或棋子为加强棋才可走)
If Int(ChessBoard(i - 1, j - 1) / 100) = NextCer(Cer) And (Cer = 1 Or ChessBoard(i, j) Mod 100 = 2) Then
x(4) = (i - 1) - 1''目标坐标
y(4) = (j - 1) - 1
x1(4) = i - 1''吃子坐标
y1(4) = j - 1
If x(4)0 And y(4)0 And x(4)9 And y(4)9 And ChessBoard(x(4), y(4)) = 0 Then _
EAT = True: IsEat(4) = True ''有可吃子 , 必需走此步 , 其余走法无效
End If
''吃子式走法2
If Int(ChessBoard(i - 1, j1) / 100) = NextCer(Cer) And (Cer = 1 Or ChessBoard(i, j) Mod 100 = 2) Then
x(5) = (i - 1) - 1
y(5) = (j1)1
x1(5) = i - 1
y1(5) = j1
If x(5)0 And y(5)0 And x(5)9 And y(5)9 And ChessBoard(x(5), y(5)) = 0 Then _
EAT = True: IsEat(5) = True ''有可吃子 , 必需走此步,其余走法无效
End If
''吃子式走法3
If Int(ChessBoard(i1, j - 1) / 100) = NextCer(Cer) And (Cer = 2 Or ChessBoard(i, j) Mod 100 = 2) Then
x(6) = (i1)1
y(6) = (j - 1) - 1
x1(6) = i1
y1(6) = j - 1
If x(6)0 And y(6)0 And x(6)9 And y(6)9 And ChessBoard(x(6), y(6)) = 0 Then _
EAT = True: IsEat(6) = True ''有可吃子 , 必需走此步,其余走法无效
End If
''吃子式走法4
If Int(ChessBoard(i1, j1) / 100) = NextCer(Cer) And (Cer = 2 Or ChessBoard(i, j) Mod 100 = 2) Then
x(7) = (i1)1
y(7) = (j1)1
x1(7) = i1
y1(7) = j1
If x(7)0 And y(7)0 And x(7)9 And y(7)9 And ChessBoard(x(7), y(7)) = 0 Then _
EAT = True: IsEat(7) = True ''有可吃子,必需走此步,其余走法无效
End If
''如果有吃子走法 , 删除没有吃子的其它走法
If EAT = True Then
For a = 0 To 7
If IsEat(a) = False Then x(a) = -1
Next a
End If
''存入Sc(走法表)中
For a = 0 To 7
'If x(a) = 5 And y(a) = 2 Then Stop
''如果超过棋盘将不能走
If x(a)0 And y(a)0 And x(a)9 And y(a)9 Then
''如果目标有棋子则不能走,为0才存入
If ChessBoard(x(a), y(a)) = 0 Then
''将走法存入“走法表”
Sc(k).Initx = i
Sc(k).Inity = j
Sc(k).ObjX = x(a)
Sc(k).ObjY = y(a)
Sc(k).x1 = x1(a)''被吃子位置
Sc(k).y1 = y1(a)
If IsEat(a) = True Then Sc(k).Allow = True ''如果有吃子,则允许此着走法
k = k1
End If
End If
Next a
'If EAT = True Then i = 100: j = 100 ''如果有吃子则不必再搜索
End If
Next j
Next i
MaxValue = https://www.04ip.com/post/-30000''当前分数
tTemp = False
''搜索是否有允许走法,如果没有则所有走法皆允许
For i = 0 To k - 1
If Sc(i).Allow = True Then tTemp = True
Next i
''如果有允许走法 , 则除允许走法外,其余走法皆不允许走
If tTemp = False Then
For i = 0 To k - 1: Sc(i).Allow = True: Next i
End If
''试走每种走法
For i = 0 To k - 1
If Sc(i).Allow = True Then
b1 = ChessBoard(Sc(i).Initx, Sc(i).Inity)''记录起点棋子和终点棋子
b2 = ChessBoard(Sc(i).ObjX, Sc(i).ObjY)
b = ChessBoard(Sc(i).x1, Sc(i).y1)''记录被吃子位置的棋子
ChessBoard(Sc(i).Initx, Sc(i).Inity) = 0 ''清除起点的棋子
ChessBoard(Sc(i).ObjX, Sc(i).ObjY) = b1 ''试下棋
ChessBoard(Sc(i).x1, Sc(i).y1) = 0''清除被吃子位置的棋子
''如果到边界则威力加强
''下句:如果是黑方(101、102)
If Cer = 1 Then
''下句:如果走到第一行则棋子变成102,威力加强
If Sc(i).ObjX = 1 Then ChessBoard(Sc(i).ObjX, Sc(i).ObjY) = 102
End If
''下句:如果是红方(201、202)
If Cer = 2 Then
''下句:如果走到第八行则棋子变成202,威力加强
If Sc(i).ObjX = 8 Then ChessBoard(Sc(i).ObjX, Sc(i).ObjY) = 202
End If
If b0 And IsLine(Cer, Sc(i).ObjX, Sc(i).ObjY) = True And EAT = True Then
''如果可连续吃子
v = CurrentValue(Cer)300''V为当前局面价值加300分
Else
v = Search(NextCer(Cer), Steps - 1, False, -UpMax)''没有连续可吃子,继续搜索
End If
''恢复棋盘
ChessBoard(Sc(i).x1, Sc(i).y1) = b''恢复被吃子
ChessBoard(Sc(i).Initx, Sc(i).Inity) = b1 ''记录起点棋子和终点棋子
ChessBoard(Sc(i).ObjX, Sc(i).ObjY) = b2
'' 显示每种走法的得分
If IsTop Then
List1.AddItem "从"Str(Sc(i).Initx)","Str(Sc(i).Inity)_
"到"Str(Sc(i).ObjX)","Str(Sc(i).ObjY)"得分:"Str(v)
End If
'如果这种走法分数高,记录
If IsTop And (vMaxValue Or MaxValue = https://www.04ip.com/post/-30000) Then
BestLocate.Initx = Sc(i).Initx
BestLocate.Inity = Sc(i).Inity
BestLocate.ObjX = Sc(i).ObjX
BestLocate.ObjY = Sc(i).ObjY
BestLocate.x1 = Sc(i).x1
BestLocate.y1 = Sc(i).y1
MaxValue = https://www.04ip.com/post/v
End If
If vMaxValue Then MaxValue = https://www.04ip.com/post/v
'下句: 如果 MaxValue = https://www.04ip.com/post/-UpMax//α-β剪枝, 符合剪枝条件的就Cut掉 。UpMax为上层的MaxValue
If IsTop = False And MaxValue = https://www.04ip.com/post/-UpMax Then i = 100''剪枝程序
End If
Next i
If IsTop = False Then Search = -MaxValue Else Search = MaxValue
End Function
Private Sub cbText_Click(Index As Integer)
Dim i As Integer, j As Integer, C As Integer''C记载吃子
Dim Temp As String, Temp2 As String, Temp3 As String
Dim x As Byte, y As Byte, x2 As Byte, y2 As Byte
If cbText(Index).BackColorHC0E0FF Then Call MsgBox("落棋无效!", vbOKOnly32, "提示:"): Exit Sub
If cSel = 0 And Trim(cbText(Index).Text)"" Then cSel = Index: cbText(cSel).ForeColor = QBColor(12): Exit Sub ''如果玩家一个也没先且当前棋盘位置有棋子,则标示玩家选择此棋子
If cSel0 And Val(cbText(Index).Text) = Val(cbText(cSel).Text) Then cbText(cSel).ForeColor = H80000008: cSel = 0: Exit Sub ''如果玩家两次选择相同的棋子则取消选择
If cSel0 Then
''下棋
cbText(Index).Text = cbText(cSel).Text
''判断是否可变成加强棋
k = Val(cbText(Index).Text)
If Int(k / 100) = 1 And Index8 Then cbText(Index).Text = "102"''如果1方走到顶端就变成加强棋
If Int(k / 100) = 2 And Index55 Then cbText(Index).Text = "202"''如果2方走到顶端就变成加强棋
cbText(cSel).Text = ""
cbText(cSel).ForeColor = H80000008
''判断有没有吃子
''向上左斜
If Index - cSel = -18 Then
cbText(Index9).Text = "":''被吃子
C = Index9
End If
''向上右斜
If Index - cSel = -14 Then
cbText(Index7).Text = "":''被吃子
C = Index7
End If
''向下左斜
If Index - cSel = 14 Then
cbText(Index - 7).Text = "":''被吃子
C = Index - 7
End If
''向下右斜
If Index - cSel = 18 Then
cbText(Index - 9).Text = "":''被吃子
C = Index - 9
End If
''存储走法
k = 0: Temp = "": Temp2 = "": Temp = ""
For i = 1 To 8
For j = 1 To 8
If k = cSel Then Temp = "从"Str(i)","Str(j)
If k = Index Then Temp2 = " 到"Str(i)","Str(j): x = i: y = j
If k = C Then Temp3 = "吃子 "Str(i)","Str(j): x2 = i: y2 = j
k = k1
Next j
Next i
List2.AddItem "第"Str(CurrentStep)"手 "Str(CurrentPlayer)"方"TempTemp2Temp3
CurrentStep = CurrentStep1
Text3.Text = TempTemp2
cSel = 0
Call ReDisplay
''下句:如果是人机模式并且玩家还没有可吃子
If 人机模式 = True And (IsLine(CurrentPlayer, x, y) = True And x21 And y22) = False Then
'If 人机模式 = True Then
''看玩家走了哪方的棋子,就运算另一方的棋子
CurrentPlayer = NextCer(Int(Val(cbText(Index).Text) / 100))
Call Command2_Click''如果是人机模式则让电脑运长
End If
End If
End Sub
Private Sub Command1_Click()
List2.Clear ''清除棋谱
CurrentStep = 1
Call Initial
Call Display
End Sub
Private Sub Command2_Click()
Dim t As Boolean
Command2.Enabled = False
t:
Text1.Text = Str(Search(CurrentPlayer, Val(Text2.Text), True, 0))
Command2.Enabled = True
With BestLocate
t = DownChess(.Initx, .Inity, .ObjX, .ObjY, .x1, .y1)
Call Display
If t = True And IsLine(CurrentPlayer, .ObjX, .ObjY) Then Call MsgBox("我还想再吃一个"): GoTo t ''如果所下之棋还能吃子(连续吃)则再运算
End With
CurrentPlayer = NextCer(CurrentPlayer)
End Sub
Rem 移棋
Rem Sx,Sy:起点棋子Ex,Ey:终点棋子Ax,Ay:被吃子
Rem 如果有吃子则返回True
Private Function DownChess(Sx As Byte, Sy As Byte, ex As Byte, ey As Byte, Ax As Byte, Ay As Byte) As Boolean
ChessBoard(ex, ey) = ChessBoard(Sx, Sy)
ChessBoard(Sx, Sy) = 0
ChessBoard(Ax, Ay) = 0 ''清除被吃子
If Ax0 And Ay0 Then DownChess = True Else DownChess = False
Text3.Text = "第"Str(CurrentStep)"手 "Str(CurrentPlayer)"方从"Str(Sx)","Str(Sy)"到"Str(ex)","Str(ey)_
"吃子 "Str(Ax)","Str(Ay)
CurrentStep = CurrentStep1
List2.AddItem Text3.Text
''下句:如果是黑方(101、102)
If Int(ChessBoard(ex, ey) / 100) = 1 Then
''下句:如果走到第一行则棋子变成102,威力加强
If ex = 1 Then ChessBoard(ex, ey) = 102
End If
''下句:如果是红方(201、202)
If Int(ChessBoard(ex, ey) / 100) = 2 Then
''下句:如果走到第八行则棋子变成202,威力加强
If ex = 8 Then ChessBoard(ex, ey) = 202
End If
End Function
Rem 运算一
Private Sub Command3_Click()
CurrentPlayer = 1
Call Command2_Click
End Sub
Rem 运算二
Private Sub Command4_Click()
CurrentPlayer = 2
Call Command2_Click
End Sub
Private Sub Command5_Click()
Call ReDisplay
End Sub
Private Sub Command6_Click()
If 人机模式 = False Then 人机模式 = True Else 人机模式 = False
If 人机模式 = False Then Command6.Caption = " 人机模式": Command6.ToolTipText = "当前模式:人人对战" Else Command6.Caption = " 休息模式": Command6.ToolTipText = "当前模式:人机对战"
End Sub
Private Sub Command7_Click()
End
End Sub
Rem 存谱
Private Sub Command8_Click()
On Error GoTo e
Dim i As Integer
Open InputBox("请输入文件名:") For Output As #1
For i = 0 To List2.ListCount - 1
Print #1, List2.List(i)
Next i
Close #1
Exit Sub
e:
Call MsgBox("存储错误!", vbOKOnly32, "提示:")
Err.Clear
Exit Sub
End Sub
Private Sub Form_Load()
人机模式 = False
cSel = 0
CurrentPlayer = 1
Call Command1_Click
End Sub
模块代码:
Type CHESSER
Chess As Byte''为何棋,在BestLocate则标明为何数组
Initx As Byte''起初棋的位置
Inity As Byte
ObjX As Byte''经运算后的落棋点
ObjY As Byte
x1 As Byte
y1 As Byte
Allow As Boolean''是否允许
End Type
用java语言编出一个西洋跳棋盘import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class Test {
public static void main(String[] args) {
JFrame frame = new JFrame();
JPanel boardPanel = new JPanel(new GridLayout(SCALE, SCALE));
final Cell[][] board = new Cell[SCALE][SCALE];
final JTextArea textArea = new JTextArea(15, 15);
ActionListener cellHandler = new ActionListener() {
public void actionPerformed(ActionEvent e) {
((Cell) e.getSource()).reverse();
display(textArea, board);
}
};
for (int i = 0; iboard.length;i) {
for (int j = 0; jboard[i].length;j) {
Cell cell = new Cell(i, j, false);
cell.addActionListener(cellHandler);
boardPanel.add(cell);
board[i][j] = cell;
}
}
display(textArea, board);
frame.add(boardPanel, BorderLayout.CENTER);
frame.add(new JScrollPane(textArea), BorderLayout.EAST);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
private static void display(JTextArea textArea, Cell[][] board) {
textArea.setText("");
for (Cell[] cells : board) {
for (Cell cell : cells)
textArea.append(cell.getText());
textArea.append("\n");
}
boolean value;
outter: for (int i = 0; iSCALE;i) {
value = https://www.04ip.com/post/board[i][0].value;
for (int j = 1; jSCALE;j)
if (value != board[i][j].value)
continue outter;
textArea.append("row "(i1)" has only "
(value ? 1 : 0)"s\n");
}
outter: for (int j = 0; jSCALE;j) {
value = https://www.04ip.com/post/board[0][j].value;
for (int i = 1; iSCALE;i)
if (value != board[i][j].value)
continue outter;
textArea.append("column "(j1)"has only "
(value ? 1 : 0)"s\n");
}
boolean flag = true;
value = https://www.04ip.com/post/board[0][0].value;
for (int i = 1; iSCALE;i)
if (value != board[i][i].value) {
flag = false;
break;
}
if (flag)
textArea.append("principal diagonal has only "
(value ? 1 : 0)"s\n");
flag = true;
value = https://www.04ip.com/post/board[0][SCALE - 1].value;
for (int i = 1; iSCALE;i)
if (value != board[i][SCALE - i - 1].value) {
flag = false;
break;
}
if (flag)
textArea.append("auxiliary diagonal has only "
(value ? 1 : 0)"s\n");
}
private static class Cell extends JButton {
private static final long serialVersionUID = 2653702443706288965L;
int row, col;
boolean value;
Cell(int row, int col, boolean value) {
this.row = row;
this.col = col;
this.value = https://www.04ip.com/post/value;
setText(value ? "1" : "0");
}
void reverse() {
value = https://www.04ip.com/post/!value;
setText(value ? "1" : "0");
}
}
private static final int SCALE = 8;
}
关于java编写跳棋源代码和java编写跳棋源代码怎么写的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站 。
推荐阅读
- 草原拍摄穿什么,在大草原拍照穿什么衣服好看
- java访问sqlserver数据库,java连接sql数据库登录界面
- 斗鱼直播剑,斗鱼剑士
- php一组数据逆序输出 php逆序排列数组
- 电视保险盒烧了怎么修复,电视保险烧了怎么办
- 手机怎么视频电话,手机怎么电话视频聊天
- php数据库添加数据爱好 php添加数据库记录
- 国外微信不支持视频号,微信海外视频号受地区限制怎么弄?
- 舞蹈如何与与新媒体融合,新媒体技术与舞蹈结合的案例