广工人工智能实验报告材料Word格式.docx

上传人:b****5 文档编号:20239629 上传时间:2023-01-21 格式:DOCX 页数:20 大小:26.66KB
下载 相关 举报
广工人工智能实验报告材料Word格式.docx_第1页
第1页 / 共20页
广工人工智能实验报告材料Word格式.docx_第2页
第2页 / 共20页
广工人工智能实验报告材料Word格式.docx_第3页
第3页 / 共20页
广工人工智能实验报告材料Word格式.docx_第4页
第4页 / 共20页
广工人工智能实验报告材料Word格式.docx_第5页
第5页 / 共20页
点击查看更多>>
下载资源
资源描述

广工人工智能实验报告材料Word格式.docx

《广工人工智能实验报告材料Word格式.docx》由会员分享,可在线阅读,更多相关《广工人工智能实验报告材料Word格式.docx(20页珍藏版)》请在冰豆网上搜索。

广工人工智能实验报告材料Word格式.docx

最后min电脑经过steps.length种走法对比之后,选择w值最小的一种走法,把棋盘回退一步,并返回棋盘估值和下棋位置。

max走法类似,人类会选择w值最大的走法下棋,所以max函数和min函数分别代表人和AI下棋,互相递归调用,直到递归到步数为0时返回N步之后的估值。

三、实验代码及数据记录

1.代码

/**

*[Matrix矩阵]

*@param{[type]}arr[矩阵二维数组]

*/

varMatrix=function(arr){

this.data=arr;

this.row=arr.length;

this.column=arr.length?

arr[0].length:

0;

};

*[multiply矩阵乘法转换]

*@param{[type]}matrix[转换矩阵]

*@return{[type]}[description]

