problem with shuffling cards
Hi, I had a problem with shuffling card in memory game. I had a class like:
public GameObject TilePrefab;
public Sprite[] sprites;
Tile[] Tiles;
public Vector2 TilesOffset = Vector2.one;
public int Width = 5;
public int Height = 5;
public bool CanMove = false;
public TextMesh WinText;
IEnumerator Start()
{
WinText.GetComponent<Renderer>().enabled = false;
CreateTiles();
ShuffleTiles();
PlaceTiles();
CanMove = false;
yield return new WaitForSeconds(2f);
CanMove = true;
HideTiles();
}
void CreateTiles()
{
var length = Width * Height;
Tiles = new Tile[length];
for (int i = 0; i < length; i++)
{
var sprite = sprites[i];
Tiles[i] = CreateTile(sprite);
}
}
Tile CreateTile(Sprite faceSprite)
{
var gameobject = Instantiate(TilePrefab);
gameobject.transform.parent = transform;
var tile = gameobject.GetComponent<Tile>();
tile.Uncovered = true;
tile.frontFace = faceSprite;
return tile;
}
void ShuffleTiles()
{
for (int i = 0; i < Tiles.Length; i++)
{
int index1 = Random.Range(0, Tiles.Length);
int index2 = Random.Range(0, Tiles.Length);
var tile1 = Tiles[index1];
var tile2 = Tiles[index2];
Tiles[index1] = tile2;
Tiles[index2] = tile1;
}
}
void PlaceTiles()
{
for (int i = 0; i < Width * Height; i++)
{
int x = i % Width;
int y = i / Width;
Tiles[i].transform.localPosition = new Vector3(x * TilesOffset.x, y * TilesOffset.y, 10);
}
}
void HideTiles()
{
Tiles.ToList().ForEach(tile => tile.Uncovered = false);
}
bool CheckIfEnd()
{
return Tiles.All(tile => !tile.Active);
}
public void CheckPair()
{
StartCoroutine(CheckPairCoroutineForThreeCard());
}
IEnumerator CheckPairCoroutine()
{
var tilesUncovered = Tiles
.Where(tile => tile.Active)
.Where(tile => tile.Uncovered)
.ToArray();
if (tilesUncovered.Length != 2)
yield break;
var tile0 = Tiles[0];
var tile1 = Tiles[1];
var tile2 = Tiles[2];
var tile3 = Tiles[3];
var tile4 = Tiles[4];
var tile5 = Tiles[5];
var tile6 = Tiles[6];
var tile7 = Tiles[7];
var tile8 = Tiles[8];
CanMove = false;
yield return new WaitForSeconds(0.5f);
CanMove = true;
if (tile0.Uncovered==true&& tile3.Uncovered == true)
{
tile0.Active = false;
tile3.Active = false;
}
if (tile1.Uncovered == true && tile2.Uncovered == true)
{
tile1.Active = false;
tile2.Active = false;
}
if (tile1.Uncovered==true &&tile4.Uncovered==true)
{
tile1.Active = false;
tile4.Active = false;
}
if (tile2.Uncovered == true && tile4.Uncovered == true)
{
tile2.Active = false;
tile4.Active = false;
}
if (tile0.Uncovered == true && tile5.Uncovered == true)
{
tile0.Active = false;
tile5.Active = false;
}
if (tile3.Uncovered == true && tile5.Uncovered == true)
{
tile3.Active = false;
tile5.Active = false;
}
if (tile6.Uncovered == true && tile7.Uncovered == true)
{
tile6.Active = false;
tile7.Active = false;
}
if (tile6.Uncovered == true && tile8.Uncovered == true)
{
tile8.Active = false;
tile6.Active = false;
}
if (tile7.Uncovered == true && tile8.Uncovered == true)
{
tile7.Active = false;
tile8.Active = false;
}
else
{
tile0.Uncovered = false;
tile1.Uncovered = false;
tile2.Uncovered = false;
tile3.Uncovered = false;
tile4.Uncovered = false;
tile5.Uncovered = false;
tile6.Uncovered = false;
tile7.Uncovered = false;
tile8.Uncovered = false;
}
if (CheckIfEnd())
{
CanMove = true;
WinText.GetComponent<Renderer>().enabled = true;
yield return new WaitForSeconds(5f);
Application.Quit();
}
}
IEnumerator CheckPairCoroutineForThreeCard()
{
var tilesUncovered = Tiles
.Where(tile => tile.Active)
.Where(tile => tile.Uncovered)
.ToArray();
if (tilesUncovered.Length != 3)
yield break;
var tile0 = Tiles[0];
var tile1 = Tiles[1];
var tile2 = Tiles[2];
var tile3 = Tiles[3];
var tile4 = Tiles[4];
var tile5 = Tiles[5];
var tile6 = Tiles[6];
var tile7 = Tiles[7];
var tile8 = Tiles[8];
CanMove = false;
yield return new WaitForSeconds(0.5f);
CanMove = true;
if (tile0.Uncovered == true && tile3.Uncovered == true&& tile5.Uncovered==true)
{
tile0.Active = false;
tile3.Active = false;
tile5.Active = false;
}
if (tile1.Uncovered == true && tile2.Uncovered == true && tile4.Uncovered == true)
{
tile1.Active = false;
tile2.Active = false;
tile4.Active = false;
}
if (tile6.Uncovered == true && tile7.Uncovered == true && tile8.Uncovered == true)
{
tile6.Active = false;
tile7.Active = false;
tile8.Active = false;
}
else
{
tile0.Uncovered = false;
tile1.Uncovered = false;
tile2.Uncovered = false;
tile3.Uncovered = false;
tile4.Uncovered = false;
tile5.Uncovered = false;
tile6.Uncovered = false;
tile7.Uncovered = false;
tile8.Uncovered = false;
}
if (CheckIfEnd())
{
CanMove = true;
WinText.GetComponent<Renderer>().enabled = true;
yield return new WaitForSeconds(5f);
Application.Quit();
}
}
} And there I had a funtcion shuffle, where I change card place, but only card and when I play I need click on the card, where lenght on "Tiles" is the same in Coroutine. So I want to assign Tiles to Sprites. Is it possible? When I do it like this:
void ShuffleTiles()
{
for (int i = 0; i < Tiles.Length; i++)
{
int index1 = Random.Range(0, Tiles.Length);
int index2 = Random.Range(0, Tiles.Length);
//int index3 = Random.Range(0, sprites.Length);
//int index4 = Random.Range(0, sprites.Length);
var tile1 = Tiles[index1];
var tile2 = Tiles[index2];
var tile3 = sprites[index1];
var tile4 = sprites[index2];
Tiles[index1] = tile2;
Tiles[index2] = tile1;
sprites[index1] = tile3;
sprites[index2] = tile4;
}
}
It dosen't work. Any ideas how can I do it?
Comment