Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class Game : MonoBehaviour {
- public bool redPlayerTurn = true;
- bool inGame = true;
- public List<Cell> cells;
- Cell[,] board;
- void Start() {
- board = new Cell[3,3];
- foreach (Cell c in cells)
- board[c.position.x,c.position.y] = c;
- }
- public void Update() {
- if (Input.GetKeyDown(KeyCode.R) && !inGame) {
- inGame = true;
- foreach (Cell c in cells)
- c.SetValue(0);
- }
- }
- public void Click(Vector2Int pos) {
- Debug.Log(pos);
- if (inGame) {
- if (board[pos.x, pos.y].GetValue() == 0) {
- board[pos.x, pos.y].SetValue(redPlayerTurn ? 1 : -1);
- CheckGameOver();
- }
- }
- }
- void CheckGameOver() {
- int[] boardInfo = new int[8];
- boardInfo[0] = board[0,0].GetValue() + board[1,0].GetValue() + board[2,0].GetValue();
- boardInfo[1] = board[0,1].GetValue() + board[1,1].GetValue() + board[2,1].GetValue();
- boardInfo[2] = board[0,2].GetValue() + board[1,2].GetValue() + board[2,2].GetValue();
- boardInfo[3] = board[0,0].GetValue() + board[0,1].GetValue() + board[0,2].GetValue();
- boardInfo[4] = board[1,0].GetValue() + board[1,1].GetValue() + board[1,2].GetValue();
- boardInfo[5] = board[2,0].GetValue() + board[2,1].GetValue() + board[2,2].GetValue();
- boardInfo[6] = board[0,0].GetValue() + board[1,1].GetValue() + board[2,2].GetValue();
- boardInfo[7] = board[0,2].GetValue() + board[1,1].GetValue() + board[2,0].GetValue();
- for (int i = 0; i < 8; i++)
- if (boardInfo[i] == 3) {
- GameOver("Red player Win!");
- return;
- } else if (boardInfo[i] == -3) {
- GameOver("Blue player Win!");
- return;
- }
- CheckDraw();
- }
- void CheckDraw() {
- for (int x = 0; x < 3; x++)
- for (int y = 0; y < 3; y++)
- if (board[x,y].GetValue() == 0) {
- redPlayerTurn = !redPlayerTurn;
- return;
- }
- GameOver("Draw!");
- }
- void GameOver(string message) {
- inGame = false;
- Debug.Log(message);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement