Advertisement
leomovskii

TicTacToe

Oct 13th, 2024
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.02 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. public class TicTacToe : MonoBehaviour {
  4.  
  5.     public TicTacToeCell cell;
  6.  
  7.     bool isPlayerTurnRed;
  8.     bool inGame;
  9.  
  10.     private TicTacToeCell[,] cells;
  11.  
  12.     private void Start() {
  13.         cells = new TicTacToeCell[3, 3];
  14.         inGame = true;
  15.  
  16.         for (int i = 0; i < 3; i++) {
  17.             for (int j = 0; j < 3; j++) {
  18.                 cells[i, j] = Instantiate(cell, new Vector3(i, j, 0), Quaternion.identity);
  19.                 cells[i, j].Init(this, i, j);
  20.             }
  21.         }
  22.     }
  23.  
  24.     public void CellPressed(int x, int y) {
  25.         if (!inGame) {
  26.             return;
  27.         }
  28.  
  29.         cells[x, y].Claim(isPlayerTurnRed);
  30.         isPlayerTurnRed = !isPlayerTurnRed;
  31.  
  32.         int[] lines = new int[8];
  33.  
  34.         lines[0] = cells[0, 0].GetNumber() + cells[1, 0].GetNumber() + cells[2, 0].GetNumber();
  35.         lines[1] = cells[0, 1].GetNumber() + cells[1, 1].GetNumber() + cells[2, 1].GetNumber();
  36.         lines[2] = cells[0, 2].GetNumber() + cells[1, 2].GetNumber() + cells[2, 2].GetNumber();
  37.  
  38.         lines[3] = cells[0, 0].GetNumber() + cells[0, 1].GetNumber() + cells[0, 2].GetNumber();
  39.         lines[4] = cells[1, 0].GetNumber() + cells[1, 1].GetNumber() + cells[1, 2].GetNumber();
  40.         lines[5] = cells[2, 0].GetNumber() + cells[2, 1].GetNumber() + cells[2, 2].GetNumber();
  41.  
  42.         lines[6] = cells[0, 0].GetNumber() + cells[1, 1].GetNumber() + cells[2, 2].GetNumber();
  43.         lines[7] = cells[2, 0].GetNumber() + cells[1, 1].GetNumber() + cells[0, 2].GetNumber();
  44.  
  45.         for (int i = 0; i < 8; i++) {
  46.             if (lines[i] == 3) {
  47.                 Debug.Log("Red Player WIN!");
  48.                 inGame = false;
  49.             } else if (lines[i] == -3) {
  50.                 Debug.Log("Green Player WIN!");
  51.                 inGame = false;
  52.             }
  53.         }
  54.  
  55.         if (!HasZero()) {
  56.             Debug.Log("Tie!");
  57.             inGame = false;
  58.         }
  59.     }
  60.  
  61.     private bool HasZero() {
  62.         for (int i = 0; i < 3; i++) {
  63.             for (int j = 0; j < 3; j++) {
  64.                 if (cells[i, j].GetNumber() == 0) {
  65.                     return true;
  66.                 }
  67.             }
  68.         }
  69.         return false;
  70.     }
  71.  
  72.     private void Update() {
  73.         if (Input.GetKeyDown(KeyCode.R)) {
  74.             for (int i = 0; i < 3; i++) {
  75.                 for (int j = 0; j < 3; j++) {
  76.                     cells[i, j].Restart();
  77.                     inGame = true;
  78.                 }
  79.             }
  80.         }
  81.     }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement