// 黑白棋算法 private void calculateConsequencialMoves(GamePlayOperation currentMove, IList <GamePlayOperation> consequencialMoves) { // 上下左右各计算一次 calculateConsequencialMovesOnDirection(currentMove, 0, consequencialMoves); calculateConsequencialMovesOnDirection(currentMove, 1, consequencialMoves); calculateConsequencialMovesOnDirection(currentMove, 2, consequencialMoves); calculateConsequencialMovesOnDirection(currentMove, 3, consequencialMoves); }
public bool IsValidMove(GamePlayOperation op) { var unit = CurrentGameState.UnitAt(op.X, op.Y); if (unit.OwnerId == GameUnit.OWNER_NONE) { // 空白位置 op.Op = GamePlayOperation.OpEnum.PLACE; return(true); } else if (unit.OwnerId == op.PlayerId) { // 当前角色所拥有的棋子 } return(false); }
// // Update is called once per frame // public override void Update () { // // } public override void HandleInput() { if (Input.GetMouseButtonUp(0)) { var mouseScreenPosition = Input.mousePosition; var ray = Camera.main.ScreenPointToRay(mouseScreenPosition); var hits = Physics.RaycastAll(ray, 100, LayerMask.GetMask(new string[] { "ChessBoard" })); if (hits != null && hits.Length > 0) { var hit = hits [0]; ChessUnit u = hit.collider.GetComponent <ChessUnit> (); if (u != null) { Debug.LogFormat("Click on unit: {0}", u); GamePlayOperation op = new GamePlayOperation(); op.Op = GamePlayOperation.OpEnum.PLACE; op.X = u.X; op.Y = u.Y; op.PlayerId = this.PlayerId; if (CurrentReferee.IsValidMove(op)) { CurrentReferee.PlayMove(op); EndTurn(CurrentReferee); // CurrentReferee.EndPlayerTurn (this); } } else { Debug.Log("Click on invalid unit"); } } else { Debug.LogFormat("Click ({0}) hit nothing", mouseScreenPosition); } } }
public bool PlayMove(GamePlayOperation op) { _lastMoves.Add(op); return(true); }
// direction: 上下左右 0123 private void calculateConsequencialMovesOnDirection(GamePlayOperation currentMove, int direction, IList <GamePlayOperation> consequencialMoves) { Vector2 step = Vector2.zero; switch (direction) { case 0: step.y = 1; break; case 1: step.y = -1; break; case 2: step.x = -1; break; case 3: step.x = 1; break; default: Debug.Assert(true, "Invalid direction: " + direction); break; } Vector2 currentPos = new Vector2(currentMove.X, currentMove.Y); List <GameUnit> opponents = new List <GameUnit>(); bool hasConsequencials = false; while (true) { currentPos += step; if (!isValidGrid((int)currentPos.x, (int)currentPos.y)) { // 超出棋盘范围 break; } var u = UnitAt((int)currentPos.x, (int)currentPos.y); if (u.OwnerId == GameUnit.OWNER_NONE) { // 空白区域,表示没有连成线 break; } if (u.OwnerId == currentMove.PlayerId) { // 还是自己的棋子 if (opponents.Count > 0) { // 夹击对方棋子 hasConsequencials = true; } break; } // 对方的棋子 opponents.Add(u); } if (hasConsequencials) { for (int i = 0; i < opponents.Count; i++) { GamePlayOperation move = new GamePlayOperation(); move.X = opponents [i].X; move.Y = opponents [i].Y; move.Op = GamePlayOperation.OpEnum.TAKEN; move.PlayerId = currentMove.PlayerId; consequencialMoves.Add(move); } } }