void Update() { seconds -= Time.deltaTime; timerText.text = ((int)(seconds)).ToString("0"); if (seconds <= 0f) { turn = ((turn == eStoneState.BLACK) ? eStoneState.WHITE : eStoneState.BLACK); isAI = ((isAI == false) ? true : false); seconds = 7; Debug.Log(isAI); } if (!isAI) { PutStone(); } for (int i = 0; i < squareZ; i++) { for (int j = 0; j < squareX; j++) { // 石の状態を確認 stoneManagers[i, j].SetState(stoneState[i, j]); } } if (isAI) { AIPutStone(); } CheckWinner(); blackScoreText.text = "黒" + blackScore + "枚"; whiteScoreText.text = "白" + whiteScore + "枚"; }
public void AIPutStone() { //x,zの値を取得 x = (int)UnityEngine.Random.Range(0, 8); z = (int)UnityEngine.Random.Range(0, 8); Debug.Log(x); Debug.Log(x); if (0 <= x && x < squareX && 0 <= z && z < squareZ && stoneState[z, x] == eStoneState.EMPTY && Turn(false) > 0) { audioSource.PlayOneShot(putStoneSE); stoneState[z, x] = turn; if (turn == eStoneState.BLACK) { blackScore++; } else if (turn == eStoneState.WHITE) { whiteScore++; } Turn(true); turn = ((turn == eStoneState.BLACK) ? eStoneState.WHITE : eStoneState.BLACK); isAI = false; } }
//タップで石を置く処理 public void PutStone() { if (Input.GetMouseButtonDown(0)) { //マウスのポジションを取得してRayに代入 Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition); //マウスのポジションからRayを投げて何かに当たったらhitに入れる if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out RaycastHit hit, 100)) { //x,zの値を取得 x = (int)hit.collider.gameObject.transform.position.x; z = (int)hit.collider.gameObject.transform.position.z; if (0 <= x && x < squareX && 0 <= z && z < squareZ && stoneState[z, x] == eStoneState.EMPTY && Turn(false) > 0) { audioSource.PlayOneShot(putStoneSE); stoneState[z, x] = turn; if (turn == eStoneState.BLACK) { blackScore++; } else if (turn == eStoneState.WHITE) { whiteScore++; } Turn(true); turn = ((turn == eStoneState.BLACK) ? eStoneState.WHITE : eStoneState.BLACK); isAI = true; } } } }
//ひっくり返す石を取得する処理 int Turn(bool isTurn) { // 相手の石の色 eStoneState enemyColor = ((turn == eStoneState.BLACK) ? eStoneState.WHITE : eStoneState.BLACK); bool isTurnable = false; // ひっくり返すことができるかどうか List <TurnableStone> turnableStoneList = new List <TurnableStone>(); //ひっくり返す石のリスト int count = 0; int turnCount = 0; int plusX = 0, plusZ = 0; for (int i = 0; i < TURN_CHECK_X.Length; i++) { int _x = x; int _z = z; plusX = TURN_CHECK_X[i]; plusZ = TURN_CHECK_Z[i]; isTurnable = false; turnableStoneList.Clear(); while (true) { _x += plusX; _z += plusZ; if (!(0 <= _x && _x < squareX && 0 <= _z && _z < squareZ)) { break; } if (stoneState[_z, _x] == enemyColor) { // ひっくり返す対象 turnableStoneList.Add(new TurnableStone(_z, _x)); } else if (stoneState[_z, _x] == turn) { // ひっくり返すことができる isTurnable = true; break; } else { break; } } //ひっくり返す処理 if (isTurnable) { count += turnableStoneList.Count; if (isTurn) { for (int j = 0; j < turnableStoneList.Count; j++) { TurnableStone ts = turnableStoneList[j]; stoneState[ts.turnZ, ts.turnX] = turn; turnCount++; if (stoneState[ts.turnZ, ts.turnX] == eStoneState.WHITE) { whiteScore++; blackScore--; } else if (stoneState[ts.turnZ, ts.turnX] == eStoneState.BLACK) { blackScore++; whiteScore--; } } seconds = 7; } } } return(count); }