Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

IN PYTHON Overview In this project, you will continue with modeling the game of

ID: 3602620 • Letter: I

Question

IN PYTHON

Overview

In this project, you will continue with modeling the game of poker. In the previous project, you wrote code to rank 5-card poker hands. In this version, there are two main changes:

Your code will use Object-Oriented Programming

Instead of just 5-card poker hands, we will have community cards and hole cards from which to make the best possible 5-card hand.

Objectives

Learn how to create and use classes and objects.

Get more practice turning pseudo-code algorithms into code.

Get more practice modularizing and using helper methods.

Get more practice with refactoring.

Get more practice choosing good variable and function/method names.

Get more practice testing and debugging.

Part I: OOP

In an OOP program, we create some objects and then do things with those objects. In order to create objects, you first need classes that specify the properties and behavior of the objects you will create.

You should refactor your Project 2 code so that it uses classes and objects. You must have the following classes (you can have others as well, but you must have these):

Card (in module card), an instance of which models a single card. You should have:

A constructor

Getter methods for getting the rank and suit

Other methods???

Deck (in module deck), an instance of which models a deck of cards. You should have:

A constructor

A method to deal a single card (i.e. remove a card from the deck and return it)

A method to shuffle the deck

Other methods???

PokerHand (in module poker_hand), an instance of which models a 5-card hand of cards. You should have:

A constructor

A method to add a card to a hand

compare_to, which compares this hand to another PokerHand. Here's the required signature:

Other methods???

Refactor your main function so that it uses Card, Deck, and Hand objects to play the game from Project 2.

Part II: Further Refactoring

AFTER you have refactored your Project 2 to use OOP, it's time to add functionality. The new game should make a simple game that draws 5 community cards from a deck and then repeatedly:

Draws two new (2-card) hands from the deck (the hole cards),

Shows the community cards and the hands to the player, asking them which hand is worth more (or if they have the same value), taking into account the community cards,

If the player was correct, they get one point and can continue.

If the player is incorrect, the game is over and the total score should be indicated.

The game is also over if there are not enough cards left to play another round.

The hole cards and the community cards are combined when we compare two hands. The value of a hand is the value of the best 5-card hand that can be made from 2 hole cards and 5 community cards, combined (so, choose the best 5 card hand from 7 cards).

Here's some possible output (your program need not output exactly like this):

New classes/methods

To support this new functionality, you must have the following new classes:

CommunityCards (in module community_cards), an instance of which represents the 5 community cards. You should have:

A constructor

Methods to add and remove cards from the community cards

Other methods???

StudPokerHand (in module stud_poker_hand), an instance of which represents a 2-card poker hand. You should have

A constructor, which should take as a parameter a reference to the CommunityCards to use.

Methods to add and remove cards to/from the hand.

compare_to, which compares this hand to another StudPokerHand. Here's the required signature:

Other methods???

Refactor your main function so that it now uses Card, Deck, CommunityCard, and StudPokerHandobjects.

Recommendations

Here are some recommendations to make the project easier to manage:

I recommend making StudPokerHand have the same API (Application Programmer's Interface) as PokerHand. Make sure that all of the public methods are implemented.

When you refactor to use StudPokerHand, the part of the code that plays that actual game should be able to call StudPokerHand methods in ways that completely match the methods it used to call on PokerHands.

The actual implementation of the public methods for StudPokerHand will be simple if you first make a helper method in the new StudPokerHand class that determines the best possible 5-card PokerHand from the available cards. Here's my version of that helper method:

Notice that it uses another helper method to get all possible PokerHands from the hole cards and the community cards. You'll need to write that still.

With this helper method that gets the best PokerHand, our compare_to method can quite simple. How?

Explanation / Answer

Driver.java

--------------------------------------

import java.util.Scanner;

import java.io.*;

public class Driver

{

public static void main(String [] args)throws IOException

{

PrintWriter pw = new PrintWriter(new FileWriter("csis.txt"));

Xref foo = new Xref(pw);

Hash hash = new Hash(pw);

hash.getHashTable();

hash.printHashTable();

foo.getInfo();

foo.traverse();

foo.query();

pw.flush();

pw.close();

}

}

-------------------------------------------

Comparable.java

----------------------

public interface Comparable

{

int compareTo(Object o);

}

--------------------------------------------

Hash.java

--------------------

import java.io.*;

public class Hash

{

private int collisionCount;

ObjectList [] hashTable = new ObjectList [37];

PrintWriter pw;

public Hash(PrintWriter pw)

{

this.pw = pw;

}

public int getCollisionCount()

{

return collisionCount;

}

public ObjectList [] getHashTable()

{

String [] omitWord = {"a", "after", "all", "and", "because", "every", "for", "from", "had", "have", "in",

"is", "it", "its", "now", "of", "on", "so", "that", "the", "their", "there", "to",

"was", "were", "which", "with"};

collisionCount = 0;

for(int i = 0; i < omitWord.length; i++)

{

int hashValue = 0;

String tempString = omitWord[i];

char [] charArray;

charArray = tempString.toCharArray();

int sum = 0;

for(int n = 0; n < charArray.length; n++)

{

sum += charArray[n];

}

hashValue = sum % 37;

ObjectListNode node = new ObjectListNode();

Word temp = new Word(pw);

temp.setWord(omitWord[i]);

node.setInfo(temp);

ObjectList foo = new ObjectList();

if(hashTable[hashValue] != null)

{

collisionCount++;

foo = hashTable[hashValue];

foo.addLast(node);

hashTable[hashValue] = foo;

}

else if(hashTable[hashValue] == null)

{

foo.addLast(node);

hashTable[hashValue] = foo;

}

}

return hashTable;

}

public void printHashTable()

{

for(int i = 0; i < hashTable.length; i++)

{

if(hashTable[i] == null)

{

System.out.println("[" + i + "] = null");

pw.println("[" + i + "] = null");

}

else

{

ObjectList tempList = hashTable[i];

ObjectListNode tempNode = (ObjectListNode)tempList.getFirstNode();

System.out.print("[" + i + "] = ");

pw.print("[" + i + "] = ");

while(tempNode != null)

{

Word foo = (Word)tempNode.getInfo();

String word = foo.getWord();

System.out.print(word + " ");

pw.print(word + " ");

tempNode = tempNode.getNext();

}

System.out.println();

pw.println();

}

}

Hash foo = new Hash(pw);

int colCount = foo.getCollisionCount();

System.out.println("Number of collisions: " + colCount);

pw.println("Number of collisions: " + colCount);

System.out.println("My hash function uses a lexicographic approach to find a number for each word, the word is then mapped to an array location via a %37 operation.");

pw.println("My hash function uses a lexicographic approach to find a number for each word, the word is then mapped to an array location via a %37 operation.");

}

public boolean inHashTable(String arg, ObjectList hash [])

{

char [] charArray = arg.toCharArray();

ObjectList [] hashTable = hash;

int sum = 0;

for(int n = 0; n < charArray.length; n++)

{

sum += charArray[n];

}

int hashValue = sum % 37;

ObjectList tempList = hashTable[hashValue];

if(tempList == null)

{

return false;

}

ObjectListNode node = (ObjectListNode)tempList.getFirstNode();

while(node != null)

{

Word temp = (Word)node.getInfo();

String tempString = temp.getWord();

if(tempString.equals(arg))

{

return true;

}

else

{

node = node.getNext();

}

}

return false;

}

}

-------------------------------------------------------------------

LinePosition.java

------------------------------------

public class LinePosition

{

private int lineCount;

private int linePosition;

public LinePo