Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Grzp · Aug 18, 2018 at 05:50 PM · listcardsshuffle

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
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

152 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

shuffling card 0 Answers

Question about "shuffling" a deck of cards 2 Answers

How to add a Gameobject to a list and remove it from Game World? 1 Answer

I'm trying to shuffle an array's order 3 Answers

Creating card game - copy of list to new list - creating cycle 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges