[JAVA]Need fix for an array
I'm making a code that simulates the card game "war," for i do not want to simulate anything harder. this is hard enough D:
Anyway, i need a relevant ArrayList example, because the user's deck size (currently array) will need to be constantly shortened and lengthened.
Here is the code, if it matters.
Code:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Random;
public class mainClass {
public static int[] deck = {2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4,
5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11 }; //2, 3, 4, 5, 6, 7, 8, 9, 10, J, Q, K, A
public static int[] firstUsersPile = {};
public static int[] secondUsersPile = {}; //Robot pile
public static String userName;
public static Random randomCard = new Random();
public static void main(String[] args) {
try {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Welcome to war! The deck is being shuffled"); //lie. it's random anyway.
System.out.println("Please enter your name: ");
if ((userName = in.readLine()) != null) {
System.out.println("Thank you, " + userName);
int deckUsed; //Initialize
for (deckUsed=0;deckUsed<=deck.length;deckUsed++) {
System.out.println(randomCard.nextInt(deck.length + 1)); //Ignore this part, it will be fixed once "Cards" can be removed from the deck,
//Currently, it just prints a random number from 0-52. Just to simulate whether i need to nullify cards that have been placed in a user's deck
}
} //End IF
}
catch (IOException IOEx) {
System.err.println("Error has occurred.\nApplication exiting...");
System.exit(1);
}
}
/*
* This is a simulation of the card game "War," in which each person has 26 cards, and draws, the larger card wins.
* The winner of each 'round' get's the opponent's card, and puts the card on the bottom of their deck. Ties
* usually cause 3 cards to be drawn, the third card being the determining factor, and if another tie it continues,
* but for the sake of keeping time in priority, each person keeps the card. Even though games can continue ENDLESSLY
* because of this, oh well.
*
* VERSION: In Development / 0.0
*
*
* System.in reader reference: http://www.java2s.com/Tutorial/Java/0120__Development/Howtoreadfromstandardinput.htm
*/
}
Re: [JAVA]Need fix for an array
Code:
public class Cardmain
{
public static void main (String[] args)
{
<strong class="highlight">War</strong> wargame = new <strong class="highlight">War</strong>();
wargame.play();
}
}
class Deck
{
static final int numberOfCards = 52;
<strong class="highlight">Card</strong>[] cards = new <strong class="highlight">Card</strong>[numberOfCards];
int index;
public Deck()
{
// Constructs all 52 <strong class="highlight">Card</strong> objects
for (int i = 0;i < numberOfCards; i++)
cards[i] = new <strong class="highlight">Card</strong>(i);
}
public void shuffle()
{
// Shuffles the deck.
<strong class="highlight">Card</strong> temp;
int a;
for (int x = 0; x < numberOfCards; x++)
{
a = (int)(Math.random() * numberOfCards);
temp = cards[x];
cards[x] = cards[a];
cards[a] = temp;
}
}
public <strong class="highlight">Card</strong> getNextCard()
{
// Gets the next <strong class="highlight">card</strong> in the deck.
if (index >= numberOfCards)
index = 0;
int temp = index;
index++;
return cards[temp];
}
}
class <strong class="highlight">Card</strong>
{
// The values 0-12 represent clubs, 13-25 diamonds,
// 26-38 hearts, and 39-51 spades. Within each suit,
// the lowest value is the ace and highest is the king.
int suit = 0; // 0 through 3
int rank = 0; // 0 through 12
public <strong class="highlight">Card</strong>(int s, int r)
{
// Sets suit and rank
if (s <= 12)
{
suit = 0;
rank = r;
}
else if (s >= 13 && s <= 25)
{
suit = 1;
rank = r % 13;
}
else if (s >= 26 && s <= 38)
{
suit = 2;
rank = r % 26;
}
else if (s >= 39 && s <= 51)
{
suit = 3;
rank = r % 39;
}
}
public <strong class="highlight">Card</strong>(int number)
{
this(number, number);
}
public String toString()
{
// Returns a string representation of a <strong class="highlight">Card</strong> in
// a format like so: The Ace of Clubs
String theCard;
String[] cardRank = {"Ace","Two","Three","Four","Five","Six","Seven",
"Eight","Nine","Ten","Jack","Queen","King"};
String[] cardSuit = {"Clubs","Diamonds","Hearts","Spades"};
// cardSuit is not needed for <strong class="highlight">War</strong>:
theCard = (cardRank[rank]);
return theCard;
}
public int compareWar(<strong class="highlight">Card</strong> other)
{
if ((rank == 0) && (other.rank != 0))
return 1;
else if ((other.rank == 0) && (rank != 0))
return -1;
else if (rank > other.rank)
return 1;
else if (other.rank > rank)
return -1;
else
return 0;
}
}
public class <strong class="highlight">War</strong>
{
public final int END_LIMIT = 8;
private Deck warDeck;
private Vector pileAbe = new Vector(30,22); //set capacity at 30
private Vector pileBob = new Vector(30,22); //increment by 22 if needed
public <strong class="highlight">War</strong>(Deck gameDeck)
{
warDeck = gameDeck;
warDeck.shuffle(); //Shuffles deck
}
public void play()
{
for(int i = 0;i < Deck.numberOfCards; i+=2)
pileAbe.addElement(warDeck.getNextCard());
for(int i = 0;i < Deck.numberOfCards; i+=2)
pileBob.addElement(warDeck.getNextCard());
System.out.println("Abe = " + (<strong class="highlight">Card</strong>)pileAbe.elementAt(0));
System.out.println("Bob = " + (<strong class="highlight">Card</strong>)pileBob.elementAt(0));
int test = ((<strong class="highlight">Card</strong>)pileAbe.firstElement()).compareWar((<strong class="highlight">Card</strong>)pileBob.firstElement());
if (test < 0)
System.out.println("Bob wins!");
else if (test > 0)
System.out.println("Abe wins!");
else
System.out.println("Battle!"); //The 2 cards match
System.out.println("==========");
}
}
Hope that helps.