246 lines
6.1 KiB
C#
246 lines
6.1 KiB
C#
![]() |
using System.Collections;
|
|||
|
using System.Collections.Generic;
|
|||
|
using UnityEngine;
|
|||
|
using UnityEngine.UI;
|
|||
|
|
|||
|
public class GameManager : MonoBehaviour
|
|||
|
{
|
|||
|
|
|||
|
// Private turn counter.
|
|||
|
private static int turn = 1;
|
|||
|
|
|||
|
// (currently unused) Static function that returns the current turn count.
|
|||
|
public static int GetTurn()
|
|||
|
{
|
|||
|
return turn;
|
|||
|
}
|
|||
|
|
|||
|
/* Used to reset the turn counter at the beginning of every game.
|
|||
|
* Needs to be at the beginning because I want to continue displaying
|
|||
|
* or having the turn counter saved until I can be sure there is a new
|
|||
|
* game starting.
|
|||
|
*/
|
|||
|
public static void ResetTurn()
|
|||
|
{
|
|||
|
turn = 1;
|
|||
|
}
|
|||
|
|
|||
|
/* This function is directly called by the BoardManager when a move is attempted,
|
|||
|
* and determines what spaces will be changed to what based on the game's rules.
|
|||
|
* This is goverened by the following:
|
|||
|
* - Which player's turn it is (which sprite to change to)
|
|||
|
* - What the space's sprite already is
|
|||
|
* - Whether the move is legal based on the game's rules
|
|||
|
*/
|
|||
|
public static void MakeMove(Vector2 bc, int numberofspaces, BoardManager.WhiteSpace[] whitespaces, int[] spaces, int[] sprites, ref bool movemade)
|
|||
|
{
|
|||
|
// Function call to determine which spaces will be changed, see definition below.
|
|||
|
DetermineSpacesChanged(bc, numberofspaces, spaces);
|
|||
|
|
|||
|
// Function call to determine which sprites the spaces will change to, see definition below.
|
|||
|
DetermineSpritesChanged(whitespaces, spaces, sprites);
|
|||
|
|
|||
|
// Increment the turn counter.
|
|||
|
EndTurn();
|
|||
|
|
|||
|
// Tell BoardManager that the move is finished.
|
|||
|
movemade = true;
|
|||
|
}
|
|||
|
|
|||
|
/* This function determines which spaces *can* be changed based on the board's
|
|||
|
* limitations.
|
|||
|
*/
|
|||
|
private static void DetermineSpacesChanged(Vector2 bc, int numberofspaces, int[] spaces)
|
|||
|
{
|
|||
|
// Used to check adjacent spaces without changing the bc reference passed in.
|
|||
|
Vector2 tempbc = bc;
|
|||
|
|
|||
|
// "White Space Index", holder variable to save the return of converting from
|
|||
|
// board coordinates to the equivalent index in the whitespaces array.
|
|||
|
int wsi = 0;
|
|||
|
|
|||
|
/* Intricate set of if/elseif statements to determine which moves are
|
|||
|
* legate according to the board's size values.
|
|||
|
*/
|
|||
|
if(bc.x > 0 && bc.x < (numberofspaces - 1))
|
|||
|
{
|
|||
|
if(bc.y > 0)
|
|||
|
{
|
|||
|
/*
|
|||
|
Debug.Log(
|
|||
|
"changing space:" +
|
|||
|
(bc.x) + " " + (bc.y - 1)
|
|||
|
); */
|
|||
|
tempbc = bc;
|
|||
|
tempbc.y -= 1;
|
|||
|
wsi = BoardManager.ConvertImageIndex(tempbc);
|
|||
|
spaces[3] = wsi;
|
|||
|
}
|
|||
|
if(bc.y < (numberofspaces - 1))
|
|||
|
{
|
|||
|
/*
|
|||
|
Debug.Log(
|
|||
|
"changing space:" +
|
|||
|
(bc.x) + " " + (bc.y + 1)
|
|||
|
); */
|
|||
|
tempbc = bc;
|
|||
|
tempbc.y += 1;
|
|||
|
wsi = BoardManager.ConvertImageIndex(tempbc);
|
|||
|
spaces[1] = wsi;
|
|||
|
}
|
|||
|
/*
|
|||
|
Debug.Log(
|
|||
|
"changing spaces:" +
|
|||
|
(bc.x - 1) + " " + (bc.y) + "\t" +
|
|||
|
(bc.x + 1) + " " + (bc.y)
|
|||
|
); */
|
|||
|
tempbc = bc;
|
|||
|
|
|||
|
tempbc.x -= 1;
|
|||
|
wsi = BoardManager.ConvertImageIndex(tempbc);
|
|||
|
spaces[0] = wsi;
|
|||
|
|
|||
|
tempbc.x += 2;
|
|||
|
wsi = BoardManager.ConvertImageIndex(tempbc);
|
|||
|
spaces[2] = wsi;
|
|||
|
}
|
|||
|
else if(bc.y > 0 && bc.y < (numberofspaces - 1))
|
|||
|
{
|
|||
|
if(bc.x == 0)
|
|||
|
{
|
|||
|
/* Debug.Log(
|
|||
|
"changing space:" +
|
|||
|
(bc.x + 1) + " " + (bc.y)
|
|||
|
); */
|
|||
|
tempbc = bc;
|
|||
|
tempbc.x += 1;
|
|||
|
wsi = BoardManager.ConvertImageIndex(tempbc);
|
|||
|
spaces[2] = wsi;
|
|||
|
}
|
|||
|
if(bc.x == (numberofspaces - 1))
|
|||
|
{
|
|||
|
/* Debug.Log(
|
|||
|
"changing space:" +
|
|||
|
(bc.x - 1) + " " + (bc.y)
|
|||
|
); */
|
|||
|
tempbc = bc;
|
|||
|
tempbc.x -= 1;
|
|||
|
wsi = BoardManager.ConvertImageIndex(tempbc);
|
|||
|
spaces[0] = wsi;
|
|||
|
}
|
|||
|
/* Debug.Log(
|
|||
|
"changing spaces:" +
|
|||
|
(bc.x) + " " + (bc.y - 1) + "\t" +
|
|||
|
(bc.x) + " " + (bc.y + 1)
|
|||
|
); */
|
|||
|
tempbc = bc;
|
|||
|
|
|||
|
tempbc.y -= 1;
|
|||
|
wsi = BoardManager.ConvertImageIndex(tempbc);
|
|||
|
spaces[3] = wsi;
|
|||
|
|
|||
|
tempbc.y += 2;
|
|||
|
wsi = BoardManager.ConvertImageIndex(tempbc);
|
|||
|
spaces[1] = wsi;
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
if(bc.x == 0)
|
|||
|
{
|
|||
|
/* Debug.Log(
|
|||
|
"changing space:" +
|
|||
|
(bc.x + 1) + " " + (bc.y)
|
|||
|
); */
|
|||
|
tempbc = bc;
|
|||
|
tempbc.x += 1;
|
|||
|
wsi = BoardManager.ConvertImageIndex(tempbc);
|
|||
|
spaces[2] = wsi;
|
|||
|
}
|
|||
|
if(bc.x == (numberofspaces - 1))
|
|||
|
{
|
|||
|
/* Debug.Log(
|
|||
|
"changing space:" +
|
|||
|
(bc.x - 1) + " " + (bc.y)
|
|||
|
); */
|
|||
|
tempbc = bc;
|
|||
|
tempbc.x -= 1;
|
|||
|
wsi = BoardManager.ConvertImageIndex(tempbc);
|
|||
|
spaces[0] = wsi;
|
|||
|
}
|
|||
|
if(bc.y > 0)
|
|||
|
{
|
|||
|
/* Debug.Log(
|
|||
|
"changing space:" +
|
|||
|
(bc.x) + " " + (bc.y - 1)
|
|||
|
); */
|
|||
|
tempbc = bc;
|
|||
|
tempbc.y -= 1;
|
|||
|
wsi = BoardManager.ConvertImageIndex(tempbc);
|
|||
|
spaces[3] = wsi;
|
|||
|
}
|
|||
|
if(bc.y < (numberofspaces - 1))
|
|||
|
{
|
|||
|
/* Debug.Log(
|
|||
|
"changing space:" +
|
|||
|
(bc.x) + " " + (bc.y + 1)
|
|||
|
); */
|
|||
|
tempbc = bc;
|
|||
|
tempbc.y += 1;
|
|||
|
wsi = BoardManager.ConvertImageIndex(tempbc);
|
|||
|
spaces[1] = wsi;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/* After determining which spaces are legal to change based on the board's limitations,
|
|||
|
* this function is called to determine what sprites those spaces will change to, given
|
|||
|
* current state of the game and the game's rule set.
|
|||
|
*/
|
|||
|
private static void DetermineSpritesChanged(BoardManager.WhiteSpace[] whitespaces, int[] spaces, int[] sprites)
|
|||
|
{
|
|||
|
int currentplayerturn = (turn -1) % 2;
|
|||
|
|
|||
|
for(int i = 0; i < spaces.Length; ++i)
|
|||
|
{
|
|||
|
// If this is a legal move. No need for an else statement, since if it's not,
|
|||
|
// it will already be ignored by the BoardManager.
|
|||
|
if(spaces[i] >= 0 && spaces[i] < whitespaces.Length)
|
|||
|
{
|
|||
|
// Check what space's sprite is currently set to, then set sprites[i]
|
|||
|
// to the sprite to be changed to, according to the game's rules.
|
|||
|
switch(whitespaces[spaces[i]].spriteindex)
|
|||
|
{
|
|||
|
case 0:
|
|||
|
if(currentplayerturn != 0)
|
|||
|
{
|
|||
|
sprites[i] = 2;
|
|||
|
}
|
|||
|
break;
|
|||
|
case 1:
|
|||
|
if(currentplayerturn != 1)
|
|||
|
{
|
|||
|
sprites[i] = 2;
|
|||
|
}
|
|||
|
break;
|
|||
|
case 2:
|
|||
|
sprites[i] = currentplayerturn;
|
|||
|
break;
|
|||
|
default:
|
|||
|
Debug.Log("Illegal sprite index!");
|
|||
|
return;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/* This should be called to increment the turn counter at the end of every turn.
|
|||
|
* This is at the end because if an illegal move is made or some other rule is
|
|||
|
* broken, it will keep the turn counter from being incremented unnecessarily.
|
|||
|
* E.g. don't increment if the turn isn't actually over.
|
|||
|
*/
|
|||
|
private static void EndTurn()
|
|||
|
{
|
|||
|
++turn;
|
|||
|
}
|
|||
|
}
|