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 ScoreCounter : MonoBehaviour {
- public int maxCoord = 10;
- int[,,] scoreArray = new int[20,20,2];
- // Use this for initialization
- void Start () {
- CreateScoreArray();
- Invoke("GetTileGrid", 2);
- }
- // Update is called once per frame
- void Update () {
- }
- // Calculate the score for a given color
- void CalculateScore(int color){
- int scoreCounter = 0;
- for(int z = 0; z < maxCoord * 2; z++){
- for(int x = 0; x < maxCoord * 2; x++){
- Coords tilePos = new Coords(x, z);
- scoreCounter++;
- scoreArray[x,z,1] = scoreCounter;
- List<Coords> colorTiles = CheckColorAroundTile(tilePos, color); // get list of tiles around this tile that has the same color
- if(colorTiles.Count > 0){
- int highestColor = FindHighestColorScore(colorTiles, color);
- if(highestColor > 0){
- SetColorScoreOnTile(highestColor, scoreCounter);
- }
- }
- }
- }
- // TODO find the highest scoring area of this color
- }
- // Find the coordinates around a given tile that share the same color
- List<Coords> CheckColorAroundTile(Coords coord, int color){
- List<Coords> colorTiles = new List<Coords>();
- foreach(Coords tile in ReturnAreasToCheck(coord)){
- if(scoreArray[coord.x,coord.y,0] == color){
- colorTiles.Add(tile);
- }
- }
- return colorTiles;
- }
- // Fing the highest score ID in a list of tiles
- int FindHighestColorScore(List<Coords> colorTiles, int color){
- int highestColor = 0;
- foreach(Coords tile in colorTiles){
- if(scoreArray[tile.x,tile.y,0] == color && scoreArray[tile.x,tile.y,1] > highestColor){
- highestColor = scoreArray[tile.x,tile.y,1];
- }
- }
- return highestColor;
- }
- // Loop through all tile spaces on the board to set the old score ID to the latest score ID
- void SetColorScoreOnTile(int oldScore, int colorScoreToSet){
- for(int z=0; z < maxCoord * 2; z++){
- for(int x=0; x < maxCoord * 2; x++){
- if(scoreArray[x,z,1] == oldScore){
- scoreArray[x,z,1] = colorScoreToSet;
- }
- }
- }
- }
- // Find tiles that score and put them on the scoring grid
- void GetTileGrid(){
- for (int z = 0; z < maxCoord; z++){
- for (int x = 0; x < maxCoord; x++){
- int[] tilePosition = new int[]{x,z};
- Collider[] intersecting = Physics.OverlapSphere(new Vector3(x, 0, z), .2f);
- foreach(Collider collider in intersecting){
- GameObject tileBody = collider.gameObject;
- if(tileBody.name == "Body"){
- CustomTile ct = tileBody.transform.parent.gameObject.GetComponent<CustomTile>();
- if(ct){
- AddScoreToGrid(ct.colorPattern, tilePosition);
- }
- else {
- //create an empty scoring grid
- int[,] emptyPattern = new int[,]{{0,0},{0,0}};
- AddScoreToGrid(emptyPattern, tilePosition);
- }
- }
- }
- }
- }
- CalculateScore(1);
- PrintScoreArray();
- }
- void AddScoreToGrid(int[,] colorPattern, int[] tilePosition){
- scoreArray[tilePosition[0] * 2, tilePosition[1] * 2, 0] = colorPattern[0,0];
- scoreArray[(tilePosition[0] * 2) + 1, tilePosition[1] * 2, 0] = colorPattern[1,0];
- scoreArray[tilePosition[0] * 2, (tilePosition[1] * 2) + 1, 0] = colorPattern[0,1];
- scoreArray[(tilePosition[0] * 2) + 1, (tilePosition[1] * 2) + 1, 0] = colorPattern[1,1];
- }
- // Find the coordinates of the tiles around a given tile, taking into account the limit of the game board
- List<Coords> ReturnAreasToCheck(Coords coord){
- List<Coords> tempList = new List<Coords>();
- if(FindMinLimit(coord.x) >= 0){
- tempList.Add(new Coords(FindMinLimit(coord.x), coord.y));
- }
- if(FindMaxLimit(coord.x) >= 0){
- tempList.Add(new Coords(FindMaxLimit(coord.x), coord.y));
- }
- if(FindMinLimit(coord.y) >= 0){
- tempList.Add(new Coords(coord.x,FindMinLimit(coord.y)));
- }
- if(FindMaxLimit(coord.y) >= 0){
- tempList.Add(new Coords(coord.x,FindMaxLimit(coord.y)));
- }
- return tempList;
- }
- // Find the limit of the game board
- int FindMinLimit(int q){
- if(q < 1){return -1;}
- else {return q - 1;}
- }
- int FindMaxLimit(int q){
- if(q == (maxCoord * 2) - 1){return -1;}
- else {return q + 1;}
- }
- // Create an empty scoring grid
- void CreateScoreArray(){
- for(int i = 0; i < maxCoord * 2; i++){
- for(int j = 0; j < maxCoord * 2; j++){
- scoreArray[j,i,0] = 0;
- scoreArray[j,i,1] = 0;
- }
- }
- }
- // Test print score grid
- void PrintScoreArray(){
- for(int i = 0; i < 20; i++){
- string rowLine = "";
- for(int j = 0; j < 20; j++){
- rowLine = rowLine + " " + scoreArray[j,i,1];
- }
- print ("Row " + i + ": " + rowLine);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement