War Card Game Python

Goals

War card game is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page. This is my python code for war card game. I'm struggling with implementing the idea of when you have a tie. Each player will place two cards down and the third card for both will determine who wins the 8 cards.

  • Learn how to use classes to implement linked lists and other abstract data types in Java
  • Learn about building specialized data structures in Java

Submit

Submit LinkedList.java, Deck.java, WarGame.java,and a completed readme_war.txt using thesubmission link on the left.

Extra credit: submit a zip file named extra.zip thatcontains a complete version of your program (all .java files) based onJava generics where LinkedList is based on the GenericList.javainterface (described below).

Introduction

In this project, we will write a program to play a simple card gameknownas War.In War, a standard deck of 52 playing cards is shuffled and dividedevenly among two players (the user and the computer). Playerssimultaneously turn over the top card of their respective decks, andthe player with the high value card takes both cards, adding them tothe bottom of their deck. In the case of a tie (the two cardshave equal value), there is a war where players each place three cardsface down and then one card face up; the player with the higher valuetakes all (ten) cards. In the case of another tie, thisprocess repeats until there is a winner. For added complexity, aplayer may choose to shuffle their deck of cards before eachbattle. The game continues until one player is out ofcards. For more details,see War.

In playing the game, there are several piles of cards: the twoplayers' decks, and the piles of cards in the 'battle' by each player.Cards are continually moved between these different piles. Since thecards in the pile have a specific order, we will use a List torepresent these card piles.

In this assignment, you will implement two variations of the listAbstract Data Type (ADT):

  1. a linked list, exactly as we discussed in class.
  2. a new data structure known as a deck that is based on a linked list. Essentially, the deck is a queue of cards that can be shuffled.

You will also implement a WarGame class that runs the interactive Wargame.

We have already provided you with a skeleton for the LinkedList class,which you can download here:


Additionally, you will also need to download the following files forthis assignment:

Be sure to compile all of the .java files you downloaded.

The Card Data Structure

The Card object represents a single playing card, which has both arank and suit. Aces will be considered 'high'. The Card object usesthe following API:

Note that the Card object provides getters for the rank and suit,which cannot be changed once they are initialized by the constructor.It also provides a set of static final constants to represent facecards and the suits.
Here is a simple example of how to use the Card object:
The Card class is complete as given -- you should not modify it incompleting the assignment.

LinkedList Abstract Data Type

You should start by implementing the LinkedList class, which shouldrepresent a singly linked list as discussed in class. In this case,it will be a list of Card objects. To get you started, we've provideda skeleton LinkedList.java.


The List Interface

Your LinkedList class must implement the provided List interface(List.java) exactly as it is given. You are not permitted tomodify List.java. In Java, an interface is a specialtype of construct that defines a public API. Essentially it defines a set of public methods that a class whichimplements that interface must have, but it doesn't provide anyimplementation.
  • For example, a TV interface might specify methods: togglePower(), volumeUp(), volumeDown(), and setChannel(int n). Then, any class like PanasonicTelevision or SamsungTelevision that implements this interface must provide these public methods.

We have provided the file List.java that specifies the (minimal) setof public methods that your LinkedList class must contain. In orderto have Java enforce this automatically, the LinkedList class must bedefined as follows:

War Card Friv

The implements keyword in Java allows us to specify the nameof an interface that the class must use, in this case, List. Thestandard LinkedList provides a number of other methods that you arenot required to implement in this assignment. For a full list ofthese methods for a standard LinkedList, you can refer to thejavadocs:http://docs.oracle.com/javase/6/docs/api/java/util/LinkedList.html.


Implementing LinkedList

You must complete the provided LinkedList.java skeleton. You must alsoimplement the inner class: Node. (Hint: Your node will need twofields: a next reference and a Card data element). Skeleton methodsand explanations for LinkedList are provided in the file.

Notes:

  • Review the three *LinkedQueueOfDouble.java examples from lecture. They are all posted on the syllabus page. You are welcome to use sentinel nodes or not, and to use recursion or not in your own linked list methods.
  • The add() method is overloaded in the List. One version, add(element), adds an element to the end of the list and always returns true. The other version, add(index, element), adds an element to a specific index of the list.
    • Although it might seem odd to have add(element) always return true, this choice makes the List interface suitable for different types of lists. For example, it would also work for implementing a UniqueList where duplicate entries are not allowed. In the UniqueList, add(element)would not always return true, but in our case, it does.
  • Several methods, such as get(index), take in an index and return an object. In situations where the index is invalid, it doesn't make sense for the method to signify this by returning null, since null is a valid value for an object (and could certainly be contained in the list). Instead, your implementation should throw an IllegalArgumentException (with an appropriately detailed message) when index is invalid.
  • add(index, element) should work correctly if 0 <= index <= size(). For example, this would allow you to call add(0, new Card(Card.ACE, Card.SPADES)) on an empty list and have it insert the ace of spaces as the new head (and tail) element.
  • In contrast to the above note, a node must already exist at the specified index for set(index, element) to work. Therefore, set(index, element) can't be used to add new nodes to a list.
  • Your War Game program may not use all of the methods in the List interface, but your LinkedList class must still implement them. The reason for this is that we're writing a general LinkedList of cards that can be used in any program; those other programs may use LinkedList methods that are not necessary to write WarGame.

