Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- using BeardedManStudios.Network;
- public class BoardManager : SimpleNetworkedMonoBehavior
- {
- public int playerOneWins = 0;
- public int playerTwoWins = 0;
- public int catWins = 0;
- public GameObject x = null;
- public GameObject o = null;
- private List<GameObject> spawns = new List<GameObject>();
- private byte[] board = new byte[9];
- public Transform[] spaces = null;
- public bool playerOneTurn = true;
- private bool gameOver = false;
- protected override void Start()
- {
- base.Start();
- Setup();
- }
- private void Setup()
- {
- for (int i = 0; i < spawns.Count; i++)
- Destroy(spawns[i]);
- spawns.Clear();
- for (int i = 0; i < board.Length; i++)
- board[i] = 0;
- gameOver = false;
- }
- protected override void Update()
- {
- base.Update();
- if (gameOver)
- {
- if (Input.GetKeyDown(KeyCode.Space))
- RPC("ResetGame");
- return;
- }
- if (playerOneTurn != OwningNetWorker.IsServer)
- return;
- if (Input.GetMouseButtonDown(0))
- {
- RaycastHit hit;
- if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit))
- {
- for (int i = 0; i < spaces.Length; i++)
- {
- if (hit.transform == spaces[i])
- {
- RPC("PlacePiece", i);
- break;
- }
- }
- }
- }
- }
- [BRPC]
- private void PlacePiece(int location)
- {
- if (gameOver)
- return;
- GameObject spawn = null;
- if (playerOneTurn)
- {
- spawn = Instantiate(x);
- board[location] = 1;
- }
- else
- {
- spawn = Instantiate(o);
- board[location] = 2;
- }
- spawn.transform.position = spaces[location].position;
- spawns.Add(spawn);
- playerOneTurn = !playerOneTurn;
- // Only the server will check the game rules
- if (OwningNetWorker.IsServer)
- EvaluateBoard();
- }
- [BRPC]
- private void Winner(byte winner)
- {
- gameOver = true;
- switch (winner)
- {
- case 1:
- Debug.Log("Player One wins!");
- playerOneWins++;
- break;
- case 2:
- Debug.Log("Player Two wins!");
- playerTwoWins++;
- break;
- default:
- Debug.Log("CAT wins!");
- catWins++;
- break;
- }
- Debug.Log("Scoreboard\nPlayer One: " + playerOneWins + "\nPlayer Two: " + playerTwoWins + "\nCAT: " + catWins);
- }
- [BRPC]
- private void ResetGame()
- {
- if (!gameObject)
- return;
- Setup();
- Debug.Log("Game Reset!");
- }
- private void EvaluateBoard()
- {
- if (board[4] != 0)
- {
- if (Equals(board[0], board[4], board[8]) ||
- Equals(board[2], board[4], board[6]) ||
- Equals(board[1], board[4], board[7]) ||
- Equals(board[3], board[4], board[5]))
- {
- RPC("Winner", board[4]);
- return;
- }
- }
- if (board[0] != 0)
- {
- if (Equals(board[0], board[1], board[2]) ||
- Equals(board[0], board[3], board[6]))
- {
- RPC("Winner", board[0]);
- return;
- }
- }
- if (board[8] != 0)
- {
- if (Equals(board[6], board[7], board[8]) ||
- Equals(board[2], board[5], board[8]))
- {
- RPC("Winner", board[8]);
- return;
- }
- }
- bool cat = true;
- for (int i = 0; i < board.Length; i++)
- {
- if (board[i] == 0)
- {
- cat = false;
- break;
- }
- }
- if (cat)
- RPC("Winner", (byte)0);
- }
- private bool Equals(params byte[] args)
- {
- for (int i = 0; i < args.Length; i++)
- {
- if (args[0] != args[i])
- return false;
- }
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment