Advertisement
RamonB

Tile counter

Jan 18th, 2018
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.58 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class ScoreCounter : MonoBehaviour {
  6.  
  7.     public int maxCoord = 10;
  8.     int[,,] scoreArray = new int[20,20,2];
  9.  
  10.     // Use this for initialization
  11.     void Start () {
  12.         CreateScoreArray();
  13.         Invoke("GetTileGrid", 2);
  14.     }
  15.    
  16.     // Update is called once per frame
  17.     void Update () {
  18.        
  19.     }
  20.  
  21.     // Calculate the score for a given color
  22.     void CalculateScore(int color){
  23.         int scoreCounter = 0;
  24.         for(int z = 0; z < maxCoord * 2; z++){
  25.             for(int x = 0; x < maxCoord * 2; x++){
  26.                 Coords tilePos = new Coords(x, z);
  27.                 scoreCounter++;
  28.                 scoreArray[x,z,1] = scoreCounter;
  29.                 List<Coords> colorTiles = CheckColorAroundTile(tilePos, color); // get list of tiles around this tile that has the same color
  30.                 if(colorTiles.Count > 0){
  31.                     int highestColor = FindHighestColorScore(colorTiles, color);
  32.                     if(highestColor > 0){
  33.                         SetColorScoreOnTile(highestColor, scoreCounter);
  34.                     }
  35.                 }
  36.             }
  37.         }
  38.         // TODO find the highest scoring area of this color
  39.     }
  40.  
  41.     // Find the coordinates around a given tile that share the same color
  42.     List<Coords> CheckColorAroundTile(Coords coord, int color){
  43.         List<Coords> colorTiles = new List<Coords>();
  44.         foreach(Coords tile in ReturnAreasToCheck(coord)){
  45.             if(scoreArray[coord.x,coord.y,0] == color){
  46.                 colorTiles.Add(tile);
  47.             }
  48.         }
  49.      return colorTiles;
  50.     }
  51.  
  52.     // Fing the highest score ID in a list of tiles
  53.     int FindHighestColorScore(List<Coords> colorTiles, int color){
  54.         int highestColor = 0;
  55.         foreach(Coords tile in colorTiles){
  56.             if(scoreArray[tile.x,tile.y,0] == color && scoreArray[tile.x,tile.y,1] > highestColor){
  57.                 highestColor = scoreArray[tile.x,tile.y,1];
  58.             }
  59.         }
  60.         return highestColor;
  61.     }
  62.  
  63.     // Loop through all tile spaces on the board to set the old score ID to the latest score ID
  64.     void SetColorScoreOnTile(int oldScore, int colorScoreToSet){
  65.         for(int z=0; z < maxCoord * 2; z++){
  66.             for(int x=0; x < maxCoord * 2; x++){
  67.                 if(scoreArray[x,z,1] == oldScore){
  68.                     scoreArray[x,z,1] = colorScoreToSet;
  69.                 }
  70.             }
  71.         }
  72.     }
  73.  
  74.     // Find tiles that score and put them on the scoring grid
  75.     void GetTileGrid(){
  76.         for (int z = 0; z < maxCoord; z++){
  77.             for (int x = 0; x < maxCoord; x++){
  78.                 int[] tilePosition = new int[]{x,z};
  79.                 Collider[] intersecting = Physics.OverlapSphere(new Vector3(x, 0, z), .2f);
  80.                 foreach(Collider collider in intersecting){
  81.                     GameObject tileBody = collider.gameObject;
  82.                     if(tileBody.name == "Body"){
  83.                         CustomTile ct = tileBody.transform.parent.gameObject.GetComponent<CustomTile>();
  84.                         if(ct){
  85.                             AddScoreToGrid(ct.colorPattern, tilePosition);
  86.                         }
  87.                         else {
  88.                             //create an empty scoring grid
  89.                             int[,] emptyPattern = new int[,]{{0,0},{0,0}};
  90.                             AddScoreToGrid(emptyPattern, tilePosition);
  91.                         }
  92.                     }
  93.                 }
  94.             }
  95.         }
  96.     CalculateScore(1);
  97.     PrintScoreArray();
  98.     }
  99.  
  100.     void AddScoreToGrid(int[,] colorPattern, int[] tilePosition){
  101.         scoreArray[tilePosition[0] * 2, tilePosition[1] * 2, 0] = colorPattern[0,0];
  102.         scoreArray[(tilePosition[0] * 2) + 1, tilePosition[1] * 2, 0] = colorPattern[1,0];
  103.         scoreArray[tilePosition[0] * 2, (tilePosition[1] * 2) + 1, 0] = colorPattern[0,1];
  104.         scoreArray[(tilePosition[0] * 2) + 1, (tilePosition[1] * 2) + 1, 0] = colorPattern[1,1];
  105.     }
  106.  
  107.     // Find the coordinates of the tiles around a given tile, taking into account the limit of the game board
  108.     List<Coords> ReturnAreasToCheck(Coords coord){
  109.         List<Coords> tempList = new List<Coords>();
  110.         if(FindMinLimit(coord.x) >= 0){
  111.             tempList.Add(new Coords(FindMinLimit(coord.x), coord.y));
  112.         }
  113.         if(FindMaxLimit(coord.x) >= 0){
  114.             tempList.Add(new Coords(FindMaxLimit(coord.x), coord.y));
  115.         }
  116.         if(FindMinLimit(coord.y) >= 0){
  117.             tempList.Add(new Coords(coord.x,FindMinLimit(coord.y)));
  118.         }
  119.         if(FindMaxLimit(coord.y) >= 0){
  120.             tempList.Add(new Coords(coord.x,FindMaxLimit(coord.y)));
  121.         }
  122.         return tempList;
  123.     }
  124.  
  125.     // Find the limit of the game board
  126.     int FindMinLimit(int q){
  127.         if(q < 1){return -1;}
  128.         else {return q - 1;}
  129.     }
  130.  
  131.     int FindMaxLimit(int q){
  132.         if(q == (maxCoord * 2) - 1){return -1;}
  133.         else {return q + 1;}
  134.     }
  135.  
  136.     // Create an empty scoring grid
  137.     void CreateScoreArray(){
  138.         for(int i = 0; i < maxCoord * 2; i++){
  139.             for(int j = 0; j < maxCoord * 2; j++){
  140.                 scoreArray[j,i,0] = 0;
  141.                 scoreArray[j,i,1] = 0;
  142.             }
  143.         }
  144.     }
  145.  
  146.     // Test print score grid
  147.     void PrintScoreArray(){
  148.         for(int i = 0; i < 20; i++){
  149.             string rowLine = "";
  150.             for(int j = 0; j < 20; j++){
  151.                 rowLine = rowLine + " " + scoreArray[j,i,1];
  152.             }
  153.             print ("Row " + i + ": " + rowLine);
  154.         }
  155.     }
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement