不過因為各種原因就用NGUI做了一個~ 等新UI出來在做一個看看
A.邏輯部分 用一個2維陣列,對應到每個格子,格子空著0分,
在格子裡面畫O就給1分,畫X給-1分。 用迴圈跑一次橫排,
跑一次直排,算兩個對角線的分數。 如果其中一條是3分,
就是O勝利。 如果其中一條是-3分,就是X勝利。
void CheckWin()
{
int pSum =0 ;
for(int i=0 ; i<3 ;i++)
{
pSum =0;
for(int j=0 ; j<3 ; j++)
{
pSum += GameArray[i,j];
}
if(pSum == m_CheckNumber)
{
isFinish =true;
m_labelText.text = "o win,V"+playa;
}
if(pSum == m_CheckNumber*-1)
{
isFinish =true;
m_labelText.text = "V win,O"+playB;
}
}
if(isFinish == false)
{
for( int i=0 ; i<3 ;i++)
{
pSum =0 ;
for( int j=0 ; j<3 ; j++)
{
pSum += GameArray[j,i];
}
if(pSum == m_CheckNumber)
{
isFinish =true;
m_labelText.text = "o win,V"+playa;
}
if(pSum == m_CheckNumber*-1)
{
isFinish =true;
m_labelText.text = "V win,O"+playB;
}
}
}
if(isFinish == false)
{
pSum =0 ;
pSum = GameArray[0,0]+GameArray[1,1]+GameArray[2,2];
if(pSum == m_CheckNumber)
{
isFinish =true;
m_labelText.text = "o win,V"+playa;
}
if(pSum == m_CheckNumber*-1)
{
isFinish =true;
m_labelText.text = "V win,O"+playB;
}
}
if(isFinish == false)
{
pSum =0 ;
pSum = GameArray[0,2]+GameArray[1,1]+GameArray[2,0];
if(pSum == m_CheckNumber)
{
isFinish =true;
m_labelText.text = "o win,V"+playa;
}
if(pSum == m_CheckNumber*-1)
{
isFinish =true;
m_labelText.text = "V win,O"+playB;
}
}
}//void CheckWin()
B.顯示部分做3張圖 分別是方格、圈圈和叉叉(PS 這邊用勾勾代替一下)
將方格做成Button,滑鼠點擊的時候依照輪到的順序,顯示圈圈或是叉叉
public void Click()
{
if(isFinish == true)
return;
UIButton btn = UIButton.current;
int num = int.Parse(btn.name);
int row =num/3;
int col =num%3;
GameObject go;
if(GameArray[row ,col] ==0)
{
if(m_player == 1)
{
go =btn.transform.FindChild("IconO").gameObject;
NGUITools.SetActive(go , true);
m_player =2;
GameArray[row ,col]=1;
}
else
{
go =btn.transform.FindChild("IconX").gameObject;
NGUITools.SetActive(go , true);
m_player =1;
GameArray[row ,col]=-1;
}
CheckWin();
}
}
沒有留言:
張貼留言