dimanche 1 mars 2015

Java - finding the winner between 4 players using getter (War card game)



Hi there I am trying to get this to calculate the winner from 4 players (The game is the classic card game war) but I can only get it to do two. the first bit of the code is my Card class and the second bit is the main program where there are more if statements for working out the winner. the issue i'm having is with the method "getWinner". I have included the deck class also if that is needed (at the bottom). package warcard;



public class Card
{
private int value, suit;
private String result, suitStr;

/**
* @param num represents value of card
* @param type represents the suit of card
*/

public Card(int num, int type)
{
/**
* Class constructor.
*/

value = num;
suit = type;

switch(value)
{
case 1: result = "Ace";
break;
case 2: result = "Two";
break;
case 3: result = "Three";
break;
case 4: result = "Four";
break;
case 5: result = "Five";
break;
case 6: result = "Six";
break;
case 7: result = "Seven";
break;
case 8: result = "Eight";
break;
case 9: result = "Nine";
break;
case 10: result = "Ten";
break;
case 11: result = "Jack";
break;
case 12: result = "Queen";
break;
case 13: result = "King";
break;
default: result = "";
break;
}


switch(suit)
{
case 1: suitStr = "Clubs";
break;
case 2: suitStr = "Diamonds";
break;
case 3: suitStr = "Hearts";
break;
case 4: suitStr = "Spades";
break;
}

}


/**
* getters
*/

public int getNum()
{
return value;
}



public int getSuit()
{
return suit;
}

/**
* setters
*/

public void setVal(int choice)
{
value = choice;
}

public void setSuit(int choice)
{
suit = choice;
}

/**
* @param play represents Cards object
* @return flag the String result of the game
*/

public String getWinner(Card play)
{
String flag = "";

if(value == play.getNum())
{
if(suit > play.getSuit())
flag = "win";
else if (suit == play.getSuit())
flag = "tie";
else
flag = "lose";
}
else if (value > play.getNum())
flag = "win";
else
flag = "lose";

return flag;

}


/**
* toString method
*/

public String toString()
{
String info = "";
info = info + result + " of " + suitStr;
return info;
}

} /** end Cards class */


main program



package warcard;

public class Warcard
{

public static void main(String[] args)
{
// instantiation of 2 decks
Deck hand1, hand2, hand3, hand4;
hand1 = new Deck();
hand2 = new Deck();
hand3 = new Deck();
hand4 = new Deck();

String result = "";

// shuffle the decks
hand1.shuffle();
hand2.shuffle();
hand3.shuffle();
hand4.shuffle();


/** tracker variables
* wins1 tracks user's wins
* wins2 tracks computer's wins
*/
int count = 0;
int wins1 = 0;
int wins2 = 0;
int wins3 = 0;
int wins4 = 0;
int ties = 0;


for (int i= 0; i < 52; i++)
{


Card tester = hand1.getCard(count);
Card tester2 = hand2.getCard(count);
Card tester3 = hand3.getCard(count);
Card tester4 = hand4.getCard(count);
count++;
System.out.println("Player 1's hand: " + tester);
System.out.println("Player 2's hand: " + tester2);
System.out.println("Player 3's hand: " + tester3);
System.out.println("Player 4's hand: " + tester4);
result = tester.getWinner(tester2);


if(result == "win")
{
System.out.println("Win!");
wins1++;
}
else if(result == "tie")
{
System.out.println("Tie!");
ties++;
}
else
{
System.out.println("Lose!");
wins2++;
}

} // end for loop

System.out.println();
System.out.println("Total score:");
System.out.println();
System.out.println("Player 1's wins: " + wins1 + "\n" + "Player 2's wins: " + wins2 + "\n" + "Player 3's wins: " + wins3 + "\n" + "Player 4's wins: " + wins4 + "\n" + "Ties: " + ties);
if (wins1 > wins2 && wins1 > wins3 && wins1 > wins4)
System.out.println("Player 1 won the game!");
else if(wins2 > wins1 && wins2 > wins3 && wins2 > wins4)
System.out.println("Player 2 wins!");
else if(wins3 > wins1 && wins3 > wins2 && wins3 > wins4)
System.out.println("Player 3 wins!");
else if(wins4 > wins1 && wins4 > wins2 && wins4 > wins3)
System.out.println("Player 4 wins!");
else if(wins1 == wins2 && wins1 == wins3 && wins1 == wins4)
System.out.println("It's a tie!");


} // end main

} // end class


Deck class



package warcard;

import java.util.Random;

public class Deck
{
private Card[] deck;
private int[] suit = {1, 2, 3, 4};
private int[] numbers = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14};
private int count;

public Deck()
{
deck = new Card[52];
count = 0;

for(int i = 0; i <= 3; i++)
{
for(int k = 2; k <= 14; k++)
{
deck[count] = new Card(k, suit[i]);
count++;
}
} /**
* end outer loop
*/

}
/**
* end constructor
*/



public Card getCard(int index)
{

return deck[index];
}




public void shuffle()
{
int rand;
Random mix = new Random();

for (int i = 0; i < deck.length; i++)
{
/**
* shuffles deck
*/

rand = mix.nextInt(deck.length);
Card temp = deck[i];
deck[i] = deck[rand];
deck[rand] = temp;

} /**
* end for loop
*/

}



} /** end DeckCards class */



Aucun commentaire:

Enregistrer un commentaire