bool HasMatchingType(CircleSpot r1, CircleSpot r2, CircleSpot r3, CircleSpot r4) { CircleSpot[] row = new CircleSpot[4] { r1, r2, r3, r4 }; return(HasMatchingType(row)); }
void Awake() { if (controller == null) { controller = this; //DontDestroyOnLoad(gameObject); } else if (controller != this) { Destroy(gameObject); } gameRows = new CircleSpot[4][]; for (int i = 0; i < 4; i++) { gameRows[i] = new CircleSpot[4]; for (int k = 0; k < 4; k++) { //Debug.Log(gameRows[i].ToString()); gameRows[i][k] = GameObject.Find("CircleSpot" + (i + 1) + (k + 1)).GetComponent <CircleSpot>(); } } }
IEnumerator StartGame() { Debug.Log("Starting game"); YourTurn = (UnityEngine.Random.Range(0, 11) % 2 == 0) ? true : false; Debug.Log("Starting turn: " + (YourTurn ? "Your turn" : "Computers turn")); if (YourTurn) { //prompt to choose piece - after piece is selected while (!StartingSpace.ss.HasPiece) { yield return(null); } YourTurn = false; } else { if (singlePlayer) { //choose a starting piece for the player at random GameObject[] g = GameObject.FindGameObjectsWithTag("GamePiece"); g[UnityEngine.Random.Range(0, g.Length)].GetComponent <GamePiece>().OnVirtualMouseDown(); YourTurn = true; } else { while (!StartingSpace.ss.HasPiece) { yield return(null); } YourTurn = true; } } //Debug.Log("Regular turns are starting"); int turnCount = 1; bool foundMatch = false; GameObject[] gamePieces = GameObject.FindGameObjectsWithTag("GamePiece"); while (!foundMatch) { if (YourTurn) { Debug.Log("Your turn - choose a spot on the board"); //wait for you to place piece on board while (StartingSpace.ss.HasPiece) { isChoosingCircleSpot = true; yield return(null); } foundMatch = CheckBoardForMatches(gameRows); hasWon = foundMatch; //Debug.Log("Your turn - choose a new piece"); //wait for you to pick a piece for computer while (!StartingSpace.ss.HasPiece) { isChoosingCircleSpot = false; yield return(null); } YourTurn = false; } else { if (singlePlayer) { //pick a spot on the board //look for a winning solution - through all remaining places, if no place, pick random //CircleSpot[][] testRow = gameRows; //for(int i = 0; i < 4; i++) //{ // for (int k = 0; k < 4; k++) // { // if (gameRows[i][k].HasPiece) continue; // testRow = gameRows; // testRow[i][k].OnVirtualMouseDownTest(); // Debug.Log("Testing " + CheckBoardForMatches(testRow)); // testRow[i][k].UndoTest(); // } //} //Debug.Log("Computers turn - picking a spot to place piece"); //random for now CircleSpot cSpot = gameRows[UnityEngine.Random.Range(0, 4)][UnityEngine.Random.Range(0, 4)]; while (cSpot.HasPiece) { //pick a new piece cSpot = gameRows[UnityEngine.Random.Range(0, 4)][UnityEngine.Random.Range(0, 4)]; Debug.Log(cSpot.HasPiece); yield return(null); } yield return(new WaitForSeconds(0.25f)); cSpot.OnVirtualMouseDown(); //Debug.Log("Computers turn - choosing a new piece"); //Don't choose a piece that will cause a match (only matters if any row has more than 3 pieces) GamePiece gp = gamePieces[UnityEngine.Random.Range(0, gamePieces.Length)].GetComponent <GamePiece>(); yield return(new WaitForSeconds(0.25f)); while (gp.Placed) { gp = gamePieces[UnityEngine.Random.Range(0, gamePieces.Length)].GetComponent <GamePiece>(); yield return(null); } gp.OnVirtualMouseDown(); YourTurn = true; } else { //2nd Players turn Debug.Log("Other players turn - choose a spot on the board"); //wait for you to place piece on board while (StartingSpace.ss.HasPiece) { isChoosingCircleSpot = true; yield return(null); } foundMatch = CheckBoardForMatches(gameRows); hasWon = foundMatch; //Debug.Log("Your turn - choose a new piece"); //wait for you to pick a piece for computer while (!StartingSpace.ss.HasPiece) { isChoosingCircleSpot = false; yield return(null); } YourTurn = true; } } turnCount++; //choose who starts //- if your turn //pick a starting piece - your turn = false //computer starts regular turn: choose where to place piece, and choose another piece to place, yourturn = true //you do your turn //check for wins //loop until game over //- computer turn foundMatch = CheckBoardForMatches(gameRows); hasWon = foundMatch; yield return(null); } Debug.Log("Someone has won!"); }