Last active
December 17, 2015 07:38
-
-
Save jacks205/5573875 to your computer and use it in GitHub Desktop.
A simulation of the card game "War." You enter the amount of games you want to simulate and it will give you: Average Wars,
Average Double Wars,
Average Battles,
Max/Min Battles per game,
Max/Min Wars per game,
Max/Min Double Wars per game Uses LinkedLists to keep track of decks and exchanges of cards.
This file contains hidden or 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
public class Card { | |
private int value; //didnt include suit because it messed with calculations (not important for the War game, but in the future I will try to include things like that for reuse-ability) | |
public Card(int i){ | |
value = i; | |
} | |
public int getCardValue(){ | |
return value; | |
} | |
} |
This file contains hidden or 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
import java.util.LinkedList; | |
import java.util.Random; | |
public class Deck { | |
private LinkedList<Card> deck; | |
public Deck(){ | |
deck = new LinkedList<Card>(); | |
for(int i = 2; i <= 14; ++i){ | |
Card card = new Card(i); | |
deck.add(card); //accounting 2 to Ace for diamonds | |
} | |
for(int i = 2; i <= 14; ++i){ | |
Card card = new Card(i); | |
deck.add(card); //accounting for clubs | |
} | |
for(int i = 2; i <= 14; ++i){ | |
Card card = new Card(i); | |
deck.add(card); //accounting for spades | |
} | |
for(int i = 2; i <= 14; ++i){ | |
Card card = new Card(i); | |
deck.add(card); //accounting for hearts | |
} | |
} | |
public Card deal(){ //returns a random card from the deck to shuffle | |
Random random = new Random(); | |
Card card = deck.get(random.nextInt(deck.size())); | |
deck.remove(card); | |
return card; | |
} | |
} |
This file contains hidden or 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
public class Game { | |
private int battles; | |
private int wars; | |
private int warsDub; | |
public Game(){ | |
this.battles = battles; | |
this.wars = wars; | |
this.warsDub = warsDub; | |
} | |
public void play(){ | |
Player play = new Player(); | |
battles = 0; | |
wars = 0; | |
warsDub = 0; | |
play.shuffle(play.getPlayer1Deck()); | |
play.shuffle(play.getPlayer2Deck()); | |
while(play.hasWon() == false){ | |
if(battles % 52 == 0 && battles > 0){ | |
play.shuffle(play.getPlayer1Deck()); | |
play.shuffle(play.getPlayer2Deck()); | |
} | |
// System.out.println("Player 1 = " + play.getPlayer1Deck().getFirst().getCardValue()); | |
// System.out.println("Player 2 = " + play.getPlayer2Deck().getFirst().getCardValue()); | |
if(play.getPlayer1Deck().getFirst().getCardValue() == play.getPlayer2Deck().getFirst().getCardValue()){ | |
wars += play.war(); | |
if(play.war() == 2){ | |
warsDub++; | |
} | |
}else if(play.getPlayer1Deck().getFirst().getCardValue() > play.getPlayer2Deck().getFirst().getCardValue()){ | |
battles++; | |
play.collect(play.getPlayer1Deck()); | |
play.collect(play.getPlayer2Deck()); | |
play.flip(play.getPlayer1Deck()); | |
play.flip(play.getPlayer2Deck()); | |
play.player1Wins(); | |
// System.out.println("Player 1 Wins" + "Size : " + play.getPlayer1Deck().size()); | |
} | |
else{ | |
battles++; | |
play.collect(play.getPlayer1Deck()); | |
play.collect(play.getPlayer2Deck()); | |
play.flip(play.getPlayer1Deck()); | |
play.flip(play.getPlayer2Deck()); | |
play.player2Wins(); | |
// System.out.println("Player 2 Wins" + "Size : " + play.getPlayer2Deck().size()); | |
} | |
} | |
play.getPlacedCards().clear(); | |
play.getPlayer1Deck().clear(); | |
play.getPlayer2Deck().clear(); | |
// System.out.println("Game Over"); | |
} | |
public int getBattles(){ | |
return battles; | |
} | |
public int getWars(){ | |
return wars; | |
} | |
public int getWarsDub(){ | |
return warsDub; | |
} | |
} |
This file contains hidden or 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
import java.util.LinkedList; | |
import java.util.Random; | |
public class Player { | |
private LinkedList<Card> player1Deck = new LinkedList<Card>(); | |
private LinkedList<Card> player2Deck = new LinkedList<Card>(); | |
private LinkedList<Card> placedCards = new LinkedList<Card>(); | |
public Player(){ //constructor | |
Deck deck = new Deck(); | |
for(int i = 0; i < 26; ++i){ //shuffling cards between players | |
player1Deck.add(deck.deal()); | |
} | |
for(int i = 0; i < 26; ++i){ //shuffling cards between players | |
player2Deck.add(deck.deal()); | |
} | |
} | |
public LinkedList<Card> getPlayer1Deck(){ //accessor | |
return player1Deck; | |
} | |
public LinkedList<Card> getPlayer2Deck(){ //accessor | |
return player2Deck; | |
} | |
public LinkedList<Card> getPlacedCards(){ | |
return placedCards; | |
} | |
public void collect(LinkedList<Card> deck){ //adds first card of a deck to the pile | |
placedCards.add(deck.getFirst()); | |
} | |
public void flip(LinkedList<Card> deck){ //removes the first card of a deck | |
deck.removeFirst(); | |
} | |
public void player1Wins(){ //if player 1 wins a battle/war | |
player1Deck.addAll(placedCards); | |
placedCards.clear(); | |
} | |
public void player2Wins(){ //if player 2 wins a battle/war | |
player2Deck.addAll(placedCards); | |
placedCards.clear(); | |
} | |
public boolean hasWon(){ //checks if someone has all 52 cards or 0 cards | |
if(player1Deck.size() == 52|| player2Deck.size() == 52){ | |
return true; | |
} | |
else{ | |
return false; | |
} | |
} | |
public boolean hasOneCard(){ //checks to see if someone has one card left | |
if(player1Deck.size() == 1 || player2Deck.size() == 1){ | |
return true; | |
} | |
else{ | |
return false; | |
} | |
} | |
public boolean hasThreeCards(){ //checks to see if someone has three cards left | |
if(player1Deck.size() == 3 || player2Deck.size() == 3){ | |
return true; | |
} | |
else{ | |
return false; | |
} | |
} | |
public boolean hasTwoCards(){ //checks to see if someone has two cards left | |
if(player1Deck.size() == 2 || player2Deck.size() == 2){ | |
return true; | |
} | |
else{ | |
return false; | |
} | |
} | |
public int war(){ //plays through war, throwing down 3 cards and flipping up the 4th. Takes in account if someone runs out of cards | |
int warCounter = 0; | |
// System.out.println("WAR"); | |
do{ | |
while(player1Deck.getFirst().getCardValue() == player2Deck.getFirst().getCardValue() && hasTwoCards() && hasOneCard() == false){ // a person has 2 cards left | |
// System.out.println("TYPE 1"); | |
collect(player1Deck); | |
collect(player2Deck); | |
flip(player1Deck); | |
flip(player2Deck); | |
// System.out.println("Player 1 = " + player1Deck.getFirst().getCardValue()); | |
// System.out.println("Player 2 = " + player2Deck.getFirst().getCardValue()); | |
warCounter++; | |
break; | |
} | |
while(player1Deck.getFirst().getCardValue() == player2Deck.getFirst().getCardValue() && hasThreeCards() && hasOneCard() == false){ //a person has 3 cards left | |
// System.out.println("TYPE 2"); | |
for(int i = 0; i < 2; ++i){ | |
collect(player1Deck); | |
flip(player1Deck); | |
collect(player2Deck); | |
flip(player2Deck); | |
if(hasOneCard()){ | |
break; | |
} | |
// System.out.println("Player 1 = " + player1Deck.getFirst().getCardValue()); | |
// System.out.println("Player 2 = " + player2Deck.getFirst().getCardValue()); | |
} | |
warCounter++; | |
} | |
while(player1Deck.getFirst().getCardValue() == player2Deck.getFirst().getCardValue() && hasTwoCards() == false && hasThreeCards() == false && hasOneCard() == false){ // more than 3 cards left | |
// System.out.println("TYPE 3"); | |
for(int i = 0; i < 3; ++i){ | |
collect(player1Deck); | |
flip(player1Deck); | |
collect(player2Deck); | |
flip(player2Deck); | |
if(hasOneCard()){ | |
break; | |
} | |
// System.out.println("Player 1 = " + player1Deck.getFirst().getCardValue()); | |
// System.out.println("Player 2 = " + player2Deck.getFirst().getCardValue()); | |
} | |
warCounter++; | |
} | |
}while(player1Deck.getFirst().getCardValue() == player2Deck.getFirst().getCardValue() && hasOneCard() == false); | |
while(player1Deck.getFirst().getCardValue() == player2Deck.getFirst().getCardValue() && hasOneCard()){ // takes in account if on the last flip, and someone has 1 card left, to keep flipping the other persons deck until they dont match | |
// System.out.println("TYPE 4"); | |
if(player2Deck.size() == 1){ | |
for(int i = 0; i < 3; ++i){ | |
collect(player1Deck); | |
flip(player1Deck); | |
// System.out.println("Player 1 = " + player1Deck.getFirst().getCardValue()); | |
// System.out.println("Player 2 = " + player2Deck.getFirst().getCardValue()); | |
} | |
}else if(player1Deck.size() == 1){ | |
for(int i = 0; i < 3; ++i){ | |
collect(player2Deck); | |
flip(player2Deck); | |
// System.out.println("Player 1 = " + player1Deck.getFirst().getCardValue()); | |
// System.out.println("Player 2 = " + player2Deck.getFirst().getCardValue()); | |
} | |
} | |
} | |
if(player1Deck.getFirst().getCardValue() > player2Deck.getFirst().getCardValue()){ | |
player1Wins(); | |
// System.out.println("Player 1 Wins" + "Size : " + getPlayer1Deck().size()); | |
} | |
else if(player2Deck.getFirst().getCardValue() > player1Deck.getFirst().getCardValue()){ | |
player2Wins(); | |
// System.out.println("Player 2 Wins" + "Size : " + getPlayer2Deck().size()); | |
} | |
// System.out.println("WAR END"); | |
return warCounter; | |
} | |
//shuffler | |
public void shuffle(LinkedList<Card> list){ //I found that we had problems with game simulations being ran with 50+ games, and messed around and found a shuffle method that shuffles the cards every 52 games helps for some weird instance | |
Random random = new Random(); | |
LinkedList<Card> shuffleDeck = new LinkedList<Card>(); | |
for(int i = 0; i < list.size(); ++i){ | |
shuffleDeck.add(list.remove(random.nextInt(list.size()))); //shuffles players cards | |
} | |
list.addAll(shuffleDeck); //re-adds shuffled deck | |
} | |
} |
This file contains hidden or 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
public class Simulation { | |
private int totalWars; | |
private int totalWarsDub; | |
private int totalBattles; | |
private double avgBattlesPerGame; | |
private double avgWarsPerGame; | |
private double avgWarsDoublePerGame; | |
private double maxNumBattlesInGame; | |
private double minNumBattlesInGame; | |
private double maxNumWarsInGame; | |
private double minNumWarsInGame; | |
public Simulation(int games){ | |
double[] battlesArr = new double[games]; | |
double[] warsArr = new double[games]; | |
double[] warsDubArr = new double[games]; | |
totalWars = 0; | |
totalWarsDub = 0; | |
totalBattles = 0; | |
maxNumBattlesInGame = 0; | |
maxNumWarsInGame = 0; | |
for(int i = 0; i < games; ++i){ | |
Game game = new Game(); | |
game.play(); | |
battlesArr[i] = game.getBattles(); | |
warsArr[i] = game.getWars(); | |
warsDubArr[i] = game.getWarsDub(); | |
if(battlesArr[i] >= maxNumBattlesInGame){ //initializes maxBattles and minBattles (minBattles included in order to make first value) | |
minNumBattlesInGame = maxNumBattlesInGame; | |
maxNumBattlesInGame = battlesArr[i]; | |
} | |
if (battlesArr[i] <= minNumBattlesInGame){ //re declares minBattles to a lesser value | |
minNumBattlesInGame = battlesArr[i]; | |
} | |
if(warsArr[i] >= maxNumWarsInGame){ | |
minNumWarsInGame = maxNumWarsInGame; | |
maxNumWarsInGame = warsArr[i]; | |
} | |
if (warsArr[i] <= minNumWarsInGame){ | |
minNumWarsInGame = warsArr[i]; | |
} | |
totalBattles += battlesArr[i]; | |
totalWars += warsArr[i]; | |
totalWarsDub += warsDubArr[i]; | |
} | |
compute(games, totalBattles, totalWars, totalWarsDub); | |
report(); | |
} | |
public void compute(int games, int battles, int wars, int warsDub){ | |
avgBattlesPerGame = (double) battles / games; | |
avgWarsPerGame = (double) wars / games; | |
avgWarsDoublePerGame = (double) warsDub / games; | |
} | |
public void report(){ | |
System.out.println("Average Battles Per Game: " + avgBattlesPerGame); | |
System.out.println("Average Wars Per Game: " + avgWarsPerGame); | |
System.out.println("Average War Doubles Per Game: " + avgWarsDoublePerGame); | |
System.out.println("Maximum Number of Battles in a Game: " + maxNumBattlesInGame); | |
System.out.println("Minimum Number of Battles in a Game: " + minNumBattlesInGame); | |
System.out.println("Maximum Number of Wars in a Game: " + maxNumWarsInGame); | |
System.out.println("Minimum Number of Wars in a Game: " + minNumWarsInGame); | |
} | |
public static void main(String[] args){ | |
int games = Integer.parseInt(args[0]); | |
Simulation simulate = new Simulation (games); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment