Search Unity

Increment position if string data array is different c# (Unity)

Discussion in 'Scripting' started by Ginxx009, May 2, 2018.

  1. Ginxx009

    Ginxx009

    Joined:
    Sep 11, 2016
    Posts:
    89
    Hello guys i need a help about a scoreboard i am making here's the scoreboard i want to achieve .



    I actually have 4 tables and each tables has it's own scoreboard but for this question only for table 1 is i need so here's how I am creating this kind of scoreboard

    Now what i have done so far is this


    Code (CSharp):
    1. if (gametable_no == 1)
    2.             {
    3.                 for (int i = 0; i < tzPlayInfo.Instance.bc_gametable_history_list.Count; i++)
    4.                 {
    5.                     newString[0] += tzPlayInfo.Instance.bc_gametable_history_list[i].r;
    6.                     newString[0] += ",";
    7.                 }
    8.                 string[] groupedString = newString[0].Split(',');
    9.  
    10.                 foreach (string allchars in groupedString)
    11.                 {
    12.                     int x = 0;
    13.                     int y = 0;
    14.  
    15.                     GameObject o = Instantiate(prefab_big_road[0]) as GameObject;
    16.                     o.transform.SetParent(pos_big_road[0]);
    17.                     o.transform.localScale = Vector3.one;
    18.                     o.transform.localPosition = new Vector3(2.0f, -5.0f, 0f);
    19.                     o.transform.localPosition = new Vector3(o.transform.localPosition.x + x,
    20.                         o.transform.localPosition.y + y, o.transform.localPosition.z);
    21.  
    22.  
    23.                     if (allchars.Contains(playerwinnopairboth))
    24.                     {
    25.                         o.GetComponent<UISprite>().spriteName = "layout_player_bigline-01";
    26.                         NGUITools.SetActive(o, true);
    27.                     }
    28.                     if (allchars.Contains(bankerwinnopairboth))
    29.                     {
    30.                         o.GetComponent<UISprite>().spriteName = "layout_banker_bigline-01";
    31.                         NGUITools.SetActive(o, true);
    32.                     }
    33.                     if (allchars.Contains(tienopairboth))
    34.                     {
    35.                         o.GetComponent<UISprite>().spriteName = "layout_tie_bigline-01";
    36.                         NGUITools.SetActive(o, true);
    37.                     }
    38.                 }
    39.             }
    This code will achieve only like this



    The output is generated randomly so for example i have this output data:

    Gametable 1 history : P ,P ,P ,B ,B P,P P,TBP

    What i am trying to do here is that I need to know if the character is **changing** in the `gametable 1 history data`

    For example: *The output must be like this*



    If the value is not changing like `P ,P ,P ` then the position will only increment on y axis then if the next value is different like `B ,` then it will move to the x axis.

    So my question is, Is there a way of detecting if the character on your array of string is different from the the other value?
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    It sounds like if you store the first string in a 'lastString' variable, and you begin your loop at index 1, you can compare 'lastString' to your index, and update it (lastString) as you go.
     
  3. Ginxx009

    Ginxx009

    Joined:
    Sep 11, 2016
    Posts:
    89
    Just like this?

    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using UnityEngine;
    4. using System.Linq;
    5.  
    6. public class BetBoard_Test : MonoBehaviour
    7. {
    8.     [SerializeField] protected GameObject[] prefab_big_road = null;
    9.     [SerializeField] Transform[] pos_big_road = null;
    10.  
    11.     string playerwinnopairboth = "P  ";
    12.     string bankerwinnopairboth = "B  ";
    13.     string tienopairboth = "T  ";
    14.  
    15.     string bankerwinplayerpairnopair = "BP ";
    16.     string bankerwinnopairplayerpair = "B P";
    17.     string bankerwinnopairbankerpair = "B B";
    18.     string bankerwinbankerpairnopair = "BB ";
    19.  
    20.     string playerwinbankerpairnopair = "PB ";
    21.     string playerwinnopairbankerpair = "P B";
    22.     string playerwinnopairplayerpair = "P P";
    23.     string playerwinplayerpairnopair = "PP ";
    24.  
    25.     string tieplayerpairnopair = "TP ";
    26.     string tiebankerpairnopair = "TB ";
    27.     string tienopairplayerpair = "T P";
    28.     string tienopairbankerpair = "T B";
    29.     string alltie = "TTT";
    30.  
    31.     string bankerwinplayerpairbankerpair = "BPB";
    32.     string bankerwinplayerpairplayerpair = "BPP";
    33.     string bankerwinbankerpairbankerpair = "BBB";
    34.  
    35.     string playerwinplayerpairbankerpair = "PPB";
    36.     string playerwinbankerpairbankerpair = "PBB";
    37.     string playerwinplayerpairplayerpair = "PPP";
    38.  
    39.     public void Start()
    40.     {
    41.         NetworkManager.instance.WebSocketServer.OnCallBack_SC_WEBSOCKET_BcSetGame += CallBack_CS_WEBSOCKET_BcSetGame;
    42.     }
    43.  
    44.     private void CallBack_CS_WEBSOCKET_BcSetGame(bool success, Int32 table_no, Int32 gametable_no, Int32 gameset, Int32 gap)
    45.     {
    46.  
    47.  
    48.         if (tzPlayInfo.Instance.BcGameTableNo == gametable_no)
    49.         {
    50.             switch ((GameCommon.GAMEBC_COMMAND)gameset)
    51.             {
    52.                 case GameCommon.GAMEBC_COMMAND.end:
    53.                     //SEND THE PACKET
    54.                     //ORIGINAL CODE
    55.                     for (int i = 0; i < tzPlayInfo.Instance.bc_gametablelist.Count; i++)
    56.                     {
    57.                         NetworkManager.Instance.WebSocketServer.CS_WEBSOCKET_BcGametableHistory(
    58.                        tzPlayInfo.Instance.bc_gametablelist[i].gametable_no,
    59.    0, 0, 0, 0);
    60.                     }
    61.                     WinLog();
    62.                     break;
    63.             }
    64.         }
    65.     }
    66.  
    67.     IEnumerator WinLog_big_road()
    68.     {
    69.         foreach (Transform pos_big_roads in pos_big_road)
    70.         {
    71.             DeleteChildrens(pos_big_roads);
    72.         }
    73.         yield return new WaitForEndOfFrame();
    74.         NetworkManager.Instance.WebSocketServer.OnCallBack_SC_WEBSOCKET_BcGametableHistory += CallBack_CS_WEBSOCKET_BcGametableHistory;
    75.         yield return new WaitForEndOfFrame();
    76.         yield break;
    77.     }
    78.  
    79.     void DeleteChildrens(Transform t)
    80.     {
    81.         NGUITools.DestroyChildren(t);
    82.     }
    83.  
    84.     public void WinLog()
    85.     {
    86.         StartCoroutine(WinLog_big_road());
    87.     }
    88.  
    89.     private void CallBack_CS_WEBSOCKET_BcGametableHistory(bool success, Int32 gametable_no, Int32 year, Int32 month, Int32 day, Int32 shoe_no, bc_gametable_history_list list)
    90.     {
    91.         string[] newString = new string[4];
    92.      
    93.         string[,] table = new string[70, 6];
    94.  
    95.         string previousValue = "placeholder";
    96.         int xIndex = -1;
    97.         int yIndex = 0;
    98.  
    99.         //1st table
    100.         if (gametable_no == 1)
    101.         {
    102.             for (int i = 0; i < tzPlayInfo.Instance.bc_gametable_history_list.Count; i++)
    103.             {
    104.                 newString[0] += tzPlayInfo.Instance.bc_gametable_history_list[i].r;
    105.                 newString[0] += ",";
    106.             }
    107.             //string[] groupedString = GroupString(newString[0]);
    108.             string[] groupedString = newString[0].Split(',');
    109.             foreach (var allchars in groupedString)
    110.             {
    111.                 GameObject o = Instantiate(prefab_big_road[0]) as GameObject;
    112.                 o.transform.SetParent(pos_big_road[0]);
    113.                 o.transform.localScale = Vector3.one;
    114.  
    115.                 if (table.GetLength(0) < xIndex)
    116.                 {
    117.                     break;
    118.                 }
    119.  
    120.                 if (allchars.Equals(previousValue) && table.GetLength(1) < yIndex)
    121.                 {
    122.                     yIndex += 1;
    123.                     table[xIndex, yIndex] = allchars;
    124.                 }
    125.                 else
    126.                 {
    127.                     xIndex += 1;
    128.                     yIndex = 0;
    129.                     table[xIndex, yIndex] = allchars;
    130.                 }
    131.                 previousValue = allchars;
    132.  
    133.                 o.transform.localPosition = new Vector3(xIndex * 15, yIndex * 15, 0f);
    134.  
    135.                 //match the sprites
    136.                 if (previousValue.Contains(playerwinnopairboth))
    137.                 {
    138.                     o.GetComponent<UISprite>().spriteName = "layout_player_bigline-01";
    139.                     NGUITools.SetActive(o, true);
    140.                 }
    141.  
    142.                 if (previousValue.Contains(bankerwinnopairboth))
    143.                 {
    144.                     o.GetComponent<UISprite>().spriteName = "layout_banker_bigline-01";
    145.                     NGUITools.SetActive(o, true);
    146.                 }
    147.  
    148.                 if (previousValue.Contains(tienopairboth))
    149.                 {
    150.                     o.GetComponent<UISprite>().spriteName = "layout_tie_bigline-01";
    151.                     NGUITools.SetActive(o, true);
    152.                 }
    153.  
    154.                 //banker sequel
    155.                 if (previousValue.Contains(bankerwinplayerpairnopair))
    156.                 {
    157.                     o.GetComponent<UISprite>().spriteName = "layout_pairbanker_bigline-01";
    158.                     NGUITools.SetActive(o, true);
    159.                 }
    160.  
    161.                 if (previousValue.Contains(bankerwinnopairplayerpair))
    162.                 {
    163.                     o.GetComponent<UISprite>().spriteName = "layout_pairbanker_bigline-01";
    164.                     NGUITools.SetActive(o, true);
    165.                 }
    166.  
    167.                 if (previousValue.Contains(bankerwinnopairbankerpair))
    168.                 {
    169.                     o.GetComponent<UISprite>().spriteName = "layout_pairbanker_bigline-01";
    170.                     NGUITools.SetActive(o, true);
    171.                 }
    172.  
    173.                 if (previousValue.Contains(bankerwinbankerpairnopair))
    174.                 {
    175.                     o.GetComponent<UISprite>().spriteName = "layout_pairbanker_bigline-01";
    176.                     NGUITools.SetActive(o, true);
    177.                 }
    178.  
    179.                 //player sequel
    180.                 if (previousValue.Contains(playerwinbankerpairnopair))
    181.                 {
    182.                     o.GetComponent<UISprite>().spriteName = "layout_pairplayer_bigline-01";
    183.                     NGUITools.SetActive(o, true);
    184.                 }
    185.  
    186.                 if (previousValue.Contains(playerwinnopairbankerpair))
    187.                 {
    188.                     o.GetComponent<UISprite>().spriteName = "layout_pairplayer_bigline-01";
    189.                     NGUITools.SetActive(o, true);
    190.                 }
    191.  
    192.                 if (previousValue.Contains(playerwinnopairplayerpair))
    193.                 {
    194.                     o.GetComponent<UISprite>().spriteName = "layout_pairplayer_bigline-01";
    195.                     NGUITools.SetActive(o, true);
    196.                 }
    197.  
    198.                 if (previousValue.Contains(playerwinplayerpairnopair))
    199.                 {
    200.                     o.GetComponent<UISprite>().spriteName = "layout_pairplayer_bigline-01";
    201.                     NGUITools.SetActive(o, true);
    202.                 }
    203.  
    204.                 //tie
    205.                 if (previousValue.Contains(tieplayerpairnopair))
    206.                 {
    207.                     o.GetComponent<UISprite>().spriteName = "layout_tie_bigline-01";
    208.                     NGUITools.SetActive(o, true);
    209.                 }
    210.  
    211.                 if (previousValue.Contains(tiebankerpairnopair))
    212.                 {
    213.                     o.GetComponent<UISprite>().spriteName = "layout_tie_bigline-01";
    214.                     NGUITools.SetActive(o, true);
    215.                 }
    216.  
    217.                 if (previousValue.Contains(tienopairplayerpair))
    218.                 {
    219.                     o.GetComponent<UISprite>().spriteName = "layout_tie_bigline-01";
    220.                     NGUITools.SetActive(o, true);
    221.                 }
    222.  
    223.                 if (previousValue.Contains(tienopairbankerpair))
    224.                 {
    225.                     o.GetComponent<UISprite>().spriteName = "layout_tie_bigline-01";
    226.                     NGUITools.SetActive(o, true);
    227.                 }
    228.  
    229.                 if (previousValue.Contains(alltie))
    230.                 {
    231.                     o.GetComponent<UISprite>().spriteName = "layout_tie_bigline-01";
    232.                     NGUITools.SetActive(o, true);
    233.                 }
    234.  
    235.                 //all 3
    236.             }
    237.         }
    238.     }
    239. }
    240.  
    241.  
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Well, ... is it working for you? :)

    It looks like you created a slight variation, -1 x to start and storing "placeholder".

    For future reference, you probably could have cut a lot of that post and still shown your changes. ;)
     
  5. Ginxx009

    Ginxx009

    Joined:
    Sep 11, 2016
    Posts:
    89
    Almost working . The problem is that the'yre all in y axis only
     
  6. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Hmm, well I would try to Debug some values here:
    Code (csharp):
    1.  
    2. if (allchars.Equals(previousValue) && table.GetLength(1) < yIndex)
    3. {
    4.      yIndex += 1;
    5.      table[xIndex, yIndex] = allchars;
    6. }
    7. else
    8. {
    9.      xIndex += 1;
    10.      yIndex = 0;
    11.      table[xIndex, yIndex] = allchars;
    12.  }
    Maybe debug the value of x and y indexes as well as allchars and previousValue.
     
  7. Ginxx009

    Ginxx009

    Joined:
    Sep 11, 2016
    Posts:
    89
    I debug it

    I debug it like this sir

    Code (CSharp):
    1.  if (previousValue.Equals(NewPreviousValue) && table.GetLength(1) < yIndex)
    2.                 {
    3.                     yIndex += 1;
    4.                     table[xIndex, yIndex] = previousValue;
    5.                     Debug.Log("LETS PRINT THE YINDEX += 1 : " + yIndex);
    6.                     Debug.Log("LETS PRINT TABLE[xIndex, yIndex] :" + table[xIndex,yIndex]);
    7.                 }
    8.                 else
    9.                 {
    10.                     xIndex += 1;
    11.                     yIndex = 0;
    12.                     table[xIndex, yIndex] = previousValue;
    13.                     Debug.Log("LETS PRINT THE XINDEX += 1 : " + xIndex);
    14.                     Debug.Log("LETS PRINT THE YINDEX = 0 :" + yIndex);
    15.                     Debug.Log("LETS PRINT TABLE[xIndex, yIndex] :" + table[xIndex, yIndex]);
    16.  
    17.                 }
    And its output is this

    Gametable 1 History : P ,B ,P ,B P,P ,P

    LETS PRINT THE XINDEX : 2
    LETS PRINT THE YINDEX: 0
    LETS PRINT THE TABLE[xIndex, yIndex] : P

    LETS PRINT THE XINDEX : 3
    LETS PRINT THE YINDEX: 0
    LETS PRINT THE TABLE[xIndex, yIndex] : B

    LETS PRINT THE XINDEX : 4
    LETS PRINT THE YINDEX: 0
    LETS PRINT THE TABLE[xIndex, yIndex] : P

    LETS PRINT THE XINDEX : 5
    LETS PRINT THE YINDEX: 0
    LETS PRINT THE TABLE[xIndex, yIndex] : B P

    ->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    LETS PRINT THE XINDEX : 6
    LETS PRINT THE YINDEX: 0
    LETS PRINT THE TABLE[xIndex, yIndex] : P

    LETS PRINT THE XINDEX : 7
    LETS PRINT THE YINDEX: 0
    LETS PRINT THE TABLE[xIndex, yIndex] : P

    this should increment the yaxis because it has the same value
     
  8. Ginxx009

    Ginxx009

    Joined:
    Sep 11, 2016
    Posts:
    89
    Code (CSharp):
    1. if (table.GetLength(0) < xIndex)
    2.                 {
    3.                     break;
    4.                 }
    5.  
    6.                 if (previousValue.Equals(newPreviousValue) && yIndex < table.GetLength(1))
    7.                 {
    8.                     yIndex += 1;
    9.                     table[xIndex, yIndex] = previousValue;
    10.                 }
    11.                 else
    12.                 {
    13.                     xIndex += 1;
    14.                     yIndex = 0;
    15.                     table[xIndex, yIndex] = previousValue;
    16.                 }
    17.                 newPreviousValue = previousValue;
    Thanks . Just missing the condition instead of table.GetLength(1) < yIndex i used this yIndex < table.GetLength(1)
     
  9. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Nice, totally missed that. :) Glad you got it fixed.
     
  10. Ginxx009

    Ginxx009

    Joined:
    Sep 11, 2016
    Posts:
    89
    Yup . Thanks :)