//悔棋 public bool RetractChess() { if (chessLine.Count > 1)//最少大於一顆 { for (int i = 0; i < 2; i++) { ChessAttribute chessInfo = chessLine[chessLine.Count - 1].GetComponent <ChessAttribute>(); grid[(int)chessInfo.gridPos.x, (int)chessInfo.gridPos.y] = 0; //清空該棋盤格位置 Destroy(chessLine[chessLine.Count - 1].gameObject); //移除物件 chessLine.RemoveAt(chessLine.Count - 1); //縮減對列 } return(true); } return(false); }
} //取得棋子屬性 //------------------------------------------------------------------------------------------------------------------- //棋子初始化 public void ChessInitialize(ChessAttribute _attribute, Camps player, bool _isKing) { attribute = _attribute; //設定棋子屬性 //this.name = player.ToString() + " / " + _attribute.chessName.ToString(); //更改遊戲物件名稱 //初始化棋子尺寸 this.GetComponent <RectTransform>().sizeDelta = Vector2.zero; //初始化棋子圖示 animalIcon.sprite = attribute.animalIcon; //依照陣營變換棋子顏色 if (player == Camps.正面方) { frameImage.color = UIManager.Instance.playerColor; } else if (player == Camps.反面方) { frameImage.color = UIManager.Instance.enemyColor; } chessPlayer = player; //設定陣營 isKing = _isKing; //是否為王 //顯示可移動方向在棋子上 directionType = new List <DirectionType>(); //初始化可移動方向陣列 for (int i = 0; i < directionImages.Length; i++) //先關閉全部的可移動方向 { directionImages[i].gameObject.SetActive(false); } for (int i = 0; i < attribute.directionType.Count; i++) //設定可移動方向 { directionType.Add(attribute.directionType[i]); } if (player == Camps.反面方) //棋子為反面方時, 條件性翻轉可移動方向 { //需實施反轉的方向(一對) List <DirectionType[]> exchangeDir = new List <DirectionType[]>(); exchangeDir.Add(new DirectionType[] { DirectionType.左上, DirectionType.左下 }); exchangeDir.Add(new DirectionType[] { DirectionType., DirectionType. });
//檢測棋子連線 public bool CheckOneLine(Vector2 pos, int[] offset) { int linkNum = 1; //針對右邊for迴圈,針對棋子的x與y座標做遍歷;限定遍歷範圍;每遍歷一次遞增 for (int i = offset[0], j = offset[1]; (pos.x + i >= 0 && pos.x + i <= size.x) && (pos.y + j >= 0 && pos.y + j <= size.y); i += offset[0], j += offset[1]) { if (grid[(int)pos.x + i, (int)pos.y + j] == (int)turn + 1) { linkNum++; } else { break; } } //針對左邊for迴圈,針對棋子的x與y座標做遍歷;限定遍歷範圍;每遍歷一次遞減 for (int i = -offset[0], j = -offset[1]; (pos[0] + i >= 0 && pos[0] + i <= size.x) && (pos[1] + j >= 0 && pos[1] + j <= size.y); i -= offset[0], j -= offset[1]) { if (grid[(int)pos.x + i, (int)pos.y + j] == (int)turn + 1) { linkNum++; } else { break; } } if (linkNum > 4) //當棋子大於4顆 { //針對連成五子(以上)的棋子, 賦予動畫 for (int k = 0; k < chessLine.Count; k++) { ChessAttribute info = chessLine[k].GetComponent <ChessAttribute>(); if (info.gridPos.x == (int)pos.x && info.gridPos.y == (int)pos.y) { AnimationScript anim = chessLine[k].GetComponent <AnimationScript>(); winnerChessAnim.Add(anim); anim.PlayAnimation("Interlude_SuccessChess", new AnimationBehavior(PlayMode.循環撥放), false); } } for (int i = offset[0], j = offset[1]; (pos.x + i >= 0 && pos.x + i <= size.x) && (pos.y + j >= 0 && pos.y + j <= size.y); i += offset[0], j += offset[1]) { if (grid[(int)pos.x + i, (int)pos.y + j] == (int)turn + 1) { for (int k = 0; k < chessLine.Count; k++) { ChessAttribute info = chessLine[k].GetComponent <ChessAttribute>(); if (info.gridPos.x == (int)pos.x + i && info.gridPos.y == (int)pos.y + j) { AnimationScript anim = chessLine[k].GetComponent <AnimationScript>(); winnerChessAnim.Add(anim); anim.PlayAnimation("Interlude_SuccessChess", new AnimationBehavior(PlayMode.循環撥放), false); } } } else { break; } } for (int i = -offset[0], j = -offset[1]; (pos[0] + i >= 0 && pos[0] + i <= size.x) && (pos[1] + j >= 0 && pos[1] + j <= size.y); i -= offset[0], j -= offset[1]) { if (grid[(int)pos.x + i, (int)pos.y + j] == (int)turn + 1) { for (int k = 0; k < chessLine.Count; k++) { ChessAttribute info = chessLine[k].GetComponent <ChessAttribute>(); if (info.gridPos.x == (int)pos.x + i && info.gridPos.y == (int)pos.y + j) { AnimationScript anim = chessLine[k].GetComponent <AnimationScript>(); winnerChessAnim.Add(anim); anim.PlayAnimation("Interlude_SuccessChess", new AnimationBehavior(PlayMode.循環撥放), false); } } } else { break; } } return(true); } return(false); }