choice = uicontrol(\'Style\',\'text\',... \'String\', {\'Choose Level:\'},... \'
ID: 3663824 • Letter: C
Question
choice = uicontrol('Style','text',...
'String', {'Choose Level:'},...
'Position', [320 320 100 20],...
'callback',@setmap);
popup = uicontrol('Style','popup',...
'String', {'Easy', 'Normal'},...
'Position', [300 300 100 20],...
'callback',@setpmap);
set(popup,'String');
if popup == 1;
MemoryBoard = cell(4);
[Row,Col]= size(MemoryBoard);
for k= 1:Row
for kc= 1:Col
MemoryBoard{k,kc}= RedDeck{55};
end
end
imshow([MemoryBoard{:,:}])
ShuffleCards = randperm(52);
TimesPlayed= 0;
elseif popup == 2
MemoryBoard = cell(8);
[Row,Col]= size(MemoryBoard);
for k= 1:Row
for kc= 1:Col
MemoryBoard{k,kc}= RedDeck{55};
end
end
imshow([MemoryBoard{:,:}])
ShuffleCards = randperm(52);
TimesPlayed= 0;
end
Im making a card game where i am making a game menu(popup) which asks you the level you want to choose, once the user chose the level then the deck card will show up according to the level you user choose.
Explanation / Answer
package javacards;
import java.util.Random;
public class Deck {
private Card[] cards;
int i;
Deck()
{
i=51;
cards = new Card[52];
int x=0;
for (int a=0; a<=3; a++)
{
for (int b=0; b<=12; b++)
{
cards[x] = new Card(a,b);
x++;
}
}
}
public Card drawFromDeck()
{
Random generator = new Random();
int index=0;
do {
index = generator.nextInt( 52 );
} while (cards[index] == null);
i--;
Card temp = cards[index];
cards[index]= null;
return temp;
}
public int getTotalCards()
{
return i;
}
}
First, we have a constructor which fills our deck with cards of various levels and ranks. Every time we add a card at x, we increment x to put us at a fresh spot. drawFromDeck() returns a random card from our container.
Now, we need to write a test for this program. We want to make sure the cards that get drawn really are drawn in a random order. So, we have Main:
package javacards;
public class Main { public static void main(String[] args)
{
Deck deck = new Deck();
Card C;
System.out.println( deck.getTotalCards() );
while (deck.getTotalCards()!= 0 )
{
C = deck.drawFromDeck();
System.out.println( C.toString() );
}
}
When we run this, we get a good look at how the cards would come off the deck with the level chossed.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.