The Problem
I am dealing from another class and into a player's Tray(A class with a List of Tiles and an array of bools to show that a slot in the tray is full). When dealing into the player's tray, I update the list and bool array after each tile. The first element of the bool array never gets filled, and I think its a logic error with my loops. I am looking for suggestions about how to make sure that the first element of the bool array gets populated after updating each tile.
The image shows what happens to the bool array in the inspector when one tile is dealt. The second part of the image is when the second card is dealt.
BackGround
In Unity 2D: I have a Tile class, PlayerTileTray class, PlayerTileTrayManager class and a TileManager class.
TileManager creates a List and adds elements to the list, setting the Tile variables. TileManager has a Deal Method with an OnTileSent(Tile sentTile) event that sends the top tile as a parameter.
PlayerTileTrayManager listens to the event using the OnTileSent Method. PlayerTileTrayManager's job is to take that tileSent and place it in one of several _trays in a PlaceTileInFirstEmptySlotOfTrays method. I am currently using an array of trays called _tray[].
If an next element of the _tray array is null, I Instantiate a gameObject Prefab representing the tray and add a PlayerTileTray instance to the prefab as _tray[trayIndex].
The PlayerTileTray has an array of bools,slotFull[], to show whether a slot is full. In the PlayerTileTrayManager: I loop through _tray and then slots of that tray to call the UpdateTrayWithTile method of PlayerTileTray UpdateTrayWiltTile adds the tile to a List in _tray, then trys to update the _tray bool variables emptyTray, fullTray and slotFull[].
The main problem I am having I am currently using a nested for loop. I have tried do while loops. The first bool element slotFull[0] never gets set to true despite how I try to make it true. When the first tile is dealt, slotFull[0] stays false. When the second tile is dealt both slotFull[0] and slotFull1 are true;
The results of everything else is achieved or will be if I can fix the first element of slotFull updating properly..
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
public class TileManager :MonoBehaviour
{
//For testing
private int _topTile = 0;
private List<Tile> _fullStackTiles = new List<Tile>();
private static int _stockTileCount = 16;
private static int _numberOfTileSets = 4;
public int tilesToDeal = 1;
public static Dictionary <string,Tile> stockTiles = new Dictionary <string,Tile>();
public static List<Tile> shuffledTiles = new List<Tile>() ;
public static int maxTiles = _numberOfTileSets * _stockTileCount;
/ **Singleton Stuff and Delegate,Event and Publisher Method removed for Brevity */
void Awake(){
_instance = this; // <--Singleton
}
void Start(){
AssignTiles ();
createFullStackWithSetOf(_numberOfTileSets);
shuffledTiles = _fullStackTiles;
//Shuffle is located in MyExtensions
shuffledTiles.Shuffle();
}
void AssignTiles(){/* Method Contents removed for Brevity */ }
void createFullStackWithSetOf(int numSets){
//Create a stack of tiles with _numOfTileSets of stockTiles
foreach(KeyValuePair<string,Tile> t in stockTiles){
for(int i = 0; i < numSets; i++)
_fullStackTiles.Add(t.Value);
}
}
public void Deal(int numIilesToDeal){
for(int i = 0; i < numIilesToDeal; i++){
//TileSend method is near the beginning of this script in the Delegate #region
//TileSend (shuffledTiles[_topTile]);
OnTileSent(shuffledTiles[_topTile]);
shuffledTiles.RemoveAt(_topTile);
//create node for instantiated tile
//insert tile into Node
}
}
}
PlayerTileTrayManager
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class PlayerTileTrayManager : MonoBehaviour {
private static int _maxNumberOfTrays = TileManager.maxTiles / PlayerTileTray.trayMax;
private List<Tile> _playerTiles = new List<Tile>();
private GameObject _tilePrefab;
private PlayerTileTray[] _tray = new PlayerTileTray[8];
public Tile tileFromDraw;
public GameObject TrayPrefab;
List <GameObject> _trayObject = new List<GameObject>();
void OnEnable(){
TileManager.Instance.TileSent += OnTileSent;
}
void OnDisable(){
TileManager.Instance.TileSent -= OnTileSent;
}
void OnTileSent(object source, Tile tileSent){
_playerTiles.Add (tileSent);
PlaceTileInFirstEmptySlotOfTrays (tileSent);
GameObject go = PrefabMap.Instance.GetValueByKey(tileSent.tileName);
Instantiate (go,Vector3.zero,Quaternion.identity);
//move _tilePrefab to childLocation
}
void PlaceTileInFirstEmptySlotOfTrays(Tile placeTile)
{
for(int trayIndex = 0;trayIndex < _maxNumberOfTrays;trayIndex++){
//for(int slotIndex = 0; slotIndex < PlayerTileTray.trayMax; slotIndex ++)
int slotIndex = 0;
do{
if(_tray[trayIndex] == null)
{
_trayObject.Add (Instantiate (TrayPrefab, Vector3.zero, Quaternion.identity) as GameObject);
_tray[trayIndex] = _trayObject[trayIndex].GetComponent<PlayerTileTray>();
}
if (!_tray[trayIndex].fullTray)
{
if(!_tray[trayIndex].fullSlot[slotIndex])
{
_tray[trayIndex].UpdateTrayWithTile(placeTile);
Debug.Log (placeTile.tileName + "was placed in the " + trayIndex + " trayIndex iteration and in the " +
slotIndex + " slotIndex iteration.");
return;
}
}
slotIndex++;
}while(slotIndex < PlayerTileTray.trayMax);
}
}
public void showHand(){/* Not relevant to the Question*/ }
//CycleTrays
}
PlayerTileTray.cs
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class PlayerTileTray :MonoBehaviour{
public static int trayMax = 8;
public bool[] fullSlot;
public bool fullTray;
public bool emptyTray;
public Tile tileFromTrayManager;
public List<Tile> tilesInTray = new List<Tile>();
public PlayerTileTray(){
fullSlot = new bool[trayMax];
fullTray = false;
emptyTray = true;
}
void Start(){
for(int i = 0; i < trayMax; i++)
fullSlot[i] = false;
}
public void UpdateTrayWithTile(Tile tileToTray){
tilesInTray.Add (tileToTray);
for (int i = 0; i < tilesInTray.Count; i++)
fullSlot[i] = true;
if(tilesInTray.Count >= 0)
emptyTray = false;
if(tilesInTray.Count >= 8)
fullTray = true;
}
}