黑白棋中吃子的算法

本文介绍了一个简单的黑白棋对战程序实现,使用Java语言。该程序允许用户输入棋子颜色和坐标来模拟对局过程,并通过算法检查每一步是否合法。程序采用10x10的棋盘,边缘默认为不可落子区域。

由于还没有学到图像什么的,黑白棋只能用1 -1来表示 空棋盘用0表示

import java.util.Scanner;

public class DEMO5 {//下棋算法
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.println("输入你要走的步数");
        int step = in.nextInt();
        System.out.println("输出第一个走的棋子的颜色 -1 或 1 ");
        int first = in.nextInt();
        int second;
        if (first == 1) second = -1;
        else second = 1;
        int[][] chess_board = new int[10][10];
        for (int i = 0; i < 10; i++) {
            chess_board[0][i] = 2;
            chess_board[9][i] = 2;
            chess_board[i][0] = 2;
            chess_board[i][9] = 2;
        }
        System.out.println("输入你要走的点的坐标 按数组坐标输入");
        for (int i = 1; i < 9; i++) {
            for (int j = 1; j < 9; j++) {
                chess_board[i][j] = in.nextInt();
            }
        }//initialize the board
        for (int i = 1; i <= step; i++) {
            if (i % 2 == 1) {
                int X = in.nextInt();
                int Y = in.nextInt();
                chess_board = check_valid(first, second, X + 1, Y + 1, chess_board);
                if(chess_board[9][9]==10)break;
            }
            if (i % 2 == 0) {
                int X = in.nextInt();
                int Y = in.nextInt();
                chess_board = check_valid(second, first, X + 1, Y + 1, chess_board);
                if(chess_board[9][9]==10)break;
            }

        }
        for (int i = 1; i < 9; i++) {
            for (int j = 1; j < 9; j++) {
                System.out.printf("%3d", chess_board[i][j]);
            }
            System.out.println("");
        }//print output


    }


    public static int[][] check_valid(int mine, int opponent, int x, int y, int[][] arrays) {
        //right check
        int loop_count = 1;
        int check = 0;
        arrays [9][9]=2;
        while (arrays[x][y + loop_count] == opponent) {
            loop_count++;
        }
        if (arrays[x][y + loop_count] == mine && loop_count != 1) {
            check = 1;
        }//valid test
        if (check == 1) {
            for (int counter = 0; counter < loop_count; counter++) {
                arrays[x][y + counter] = mine;
            }
        } else arrays[9][9]++;

        //left check
        int loop_count1 = 1;
        int check1 = 0;
        while (arrays[x][y - loop_count1] == opponent) {
            loop_count1++;
        }
        if (arrays[x][y - loop_count1] == mine && loop_count1 != 1) {
            check1 = 1;
        }//valid test
        if (check1 == 1) {
            for (int counter = 0; counter < loop_count1; counter++) {
                arrays[x][y - counter] = mine;
            }
        }else arrays[9][9]++;

        //up check
        int loop_count2 = 1;
        int check2 = 0;
        while (arrays[x + loop_count2][y] == opponent) {
            loop_count2++;
        }
        if (arrays[x + loop_count2][y] == mine && loop_count2 != 1) {
            check2 = 1;
        }//valid test

        if (check2 == 1) {
            for (int counter = 0; counter < loop_count2; counter++) {
                arrays[x + counter][y] = mine;
            }
        }else arrays[9][9]++;

        // down check
        int loop_count3 = 1;
        int check3 = 0;
        while (arrays[x - loop_count3][y] == opponent) {
            loop_count3++;
        }
        if (arrays[x - loop_count3][y] == mine && loop_count3 != 1) {
            check3 = 1;
        }//valid test
        if (check3 == 1) {
            for (int counter = 0; counter < loop_count3; counter++) {
                arrays[x - counter][y] = mine;
            }
        }else arrays[9][9]++;


        //right down check
        int loop_count5 = 1;
        int check5 = 0;
        while (arrays[x + loop_count5][y + loop_count5] == opponent) {
            loop_count5++;
        }
        if (arrays[x + loop_count5][y + loop_count5] == mine && loop_count5 != 1) {
            check5 = 1;
        }//valid test
        if (check5 == 1) {
            for (int counter = 0; counter < loop_count5; counter++) {
                arrays[x + counter][y + counter] = mine;
            }
        }else arrays[9][9]++;
        //left down  check
        int loop_count6 = 1;
        int check6 = 0;
        while (arrays[x + loop_count6][y - loop_count6] == opponent) {
            loop_count6++;
        }
        if (arrays[x + loop_count6][y - loop_count6] == mine && loop_count6 != 1) {
            check6 = 1;
        }//valid test
        if (check6 == 1) {
            for (int counter = 0; counter < loop_count6; counter++) {
                arrays[x + counter][y - counter] = mine;
            }
        }else arrays[9][9]++;
        //left up check
        int loop_count7 = 1;
        int check7 = 0;
        while (arrays[x - loop_count7][y - loop_count7] == opponent) {
            loop_count7++;
        }
        if (arrays[x - loop_count7][y - loop_count7] == mine && loop_count7 != 1) {
            check7 = 1;
        }//valid test
        if (check7 == 1) {
            for (int counter = 0; counter < loop_count7; counter++) {
                arrays[x - counter][y - counter] = mine;
            }
        }else arrays[9][9]++;

        //right up check
        int loop_count8 = 1;
        int check8 = 0;
        while (arrays[x - loop_count8][y + loop_count8] == opponent) {
            loop_count8++;
        }
        if (arrays[x - loop_count8][y + loop_count8] == mine && loop_count8 != 1) {
            check8 = 1;
        }//valid test
        if (check8 == 1) {
            for (int counter = 0; counter < loop_count8; counter++) {
                arrays[x - counter][y + counter] = mine;
            }
        }else arrays[9][9]++;

        return arrays;
    }

}


输入和输出的样例

 

•Alpha-Beta剪枝(Alpha-Beta pruning) 对于一般的最大最小搜索,即使每一步只有很少的下法,搜索的位置也会增长非常快;在大多数的中局形中,每步平均有十个位置可以下,于是假设搜索九步(程序术语称为搜索深度为九),就要搜索十亿个位置(十的九次方),极大地限制了电脑的力。于是采用了一个方法,叫“alpha-beta剪枝”,它大为减少了检测的数目,提高电脑搜索的速度。各种各样的这种算法用于所有的强力Othello程序。(同样用于其他类游戏,如国际象和跳)。为了搜索九步,一个好的程序只用搜索十万到一百万个位置,而不是没用前的十亿次。 •估值 这是一个程序中最重要的部分,如果这个模块太弱,则就算算法再好也没有用。我将要叙述三种不同的估值函数范例。我相信,大多数的Othello程序都可以归结于此。 格表:这种算法的意思是,不同的格有不同的值,角的值大而角旁边的格子值要小。忽视对称的话,盘上有10个不同的位置,每个格子根据三种可能性赋值:黑、白和空。更有经验的逼近是在游戏的不同阶段对格子赋予不同的值。例如,角在开局阶段和中局开始阶段比终局阶段更重要。采用这种算法的程序总是很弱(我这样认为),但另一方面,它很容易实现,于是许多程序开始采用这种逼近。 基于行动力的估值:这种更久远的接近有很强的全局观,而不像格表那样局部化。观察表明,许多人类玩者努力获得最大的行动力(可下的数目)和潜在行动力(临近对手子的空格,见技巧篇)。如果代码有效率的话,可以很快发现,它们提高力很多。 基于模版的估值 :正如上面提及的,许多中等力量的程序经常合并一些边角判断的知识,最大行动力和潜在行动力是全局特性,但是他们可以被切割成局部配置,再加在一起。子最少化也是如此。这导致了以下的概括:在估值函数中仅用局部配置(模版),这通常用单独计算每一行、一列、斜边和角落判断,再加在一起来实现。 估值合并:一般程序的估值基于许多的参数,如行动力、潜在行动力、余裕手、边角判断、稳定子。但是怎么样将他们合并起来得到一个估值呢?一般采用线性合并。设a1,a2,a3,a4为参数,则估值s:=n1*a1+n2*a2+n3*a3+n4*a4。其中n1,n2,n3,n4为常数,术语叫“权重”(weight),它决定了参数的重要性,它们取决于统计值。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值