Hint: To get started implementing the LinkedList, start byimplementing the Node inner class, the constructor,

War Python Code

add(), andget(). Then, use the unit testing code described below toensure that you can add items and print out the list. Once thesemethods are working, move on to implement the others.


Unit Testing LinkedList

Test your LinkedList implementation before continuing to the next partof the assignment. Copy the following code into the main function ofLinkedList to test add() and get().

You should see the following output when you run LinkedList.Add additional code to your main method to test the other methods ofLinkedList. If you do not have tests for your LinkedListimplementation, we cannot guarantee we can help you on later parts ofthe assignment.
The submission script runs extensive testing on your LinkedListimplementation, so you should also try submitting yourLinkedList.java file before moving on to the next part of thisassignment. You can always re-submit it later, and the submissionscript testing may prove very useful for helping to identify problemsin your LinkedList implementation. Note that the online submissiontesting is not a substitute for doing your own unit testingin main(); the online tests only identify that you have aproblem in a particular method, but do not help you debug that problemlike your unit tests will.

Deck Abstract Data Type


GamesOnce you've completely finished and tested your LinkedList class, moveon to your Deck class.

The Deck object will represent complete or partial decks of cards.The deck will operate like a queue, allowing you to remove cards fromthe top of the deck and add cards to the bottom of the deck. It willalso allow you to shuffle the cards in the deck, randomizing theirorder.

Building the Deck from LinkedList
While we could completely re-implement this class from scratch, thebest way is to build the Deck off of LinkedList. The Deck classshould have a LinkedList of cards as a private field. The variousmethods for the Deck class will then call the corresponding methods onthe private LinkedList field to implement the functionality.

Hint: your Deck class should be much shorter than your LinkedListclass. By building Deck off of LinkedList, you gain much of thefunctionality you need with minimal effort.

Implementing Deck
Your Deck class must conform to the following API:

Unit Testing Deck (Except shuffle())

War Card Game In Python


To test your Deck, copy the following code into your main method:When run, this should output all of the cards in the deck, with thelast card being a duplicate ace of spades.
Write a few other tests in Deck.main() to make certain that your Deckclass works.

Implementing shuffle().Shuffling a deck of cards works very much like selection sort.However instead of picking the smallest remaining card to swap intoposition, you should pick a random card. There is example code forshuffling an array in Section 1.4 of the textbook (and booksite).

Since you are shuffling a linked list, not an array, you can simplymove the cards into position; there is no need to swap. Moreover, youcan always move cards to the very front (or very back) of the array,rather than to position i as the array version does.

Your shuffle() implementation must be entirely linkedlist-based. You may not use arrays.

Testing shuffle(). Add a callto shuffle() immediately after the call to fill() inthe testing code above. Your deck should now print in random order,with an extra ace of spades at the end. You should get a differentrandom order each time you run your unit test.

The War Card Game Program

Your WarGame.java program should simulate a game of War, as describedin the Introduction. It should not take in any command linearguments. Each round of play, the user will have the option ofpressing the 'Enter' key to play a card against the computer or the'S' (or 's') key to shuffle the deck. The game is over when oneplayer runs out of cards. If there is a war, and one player hasfewer than four remaining cards, the other player wins.

You should not be using the Processing library for this assignment.You can read input from the user via StdDraw.nextKeyTyped(),just as in GuitarHero.

War Game In Python

Here is an example of how your WarGame program should operate. Youmay enhance the text of the program as you like, but the interaction(keys the user can press and their effect) must be the same as in thisexample.

Extra Credit 1: Animating the game

Write a program, AnimatedWarGame.java that plays war anddraws and animates the cards. You may use either StdDraw orProcessing for this version; it's completelly up to you. Submit yourcode and any images in your extra.zip file. You should beable to find card images you like on the internet. Here is a linkto two sets of card images,but you can find many others. Make sure to credit the source of yourimages in your readme and in a comment in your .java file.

Extra Credit 2: Java Generics

War Card Game Using Python

The LinkedList class we wrote above can contain only Card objects;however, it is often useful to implement data structures in a moregeneric way such that they can hold any type of data. Java providesGenerics specifically for this purpose.

For extra credit, modify your LinkedList class to be based upon theinterface GenericList.java instead ofList.java. The methods in GenericList are exactly the same as in List,but they are based on a generic type T that is specified when theLinkedList is declared and initialized. This will enable yourLinkedList to contain any type of object, not just Card objects. Youwill likely need to modify your other classes as well to enable themto use the generic form of the LinkedList.

If you do the extra credit, you will submit two versions of theassignment: the original (non-generic) version of all files as normal,and an extra.zip file containing new versions of all three .java files(LinkedList.java, Deck.java, WarGame.java)modified to work with the generic LinkedList. Even if one or more ofthese files are unchanged from your non-generic version, you shouldstill include it in extra.zip.