import java.awt.*;
網(wǎng)站建設(shè)哪家好,找成都創(chuàng)新互聯(lián)公司!專注于網(wǎng)頁設(shè)計(jì)、網(wǎng)站建設(shè)、微信開發(fā)、重慶小程序開發(fā)公司、集團(tuán)企業(yè)網(wǎng)站建設(shè)等服務(wù)項(xiàng)目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了武進(jìn)免費(fèi)建站歡迎大家使用!
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;
}
先導(dǎo)入三個(gè)按鈕圖片?
正常顯示的圖片"Begin1.jpg"
鼠標(biāo)移動(dòng)到按鈕上時(shí)顯示的圖片"Begin2.jpg"
按下鼠標(biāo)時(shí)顯示的圖片"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");
創(chuàng)建按鈕?
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) { //鼠標(biāo)移動(dòng)到按鈕上方
lblBegin.setImage(imageBegin2.getImage());
}
public void mouseExit(MouseEvent e) { //鼠標(biāo)從按鈕上方移開
lblBegin.setImage(imageBegin1.getImage());
}
});
lblBegin.addMouseListener(new MouseAdapter() {
public void mouseDown(MouseEvent e) {
if (e.button == 1) {//按下鼠標(biāo)左鍵
lblBegin.setImage(imageBegin3.getImage()); //在這里寫入單擊事件代碼
}
}
public void mouseUp(MouseEvent e) {
if (e.button == 1) {//釋放鼠標(biāo)左鍵
lblBegin.setImage(imageBegin2.getImage());
}
}
});
如圖所示,當(dāng)X坐標(biāo)為1時(shí),Y的坐標(biāo)只能為5,當(dāng)X坐標(biāo)為2時(shí),Y的坐標(biāo)可以5或6。于是我們建立一個(gè)數(shù)組:
final static private int[][] pos = {
{5,5}, //X坐標(biāo)為1,Y的上限是5,下限是5
{5,6}, //X坐標(biāo)為2,Y的上限是5,下限是6
{5,7}, //X坐標(biāo)為3,Y的上限是5,下限是7
{5,8}, //X坐標(biāo)為4,Y的上限是5,下限是8
{1,13}, //X坐標(biāo)為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函數(shù)可以確定一個(gè)坐標(biāo)是否合法
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)中棋子和坐標(biāo)的索引關(guān)系
?棋盤中所有Chess集合
private Chess[] chesses = null;//所有棋子對象都保存這個(gè)數(shù)組當(dāng)中
下面函數(shù)可以根據(jù)索引號(hào)返回棋子對象
public Chess getChess(int index) {
return chesses[index];
}
?棋子和坐標(biāo)的對應(yīng)關(guān)系
private Position[] chessesPosition = null;//所有棋子坐標(biāo)都保存在這個(gè)數(shù)組當(dāng)中
下面函數(shù)可以根據(jù)棋子對象或棋子索引號(hào)返回坐標(biāo)
public Position getPosition(Chess chess) {
return chessesPosition[chess.getindex()];
}
public Position getPosition(int index) {
return chessesPosition[index];
}
?坐標(biāo)和棋子的對應(yīng)關(guān)系
private Chess[][] chessesIndex = new Chess[17][17];//數(shù)組保存了17*17個(gè)棋子對象指針
下面函數(shù)可以根據(jù)棋子坐標(biāo)返回該位置上的棋子,如果沒有棋子返回Null
public Chess getChess(Position position) {
if (position == null){
return null;
}
return chessesIndex[position.getx() - 1][position.gety() - 1];
}
這個(gè)其實(shí)和五子棋 象棋的尋址區(qū)別不大,都是最佳算法
其實(shí)主要看搜索深度的,我以前自己做過五子棋和象棋的java版本
有很多算法實(shí)現(xiàn) 你用alpha beta或者置換表都可以
驗(yàn)證做好的是最憂搜索+置換表+歷史啟發(fā),現(xiàn)在不寫那些了 代碼不好找 你去網(wǎng)上搜索五子棋人工智能就應(yīng)該有