// 玩家或电脑输入 private void Update() { if (!gameRunning) { return; } if (thisTurn == playerPieceType) // 轮到玩家 { List <int> availableMoves = board.GetAvailableMoves(); if (availableMoves[0] == width * width) // 只能过的情况 { EndThisTurn(width * width); return; } Vector3 pointedPosition; if (Input.GetMouseButtonDown(0)) // 鼠标操作 { pointedPosition = Input.mousePosition; } else if (Input.touchCount == 1) // 触屏操作 { pointedPosition = Input.touches[0].position; } else { return; } RaycastHit2D hit2D = Physics2D.CircleCast(Camera.main.ScreenToWorldPoint(pointedPosition), 0.1f, Vector2.zero); if (hit2D.collider == null) { return; // 没点到下棋子的位置 } Piece hitPiece = hit2D.collider.GetComponent <Piece>(); if (boardSituation[hitPiece.index] != PieceType.Empty) { return; // 点的位置已经有棋子了 } if (!availableMoves.Contains(hitPiece.index)) { return; // 点的位置不合法 } // 成功下棋 EndThisTurn(hitPiece.index); hintCanvas.HideHint(); } else // 轮到电脑 { int move = mctsPlayer.GetAction(board); EndThisTurn(move); } }
// 玩家或电脑输入 private void Update() { if (!gameRunning) { return; } if (thisTurn == playerPieceType) // 轮到玩家 { if (aiMoved) // 防连续触两次 { aiMoved = false; return; } Vector3 pointedPosition; if (Input.GetMouseButtonDown(0)) // 鼠标操作 { pointedPosition = Input.mousePosition; } else if (Input.touchCount == 1) // 触屏操作 { pointedPosition = Input.touches[0].position; } else { return; } RaycastHit2D hit2D = Physics2D.CircleCast(Camera.main.ScreenToWorldPoint(pointedPosition), 0.1f, Vector2.zero); if (hit2D.collider == null) { return; // 没点到下棋子的位置 } Connect4Action hitPiece = hit2D.collider.GetComponent <Connect4Action>(); if (!board.GetAvailableMoves().Contains(hitPiece.action)) { return; // 点的位置不合法 } // 成功下棋 EndThisTurn(hitPiece.action); hintCanvas.HideHint(); } else // 轮到电脑 { aiMoved = true; int move = mctsPlayer.GetAction(board); EndThisTurn(move); } }
// 玩家或电脑输入 private void Update() { if (!gameRunning) { return; } if (thisTurn == playerPieceType) // 轮到玩家 { Vector3 pointedPosition; if (Input.GetMouseButtonDown(0)) // 鼠标操作 { pointedPosition = Input.mousePosition; } else if (Input.touchCount == 1) // 触屏操作 { pointedPosition = Input.touches[0].position; } else { return; } RaycastHit2D hit2D = Physics2D.CircleCast(Camera.main.ScreenToWorldPoint(pointedPosition), 0.1f, Vector2.zero); if (hit2D.collider == null) { return; // 没点到下棋子的位置 } Piece hitPiece = hit2D.collider.GetComponent <Piece>(); if (boardSituation[hitPiece.index] != PieceType.Empty) { return; // 点的位置已经有棋子了 } // 成功下棋 hitPiece.SetPiece(thisTurn); boardSituation[hitPiece.index] = thisTurn; EndThisTurn(hitPiece.index); hintCanvas.HideHint(); } else // 轮到电脑 { int move = mctsPlayer.GetAction(board); pieces[move].SetPiece(thisTurn); boardSituation[move] = thisTurn; EndThisTurn(move); } }