// The only script in the game // using System.Collections.Generic; using TMPro; using UnityEngine; using UnityEngine.UI; public class gameFunctionality : MonoBehaviour { public GameObject playAgainButton; public GameObject VictoryText; public TMP_Text commandLineText; private string[] board; private int[] winningCombinations; public string PlayerTeam { get; private set; } public Button[] buttons; public float aiMoveDelay = 1f; // Delay for AI move private void Start() { winningCombinations = new int[] { 0, 1, 2, // Row 1 3, 4, 5, // Row 2 6, 7, 8, // Row 3 0, 3, 6, // Column 1 1, 4, 7, // Column 2 2, 5, 8, // Column 3 0, 4, 8, // Diagonal \ 2, 4, 6 // Diagonal / }; } // Resets the game board to being empty public void ResetBoard() { commandLineText.text = commandLineText.text + "\n" + ">" + "Board Reset"; // Initialize the game board with 9 empty spaces board = new string[9]; for (int i = 0; i < board.Length; i++) { board[i] = " "; // " " represents an empty space } } // Logs the current state of the game board to the console public void LogBoard() { Debug.Log( board[0] + " " + board[1] + " " + board[2] + " " + "\n" + board[3] + " " + board[4] + " " + board[5] + " " + "\n" + board[6] + " " + board[7] + " " + board[8] ); } // Checks if there is a winner and returns the winning player value public bool CheckWinCondition() { int occupiedSpaces = 0; for (int i = 0; i < winningCombinations.Length; i += 3) { if (board[winningCombinations[i]] != " " && board[winningCombinations[i]] == board[winningCombinations[i + 1]] && board[winningCombinations[i]] == board[winningCombinations[i + 2]]) { EndGame(board[winningCombinations[i]]); return true; } } for (int i = 0; i < board.Length; i++) { if (board[i] != " ") { occupiedSpaces++; } if (occupiedSpaces == 9) { EndGame("Draw"); // All spaces occupied, no winner return true; } } commandLineText.text = commandLineText.text + "\n" + ">" + "Occupied Spaces: " + occupiedSpaces; Debug.Log("Occupied Spaces: " + occupiedSpaces); return false; } public void SetPlayerTeam(string team) { if (team != "X" && team != "O") { Debug.LogError("Invalid team. Choose 'X' or 'O'."); commandLineText.text = commandLineText.text + "\n" + ">" + "Player team: " + team; return; } PlayerTeam = team; BeginGame(); } public void SetBoardActive(bool active) { for (int i = 0; i < buttons.Length; i++) { buttons[i].interactable = active; } } public void PlayerMove(int position) { if (board[position] == " ") { board[position] = PlayerTeam; commandLineText.text = commandLineText.text + "\n" + ">" + "Player: " + PlayerTeam + " played position: " + (position + 1); UpdateBoard(); if (CheckWinCondition() == true) { return; } SetBoardActive(false); LogBoard(); Invoke("AIMove", aiMoveDelay); // Wait for AI to make a move return; } } public void AIMove() { string AITeam = PlayerTeam == "X" ? "O" : "X"; // AI plays the opposite team // Chooses a random available position for the AI to make a move List availablePositions = new List(); // Simple AI logic to make a move for (int i = 0; i < board.Length; i++) { if (board[i] == " ") { availablePositions.Add(i); } } int Pos = availablePositions[Random.Range(0, availablePositions.Count)]; { // Moves to block the player from winning if (PlayerTeam == board[0] && PlayerTeam == board[1] && " " == board[2]) { Pos = 2; } else if (PlayerTeam == board[2] && PlayerTeam == board[1] && " " == board[2]) { Pos = 0; } // Top Row else if (PlayerTeam == board[0] && PlayerTeam == board[2] && " " == board[2]) { Pos = 1; } else if (PlayerTeam == board[3] && PlayerTeam == board[4] && " " == board[5]) { Pos = 5; } else if (PlayerTeam == board[5] && PlayerTeam == board[4] && " " == board[3]) { Pos = 3; } // Middle Row else if (PlayerTeam == board[3] && PlayerTeam == board[5] && " " == board[4]) { Pos = 4; } else if (PlayerTeam == board[6] && PlayerTeam == board[7] && " " == board[8]) { Pos = 8; } else if (PlayerTeam == board[8] && PlayerTeam == board[7] && " " == board[6]) { Pos = 6; } // Bottom Row else if (PlayerTeam == board[6] && PlayerTeam == board[8] && " " == board[7]) { Pos = 7; } else if (PlayerTeam == board[0] && PlayerTeam == board[3] && " " == board[6]) { Pos = 6; } else if (PlayerTeam == board[6] && PlayerTeam == board[3] && " " == board[0]) { Pos = 0; } // Left Column else if (PlayerTeam == board[0] && PlayerTeam == board[6] && " " == board[3]) { Pos = 3; } else if (PlayerTeam == board[1] && PlayerTeam == board[4] && " " == board[7]) { Pos = 7; } else if (PlayerTeam == board[7] && PlayerTeam == board[4] && " " == board[1]) { Pos = 1; } // Middle Column else if (PlayerTeam == board[1] && PlayerTeam == board[7] && " " == board[4]) { Pos = 4; } else if (PlayerTeam == board[2] && PlayerTeam == board[5] && " " == board[8]) { Pos = 8; } else if (PlayerTeam == board[8] && PlayerTeam == board[5] && " " == board[2]) { Pos = 2; } // Right Column else if (PlayerTeam == board[0] && PlayerTeam == board[6] && " " == board[3]) { Pos = 3; } else if (PlayerTeam == board[0] && PlayerTeam == board[4] && " " == board[8]) { Pos = 8; } else if (PlayerTeam == board[8] && PlayerTeam == board[4] && " " == board[0]) { Pos = 0; } // Diagonal \ else if (PlayerTeam == board[2] && PlayerTeam == board[4] && " " == board[6]) { Pos = 6; } else if (PlayerTeam == board[2] && PlayerTeam == board[4] && " " == board[6]) { Pos = 6; } else if (PlayerTeam == board[6] && PlayerTeam == board[4] && " " == board[2]) { Pos = 2; } // Diagonal / else if (PlayerTeam == board[2] && PlayerTeam == board[6] && " " == board[4]) { Pos = 4; } // Moves to win if (AITeam == board[0] && AITeam == board[1] && " " == board[2]) { Pos = 2; } else if (AITeam == board[2] && AITeam == board[1] && " " == board[0]) { Pos = 0; } // Top Row else if (AITeam == board[0] && AITeam == board[2] && " " == board[1]) { Pos = 1; } else if (AITeam == board[3] && AITeam == board[4] && " " == board[5]) { Pos = 5; } else if (AITeam == board[5] && AITeam == board[4] && " " == board[3]) { Pos = 3; } // Middle Row else if (AITeam == board[3] && AITeam == board[5] && " " == board[4]) { Pos = 4; } else if (AITeam == board[6] && AITeam == board[7] && " " == board[8]) { Pos = 8; } else if (AITeam == board[8] && AITeam == board[7] && " " == board[6]) { Pos = 6; } // Bottom Row else if (AITeam == board[6] && AITeam == board[8] && " " == board[7]) { Pos = 7; } else if (AITeam == board[0] && AITeam == board[3] && " " == board[6]) { Pos = 6; } else if (AITeam == board[6] && AITeam == board[3] && " " == board[0]) { Pos = 0; } // Left Column else if (AITeam == board[0] && AITeam == board[6] && " " == board[3]) { Pos = 3; } else if (AITeam == board[1] && AITeam == board[4] && " " == board[7]) { Pos = 7; } else if (AITeam == board[7] && AITeam == board[4] && " " == board[1]) { Pos = 1; } // Middle Column else if (AITeam == board[1] && AITeam == board[7] && " " == board[4]) { Pos = 4; } else if (AITeam == board[2] && AITeam == board[5] && " " == board[8]) { Pos = 8; } else if (AITeam == board[8] && AITeam == board[5] && " " == board[2]) { Pos = 2; } // Right Column else if (AITeam == board[0] && AITeam == board[6] && " " == board[3]) { Pos = 3; } else if (AITeam == board[0] && AITeam == board[4] && " " == board[8]) { Pos = 8; } else if (AITeam == board[8] && AITeam == board[4] && " " == board[0]) { Pos = 0; } // Diagonal \ else if (AITeam == board[2] && AITeam == board[4] && " " == board[6]) { Pos = 6; } else if (AITeam == board[2] && AITeam == board[4] && " " == board[6]) { Pos = 6; } else if (AITeam == board[6] && AITeam == board[4] && " " == board[2]) { Pos = 2; } // Diagonal / else if (AITeam == board[2] && AITeam == board[6] && " " == board[4]) { Pos = 4; } } board[Pos] = AITeam; UpdateBoard(); commandLineText.text = commandLineText.text + "\n" + ">" + "Player: " + AITeam + " played position: " + (Pos + 1); if (CheckWinCondition() == true) { return; } SetBoardActive(true); LogBoard(); } public void BeginGame() { ResetBoard(); UpdateBoard(); LogBoard(); commandLineText.text = ">" + "Game Begin"; if (PlayerTeam == "X") { SetBoardActive(true); } else { SetBoardActive(false); Invoke("AIMove", aiMoveDelay); // AI makes the first move if Player is "O" } } public void UpdateBoard() { for (int i = 0; i < board.Length; i++) { buttons[i].GetComponentInChildren().text = board[i]; if (board[i] == "X") { buttons[i].GetComponentInChildren().color = Color.red; // Change text color for X } else if (board[i] == "O") { buttons[i].GetComponentInChildren().color = Color.blue; // Change text color for O } } } public void EndGame(string winner) { SetBoardActive(false); commandLineText.text = winner == "Draw" ? commandLineText.text + "\n" + ">" + "It's a Draw!" : commandLineText.text + "\n" + ">" + winner + " Wins!"; playAgainButton.SetActive(true); VictoryText.GetComponent().text = winner == "Draw" ? "It's a Draw!" : winner + " Wins!"; VictoryText.SetActive(true); } }