黑白棋代码——完结版

底层算法代码

Algorithems.java

package com.jida.java.play;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.Queue;


public class Algorithm {


//当前棋盘状况
private int chess[][]=new int[8][8];   //黑色为1,白色为-1

private final int valuechess[][]={ {100, -5, 10,  5,  5, 10, -5,100},


            { -5,-5,  1,  1,  1,  1,-5, -5},


            { 10,  1,  3,  2,  2,  3,  1, 10},


            {  5,  1,  2,  1,  1,  2,  1,  5},


            {  5,  1,  2,  1,  1,  2,  1,  5},


            { 10,  1,  3,  2,  2,  3,  1, 10},


            { -5,-5,  1,  1,  1,  1,-5, -5},


            {100, -5, 10,  5,  5, 10, -5,100}};

//构造函数
public Algorithm(){
chess[3][3]=chess[4][4]=1;
chess[3][4]=chess[4][3]=-1;
}

public int[][] getChess(){
return chess;
}

//重新开始
public void reStart(){
for(int i = 0 ; i < 8 ; i++){
for(int j = 0 ; j < 8 ; j ++){
chess[i][j] = 0;
}
}
chess[3][3]=chess[4][4]=1;
chess[3][4]=chess[4][3]=-1;
}


//落子、翻转
public Location getNewSituation(int xp,int yp,int n,boolean flag){ //n为-1落白子,1落黑子
Location loc=null;
if(flag)
chess[xp][yp]=n;
else{
loc=new Location();
loc.setX(xp);
loc.setY(yp);
}
//棋盘翻转
for(int i=0;i<8;i++){
switch(i){
case 0:
if(xp!=0){          //向北翻转
int j=xp-1;
while(j>=0&&chess[j][yp]==(-1)*n)
j--;
if(j>=0&&chess[j][yp]==n){
for(j=j+1;jif(flag)
chess[j][yp]=n;
else
loc.addN(0);
}
}break;
case 1:
if(xp!=0&&yp!=7){    //东北翻转
int x=xp-1;
int y=yp+1;
while(x>=0&&y<=7&&chess[x][y]==-1*n){
x--;
y++;
}
if(x>=0&&y<=7&&chess[x][y]==n)
for(x=x+1,y=y-1;xif(flag)
chess[x][y]=n;
else
loc.addN(1);
}break;
case 2:
if(yp!=7){   //向东翻转
int y=yp+1;
while(y<=7&&chess[xp][y]==-1*n)
y++;
if(y<=7&&chess[xp][y]==n)
for(y--;y>yp;y--)
if(flag)
chess[xp][y]=n;
else
loc.addN(2);
}break;
case 3:
if(xp!=7&&yp!=7){    //东南翻转
int x=xp+1;
int y=yp+1;
while(x<=7&&y<=7&&chess[x][y]==-1*n){
x++;
y++;
}
if(x<=7&&y<=7&&chess[x][y]==n)
for(x=x-1,y=y-1;x>xp;x--,y--)
if(flag)
chess[x][y]=n;
else
loc.addN(3);
}break;
case 4:          //向南翻转
if(xp!=7){
int j=xp+1;
while(j<=7&&chess[j][yp]==-1*n)
j++;
if(j<=7&&chess[j][yp]==n){
for(j=j-1;j>xp;j--)
if(flag)
chess[j][yp]=n;
else
loc.addN(4);
}
}break;
case 5:    //西南翻转
if(xp!=7&&yp!=0){
int x=xp+1;
int y=yp-1;
while(x<=7&&y>=0&&chess[x][y]==-1*n){
x++;
y--;
}
if(x<=7&&y>=0&&chess[x][y]==n)
for(x=x-1,y=y+1;x>xp;x--,y++)
if(flag)
chess[x][y]=n;
else
loc.addN(5);
}break;
case 6:     //向西翻转
if(yp!=0){
int y=yp-1;
while(y>=0&&chess[xp][y]==-1*n)
y--;
if(y>=0&&chess[xp][y]==n)
for(y++;yif(flag)
chess[xp][y]=n;
else
loc.addN(6);
}break;
case 7:   //西北翻转
if(xp!=0&&yp!=0){
int x=xp-1;
int y=yp-1;
while(x>=0&&y>=0&&chess[x][y]==-1*n){
x--;
y--;
}
if(x>=0&&y>=0&&chess[x][y]==n)
for(x=x+1,y=y+1;xif(flag)
chess[x][y]=n;
else
loc.addN(7);
}
}
}
return loc;


}

//当前可落子位置
public ArrayList getAvailableLocation(int n){
ArrayList rever=new ArrayList();
Location loc;
for(int i=0;i<8;i++){
for(int j=0;j<8;j++){
if(chess[i][j]==0){
loc=this.getNewSituation(i, j, n, false);
int a[]=loc.getS();
for(int m=0;m<8;m++)
if(a[m]!=0){
//System.out.println("keluozi "+loc.getX()+","+loc.getY());
rever.add(loc);
break;
}
}
}
}//System.out.println("huanfang");
return rever;
}

//翻转回溯
private void backReverse(Location loc){
chess[loc.getX()][loc.getY()]=0;
int num[]=loc.getS();
for(int i=0;i<8;i++){
switch(i){
case 0:
{
for(int j=1;j<=num[i];j++)
chess[loc.getX()-j][loc.getY()]*=(-1);
break;
}
case 1:
{
for(int j=1;j<=num[i];j++)
chess[loc.getX()-j][loc.getY()+j]*=(-1);
break;
}
case 2:
{
for(int j=1;j<=num[i];j++)
chess[loc.getX()][loc.getY()+j]*=(-1);
break;
}
case 3:
{
for(int j=1;j<=num[i];j++)
chess[loc.getX()+j][loc.getY()+j]*=(-1);
break;
}
case 4:
{
for(int j=1;j<=num[i];j++)
chess[loc.getX()+j][loc.getY()]*=(-1);
break;
}
case 5:
{
for(int j=1;j<=num[i];j++)
chess[loc.getX()+j][loc.getY()-j]*=(-1);
break;
}
case 6:
{
for(int j=1;j<=num[i];j++)
chess[loc.getX()][loc.getY()-j]*=(-1);
break;
}
case 7:
{
for(int j=1;j<=num[i];j++)
chess[loc.getX()-j][loc.getY()-j]*=(-1);
break;
}
}
}
}


public Location bestLocation(int deep){
/*Location loc=new Location();
if(has(loc,1)){
getNewSituation(loc.getX(),loc.getY(),1,true);
System.out.println("best"+loc.getX()+","+loc.getY());
return loc;
}else{*/
ArrayList al=this.getAvailableLocation(1);
if(al.size()==0){
return new Location();
}
float maxvalue=-100000;
int maxloc=-1;
for(int i=0;ifloat s=this.getBestLocation(deep,deep, al.get(i),100000,-100000).getValue();
if(s>maxvalue){
maxvalue=s;
maxloc=i;
}
}
//System.out.println("maxloc "+maxloc);
if(maxloc==-1)
return new Location();
else
return al.get(maxloc);
//}
}

private Location getBestLocation(int n,int deep,Location loc,float alpha,float belta)
{
if(deep==1)
{
loc.setValue(this.value());
return loc;
}
float minvalue=100000;
float maxvalue=-100000;
switch((n-deep)%2)
{
case 1:
{
ArrayList al=this.getAvailableLocation(1);
for(int i=0;i{
this.getNewSituation(al.get(i).getX(), al.get(i).getY(), 1, true);
float s=getBestLocation(n,deep-1,al.get(i),minvalue,maxvalue).getValue();
this.backReverse(al.get(i));
if (s>=alpha)
break;
if(s>maxvalue&&s!=100000){
maxvalue=s;
}
}loc.setValue(maxvalue);
}break;
case 0:
{
ArrayList al=this.getAvailableLocation(-1);
for(int i=0;ithis.getNewSituation(al.get(i).getX(), al.get(i).getY(), -1, true);
float s=getBestLocation(n,deep-1,al.get(i),minvalue,maxvalue).getValue();
this.backReverse(al.get(i));
if (s<=belta)
break;
if(sminvalue=s;
}
}loc.setValue(minvalue);
}
}
return loc;
}
     
/*public Location getBlackLoc(int deep){
Location loc=bestLocation(deep);
return loc;
}*/

public Location setBlack(int deep){
Location loc=new Location();
if(has(loc,1)){
getNewSituation(loc.getX(),loc.getY(),1,true);
//System.out.println("best"+loc.getX()+","+loc.getY());
}else{
loc=bestLocation(deep);
if(loc.getX()!=-1){
//System.out.println("best"+loc.getX()+","+loc.getY());
getNewSituation(loc.getX(),loc.getY(),1,true);
}
}
return loc;
}

//算法4:估值算法
private int value_1(){             //棋子数估值
int values=0;
int values1=0;
for(int i=0;i<8;i++){
for(int j=0;j<8;j++){
values+=chess[i][j]*valuechess[i][j];
if(chess[i][j]!=0){
switch(i){
case 0:{
if(j!=0&&j!=7){
if(chess[i][j-1]==chess[i][j]*(-1) && chess[i][j+1]==chess[i][j]*(-1) && chess[i+1][j]==chess[i][j]*(-1) &&chess[i+1][j-1]==chess[i][j]*(-1)&&chess[i+1][j+1]==chess[i][j]*(-1)){
values1+=chess[i][j]*2;
}
}break;
}case 7:{
if(j!=0&&j!=7){
if(chess[i][j-1]==chess[i][j]*(-1) && chess[i][j+1]==chess[i][j]*(-1) && chess[i-1][j]==chess[i][j]*(-1) &&chess[i-1][j-1]==chess[i][j]*(-1)&&chess[i-1][j+1]==chess[i][j]*(-1)){
values1+=chess[i][j]*2;
}
}break;
}default:{
switch(j){
case 0:{
if(i!=0&&i!=7){
if(chess[i-1][j]==chess[i][j]*(-1) && chess[i+1][j]==chess[i][j]*(-1) && chess[i-1][j+1]==chess[i][j]*(-1) &&chess[i][j+1]==chess[i][j]*(-1)&&chess[i+1][j+1]==chess[i][j]*(-1)){
values1+=chess[i][j]*2;
}
}break;
}
case 7:{
if(i!=0&&i!=7){
if(chess[i-1][j]==chess[i][j]*(-1) && chess[i+1][j]==chess[i][j]*(-1) && chess[i-1][j-1]==chess[i][j]*(-1) &&chess[i][j-1]==chess[i][j]*(-1)&&chess[i+1][j-1]==chess[i][j]*(-1)){
values1+=chess[i][j]*2;
}
}break;
}
default:{
if(chess[i-1][j-1]==chess[i][j]*(-1) && chess[i-1][j]==chess[i][j]*(-1) && chess[i-1][j+1]==chess[i][j]*(-1) && chess[i][j-1]==chess[i][j]*(-1) && chess[i][j+1]==chess[i][j]*(-1) && chess[i+1][j-1]==chess[i][j]*(-1) &&chess[i+1][j]==chess[i][j]*(-1)&&chess[i+1][j+1]==chess[i][j]*(-1)){
values1+=chess[i][j]*2;
}
}
}
}
}
}
}
}
int k=1;
int x=0,y=0;
for(k=1;k<8;k++){
if(chess[0][k]==0)
break;
if(chess[0][k]==1)
x++;
if(chess[0][k]==-1)
y++;
}
if(k==7 && chess[0][7]!=0)
values+=(x*2-y*2);

x=0;
y=0;
for(k=1;k<8;k++){
if(chess[7][k]==0)
break;
if(chess[7][k]==1)
x++;
if(chess[7][k]==-1)
y++;
}
if(k==7 && chess[7][7]!=0)
values+=(x*2-y*2);

x=0;
y=0;
for(k=1;k<8;k++){
if(chess[k][0]==0)
break;
if(chess[k][0]==1)
x++;
if(chess[k][0]==-1)
y++;
}
if(k==7 && chess[7][0]!=0)
values+=(x*2-y*2);

x=0;
y=0;
for(k=1;k<8;k++){
if(chess[k][7]==0)
break;
if(chess[k][7]==1)
x++;
if(chess[k][7]==-1)
y++;
}
if(k==7 && chess[7][7]!=0)
values+=(x*2-y*2);

return values+values1;
}

private int value_2(){            //行动力估值
return 5*getAvailableLocation(1).size();
}



private float value(){
return (float) (this.value_1()+this.value_2());
}

private boolean has(Location loc,int n){
ArrayList local_avaliable = new ArrayList();
local_avaliable = this.getAvailableLocation(n);
for(int i = 0 ; iint x0 = local_avaliable.get(i).getX();
int y0 = local_avaliable.get(i).getY();
if((x0 == 0||x0==7) && (y0 == 0||y0==7)){
loc.setX(x0);
loc.setY(y0);
//System.out.println("uidfs"+x0+","+y0);
return true;
}
}
return false;
}
}

你可能感兴趣的:(程序设计)