The Assignment Now that you know how to write OO Java code let’s put your skills
ID: 3640928 • Letter: T
Question
The AssignmentNow that you know how to write OO Java code let’s put your skills to use in your first “real” programming assignment. Your task for the next week is to write a simulation of the card game War.
In the game of War, two players battle each other to see who can win the most cards. At the start of a single game a deck of 52 cards (legal values 2-10,J,Q,K,A) is split evenly between the two players such that each player holds a stack of 26 random cards. For each battle of the game both players turn over the card at the top of their stack. The player with the higher-valued card takes both cards and places them on the bottom of their stack. If the two cards played are of equal value, each player lays down three face-down cards and a fourth card face-up (a "war"), and the higher-valued card wins all of the cards on the table, which are then added to the bottom of the player's stack. In the case of another tie, the war process is repeated until there is no tie.
A player wins by collecting all the cards. If a player runs out of cards while dealing the face-down cards of a war, he may play the last card in his deck as his face-up card and still have a chance to stay in the game. Once a single player holds all 52 cards, the game ends.
You are to write a program that takes the number of games, n, to play as a command line parameter (get passed in the String[] args parameter to main, not from standard input) and then simulates the n games. At the conclusion of all the games your program will calculate and output the following statistics:
Average number of battles per game
Average number of wars per game
Average number of double wars per game
Max number of battles in a game
Min number of battles in a game
Max number of wars in a game
Min number of wars in a game
Designing a Solution
As you can probably tell, this assignment is fairly involved. In fact, the only way you can pull it off is if you are smart about the abstractions you use and how you break your program into separate classes. It is recommended that you design the following classes in implementing your solution:
Card: A card consists of a value (low to highest: 2-10,J,Q,K,A) and a suit (hearts, spaces, clubs, diamonds). Provide appropriate constructors as well as accessor methods.
Deck: A new deck consists of all 52 cards in a LinkedList. It contains a method, deal, that removes a random card from the list and returns that card. (This can be achieved by generating a random number between 0 (inclusive) and the length of the list (exclusive) and removing/returning the card at that list position.
Player: A player consists of a number (1 or 2) and a LinkedList of owned cards. At the start of the game each player holds 26 unique cards. A method, flip, removes and returns the card at the front of the list. A method, collect, takes a card as a parameter and adds it to the list of owned cards. The player class should contain the appropriate constructors and other helper methods as required. For example, a hasWon method would return true if a player owned all 52 cards, and false otherwise, and a method, war, might handle the logic for breaking ties.
Game: A game consists of two players, as well as other instance variables for keeping track of the statistics for each game. A method, play, carries out the rules of the game of war until one of the players has won. This class should have a default constructor and accessor methods, as well as well as any other methods you feel are appropriate.
Simulation: This class contains a constructor that takes the number of games to simulate as well as a function, simulate, that plays the specified number of games. A method, calculate, computes the aggregate statistics from all games, which will require you to define the appropriate class member variables to keep track of the results of each game. Another method, report, prints the required statistics to the screen in a nicely-formatted manner. The main method for running your War program is contained in this class
Explanation / Answer
Was a challenge to make but the result is a lot of fun to play with. You should give a single command line argument which will defined how many games it will simulate. Have fun! Links: - Card.java --> http://bit.ly/GIVhba http://bit.ly/GKGJJ4 http://bit.ly/GKGJsz http://bit.ly/GM5GRw http://bit.ly/GTS6Sa compareCard.getValue()) return 1; else if(value 0 && i < 4; i++) playerOnePile.addFirst(playerOne.flip()); for(int i = 0; playerTwo.getNumCards() > 0 && i < 4; i++) playerTwoPile.addFirst(playerTwo.flip()); int warWinner = playerOnePile.getFirst().compare(playerTwoPile.getFirst()); if(warWinner == 0) { //Double War! numDoubleWars++; warWinner = war(); } return warWinner; } } ------------------------------------- Player.java ------------------------------------- import java.util.LinkedList; public class Player { int playerNumber; LinkedList ownedCards; public Player(int playerNumber) { this.playerNumber = playerNumber; ownedCards = new LinkedList(); } public Card flip() { return ownedCards.removeFirst(); } public void collect(Card newCard) { ownedCards.addLast(newCard); } public int getNumCards() { return ownedCards.size(); } public boolean hasWon() { if(ownedCards.size() >= 52) return true; return false; } public void collectPile(LinkedList pile) { while(pile.size() > 0) collect(pile.removeFirst()); } } ------------------------------------- Simulation.java ------------------------------------- import java.util.Scanner; public class Simulation { int totalGames; int totalBattles; int totalWars; int totalDoubleWars; int maxBattles; int minBattles; int maxWars; int minWars; int avgBattles; int avgWars; int avgDoubleWars; public Simulation(int numGames) { totalGames = numGames; totalBattles = 0; totalWars = 0; totalDoubleWars = 0; maxBattles = Integer.MIN_VALUE; minBattles = Integer.MAX_VALUE; maxWars = Integer.MIN_VALUE; minWars = Integer.MAX_VALUE; } public static void main(String[] args) { Scanner input = new Scanner(System.in); if(args.length == 0 || args.length > 1) { System.out.println("Incorrect Number of Arguments"); System.out.println("Proper usage:"); System.out.println(" Argument 1: Number of Games to Play"); input.nextLine(); System.exit(0); } int numGames = Integer.parseInt(args[0]); Simulation simulator = new Simulation(numGames); simulator.simulate(); simulator.calculate(); simulator.report(); System.out.println(" Press Enter to Exit."); input.nextLine(); } public void simulate() { for(int i = 0; iRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.