2016-10-08 38 views
0

我正在使用Unity製作二維國際象棋遊戲。算法C#統一象棋遊戲

我正在寫代碼,告訴如果片可以移動到這個位置或不。

這裏是我是如何做的解釋:

  • 有一個「牆紙」,代表董事會。
  • 在「牆紙」的每個正方形上,我放置了一個帶有去激活的網格渲染器的其他正方形。
  • 當玩家選擇一個正方形時,如果這個位置上有一塊棋子,我將激活該棋子可以穿過的所有正方形的網格渲染器。
  • 所有方塊都存儲在一個簡單的數組中,我用它來確定可能的位置。

隨着兵,沒問題,它完美的作品,但與白嘴鴉或騎士,打破更復雜,我有這樣做的問題。

這裏我會和騎士一起工作。

當它在板的中心,沒有任何問題:http://prntscr.com/crcogo

但是,當我在板的一側,那麼請自己:http://prntscr.com/crcozu

下面是代碼:

case "White_Knight(Clone)_0": 
       int Index_Knight_1 = Array.IndexOf(Board, Square_Selected); 
       // Les cases en + 
       if (Index_Knight_1 + 6 <= 64) 
       { 
        Board[Index_Knight_1 + 6].GetComponent<MeshRenderer>().enabled = true; 
       } 
       if (Index_Knight_1 + 10 <= 64) 
       { 
        Board[Index_Knight_1 + 10].GetComponent<MeshRenderer>().enabled = true; 
       } 
       if (Index_Knight_1 + 15 <= 64) 
       { 
        Board[Index_Knight_1 + 15].GetComponent<MeshRenderer>().enabled = true; 
       } 
       if (Index_Knight_1 + 17 <= 64) 
       { 
        Board[Index_Knight_1 + 17].GetComponent<MeshRenderer>().enabled = true; 
       } 
       // les cases en - 
       if (Index_Knight_1 - 6 >= 0) 
       { 
        Board[Index_Knight_1 - 6].GetComponent<MeshRenderer>().enabled = true; 
       } 
       if (Index_Knight_1 - 10 >= 0) 
       { 
        Board[Index_Knight_1 - 10].GetComponent<MeshRenderer>().enabled = true; 
       } 
       if (Index_Knight_1 - 15 >= 0) 
       { 
        Board[Index_Knight_1 - 15].GetComponent<MeshRenderer>().enabled = true; 
       } 
       if (Index_Knight_1 - 17 >= 0) 
       { 
        Board[Index_Knight_1 - 17].GetComponent<MeshRenderer>().enabled = true; 
       } 

我thnik我不使用正確的方法來做到這一點,但我想腳本它自己沒有考慮的代碼段有或沒有。

在這種情況下,我想過用8的整數倍,但我堅持在這裏:d

可能有人給我一塊的建議?

謝謝!

Ho,我知道它不應該是「64」的條件,而是63,剛看到它。

+0

爲什麼不使用2d座標然後來回轉換? – Bijan

+0

我從一維切換到二維數組,更簡單。但是我找不到找到數組中特定元素位置的方法(我正在搜索objets) – Andromelus

+1

二維數組絕對是要走的路:Board [Index_Knight_X + 2,Index_Knight_Y - 1] .GetComponent ()。enabled = true;您需要檢查數組的限制以確保Index_Knight_X + 2或Index_Knight_Y - 1]不是負數 – Absinthe

回答

0

我同意Absinte - 我正在使用7x7板的類似遊戲。使用2維數組更容易。當我選擇了其中一件作品時,我會循環列出它可以做出的動作 - 這些是+/-偏移量,假設作品位於中心。對於一個棋子,x,y偏移將爲: 0,+ 1; 0,+ 2(僅限第一次移動),+ 1,+ 1,-1,+ 1(最後兩個是捕獲)。

我將這些應用到當前的x,y位置,如果x和y都在0和6之間,那麼我會將它顯示爲可能移動的位置。