-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement card organisation and movement between decks
- extension of ArrayList for Deck class to provide methods for movement between e.g. handcards, playarea, discard-pile … - basic Market + purchase functionality - Singleton for market as only one per game-session is allowed closes #24, closes #21, relates to #28, relates to #55, #54, #53, relates to #80
- Loading branch information
Showing
4 changed files
with
144 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* Licensed under GNU GPL v3.0 (C) 2023 */ | ||
package at.vunfer.openrealms.model; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Random; | ||
|
||
public class Deck<T> extends ArrayList<T> { | ||
private static final Random rand = new Random(); | ||
|
||
public T drawRandom() throws IllegalStateException { | ||
if (super.isEmpty()) { | ||
throw new IllegalStateException("Cannot draw from empty source."); | ||
} | ||
|
||
int index = rand.nextInt(super.size()); | ||
T drawn = super.get(index); | ||
super.remove(drawn); | ||
|
||
return drawn; | ||
} | ||
|
||
public T draw(T item) throws IllegalArgumentException { | ||
if (!super.contains(item)) { | ||
throw new IllegalArgumentException("Item not in deck."); | ||
} | ||
super.remove(item); | ||
return item; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,47 @@ | ||
/* Licensed under GNU GPL v3.0 (C) 2023 */ | ||
package at.vunfer.openrealms.model; | ||
|
||
public class Market {} | ||
import android.util.Log; | ||
import java.util.ArrayList; | ||
|
||
public class Market { | ||
private static Market INSTANCE; | ||
private static final int TOTAL_PURCHASABLE = 5; | ||
private static final String TAG = "PlayerCards"; | ||
Deck<Card> marketDeck; | ||
Deck<Card> forPurchase; | ||
|
||
private Market() {} | ||
|
||
public static Market getInstance() { | ||
if (INSTANCE == null) { | ||
INSTANCE = new Market(); | ||
} | ||
return INSTANCE; | ||
} | ||
|
||
public ArrayList<Card> getForPurchase() { | ||
return forPurchase; | ||
} | ||
|
||
public int restock() { | ||
int restocked = 0; | ||
while (forPurchase.size() < TOTAL_PURCHASABLE) { | ||
try { | ||
forPurchase.add(marketDeck.drawRandom()); | ||
restocked++; | ||
} catch (Exception e) { | ||
Log.v(TAG, "You have no more cards to draw for the market."); | ||
} | ||
} | ||
return restocked; | ||
} | ||
|
||
public Card purchase(Card card) { | ||
if (card == null || !forPurchase.contains(card)) { | ||
throw new IllegalArgumentException("Card is not to purchase."); | ||
} | ||
forPurchase.remove(card); | ||
return card; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters