package Five_Chess;
import java.util.Arrays;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
/**
* 五子棋程序主类
*/
public class Five {
//******************************************变量定义************************************************
// 可修改参数
// 搜索的深度以及广度
protected int deep = 3, weight = 7;
// 和棋步数
protected int drawn_num = 110;
// 不可修改
// 主窗口
protected Shell shell = new Shell(SWT.TITLE | SWT.BORDER | SWT.CLOSE);
// 棋盘
private Canvas canvas;
// 记录下棋点的x,y坐标 最多 (drawn_num + 1) 个
protected int[][] pre = new int[drawn_num + 1][2];
// 棋局是否开始标志 0:未开始(默认) 1:已经开始
protected boolean startflag = false;
// 棋盘单元格大小
protected final int size = 30;
// 当前棋局 0:黑子占据,1:白子占据,2:空位
protected int[][] chess = new int[15][15];
// 当前应该下的棋色 0:黑色(默认), 1:白色
protected int bw = 0;
// 是否已经分出胜负
protected boolean win = false;
// 胜利棋色
protected int win_bw;
// 是否选择禁手标志 0:无禁手 1:有禁手(默认)
protected boolean able_flag = true;
// 禁手选择的button
protected final Group group_1 = new Group(shell, SWT.NONE);
protected final Button button_1 = new Button(group_1, SWT.RADIO);
protected final Button button_2 = new Button(group_1, SWT.RADIO);
// 玩家棋色选择 0:黑色(默认) 1:白色
protected int sbw = 0;
// 棋色选择的button
protected final Group group_2 = new Group(shell, SWT.NONE);
protected final Button button_3 = new Button(group_2, SWT.RADIO);
protected final Button button_4 = new Button(group_2, SWT.RADIO);
// 难度选择的button
protected final Group group_3 = new Group(shell, SWT.NONE);
protected final Button button_5 = new Button(group_3, SWT.RADIO);
protected final Button button_6 = new Button(group_3, SWT.RADIO);
// 游戏信息
private Label label_total;
private static Label label_time;
private Label label_wait_bw;
// 总落子数目
private int chess_num = 0;
// 相关时间,单位s
private int total_time = 0;
private Timer time;
// 边界值,用于速度优化
protected int x_max = 15, x_min = 0;
protected int y_max = 15, y_min = 0;
//**********************************主界面菜单块******************************************
/**
* Launch the application
*
* @param args
*/
public static void main(String[] args) {
try {
Five window = new Five();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Open the window
*/
public void open() {
final Display display = Display.getDefault();
createContents();
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
dispose();
}
/**
* Create contents of the window
*/
protected void createContents() {
// 初始化窗口
shell.setSize(488, 572);
shell.setText(" 五子棋--人机对战版 v1.0 ");
// 初始化棋盘
for (int i = 0; i < 15; i++)
for (int j = 0; j < 15; j++)
chess[i][j] = 2;
// 其它初始化
initCanvas();
initUp();
initDown();
// 开始计时
startTime();
}
/**
* 初始化下侧 button
*/
private void initDown() {
// 禁手选择
group_1.setBounds(2, 495, 63, 40);
button_1.setText("有禁手");
button_1.setBounds(5, 7, 55, 15);
button_1.setSelection(true);
button_2.setText("无禁手");
button_2.setBounds(5, 22, 55, 15);
// 棋色选择
group_2.setBounds(67, 495, 75, 40);
button_3.setText("黑棋先手");
button_3.setBounds(5, 7, 65, 15);
button_3.setSelection(true);
button_4.setText("白棋后手");
button_4.setBounds(5, 22, 65, 15);
// 难度选择
group_3.setBounds(144, 495, 75, 40);
button_5.setText("初级电脑");
button_5.setBounds(5, 7, 65, 15);
button_5.setSelection(true);
button_6.setText("高级电脑");
button_6.setBounds(5, 22, 65, 15);
// 开始
final Button startButton = new Button(shell, SWT.NONE);
startButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(final SelectionEvent se) {
// 初始化所有游戏信息
startflag = true;
win = false;
chess_num = 0;
total_time = 0;
bw = 0;
for (int i = 0; i < 15; i++)
for (int j = 0; j < 15; j++)
chess[i][j] = 2;
// 是否选择禁手
if (button_1.getSelection())
able_flag = true;
else
able_flag = false;
button_1.setEnabled(false);
button_2.setEnabled(false);
// 棋色
if (button_3.getSelection()) {
sbw = 0;
label_wait_bw.setText("等待 玩家 落子...");
}
else {
sbw = 1;
// 随机下一子
int x = 7, y = 7;
for (int i = 5; i < 9; i++)
for (int j = 5; j < 9; j++)
if (randomTest(20 * (Math.abs(7 - i) + Math.abs(7 - j)) + 2)) {
x = i;
y = j;
}
chess[x][y] = bw;
update( x, y );
if(x-1>=0)
x_min = x-1;
if(x-1<=15)
x_max = x+1;
if(y-1>=0)
y_min = y-1;
if(y-1<=15)
y_max = y+1;
}
button_3.setEnabled(false);
button_4.setEnabled(false);
// 难度
if(button_5.getSelection())
deep = 3;
else if (button_6.getSelection())
deep = 5;
button_5.setEnabled(false);
button_6.setEnabled(false);
startButton.setText("重新开始");
canvas.redraw();
}
});
startButton.setText("开始");
startButton.setBounds(221, 501, 65, 33);
// 悔棋
final Button backButton = new Button(shell, SWT.NONE);
backButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(final SelectionEvent se) {
if(win) {
MessageBox mb = new MessageBox(shell, SWT.ICON_INFORMATION|SWT.YES);
mb.setText("不能悔棋");
mb.setMessage("棋局已经结束!请从新开始新的棋局!");
mb.open();
return;
}
// 当前轮到玩家下棋,取消两步 否则,取消一步
if(chess_num >= 2 && bw == sbw){
chess[pre[chess_num-1][0]][pre[chess_num-1][1]] = 2;
chess[pre[chess_num-2][0]][pre[chess_num-2][1]] = 2;
chess_num -= 2;
label_total.setText("总落子