Tuesday, July 21, 2020

simple java card game?

Rick Duchane: it's not a website design. It's more of the code itself.

Rona Ising: Card games are one of those things that always seem simple, but are actually pretty tough. You're definitely on the right track with the array thing. A simple way to fill an array with numbers representing cards would be:...int[] cardDeck = generateCards();...private int[] generateCards() { int[] array = new int[52]; for(int i = 0; i for(int j = 0; j array[i * 4 + j] = (i + 1); } return array;}Notice though, that that will fill the array with "11" for "J", "12" for "Q" and "13" for "K". Once you have that, you just need a simple "getCard" method or something, which returns a random card from the "cardDeck" array, and removes it. Your "getCard" method and it's usage might look something like this:...final Random RANDOM_NUMBER = new Random();int newCard = getCard(cardDeck);...private int getCard(int[] array) { int card = RANDOM_NUMBER.nextInt(); int tempCard = array[card]; array = ! remove(array, card); return tempCard;}As was mentioned, that method returns a card and removes it from the "cardDeck" array. Finally, since Java Arrays don't have "add"/"remove" methods, unfortunately, you'll need to make one. The "remove" method might be something like this:private int[] remove(int[] array, int index) { int j = 0; int[] temp = new int[array.length - 1]; for(int i = 0; i if(i == index) j++; temp[i] = array[i + j]; } return temp;}That builds and returns a new array with all the elements except the one specified by the "index" parameter.So, that should be enough to get you started, provided I explained it well enough. Let me know if you need anything else explained. Hope that helps!...Show more

No comments:

Post a Comment