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

write a program that allows someone to play \"Top Trumps\" against the computer.

ID: 3763363 • Letter: W

Question

write a program that allows someone to play "Top Trumps" against the computer. ( using java )

You can use Easy Graphics to display the game information in an easy-to-follow manner. However, it would be perfectly acceptable for the information to simply print to System.out.

You are free to design your solution in any way that you think best. However, a well-designed solution will probably contain the following classes:

Card - a class to represent a card. It should store the name of the card and the value of each of the attributes. It will need a method to display itself. It will need accessor methods for returning the name and the attribute values.

Pack - a class to represent a complete pack of cards. It should have a method for reading the cards from the file. It may also have a method that deals a single card: i.e. when the method is called it returns the card that's on top of the pack and removes that card from the pack.

Player - a class to represent a game player. It should have a field that states whether the player is computer controlled or human controlled. It should keep an ordered record of the cards the player is holding, e.g. an array of Card objects. It should have a method for returning the next move, i.e. the choice of attribute to be played. If the player is human this method will need to request input from the keyboard: if the player is a computer, it will need to make a choice for itself (e.g. by choosing the highest scoring attribute on the card.

TopTrumps - this will be the class that contains the main method and which sets up and controls the flow of the game. The class should construct a pair of Player objects: one human and one computer-controlled. The class needs to construct a Pack object and then ask the pack object to read the cards stored in the file (reading file code will be given). It should then use the pack object to deal a hand to each player. It then needs to enter into a main game loop in which one player takes an attribute choice (magic, wisdom, etc) and all the player's cards are compared to see who has won. The winning player is then allowed to make a choice, and so on until one player has own all the cards. Note, there needs to be some mechanism for collecting the top card from each player and passing them to the winning player. It is important that the cards that have been won are sent to the bottom of the winning player's pile (it may need some carful thought to come up with an efficient way of doing this). Hint: You need to shuffle the cards each time you start the game.

Explanation / Answer

public static void startGame()
{
Player player1 = new Player();
Player computer = new Player();

var newdeck = new Deck();
newdeck.Shuffle();

while (newdeck.CountCards() != 0)
{
newdeck.dealCards();
}

MessageBox.Show("New Game Started");
This is my Deck Class
public class Deck
{

private List<Card> deckofCards = new List<Card>();



public Card this[int p]
{
get
{
return deckofCards[p];
}
}


public Deck()
{
resetDeck();
}

public void resetDeck()
{

XmlDocument xmldata = new XmlDocument();
xmldata.Load(@"C:GameofThronesXml.xml");
XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmldata.NameTable);

XmlNodeList nodeList;
XmlElement root = xmldata.DocumentElement;
nodeList = xmldata.SelectNodes("//CharacterData/record", nsmgr);

foreach (XmlElement data in nodeList)
{
Card singlecard = new Card();
singlecard.CardID = int.Parse(data.SelectSingleNode("./CardID").InnerText);
singlecard.Name = data.SelectSingleNode("./Name").InnerText;
singlecard.Intelligence = int.Parse(data.SelectSingleNode("./Intelligence").InnerText);
singlecard.Ruthlessness = int.Parse(data.SelectSingleNode("./Ruthlessness").InnerText);
singlecard.Status = data.SelectSingleNode("./Status").InnerText;
singlecard.Prestige = int.Parse(data.SelectSingleNode("./Prestige").InnerText);
singlecard.FightingSkill = int.Parse(data.SelectSingleNode("./FightingSkill").InnerText);
singlecard.AppearedIn = int.Parse(data.SelectSingleNode("./AppearedIn").InnerText);
singlecard.Description = data.SelectSingleNode("./Description").InnerText;
deckofCards.Add(singlecard);
}

  
}

public void Shuffle()
{

deckofCards = deckofCards.OrderBy(c => Guid.NewGuid())
.ToList();

}

public int CountCards()
{

int number = deckofCards.Count();
return number;

}

public Card dealCards()
{

Hand hand = Player.hand;

if (this.deckofCards.Count == 0)
throw new InvalidOperationException("There are no cards to deal.");

Card card = deckofCards[0];

hand.cardsinHand.Add(card);
deckofCards.RemoveAt(0);
return card;

}
}
Hand Class
public class Hand
{


public List<Card> cardsinHand = new List<Card>();

public int GetNumberofCards()
{
int count = cardsinHand.Count;
return count;
}

public void AddCard(Card card)
{
cardsinHand.Add(card);
}

}
Player Class
public class Player
{

public static Hand hand = new Hand();


}