unity3D学习之纯GUI实现 黑白棋AI对战Word格式.docx
- 文档编号:18235173
- 上传时间:2022-12-14
- 格式:DOCX
- 页数:8
- 大小:15.55KB
unity3D学习之纯GUI实现 黑白棋AI对战Word格式.docx
《unity3D学习之纯GUI实现 黑白棋AI对战Word格式.docx》由会员分享,可在线阅读,更多相关《unity3D学习之纯GUI实现 黑白棋AI对战Word格式.docx(8页珍藏版)》请在冰豆网上搜索。
privateintleftEdge=10;
privateinttopEdge=100;
privateintsize=10;
privateintcellsep=5;
voidRestart()
lTable.Clear();
for(inti=0;
i<
colNum;
i++)
lTable.Add(newList<
ChessCell>
());
for(intj=0;
j<
rowNum;
j++)
lTable[i].Add(newChessCell());
if(i==rowNum/2-1&
&
j==colNum/2-1)
lTable[i][j].iPlayer=1;
if(i==rowNum/2&
lTable[i][j].iPlayer=2;
if(i==rowNum/2-1&
j==colNum/2)
size=((Screen.width>
Screen.height?
(Screen.height-topEdge):
Screen.width)-(2*leftEdge))/rowNum-cellsep;
lasti=-1;
lastj=-1;
curPlayer=1;
privateintcurPlayer=1;
privatevoidChangePlayer()
curPlayer=curPlayer==1?
2:
1;
privatestringcurPlayerName
get
returncurPlayer==1?
"
O"
:
X"
;
publicboolbPC=true;
privateintlasti=-1;
privateintlastj=-1;
List<
>
lTable=newList<
();
privateintplayer1count=2;
privateintplayer2count=2;
voidOnGUI()
GUI.Label(newRect(10,10,200,30),"
CurPlayer:
"
+curPlayerName);
if(GUI.Button(newRect(220,10,80,40),"
restart"
))
if(lasti>
=0)
GUI.Box(newRect(leftEdge+lasti*(size+cellsep),topEdge+lastj*(size+cellsep),size,size),"
);
intmost1=0;
for(intii=0;
ii<
colNum;
ii++)
for(intjj=0;
jj<
jj++)
if(lTable[ii][jj].iPlayer!
continue;
inticount=CheckValid(jj,ii,false);
if(most1<
icount)
most1=icount;
if(most1==0)
ShowResult();
i<
i++)
if(lTable[i][j].iPlayer==1)
GUI.Button(newRect(leftEdge+i*(size+cellsep),topEdge+j*(size+cellsep),size,size),"
elseif(lTable[i][j].iPlayer==2)
else
if(GUI.Button(newRect(leftEdge+i*(size+cellsep),topEdge+j*(size+cellsep),size,size),"
if(!
bPC||curPlayer==1)
CheckValid(j,i,true);
if(bPC&
curPlayer==2)
intmost=0;
intmii=0;
intmjj=0;
if(most<
most=icount;
mii=ii;
mjj=jj;
if(most>
0)
CheckValid(mjj,mii,true);
lasti=mii;
lastj=mjj;
player1count=0;
player2count=0;
if(lTable[ii][jj].iPlayer==1)
player1count++;
elseif(lTable[ii][jj].iPlayer==2)
player2count++;
GUI.Label(newRect(10,55,300,40),"
P1O:
+player1count+"
P2X:
+player2count);
voidShowResult()
GUI.Box(newRect(220,55,100,40),"
GUI.Button(newRect(220,55,100,40),player1count>
player2count?
P1WIN"
P2WIN"
intCheckValid(introw,intcol,boolbSet)
lSetCells=newList<
lPendingCells=newList<
//testleft
for(inti=col-1;
i>
=0;
i--)
if(lTable[i][row].iPlayer==0)
break;
if(lTable[i][row].iPlayer!
=curPlayer)
lPendingCells.Add(lTable[i][row]);
if(lTable[i][row].iPlayer==curPlayer)
lSetCells.AddRange(lPendingCells);
//testright
lPendingCells.Clear();
for(inti=col+1;
//testup
for(inti=row-1;
if(lTable[col][i].iPlayer==0)
if(lTable[col][i].iPlayer!
lPendingCells.Add(lTable[col][i]);
if(lTable[col][i].iPlayer==curPlayer)
//testdown
for(inti=row+1;
//testlefttop
for(inti=col-1,j=row-1;
=0&
j>
i--,j--)
if(lTable[i][j].iPlayer==0)
if(lTable[i][j].iPlayer!
lPendingCells.Add(lTable[i][j]);
if(lTable[i][j].iPlayer==curPlayer)
//testrighttop
for(inti=col+1,j=row-1;
colNum&
i++,j--)
//testleftbottom
for(inti=col-1,j=row+1;
i--,j++)
//testrightbottom
for(inti=col+1,j=row+1;
i++,j++)
if(lSetCells.Count>
if(bSet)
{
lTable[col][row].iPlayer=curPlayer;
foreach(ChessCellccinlSetCells)
cc.iPlayer=curPlayer;
ChangePlayer();
returnlSetCells.Count;
classChessCell
publicintiPlayer=0;
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- unity3D学习之纯GUI实现 黑白棋AI对战 unity3D 学习 GUI 实现 白棋 AI