2014-12-02 58 views
0

我正在爲MIPS Mars中的一個撲克牌編碼。下面是包含卡片組的數組。MIPS從陣列中高效地移除一個值

cards: .asciiz "2S","2C","2D","2H", 
      "3S","3C","3D","3H", 
      "4S","4C","4D","4H", 
      "5S","5C","5D","5H", 
      "6S","6C","6D","6H", 
      "7S","7C","7D","7H", 
      "8S","8C","8D","8H", 
      "9S","9C","9D","9H", 
      "10S","10C","10D","10H", 
      "JS","JC","JD","JH", 
      "QS","QC","QD","QH", 
      "KS","KC","KD","KH", 
      "AS","AC","AD","AH" 

隨着隨機卡被選中並傳遞給玩家,我想從陣列中移除該卡。 爲了實現這個我想過:

#use recursion to store the cards in another array up to the point of random card 
#skip that random card space and continue storing. 
#prepare to pass out next random card by decrementing random gen value by 1 

這是我能想到的,現在,但是想知道是否有人能想到的更有效的方法。

+0

聽起來很痛苦。爲什麼不只是隨機洗牌呢?那麼「交易」一張卡就很簡單,只要把最後一張卡從列表的末尾拉出即可。當你處理「從中間」時,無需複製清單。 – 2014-12-02 19:43:47

回答

0

如果訂單不重要(聽起來不像),您可以將「卡組」中的最後一張卡移動到被移除的卡佔據的位置。如果您希望將來能夠重新使用該套牌,則可以交換這兩個位置,以便陣列中始終擁有全部52張牌。