基于Java實現(xiàn)五子棋小游戲(附源碼)
一、效果展示


二、游戲介紹
《五子棋》 是一種兩人對弈的純策略型棋類游戲,棋具與圍棋通用,是起源于中國古代的傳統(tǒng)黑白棋種之一。發(fā)展于日本,流行于歐美。容易上手,老少皆宜,而且趣味橫生,引人入勝;不僅能增強(qiáng)思維能力,提高智力,而且富含哲理,有助于修身養(yǎng)性。
比賽規(guī)則:
- 行棋順序:黑先、白后,相互順序落子。
- 判斷勝負(fù):最先在棋盤橫向、豎向、斜向形成連續(xù)的相同色五個棋子的一方為勝。黑棋禁手判負(fù),白棋無禁手。黑棋禁手包括三三禁手,四四禁手,長連禁手。如
- 分不出勝負(fù),則定為平局。
棋型解釋:
- 五連:五顆同色棋子連在一起,即4個方向的11111這種形式的棋型。
- 活四:有2個成五點的四顆棋子,即4個方向的011110這種形式的棋型,注意兩邊一定要有空格。
- 沖四:有1個成五點的四顆棋子,棋型有點多。
- 活三:可以形成活四的三顆棋子
禁手規(guī)則:
- 三三禁手:由于黑方落一子,同時形成二個或二個以上黑方活三的局面
- 四四禁手:由于黑方落一子,同時形成二個或二個以上黑方四(活四或者沖四)的局面
- 長連禁手:由于黑方落一子,形成六個或者六個以上的同色連續(xù)棋子
三、代碼展示
1、登錄頁面

運(yùn)行程序后需注冊賬號使用,也可使用我注冊過的賬號(賬號:liangdianjun,密碼:123456),可在項目文檔user.xls查看。


話不多說,直接上代碼
用戶登錄
private static final long servialVersionUID = 1L;
final JLabel logoLabel = new JLabel("開心五子棋");
final JLabel logo = new JLabel();
final JButton loginButton = new JButton(" 登 陸 ");
final JLabel registerLabel = new JLabel("立即注冊");
final JLabel userLabel = new JLabel("賬號:");
final JLabel passwordLabel = new JLabel("密碼:");
final static JTextField userjt = new JTextField(11);
final JPasswordField passwordjt = new JPasswordField(11);
final JCheckBox rememberPasswordjcb = new JCheckBox();
final JLabel rememberPasswordjl = new JLabel("記住密碼");
final JCheckBox automaticLoginjcb = new JCheckBox();
final JLabel automaticLoginjl = new JLabel("自動登錄");
final JLabel promptPasswordFalse = new JLabel("密碼錯誤!");
final JLabel promptRegister = new JLabel("該賬號還未注冊!");
final JLabel promptUserNameEmpty = new JLabel("請輸入賬號!");
final JLabel prompPasswordEmpty = new JLabel("請輸入密碼!");
final Color color = new Color(255, 218, 185);
final FileOperation read = new FileOperation();//創(chuàng)建文件對象
final FileOperation f = new FileOperation();
public Main() {
setTitle("開心五子棋");
setBounds(200, 200, 500, 500);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true);
//基本布局設(shè)置
SpringLayout springLayout = new SpringLayout();//使用彈簧布局管理器
Container c = getContentPane();//創(chuàng)建容器
c.setBackground(new Color(255, 218, 185));
c.setLayout(springLayout);
userjt.setFont(new Font("微軟雅黑", 0, 18 ));
userjt.setText(Register.userName);
passwordjt.setFont(new Font("微軟雅黑", 0, 18));
passwordjt.setText(Register.password);
logoLabel.setFont(new Font("微軟雅黑", 1, 48));
logoLabel.setForeground(Color.pink);
ImageIcon logoimage = new ImageIcon(Main.class.getResource("/image/logo5.jpg"));
logoimage.setImage(logoimage.getImage().getScaledInstance(260, 130, Image.SCALE_DEFAULT));
logo.setIcon(logoimage);
userLabel.setFont(new Font("微軟雅黑", 1, 20));
passwordLabel.setFont(new Font("微軟雅黑", 1, 20));
rememberPasswordjl.setFont(new Font("微軟雅黑", 0, 14));
rememberPasswordjl.setForeground(Color.gray);
automaticLoginjl.setFont(new Font("微軟雅黑", 0, 14));
automaticLoginjl.setForeground(Color.gray);
loginButton.setFont(new Font("微軟雅黑", 1, 16));
registerLabel.setFont(new Font("微軟雅黑", 1, 13));
registerLabel.setForeground(Color.gray);
promptPasswordFalse.setFont(new Font("微軟雅黑", 0, 13));
promptPasswordFalse.setForeground(Color.red);
promptUserNameEmpty.setFont(new Font("微軟雅黑", 0, 13));
promptUserNameEmpty.setForeground(Color.red);
prompPasswordEmpty.setFont(new Font("微軟雅黑", 0, 13));
prompPasswordEmpty.setForeground(Color.red);
promptRegister.setFont(new Font("微軟雅黑", 0, 13));
promptRegister.setForeground(Color.red);
rememberPasswordjcb.setBackground(new Color(255, 218, 185));
automaticLoginjcb.setBackground(new Color(255, 218, 185));
c.add(logo);//首頁圖標(biāo)
springLayout.putConstraint(springLayout.NORTH, logo, 40, springLayout.NORTH, c);
springLayout.putConstraint(springLayout.WEST, logo, 115, springLayout.WEST, c);
c.add(logoLabel);//標(biāo)題“開心五子棋”
springLayout.putConstraint(springLayout.NORTH, logoLabel, 100, springLayout.NORTH, c);
springLayout.putConstraint(springLayout.WEST, logoLabel, 120, springLayout.WEST, c);
logoLabel.setVisible(false);
c.add(userLabel);//用戶名
springLayout.putConstraint(springLayout.NORTH, userLabel, 35, springLayout.SOUTH, logoLabel);
springLayout.putConstraint(springLayout.WEST, userLabel, 110, springLayout.WEST, c);
c.add(userjt);
springLayout.putConstraint(springLayout.NORTH, userjt, 35, springLayout.SOUTH, logoLabel);
springLayout.putConstraint(springLayout.WEST, userjt, 10, springLayout.EAST, userLabel);
c.add(passwordLabel);//密碼
springLayout.putConstraint(springLayout.NORTH, passwordLabel, 10, springLayout.SOUTH, userLabel);
springLayout.putConstraint(springLayout.WEST, passwordLabel, 110, springLayout.WEST, c);
c.add(passwordjt);
springLayout.putConstraint(springLayout.NORTH, passwordjt, 10, springLayout.SOUTH, userjt);
springLayout.putConstraint(springLayout.WEST, passwordjt, 10, springLayout.EAST, passwordLabel);
c.add(rememberPasswordjcb);//復(fù)選框
springLayout.putConstraint(springLayout.NORTH, rememberPasswordjcb, 10, springLayout.SOUTH, passwordLabel);
springLayout.putConstraint(springLayout.WEST, rememberPasswordjcb, 175, springLayout.WEST, c);
c.add(rememberPasswordjl);
springLayout.putConstraint(springLayout.NORTH, rememberPasswordjl, 10, springLayout.SOUTH, passwordjt);
springLayout.putConstraint(springLayout.WEST, rememberPasswordjl, 5, springLayout.EAST, rememberPasswordjcb);
c.add(automaticLoginjcb);
springLayout.putConstraint(springLayout.NORTH, automaticLoginjcb, 10, springLayout.SOUTH, passwordjt);
springLayout.putConstraint(springLayout.WEST, automaticLoginjcb, 30, springLayout.EAST, rememberPasswordjl);
c.add(automaticLoginjl);
springLayout.putConstraint(springLayout.NORTH, automaticLoginjl, 10, springLayout.SOUTH, passwordjt);
springLayout.putConstraint(springLayout.WEST, automaticLoginjl, 5, springLayout.EAST, automaticLoginjcb);
c.add(loginButton);//登陸按鈕
springLayout.putConstraint(springLayout.NORTH, loginButton, 20, springLayout.SOUTH, rememberPasswordjl);
springLayout.putConstraint(springLayout.WEST, loginButton, 110, springLayout.WEST, c);
c.add(registerLabel);//注冊按鈕
springLayout.putConstraint(springLayout.NORTH, registerLabel, 5, springLayout.SOUTH, loginButton);
springLayout.putConstraint(springLayout.WEST, registerLabel, 320, springLayout.WEST, c);
c.add(promptRegister);//賬號未注冊提示
promptRegister.setVisible(false);
springLayout.putConstraint(springLayout.NORTH, promptRegister, 41, springLayout.SOUTH, logoLabel);
springLayout.putConstraint(springLayout.WEST, promptRegister, 5, springLayout.EAST, userjt);
c.add(promptUserNameEmpty);//請輸入賬號
promptUserNameEmpty.setVisible(false);
springLayout.putConstraint(springLayout.NORTH, promptUserNameEmpty, 41, springLayout.SOUTH, logoLabel);
springLayout.putConstraint(springLayout.WEST, promptUserNameEmpty, 5, springLayout.EAST, userjt);
c.add(promptPasswordFalse);//密碼錯誤提示
promptPasswordFalse.setVisible(false);
springLayout.putConstraint(springLayout.NORTH, promptPasswordFalse, 20, springLayout.SOUTH, promptRegister);
springLayout.putConstraint(springLayout.WEST, promptPasswordFalse, 5, springLayout.EAST, passwordjt);
c.add(prompPasswordEmpty);//密碼為空提示
prompPasswordEmpty.setVisible(false);
springLayout.putConstraint(springLayout.NORTH, prompPasswordEmpty, 20, springLayout.SOUTH, promptRegister);
springLayout.putConstraint(springLayout.WEST, prompPasswordEmpty, 5, springLayout.EAST, passwordjt);
//設(shè)置文本框鼠標(biāo)點擊事件
userjt.addMouseListener(new MouseAdapter() {//文本框
public void mouseClicked(MouseEvent e) {
userjt.setText("");
}
});
passwordjt.addMouseListener(new MouseAdapter() {//密碼框
public void mouseClicked(MouseEvent e) {
passwordjt.setText("");
}
});
//設(shè)置登陸按鈕單擊事件
loginButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String userName = userjt.getText().trim();//獲取用戶輸入的賬號和密碼
String Password = new String(passwordjt.getPassword()).trim();
//判斷賬號和密碼
if(userName.length() != 0) {//用戶名不為空
promptUserNameEmpty.setVisible(false);//關(guān)閉賬號為空顯示
if(Password.length() != 0) {//密碼不為空
if(f.readData("user.xls", userName) && Password.equals(f.backData("user.xls", userName, "password"))) {//用戶輸入的賬號和密碼正確
promptRegister.setVisible(false);//隱藏提示信息
promptPasswordFalse.setVisible(false);
prompPasswordEmpty.setVisible(false);
loginButton.setText(" 登 陸 中... ");
new Chessboard();//跳轉(zhuǎn)到五子棋棋盤頁面
dispose();//銷毀當(dāng)前頁面
}
else if( f.readData("user.xls", userName) && !Password.equals(f.backData("user.xls", userName, "password"))) {//用戶輸入密碼錯誤
promptPasswordFalse.setVisible(true);//顯示密碼錯誤提示
promptRegister.setVisible(false);
prompPasswordEmpty.setVisible(false);
passwordjt.setText("");//密碼框清空
passwordjt.requestFocus();//光標(biāo)定位到密碼框
}else {//賬號還未注冊
promptRegister.setVisible(true);
promptPasswordFalse.setVisible(false);
prompPasswordEmpty.setVisible(false);
}
}
else {//密碼為空
if(userName.equals("admin")) {//用戶名已經(jīng)注冊, 提示輸入密碼
prompPasswordEmpty.setVisible(true);
promptUserNameEmpty.setVisible(false);
promptRegister.setVisible(false);
promptPasswordFalse.setVisible(false);
}else {//用戶名未注冊
prompPasswordEmpty.setVisible(false);
promptUserNameEmpty.setVisible(false);
promptRegister.setVisible(true);
promptPasswordFalse.setVisible(false);
}
}
}else {//用戶名為空
promptUserNameEmpty.setVisible(true);//提示輸入賬號
promptRegister.setVisible(false);
promptPasswordFalse.setVisible(false);
prompPasswordEmpty.setVisible(false);
passwordjt.setText("");//將密碼框置為空
if(Password.length() == 0) {//密碼為空
prompPasswordEmpty.setVisible(true);
promptRegister.setVisible(false);
promptPasswordFalse.setVisible(false);
}
}
}
});
//注冊標(biāo)簽監(jiān)聽器
registerLabel.addMouseListener(new MouseListener() {
public void mouseClicked(MouseEvent e) {
dispose();
new Register();
}
public void mouseEntered(MouseEvent e) {
registerLabel.setForeground(Color.red);;
}
public void mouseExited(MouseEvent e) {
registerLabel.setForeground(Color.black);
}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
});
}
public static void main(String[] args) {
// TODO 自動生成的方法存根
new Main();
}
2、算法程序
該程序?qū)崿F(xiàn)了對五子棋分?jǐn)?shù)的計算,計算豎橫斜黑子和白子數(shù)量,誰先達(dá)成五分(即五子)則勝利。
返回棋盤上某個空點的分?jǐn)?shù)
public static int countScore(int map[][], int X, int Y, int computerColor) {
int sum = 0;
int count = 0;
int value[] = new int[] {0, 0, 0, 0};
int upcount[] = new int[] {0, 0, 0, 0};
int downcount[] = new int[] {0, 0, 0, 0};
int upflag[] = new int[] {0, 0, 0, 0};
int downflag[] = new int[] {0, 0, 0, 0};
for(int color = 1; color <= 2; color++) {//計算雙方的分?jǐn)?shù)
map[X][Y] = color;//先將該點放白子
/*******************************************計算橫向棋子***********************/
for(int i = X - 1; i >=0; i--) {//計算左邊棋子數(shù)量
if(map[i][Y] == color) {
upcount[0]++;
}else if(map[i][Y] != 0 && map[i][Y] != color) {//表示有對方棋子
upflag[0] = -1;
break;
}else {//表示為空
upflag[0] = 1;
if(i - 1 >= 0 && map[i][Y] == 0) {
upflag[0] = 2;//表示兩個空格
}else {
break;
}
if(i - 2 >= 0 && map[i][Y] == 0) {
upflag[0] = 3;//表示有三個空格
}else {
break;
}
break;
}
}
for(int j = X + 1; j <= 14; j++) {//計算右邊棋子數(shù)量
if(map[j][Y] == color) {
downcount[0]++;
}else if(map[j][Y] != 0 && map[j][Y] != color) {
downflag[0] = -1;
break;
}else {//表示為空
downflag[0] = 1;
if(j + 1 <= 14 && map[j][Y] == 0) {
downflag[0] = 2;
}else {
break;
}
if(j + 2 <= 14 && map[j][Y] == 0) {
downflag[0] = 3;
}else {
break;
}
break;
}
}
/******************************************************計算列項棋子***************************************/
for(int i = Y - 1; i >= 0; i--) {//計算方向向上
if(map[X][i] == color) {
upcount[1]++;
}else if(map[X][i] != 0 && map[X][i] != color) {//表示該點是對方棋子
upflag[1] = -1;
break;
}else {//表示為空
upflag[1] = 1;
if(i - 1 >= 0 && map[X][i] == 0) {
upflag[1] = 2;
}else {
break;
}
if(i - 2 >= 0 && map[X][i] == 0) {
upflag[1] = 3;
}else {
break;
}
break;
}
}
for(int j = Y + 1; j <= 14; j++) {//計算方向向下
if(map[X][j] == color) {
downcount[1]++;
}else if(map[X][j] != 0 && map[X][j] != color) {//表示該點是對方棋子
downflag[1] = -1;
break;
}else {//表示為空
downflag[1] = 1;
if(j + 1 >= 0 && map[X][j] == 0) {
downflag[1] = 2;
}else {
break;
}
if(j + 2 >= 0 && map[X][j] == 0) {
downflag[1] = 3;
}else {
break;
}
break;
}
}
/****************************************************計算斜向下棋子*********************************************/
int i = 0;
int j = 0;
for(i = X - 1, j = Y - 1; i >= 0 && j >= 0; i--, j--) {//計算斜向上
if(map[i][j] == color) {
upcount[2]++;
}else if(map[i][j] != 0 && map[i][j] != color) {
upflag[2] = -1;
break;
}else {//為空
upflag[2] = 1;
if(i - 1 >= 0 && j - 1 >= 0 && map[i][j] == 0) {
upflag[2] = 2;
}else {
break;
}
if(i - 2 >= 0 && j - 2 >= 0 && map[i][j] == 0) {
upflag[2] = 3;
}else {
break;
}
break;
}
}
for(i = X + 1, j = Y + 1; i <= 14 && j <= 14; i++, j++) {//計算斜向下
if(map[i][j] == color) {
downcount[2]++;
}else if(map[i][j] != 0 && map[i][j] != color) {
downflag[2] = -1;
break;
}else {//為空
downflag[2] = 1;
if(i + 1 <= 14 && j + 1 <= 14 && map[i][j] == 0) {
downflag[2] = 2;
}else {
break;
}
if(i + 2 <= 14 && j + 2 <= 14 && map[i][j] == 0) {
downflag[2] = 3;
}else {
break;
}
break;
}
}
/****************************************************計算斜向上棋子*************************************************/
for(i = X + 1, j = Y - 1; i <= 14 && j >= 0; i++, j--) {
if(map[i][j] == color) {
upcount[3]++;
}else if(map[i][j] != 0 && map[i][j] != color) {
upflag[3] = -1;
break;
}else {
upflag[3] = 1;
if(i + 1 <= 14 && j - 1 >= 0 && map[i][j] == 0) {
upflag[3] = 2;
}else {
break;
}
if(i + 2 <= 14 && j - 2 >= 0 && map[i][j] == 0) {
upflag[3] = 3;
}else {
break;
}
break;
}
}
for(i = X - 1, j = Y + 1; i >= 0 && j <= 14; i--, j++) {//計算斜向下
if(map[i][j] == color) {
downcount[3]++;
}else if(map[i][j] != 0 && map[i][j] != color) {
downflag[3] = -1;
break;
}else {//為空
downflag[3] = 1;
if(i - 1 >= 0 && j + 1 <= 14 && map[i][j] == 0) {
downflag[3] = 2;
}else {
break;
}
if(i - 2 >= 0 && j + 2 <= 14 && map[i][j] == 0) {
downflag[3] = 3;
}else {
break;
}
break;
}
}
//數(shù)據(jù)處理
if(map[X][Y] == computerColor) {//如果是電腦方的話分?jǐn)?shù)要高一點
for(i =0; i < 4; i++) {
count = upcount[i] + downcount[i] + 1;
if(count == 5) {//成五
value[i] = 40000;
}else if(count == 4) {
if(upflag[i] >= 1 && downflag[i] >= 1) {//活四
value[i] = 19000;
}
if((upflag[i] >= 1 && downflag[i] == -1) || (upflag[i] == -1 && downflag[i] >= 1)) {//眠四
value[i] = 3000;
}
if(upflag[i] == -1 && downflag[i] == -1) {//死四
value[i] = -50;
}
}else if(count == 3) {
if((upflag[i] >= 2 && downflag[i] >= 1) || (upflag[i] >= 1 && downflag[i] >= 2)) {//活三
value[i] = 4000;
}
if((upflag[i] >= 2 && downflag[i] == -1) || (upflag[i] == -1 && downflag[i] >= 2) ||
(upflag[i] == 1 && downflag[i] == 1)){//眠三
value[i] = 800;
}
if(upflag[i] == -1 && downflag[i] == -1) {//死三
value[i] = -50;
}
}else if(count == 2) {
if((upflag[i] >= 1 && downflag[i] >= 3) || (upflag[i] >=2 && downflag[i] >= 2) ||
(upflag[i] >= 3 && downflag[i] >= 1)) {//活二
value[i] = 1050;
}
if((upflag[i] == -1 && downflag[i] >= 3) || (upflag[i] >= 3 && downflag[i] == -1) ||
(upflag[i] == 2 && downflag[i] == 1) || (upflag[i] == 1 && downflag[i] == 2)) {//眠二
value[i] = 350;
}
if(upflag[i] == -1 && downflag[i] == -1) {//死二
value[i] = -50;
}
}else {
if((upflag[i] >= 2 && downflag[i] >= 3) || (upflag[i] >= 3 && downflag[i] >= 2)) {//活1
value[i] = 80;
}
if((upflag[i] == 2 && downflag[i] == 2) || (upflag[i] == 1 && downflag[i] == 3) ||
(upflag[i] == 3 && downflag[i] == 1)) {//眠1
value[i] = 20;
}
if((upflag[i] <= 1 && downflag[i] <= 2) || (upflag[i] <= 2 && downflag[i] <= 1)) {
value[i] = -50;
}
}
}
}else {
for(i =0; i < 4; i++) {
count = upcount[i] + downcount[i] + 1;
if(count == 5) {//成五
value[i] = 30000;
}else if(count == 4) {
if(upflag[i] >= 1 && downflag[i] >= 1) {//活四
value[i] = 15000;
}
if((upflag[i] >= 1 && downflag[i] == -1) || (upflag[i] == -1 && downflag[i] >= 1)) {//眠四
value[i] = 2500;
}
if(upflag[i] == -1 && downflag[i] == -1) {//死四
value[i] = -50;
}
}else if(count == 3) {
if((upflag[i] >= 2 && downflag[i] >= 1) || (upflag[i] >= 1 && downflag[i] >= 2)) {//活三
value[i] = 3000;
}
if((upflag[i] >= 2 && downflag[i] == -1) || (upflag[i] == -1 && downflag[i] >= 2) ||
(upflag[i] == 1 && downflag[i] == 1)){//眠三
value[i] = 500;
}
if(upflag[i] == -1 && downflag[i] == -1) {//死三
value[i] = -50;
}
}else if(count == 2) {
if((upflag[i] >= 1 && downflag[i] >= 3) || (upflag[i] >=2 && downflag[i] >= 2) ||
(upflag[i] >= 3 && downflag[i] >= 1)) {//活二
value[i] = 650;
}
if((upflag[i] == -1 && downflag[i] >= 3) || (upflag[i] >= 3 && downflag[i] == -1) ||
(upflag[i] == 2 && downflag[i] == 1) || (upflag[i] == 1 && downflag[i] == 2)) {//眠二
value[i] = 150;
}
if((upflag[i] == -1 && downflag[i] == -1) || (upflag[i] == 1 && downflag[i] == 1) ||
(upflag[i] == -1 && downflag[i] == 2) || (upflag[i] == 2 && downflag[i] == -1)) {//死二
value[i] = -50;
}
}else {
if((upflag[i] >= 2 && downflag[i] >= 3) || (upflag[i] >= 3 && downflag[i] >= 2)) {//活1
value[i] = 50;
}
if((upflag[i] == 2 && downflag[i] == 2) || (upflag[i] == 1 && downflag[i] == 3) ||
(upflag[i] == 3 && downflag[i] == 1)) {//眠1
value[i] = 10;
}
if((upflag[i] <= 1 && downflag[i] <= 2) || (upflag[i] <= 2 && downflag[i] <= 1)||
(upflag[i] <= 3 && downflag[i] == -1)|| (upflag[i] == -1 && downflag[i] <= 3)) {
value[i] = -50;
}
}
}
}
for(i = 0; i < 4; i++) {
sum += value[i];
value[i] = 0;
upcount[i] = 0;
downcount[i] = 0;
upflag[i] = 0;
downflag[i] = 0;
}
}
map[X][Y] = 0;
return sum;
}
估值算法,返回一個數(shù)組,用于記錄坐標(biāo)
public static int[] evalute(int map[][], int depth, int computerColor) {
int maxscore = 0;
Random r = new Random();
int pos[][] = new int[10][2];{
for(int i = 0; i < pos.length; i++) {
for(int j = 0; j < pos[i].length; j++) {
pos[i][j] = 0;
}
}
}
int FLAG = 0;
int score[][] = new int[15][15];{//初始化計分?jǐn)?shù)組
for(int i = 0; i < 15; i++) {
for(int j = 0; j < 15; j++) {
score[i][j] = 0;
}
}
}
int position[] = new int[]{0, 0};//初始化位置坐標(biāo)數(shù)組
for(int i = 6 - depth; i <= 8 + depth && i <= 14; i++) {//搜索橫坐標(biāo)
for(int j = 6 - depth; j <= 8 + depth && j <= 14; j++) {//搜索縱坐標(biāo)
if(map[i][j] == 0) {//表示該點在棋盤上面為空
score[i][j] = countScore(map, i, j, computerColor);
if(maxscore < score[i][j]) {
maxscore = score[i][j];//記錄當(dāng)前棋盤分?jǐn)?shù)的最大值
}
}
}
}
for(int i = 6 - depth; i <= 8 + depth && i <= 14; i++) {
for(int j = 6 - depth; j <= 8 + depth && j <= 14; j++) {
if(score[i][j] == maxscore) {
pos[FLAG][0] = i;
pos[FLAG++][1] = j;
}
}
}
int m = r.nextInt(FLAG);
position[0] = pos[m][0];
position[1] = pos[m][1];
return position;
}
//極大極小值算法
public int minimax(int map[][], int chessColor) {
return chessColor;
}
//alpha beta剪枝
public void alphaBetaCutting(int map[][], int chessColor){
}
3、棋盤實現(xiàn)
該算法按照五子棋規(guī)則,實現(xiàn)了最基本的打子,棋盤布局等功能。電腦可以計算玩家的棋子位置,嚴(yán)防死守(我完全沒有機(jī)會贏,勝利的小伙伴可評論炫一波),最終連成五子則結(jié)束。(此代碼較多,展示部分代碼,可下載完整版查看學(xué)習(xí))
電腦下棋函數(shù)
private void tuntoComputer() {//電腦下棋
if(depth >= 7) {
depth = 6;
}
position = Algorithm.evalute(map, depth, computerColor);//調(diào)用估值函數(shù)
map[position[0]][position[1]] = computerColor;
imapflag[flag] = position[0];//將坐標(biāo)存放在悔棋標(biāo)記數(shù)組中
jmapflag[flag] = position[1];
newchessX = position[0];//新棋子標(biāo)記記錄坐標(biāo)
newchessY = position[1];
int a = Math.max(Math.abs(position[0] - 7), Math.abs(position[1] - 7));//計算該點到中心的最大的距離
depth = Math.max(depth, a);//不斷更新depth的值
flag ++;
chessboardEmpty = 1;//棋盤標(biāo)記為有棋子
player = 1;//玩家下棋標(biāo)志置0
computer = 0;//電腦下棋標(biāo)志為1
judgeFlag = 1;
repaint();
}
判斷棋子是否連成五個
public void judge() {
for(t = newchessX,s = newchessY,count = 0; t >=0 && s >= 0 && count <= 4; t--,s--,count++) {
comeX = t;
comeY = s;
}
for(t = newchessX, s = newchessY, count = 0; t <=14 && s >= 0 && count <= 4; t++, s--, count++) {
toX = t;
toY = s;
}
if(winFLAG == 0) {
for(int ch = 1; ch <=2; ch++) {
CHESSCOLOR = ch;
//判斷橫向棋子
for(s = (newchessX - 4) >=0 ? (newchessX - 4) : 0 ; s <= newchessX; s++) {//表示玩家獲勝
t = newchessY;
if(map[s][t] == CHESSCOLOR && s < 11) {//行棋子數(shù)量計算
if(map[s + 1][t] == CHESSCOLOR) {
if(map[s + 2][t] == CHESSCOLOR) {
if(map[s + 3][t] == CHESSCOLOR) {
if(map[s + 4][t] == CHESSCOLOR) {
winX = s;
winY = t;
winWay = 1;
if(CHESSCOLOR == 1) {//白棋
winFLAG = 1;
}else {//黑棋
winFLAG = 2;
}
break;
}
}
}
}
}
}
if(winFLAG != 0) {//如果某一方贏了就直接退出
break;
}
//判斷列項棋子
for(t = (newchessY - 4) >=0 ? (newchessY - 4) : 0 ; t <= newchessY; t ++) {
s = newchessX;
if(map[s][t] == CHESSCOLOR && t < 11) {//列棋子數(shù)量計算
if(map[s][t + 1] == CHESSCOLOR) {
if(map[s][t + 2] == CHESSCOLOR) {
if(map[s][t + 3] == CHESSCOLOR) {
if(map[s][t + 4] == CHESSCOLOR) {
winX = s;
winY = t;
winWay = 2;
if(CHESSCOLOR == 1) {//白棋
winFLAG = 1;
}else {//黑棋
winFLAG = 2;
}
break;
}
}
}
}
}
}
if(winFLAG != 0) {//如果某一方贏了就直接退出
break;
}
//判斷左上到右下棋子
for(s = comeX, t = comeY; s <= newchessX && t <= newchessY; s ++, t++) {
if(map[s][t] == CHESSCOLOR && s < 11 && t < 11) {//斜下棋子數(shù)量計算
if(map[s + 1][t + 1] == CHESSCOLOR) {
if(map[s + 2][t + 2] == CHESSCOLOR) {
if(map[s + 3][t + 3] == CHESSCOLOR) {
if(map[s + 4][t + 4] == CHESSCOLOR) {
winX = s;
winY = t;
winWay = 3;
if(CHESSCOLOR == 1) {//白棋
winFLAG = 1;
}else {//黑棋
winFLAG = 2;
}
break;
}
}
}
}
}
}
if(winFLAG != 0) {//如果某一方贏了就直接退出
break;
}
//判斷右上到左下棋子
for(s = toX, t = toY; s >= newchessX && t <= newchessY; s --, t++) {
if(map[s][t] == CHESSCOLOR && s >= 4 && t < 11) {//斜上棋子數(shù)量計算
if(map[s - 1][t + 1] == CHESSCOLOR) {
if(map[s - 2][t + 2] == CHESSCOLOR) {
if(map[s - 3][t + 3] == CHESSCOLOR) {
if(map[s - 4][t + 4] == CHESSCOLOR) {
winX = s;
winY = t;
winWay = 4;
if(CHESSCOLOR == 1) {//白棋
winFLAG = 1;
}else {//黑棋
winFLAG = 2;
}
break;
}
}
}
}
}
}
if(winFLAG != 0) {//如果某一方贏了就直接退出
break;
}
}
}
}
四、資源下載
百度網(wǎng)盤:鏈接:https://pan.baidu.com/s/14sDR9lAme9x1TXVD8iadqQ 提取碼:p7vh
GitHub獲取:附其他小游戲源碼,感謝您的star https://github.com/liangdianjun/game/tree/main/Temp
以上就是基于Java實現(xiàn)五子棋小游戲(附源碼)的詳細(xì)內(nèi)容,更多關(guān)于Java五子棋的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
SpringBoot框架集成ElasticSearch實現(xiàn)過程示例詳解
這篇文章主要為大家介紹了SpringBoot如何集成ElasticSearch的實現(xiàn)過程示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2021-11-11
springboot?maven?打包插件介紹及注意事項說明
這篇文章主要介紹了springboot?maven?打包插件介紹及注意事項說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12
Spring定時任務(wù)@Scheduled注解(cron表達(dá)式fixedRate?fixedDelay)
這篇文章主要為大家介紹了Spring定時任務(wù)@Scheduled注解(cron表達(dá)式fixedRate?fixedDelay)使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11
Java注解中@Component和@Bean的區(qū)別
這篇文章主要介紹了@Component和@Bean的區(qū)別,在這給大家簡單介紹下作用對象不同:@Component 注解作用于類,而 @Bean 注解作用于方法,具體實例代碼參考下本文2024-03-03
Mybatis-Plus動態(tài)表名的實現(xiàn)示例
面對復(fù)雜多變的業(yè)務(wù)需求,動態(tài)表名的處理變得愈發(fā)重要,本文主要介紹了Mybatis-Plus動態(tài)表名的實現(xiàn)示例,具有一定的參考價值,感興趣的可以了解一下2024-07-07
springboot解決使用localhost或127.0.01模擬CORS失效
CORS允許不同源的網(wǎng)頁請求訪問另一個源服務(wù)器上的某些資源,本文主要介紹了springboot解決使用localhost或127.0.01模擬CORS失效,文中通過示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-07-07
IDEA遇到Internal error. Please refer to http://jb. gg/ide/crit
這篇文章主要介紹了IDEA遇到Internal error. Please refer to http://jb. gg/ide/critical-startup-errors的問題及解決辦法,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),需要的朋友可以參考下2020-08-08

