十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
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=1;row13;row++){ 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=1;row13;row++){ 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=1;row13;row++){ g.drawLine(x4,y4,x5,y5); x4=x4-40; x5=x5-20; y5=y5+30;
System.out.println();
}
int x6=510;
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; i board.length; ++i) {
for (int j = 0; j board[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; i SCALE; ++i) {
value = board[i][0].value;
for (int j = 1; j SCALE; ++j)
if (value != board[i][j].value)
continue outter;
textArea.append("row " + (i + 1) + " has only " +
(value ? 1 : 0) + "s\n");
}
outter: for (int j = 0; j SCALE; ++j) {
value = board[0][j].value;
for (int i = 1; i SCALE; ++i)
if (value != board[i][j].value)
continue outter;
textArea.append("column " + (j + 1) + "has only " +
(value ? 1 : 0) + "s\n");
}
boolean flag = true;
value = board[0][0].value;
for (int i = 1; i SCALE; ++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 = board[0][SCALE - 1].value;
for (int i = 1; i SCALE; ++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 = value;
setText(value ? "1" : "0");
}
void reverse() {
value = !value;
setText(value ? "1" : "0");
}
}
private static final int SCALE = 8;
}
先导入三个按钮图片
正常显示的图片"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或者置换表都可以
验证做好的是最忧搜索+置换表+历史启发,现在不写那些了 代码不好找 你去网上搜索五子棋人工智能就应该有