baflink

BoardManager.cs

Jul 7th, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.39 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5. using BeardedManStudios.Network;
  6.  
  7. public class BoardManager : SimpleNetworkedMonoBehavior
  8. {
  9.     public int playerOneWins = 0;
  10.     public int playerTwoWins = 0;
  11.     public int catWins = 0;
  12.  
  13.     public GameObject x = null;
  14.     public GameObject o = null;
  15.  
  16.     private List<GameObject> spawns = new List<GameObject>();
  17.     private byte[] board = new byte[9];
  18.  
  19.     public Transform[] spaces = null;
  20.     public bool playerOneTurn = true;
  21.  
  22.     private bool gameOver = false;
  23.  
  24.     protected override void Start()
  25.     {
  26.         base.Start();
  27.         Setup();
  28.     }
  29.  
  30.     private void Setup()
  31.     {
  32.         for (int i = 0; i < spawns.Count; i++)
  33.             Destroy(spawns[i]);
  34.  
  35.         spawns.Clear();
  36.  
  37.         for (int i = 0; i < board.Length; i++)
  38.             board[i] = 0;
  39.  
  40.         gameOver = false;
  41.     }
  42.  
  43.     protected override void Update()
  44.     {
  45.         base.Update();
  46.  
  47.         if (gameOver)
  48.         {
  49.             if (Input.GetKeyDown(KeyCode.Space))
  50.                 RPC("ResetGame");
  51.  
  52.             return;
  53.         }
  54.  
  55.         if (playerOneTurn != OwningNetWorker.IsServer)
  56.             return;
  57.  
  58.         if (Input.GetMouseButtonDown(0))
  59.         {
  60.             RaycastHit hit;
  61.             if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit))
  62.             {
  63.                 for (int i = 0; i < spaces.Length; i++)
  64.                 {
  65.                     if (hit.transform == spaces[i])
  66.                     {
  67.                         RPC("PlacePiece", i);
  68.                         break;
  69.                     }
  70.                 }
  71.             }
  72.         }
  73.     }
  74.  
  75.     [BRPC]
  76.     private void PlacePiece(int location)
  77.     {
  78.         if (gameOver)
  79.             return;
  80.  
  81.         GameObject spawn = null;
  82.         if (playerOneTurn)
  83.         {
  84.             spawn = Instantiate(x);
  85.             board[location] = 1;
  86.         }
  87.         else
  88.         {
  89.             spawn = Instantiate(o);
  90.             board[location] = 2;
  91.         }
  92.  
  93.         spawn.transform.position = spaces[location].position;
  94.         spawns.Add(spawn);
  95.  
  96.         playerOneTurn = !playerOneTurn;
  97.  
  98.         // Only the server will check the game rules
  99.         if (OwningNetWorker.IsServer)
  100.             EvaluateBoard();
  101.     }
  102.  
  103.     [BRPC]
  104.     private void Winner(byte winner)
  105.     {
  106.         gameOver = true;
  107.  
  108.         switch (winner)
  109.         {
  110.             case 1:
  111.                 Debug.Log("Player One wins!");
  112.                 playerOneWins++;
  113.                 break;
  114.             case 2:
  115.                 Debug.Log("Player Two wins!");
  116.                 playerTwoWins++;
  117.                 break;
  118.             default:
  119.                 Debug.Log("CAT wins!");
  120.                 catWins++;
  121.                 break;
  122.         }
  123.  
  124.         Debug.Log("Scoreboard\nPlayer One: " + playerOneWins + "\nPlayer Two: " + playerTwoWins + "\nCAT: " + catWins);
  125.     }
  126.  
  127.     [BRPC]
  128.     private void ResetGame()
  129.     {
  130.         if (!gameObject)
  131.             return;
  132.  
  133.         Setup();
  134.  
  135.         Debug.Log("Game Reset!");
  136.     }
  137.  
  138.     private void EvaluateBoard()
  139.     {
  140.         if (board[4] != 0)
  141.         {
  142.             if (Equals(board[0], board[4], board[8]) ||
  143.                 Equals(board[2], board[4], board[6]) ||
  144.                 Equals(board[1], board[4], board[7]) ||
  145.                 Equals(board[3], board[4], board[5]))
  146.             {
  147.                 RPC("Winner", board[4]);
  148.                 return;
  149.             }
  150.         }
  151.  
  152.         if (board[0] != 0)
  153.         {
  154.             if (Equals(board[0], board[1], board[2]) ||
  155.                 Equals(board[0], board[3], board[6]))
  156.             {
  157.                 RPC("Winner", board[0]);
  158.                 return;
  159.             }
  160.         }
  161.  
  162.         if (board[8] != 0)
  163.         {
  164.             if (Equals(board[6], board[7], board[8]) ||
  165.                 Equals(board[2], board[5], board[8]))
  166.             {
  167.                 RPC("Winner", board[8]);
  168.                 return;
  169.             }
  170.         }
  171.  
  172.         bool cat = true;
  173.         for (int i = 0; i < board.Length; i++)
  174.         {
  175.             if (board[i] == 0)
  176.             {
  177.                 cat = false;
  178.                 break;
  179.             }
  180.         }
  181.  
  182.         if (cat)
  183.             RPC("Winner", (byte)0);
  184.     }
  185.  
  186.     private bool Equals(params byte[] args)
  187.     {
  188.         for (int i = 0; i < args.Length; i++)
  189.         {
  190.             if (args[0] != args[i])
  191.                 return false;
  192.         }
  193.  
  194.         return true;
  195.     }
  196. }
Advertisement
Add Comment
Please, Sign In to add comment