资源描述
细心整理8.2 电脑下棋算法设计本款游戏最核心的地方就是算法,因为这是整个程序最难的模块。算法的中心思想是:利用分数代表每个位置的重要程度,越重要的位置的分数值会越高,当电脑下棋时会先将电脑和玩家棋型的分数分别计算一遍,然后选择二者中分数最高的点下子。假如玩家的分数高,那么代表电脑应当防守,假如电脑的分数高,那么代表电脑应当进攻。 具体的分数值赋值如下:分数电脑玩家活二6050半活二4035死二1010半死二1010活三950700半活三900650死三100100半死三100100活四60003500半活四50003000死四4000800半死四3600750活五2000015000半活五100003300死五2000015000半死五100003300说明一下其中的活,半活,死,半死:活:代表几个子是相连的,中间没有空格,两端都至少有一个空格。半活:代表几个子不是相连的,几个子中间有一个空格,两端都至少有一个空格。死:代表几个子是相连的,中间没有空格,但有一端紧挨着对方的棋子或有一端正好在棋盘的边界。半死:代表几个子不是相连的,几个子中间有一个空格,而且一端紧挨着对方的棋子或有一端正好在棋盘的边界。每个位置的分数的计算方式是各个方向的分数相加,最终找出电脑棋型和玩家棋型的分数的最高的位置为电脑的下棋点下棋。具体的代码如下:public void qixing()for(int i=0;i17;i+)for(int j=0;j17;j+)if(qipani+1j+1=0)/说明此处没有棋子qixingPCij = heiheng(i,j,2)+heishu(i,j,2)+heizuoxie(i,j,2)+heiyouxie(i,j,2);qixingPlayerij = heiheng(i,j,1)+heishu(i,j,1)+heizuoxie(i,j,1)+heiyouxie(i,j,1);elseqixingPCij = 0;qixingPlayerij = 0;/ 算出黑子横方向的棋型数值public int heiheng(int hang,int lie,int num)int k = 0;/记录空白处的个数int count = 1;/记录可以形成几连int n = hang+1;/对应棋盘的行int m = lie+1;/对应棋盘的列boolean left = false;/判定左边是否有黑子boolean liveLeft = false;/判定左边是活还是死boolean liveRight = false;/判定右边是活还是死while(qipannm-1!=-1)&(qipannm-1=num|qipannm-1=0)if(qipannm-1=0&k1)/第一个空白if(qipannm-2!=num)liveLeft = true;break;k+;m-;else if(qipannm-1=num)/黑子left = true;m-;else/其次个空白liveLeft = true;break;if(!left)k = 0;m = lie+1;while(qipannm+1!=-1)&(qipannm+1=num|qipannm+1=0)int t = qipannm+1;if(m=lie)count+;m+;continue;if(t=0&k0)/其次个空白liveRight = true;break;else/黑子m+;count+;return jieguo(liveLeft,liveRight,count,k,num);/算出黑子竖方向的棋型数值public int heishu(int hang,int lie,int num)int k = 0;/记录空白处的个数int count = 1;/记录可以形成几连int n = hang+1;/对应棋盘的行int m = lie+1;/对应棋盘的列boolean top = false;/判定上边是否有黑子boolean liveLeft = false;boolean liveRight = false;while(qipann-1m!=-1)&(qipann-1m=num|qipann-1m=0)if(qipann-1m=0&k1)/第一个空白if(qipann-2m!=num)liveLeft = true;break;k+;n-;else if(qipann-1m=num)/黑子top = true;n-;else/其次个空白liveLeft = true;break;if(!top)k = 0;n = hang+1;while(qipann+1m!=-1)&(qipann+1m=num|qipann+1m=0)int t = qipann+1m;if(n=hang)count+;n+;continue;if(t=0&k0)/其次个空白liveRight = true;break;else/黑子n+;count+;/return jieguo(k=0,count);return jieguo(liveLeft,liveRight,count,k,num);/算出黑子左斜方向的棋型数值public int heizuoxie(int hang,int lie,int num)int k = 0;/记录空白处的个数int count = 1;/记录可以形成几连int n = hang+1;/对应棋盘的行int m = lie+1;/对应棋盘的列boolean top = false;/判定上边是否有黑子boolean liveLeft = false;boolean liveRight = false;while(qipann+1m-1!=-1)&(qipann+1m-1=num|qipann+1m-1=0)if(qipann+1m-1=0&k1)/第一个空白if(qipann+2m-2!=num)liveLeft = true;break;k+;n+;m-;else if(qipann+1m-1=num)/黑子top = true;n+;m-;else/其次个空白liveLeft = true;break;if(!top)k = 0;n = hang+1;m = lie+1;while(qipann-1m+1!=-1)&(qipann-1m+1=num|qipann-1m+1=0)int t = qipann-1m+1;if(n=(hang+2)&m=lie)count+;n-;m+;continue;if(t=0&k0)/其次个空白liveRight = true;break;else/黑子n-;m+;count+;return jieguo(liveLeft,liveRight,count,k,num);/算出黑子右斜方向的棋型数值public int heiyouxie(int hang,int lie,int num)int k = 0;/记录空白处的个数int count = 1;/记录可以形成几连int n = hang+1;/对应棋盘的行int m = lie+1;/对应棋盘的列boolean top = false;/判定上边是否有黑子boolean liveLeft = false;boolean liveRight = false;while(qipann-1m-1!=-1)&(qipann-1m-1=num|qipann-1m-1=0)if(qipann-1m-1=0&k1)/第一个空白if(qipann-2m-2!=num)liveLeft = true;break;k+;n-;m-;else if(qipann-1m-1=num)/黑子top = true;n-;m-;else/其次个空白liveLeft = true;break;if(!top)k = 0;n = hang+1;m = lie+1;while(qipann+1m+1!=-1)&(qipann+1m+1=num|qipann+1m+1=0)int t = qipann+1m+1;if(n=hang&m=lie)count+;n+;m+;continue;if(t=0&k0)/其次个空白liveRight = true;break;else/黑子n+;m+;count+;return jieguo(liveLeft,liveRight,count,k,num);public int jieguo(boolean left,boolean right,int count,int k,int num)if(count=1)return 0;else if(count=2)if(left&right)if(k=0)if(num=2)return 60;elsereturn 50;elseif(num=2)return 40;elsereturn 35;else if(!left&!right)return 0;elsereturn 10;else if(count=3)if(left&right)if(k=0)if(num=2)return 950;elsereturn 700;elseif(num=2)return 900;elsereturn 650;else if(!left&!right)return 0;elsereturn 100;else if(count=4)if(left&right)if(k=0)if(num=2)return 6000;elsereturn 3500;elseif(num=2)return 5000;elsereturn 3000;else if(!left&!right)return 0;elseif(k=0)if(num=2)return 4000;elsereturn 800;elseif(num=2)return 3600;elsereturn 750;elseif(k=0)if(num=2)return 20000;elsereturn 15000;elseif(num=2)return 10000;elsereturn 3300;
展开阅读全文