Matrix.prototype.multiply=function(matrix){

if(this.column==matrix.row){

varrow=this.row,

column=matrix.column,

arr=[];

for(vari=0;

i<

row;

i++){

arr[i]=[];

for(varj=0;

j<

column;

j++){

varsum=0;

for(varn=0;

n<

this.column;

n++){

sum+=(this.data[i][n]*matrix.data[n][j]);

}

arr[i][j]=sum;

returnnewMatrix(arr);

*[Chessboard棋盘]

*@param{[type]}row[description]

*@param{[type]}column[description]

varChessboard=function(row,column){

this.data=[];

this.row=row;

this.column=column;

this.data[i]=[];

this.data[i][j]=Chessboard.NONE;

this.stack=[];

this.is_ended=false;

*[toString输出棋盘信息]

Chessboard.prototype.toString=function(){

returnthis.data.map(function(data){

returndata.toString();

}).join('

\n'

);

*[put下棋]

*@param{[type]}row[行]

*@param{[type]}column[列]

*@param{[type]}type[人还是AI下棋]

Chessboard.prototype.put=function(row,column,type){

if(this.data[row][column]==Chessboard.NONE){

this.data[row][column]=type;

this.stack.push({

row:

row,

column:

column,

type:

type

});

if(this.stack.length==this.row*this.column){

this.is_ended=true;

returnthis;

*[rollback悔棋]

*@param{[type]}n[后退n步]

Chessboard.prototype.rollback=function(n){

n=n||1;

n;

varstep=this.stack.pop();

if(step){

this.data[step.row][step.column]=Chessboard.NONE;

*[reset重置棋盘]

Chessboard.prototype.reset=function(){

for(vari=0,n=this.row;

for(varj=0,m=this.column;

m;

*[availableSteps获取可走的位置]

Chessboard.prototype.availableSteps=function(){

varavailableSteps=[];

if(this.data[i][j]==Chessboard.NONE){

availableSteps.push({

i,

j

returnavailableSteps;

*[rotate把棋盘旋转90度]

Chessboard.prototype.rotate=function(){

varboard=newChessboard(this.row,this.column),

dx=Math.floor(this.column/2),

dy=Math.floor(this.row/2);

this.row;

vartype=this.data[i][j];

varmatrix=newMatrix([

[i,j,1]

]);

vartranslateMatrix1=newMatrix([

[1,0,0],

[0,1,0],

[-dx,-dy,1]

vartranslateMatrix2=newMatrix([

[dx,dy,1]

varrotateMatrix=newMatrix([

[0,-1,0],

[0,0,1]

varres=matrix.multiply(translateMatrix1).multiply(rotateMatrix).multiply(translateMatrix2);

board.put(res.data[0][0],res.data[0][1],type);

returnboard;

*[hash给棋盘一个编码]

*@param{[type]}sourceRadix[来源进制]

*@param{[type]}targetRadix[目的进制]

Chessboard.prototype.hash=function(sourceRadix,targetRadix){

varstr=this.data.map(function(arr){

returnarr.join('

'

returnparseInt(str,sourceRadix).toString(targetRadix);

*[evaluate计算当前棋盘的估值]

Chessboard.prototype.evaluate=function(){

//max,min权重,max连棋数,min连棋数

varmaxW=minW=0,

maxCount,minCount;

//横向计算

//当前这一行,max连棋数,min连棋数

maxCount=minCount=0;

if(type==Chessboard.MAX){

maxCount++;

}else{

if(type==Chessboard.MIN){

minCount++;

//如果连成3子

if(maxCount==3){

returnInfinity;

if(minCount==3){

return-Infinity;

//如果没有max的棋子,则min可能连成3子

if(!

maxCount){

minW++;

//如果没有min的棋子,则max可能连成3子

minCount){

maxW++;

//纵向计算

//当前这一列,max连棋数,min连棋数

vartype=this.data[j][i];

if(maxCount==this.row){

if(minCount==this.row){

//右斜下方向计算

vartype=this.data[i][i];

//左斜下方向计算

vartype=this.data[i][this.column-i-1];

//返回双方实力差

returnmaxW-minW;

*[isMaxWin人是否赢了]

*@return{Boolean}[description]

Chessboard.prototype.isMaxWin=function(){

varw=this.evaluate();

returnw==Infinity?

true:

false;

*[isMinWinAI是否赢了]

Chessboard.prototype.isMinWin=function(){

returnw==-Infinity?

*[end结束游戏]

Chessboard.prototype.end=function(){

*[isEnded游戏是否结束]

Chessboard.prototype.isEnded=function(){

returnthis.is_ended;

*[maxmax下棋]

*@param{[type]}currentChessboard[当前棋盘]

*@param{[type]}depth[考虑深度]

varmax=function(currentChessboard,depth,beta){

//记录优势值,应该下棋的位置

varrow,column,alpha=-Infinity;

//什么都不下,直接返回当前棋盘评估值

if(depth==0){

alpha=currentChessboard.evaluate();

return{

w:

alpha

};

//获取每一步可以走的方案

varsteps=currentChessboard.availableSteps();

//console.log('

搜索MAX'

+steps.length+'

个棋局'

if(steps.length){

//对于每一种走法

for(vari=0,l=steps.length;

l;

varstep=steps[i];

//下棋

currentChessboard.put(step.row,step.column,Chessboard.MAX);

//如果已经赢了,则直接下棋,不再考虑对方下棋

if(currentChessboard.isMaxWin()){

alpha=Infinity;

row=step.row;

column=step.column;

//退回上一步下棋

currentChessboard.rollback();

break;

//考虑对方depth-1步下棋之后的优势值,如果对方没棋可下了,则返回当前棋盘估值

varres=min(currentChessboard,depth-1)||{

currentChessboard.evaluate()

if(res.w>

alpha){

//选择最大优势的走法

alpha=res.w;

//如果人可以获得更好的走法,则AI必然不会选择这一步走法,所以不用再考虑人的其他走法

if(alpha>

=beta){

MAX节点'

+l+'

个棋局,剪掉了'

+(l-1-i)+'

个MIN棋局'

alpha,

column

*[minmin下棋]

*@return{[type]}[权重和当前推荐下棋的位置]

varmin=function(currentChessboard,depth,alpha){

varrow,column,beta=Infinity;

beta=currentChessboard.evaluate();

beta

搜索MIN'

currentChessboard.put(step.row,step.column,Chessboard.MIN);

if(currentChessboard.isMinWin()){

beta=-Infinity;

varres=max(currentChessboard,depth-1,beta)||{

if(res.w<

beta){

//选择最大优势的

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 人文社科 > 法律资料

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1