Advertisement
leomovskii

Game

Jul 29th, 2022
852
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.97 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Game : MonoBehaviour {
  6.  
  7.     public bool redPlayerTurn = true;
  8.     bool inGame = true;
  9.  
  10.     public List<Cell> cells;
  11.  
  12.     Cell[,] board;
  13.  
  14.     void Start() {
  15.         board = new Cell[3,3];
  16.         foreach (Cell c in cells)
  17.             board[c.position.x,c.position.y] = c;
  18.     }
  19.  
  20.     public void Update() {
  21.         if (Input.GetKeyDown(KeyCode.R) && !inGame) {
  22.             inGame = true;
  23.             foreach (Cell c in cells)
  24.                 c.SetValue(0);
  25.         }
  26.     }
  27.  
  28.     public void Click(Vector2Int pos) {
  29.         Debug.Log(pos);
  30.         if (inGame) {
  31.             if (board[pos.x, pos.y].GetValue() == 0) {
  32.                 board[pos.x, pos.y].SetValue(redPlayerTurn ? 1 : -1);
  33.  
  34.                 CheckGameOver();
  35.             }
  36.         }
  37.     }
  38.  
  39.     void CheckGameOver() {
  40.         int[] boardInfo = new int[8];
  41.  
  42.         boardInfo[0] = board[0,0].GetValue() + board[1,0].GetValue() + board[2,0].GetValue();
  43.         boardInfo[1] = board[0,1].GetValue() + board[1,1].GetValue() + board[2,1].GetValue();
  44.         boardInfo[2] = board[0,2].GetValue() + board[1,2].GetValue() + board[2,2].GetValue();
  45.  
  46.         boardInfo[3] = board[0,0].GetValue() + board[0,1].GetValue() + board[0,2].GetValue();
  47.         boardInfo[4] = board[1,0].GetValue() + board[1,1].GetValue() + board[1,2].GetValue();
  48.         boardInfo[5] = board[2,0].GetValue() + board[2,1].GetValue() + board[2,2].GetValue();
  49.  
  50.         boardInfo[6] = board[0,0].GetValue() + board[1,1].GetValue() + board[2,2].GetValue();
  51.         boardInfo[7] = board[0,2].GetValue() + board[1,1].GetValue() + board[2,0].GetValue();
  52.  
  53.         for (int i = 0; i < 8; i++)
  54.             if (boardInfo[i] == 3) {
  55.                 GameOver("Red player Win!");
  56.                 return;
  57.             } else if (boardInfo[i] == -3) {
  58.                 GameOver("Blue player Win!");
  59.                 return;
  60.             }
  61.  
  62.         CheckDraw();
  63.     }
  64.  
  65.     void CheckDraw() {
  66.         for (int x = 0; x < 3; x++)
  67.             for (int y = 0; y < 3; y++)
  68.                 if (board[x,y].GetValue() == 0) {
  69.                     redPlayerTurn = !redPlayerTurn;
  70.                     return;
  71.                 }
  72.  
  73.         GameOver("Draw!");
  74.     }
  75.  
  76.     void GameOver(string message) {
  77.         inGame = false;
  78.         Debug.Log(message);
  79.     }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement