Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

https://1drv.ms/f/s!AlS1-b9SQZ-4aWBpegkysaFG5RM - All of the files on a ondrive where it can be eddited Email me at - Randomprog123@hotmail.com You are going to build a

https://1drv.ms/f/s!AlS1-b9SQZ-4aWBpegkysaFG5RM - All of the files on a ondrive where it can be eddited

Email me at - Randomprog123@hotmail.com

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

You are going to build a small card game called SWITCH using expendable array and linked list. You will be given two abstract classes Card and Board, from which you will implement the game specific classes CardSwitch and BoardSwitch. You will also implement an array list-like data structure that keeps track of the cards in a players hand. This is the Hand class, used to represent a Player and a Deck. The Board class maintains a linked list-like data structure that keeps track of all players in the game and decides the winner of the game.

These are the classes that are alredy given and do not need to be edited. Card,Deck,Board,PlayerSwitch

public abstract class Card {

enum Rank{

ACE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING;

}

enum Suit{

HEARTS, CLUBS, DIAMONDS, SPADES;

}

protected Rank rank;

protected Suit suit;

public Card(Rank r, Suit s){

rank = r;

suit = s;

}

public Rank getRank(){

return rank;

}

public Suit getSuit(){

return suit;

}

abstract boolean equals(Card c);

abstract int getPoints();

@Override

public abstract String toString();

}

public class Deck {

private Hand setOfCards;

public Deck(){

setOfCards = new Hand();

}

public boolean addCard(T c){

if (hasCard(c))

return false;

setOfCards.addCard(c);

return true;

}

public boolean hasCard(T c){

return (setOfCards.indexOf(c)!=-1);

}

public void shuffle() {

for ( int i = setOfCards.numCards()-1; i >= 0; i-- ) {

int rand = (int)(Math.random()*(i+1));

T temp = setOfCards.getCard(i);

setOfCards.setCard(i, setOfCards.getCard(rand));

setOfCards.setCard(rand, temp);

}

}

public T dealNextCard() {

if(setOfCards.numCards()==0) return null;

T temp = this.setOfCards.removeCard(setOfCards.numCards()-1);

return temp;

}

public boolean isEmpty() {

return this.setOfCards.numCards() == 0;

}

public int cardCount(){

return this.setOfCards.numCards();

}

public String toString(){

StringBuilder sb = new StringBuilder("Deck ");

int numCards = cardCount();

if (numCards ==0){

sb.append("currently with no cards.");

}

else{

sb.append("with "+numCards+ " cards: ");

sb.append(setOfCards.toString());

}

return sb.toString();

}

}

public abstract class Board {

protected Player currentPlayer;

protected int numPlayer;

protected Deck deck;

public Board(Deck deck){

this.currentPlayer = null;

this.numPlayer = 0;

this.deck = deck;

}

abstract Player getCurrentPlayer();

abstract int getNumPlayers();

abstract Deck getDeck();

abstract boolean changeTurn();

abstract void addPlayer(Player x);

}

import java.util.Scanner;

class PlaySwitch{

// initialize deck of 52

public static void init_deck(Deck deck){

for(Card.Suit s: Card.Suit.values()) {

for(Card.Rank r: Card.Rank.values()) {

CardSwitch card = new CardSwitch(r,s);

deck.addCard(card);

}

}

deck.shuffle();

}

//create players

public static void init_players(BoardSwitch myBoard){

Scanner input = new Scanner(System.in);

System.out.print("Enter the number of players: ");

int n = input.nextInt();

for(int i=0; i

System.out.println("Enter Name of Player "+(i+1)+": ");

String name = input.next();

myBoard.addPlayer(new Player(name));

}

}

// deal to players, each gets numToDeal cards

public static void dealCards(BoardSwitch myBoard){

while(!myBoard.getDeck().isEmpty()) {

CardSwitch card = myBoard.getDeck().dealNextCard();

myBoard.getCurrentPlayer().receiveCard(card);

myBoard.changeTurn();

}

}

public static void playRound(BoardSwitch myBoard){

int numPlayer = myBoard.getNumPlayers();

//switch one card between users

for (int i=0; i

CardSwitch card = myBoard.getCurrentPlayer().playCard(0);

System.out.print("switch from " + myBoard.getCurrentPlayer().getName()+": ");

System.out.print(" card " + card+", ");

myBoard.changeTurn();

myBoard.getCurrentPlayer().receiveCard(card);

System.out.println(" switch to " + myBoard.getCurrentPlayer().getName());

}

}

public static void main(String[] args) {

Deck deck = new Deck();

init_deck(deck);

BoardSwitch myBoard = new BoardSwitch(deck);

init_players(myBoard);

Scanner input = new Scanner(System.in);

// deal all cards

dealCards(myBoard);

System.out.println("How many cards should be switched?");

int numSwitches = input.nextInt();

//switch cards

System.out.println("-----------------------------------");

for(int i=0; i

System.out.println("-Starting ROUND "+(i+1)+"-");

playRound(myBoard);

}

System.out.println("-----------------------------------");

Player winner = myBoard.findWinner();

System.out.println("Winner is: "+winner.getName()+" with "+winner.getPoints() + " points" );

}

}

These next classes are the ones that i need help with which are all on the onedrive posted above

CardSwitch

public class CardSwitch extends Card{

// TO DO: fill the code below and add JavaDoc

public CardSwitch(Rank r, Suit s){

// constructor to create card for the game Switch

}

@Override

public boolean equals(Card anotherCard){

// checks if two cards equals and returns a boolean

}

@Override

public int getPoints(){

// return points of the card

}

@Override

public String toString(){

// convert card to string consisting of as "(rank,suit)"

// see examples below for format

}

//----------------------------------------------------

//example test code... edit this as much as you want!

public static void main(String[] args) {

CardSwitch card = new CardSwitch(Card.Rank.ACE, Card.Suit.SPADES);

if (card.getRank().equals(Card.Rank.ACE)){

System.out.println("Yay 1");

}

if (card.toString().equals("(ACE,SPADES)")){

System.out.println("Yay 2");

}

if (card.getPoints()==1){

System.out.println("Yay 3");

}

}

}

BoardSwitch

public class BoardSwitch extends Board{

// TO DO: add your implementation and JavaDoc

public BoardSwitch(Deck deck){

//constructor

//start with zero players

}

@Override

public Player getCurrentPlayer() {

// return the current player

// O(1)

}

@Override

public int getNumPlayers() {

// return how many players

// O(1)

}

@Override

public Deck getDeck(){

//return the current deck

// O(1)

}

@Override

public boolean changeTurn() {

// move the current player to the next one in the linked list

// return false if cannot change

// O(1)

}

@Override

public void addPlayer(Player x) {

// add another player in the linked list

// should add to the left of currentPlayer

// O(N)

}

public Player findWinner(){

// return the player with the highest point

// O(N)

}

//-----------------------------------------------------

// example test code... edit this as much as you want!

// you will need working CardSwitch, Hand, Player, Deck and PlaySwitch classes to run the given code

public static void main(String[] args) {

Deck deck = new Deck();

PlaySwitch.init_deck(deck);

BoardSwitch myBoard = new BoardSwitch(deck);

Player player1 = new Player("Tom");

Player player2 = new Player("Jerry");

myBoard.addPlayer(player1);

if (myBoard.getNumPlayers() ==1 && myBoard.getCurrentPlayer() == player1

&& player1.getNext() == player1){

System.out.println("Yay 1");

}

myBoard.addPlayer(player2);

if (myBoard.getNumPlayers() ==2 && myBoard.getCurrentPlayer() == player1

&& (myBoard.changeTurn()==true) && myBoard.getCurrentPlayer() == player2){

System.out.println("Yay 2");

}

player1.receiveCard(new CardSwitch(Card.Rank.ACE, Card.Suit.SPADES));

player1.receiveCard(new CardSwitch(Card.Rank.JACK, Card.Suit.CLUBS));

player2.receiveCard(new CardSwitch(Card.Rank.NINE, Card.Suit.HEARTS));

player2.receiveCard(new CardSwitch(Card.Rank.THREE, Card.Suit.SPADES));

if (player1.getNext() == player2 && player2.getNext() == player1

&& myBoard.findWinner() == player2){

System.out.println("Yay 3");

}

}

}

Hand

public class Hand{

// TO DO: add your implementation and JavaDoc

private T [] cards;

private int numCards;

public Hand(){

// constructor

// initial size of cards must be no greater than 5

}

public int numCards(){

// return the number of cards

// O(1)

}

public T getCard(int index){

// return card at index

// throw RuntimeException for invalid index

// O(1)

}

public void setCard(int index, T c){

// change the card at index to be c

// throw RuntimeException for invalid index

// O(1)

}

public void addCard(T c){

// add card c at the end

// O(N)

}

public int indexOf(T c){

// find the index of a given card c,

// returns -1 if not found

// O(N)

}

public T removeCard(int index){

// remove the card at index,

// throw RuntimeException for invalid index

// O(N)

}

public boolean removeCard(T c){

// remove card c,

// returns false if no such card

// O(N)

}

// --------------------------------------------------------

// example test code... edit this as much as you want!

// you will need a working CardSwitch class to run the given code

// Not required, update for your testing purpose

@Override

public String toString(){

// return string representation of hand

// update if you want to include information for all cards in hand

return "Hand with "+numCards+" cards";

}

public static void main(String[] args) {

CardSwitch card1 = new CardSwitch(Card.Rank.ACE, Card.Suit.SPADES);

CardSwitch card2 = new CardSwitch(Card.Rank.JACK, Card.Suit.SPADES);

CardSwitch card3 = new CardSwitch(Card.Rank.NINE, Card.Suit.HEARTS);

Hand myHand = new Hand();

myHand.addCard(card1);

myHand.addCard(card2);

if ((myHand.numCards() == 2) && (myHand.getCard(0).equals(card1))){

System.out.println("Yay 1");

}

myHand.addCard(card3);

if ( card2.equals(myHand.removeCard(1)) && myHand.getCard(1).equals(card3)){

System.out.println("Yay 2");

}

if ((myHand.indexOf(card1)==0) && (myHand.indexOf(card2) == -1 )){

System.out.println("Yay 3");

}

}

}

class Player {

// required fields

private String name;

private int points;

private Hand hand;

private Player next;

// TO DO: add your implementation and JavaDoc

public Player(String name){

//constructor

}

public void setNext(Player p){

//set next player

}

public Player getNext(){

//return next player

}

public boolean hasNext() {

// whether there is a player after me

}

public int getPoints(){

// return points of this player

// determined by cards in hand

}

public String getName(){

// return name of the player

}

public boolean receiveCard(T c){

// receive a card and add it to hand

// return?

}

public boolean hasCard(T c){

// return checking: whether we have the card in hand

}

public boolean playCard(T c){

// give away one card from hand

// return false if card not present

}

public T playCard(int index){

// give away the card at index

// throw RuntimeException for invalid index

}

//---------------------------------------------------

//example test code... edit this as much as you want!

// you will need working CardSwitch and Hand classes to run the given code

public String toString(){

// Not required; edit for your own testing

return "Player "+ name;

}

public static void main(String[] args) {

CardSwitch card1 = new CardSwitch(Card.Rank.ACE, Card.Suit.SPADES);

CardSwitch card2 = new CardSwitch(Card.Rank.JACK, Card.Suit.SPADES);

CardSwitch card3 = new CardSwitch(Card.Rank.NINE, Card.Suit.HEARTS);

Player player1 = new Player("Tom");

Player player2 = new Player("Jerry");

player1.receiveCard(card2);

player1.receiveCard(card3);

player2.receiveCard(card1);

player1.setNext(player2);

if (player1.getName().equals("Tom") && player1.getNext() == player2){

System.out.println("Yay 1");

}

if (player1.hasCard(card2) == true && player1.getPoints() == 19){

System.out.println("Yay 2");

}

if ((player2.hasNext()==false) && player1.playCard(0) == card2){

System.out.println("Yay 3");

}

}

}

project1-description.pdf-project1-description final.docx 8:21 PM 2 of8 100% Step 0: Overview You are going to build a small card be given two abstract chsses - Card and Board, from which you will implement the game specitic casses- CardSwitch and Boardswitch. Youwil also implement an array ist-ike data structune thar keeps track of the cardsina paye's hand. This is the Hand css, used to represent a Player and a Deck. The Board class maintains a linked list-like data structure that keeps track of all players in the ame and decides the winner of the game. game called SWITCH using expendable array and linked list. You will Step 1: JavaDocs You are required to complete the JavaDoc comments for ALL classes including the ones you did not implement (except for PlaySwitch . As part of understanding this pryect, start with wnnng avaDoc comments or the classes that are implemented for you: Card, Deck, and Board Yes, you will be graded on this, and yes, you need to add comments to the methods and instance variables te einentin nderstanding the There is a taof 7 classes in the projec However, you are onl rereto impmme of them. Sce the project starter pakae or all the files The abstract Card Class (see Card.java) [code provided: should not be modified except for adding JavaDoc comments)] A eard consists of ank and sit. A r deck of 52 eards conains 13 different ranks and 4 different suits, Ranks consist of ACE, TW THRE, FOUR, FIVE, SI, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, and KING. Suits consist of HEARTS, CLUBS,DIAMONDS, and SPADES. In our design, every card has a point and a pririty which should be implemcnted in its iherited classes. The CardSwitch Class (scc CardSwitch.java) Write a class specific to the game SWITCH by extending the abstract class Card. Required methods public CardSwitch[Rank rank, Suit suit Create a cand with a give n rank and suit public int geteointso .Returns an integer acconding to the point of this card. ACE-1 point, 2-2 points, 3-3 points 99 points,(10, JACK, QUEEN, KING) 10 ponts ech public boolean equals (Card anotherCard) Returns true if the card is of the same rank and suit of another Card public String toString Rcturn a string,r n of the card in the fom of Rank,Suit with ll capita letters and no project1-description.pdf-project1-description final.docx 8:21 PM 2 of8 100% Step 0: Overview You are going to build a small card be given two abstract chsses - Card and Board, from which you will implement the game specitic casses- CardSwitch and Boardswitch. Youwil also implement an array ist-ike data structune thar keeps track of the cardsina paye's hand. This is the Hand css, used to represent a Player and a Deck. The Board class maintains a linked list-like data structure that keeps track of all players in the ame and decides the winner of the game. game called SWITCH using expendable array and linked list. You will Step 1: JavaDocs You are required to complete the JavaDoc comments for ALL classes including the ones you did not implement (except for PlaySwitch . As part of understanding this pryect, start with wnnng avaDoc comments or the classes that are implemented for you: Card, Deck, and Board Yes, you will be graded on this, and yes, you need to add comments to the methods and instance variables te einentin nderstanding the There is a taof 7 classes in the projec However, you are onl rereto impmme of them. Sce the project starter pakae or all the files The abstract Card Class (see Card.java) [code provided: should not be modified except for adding JavaDoc comments)] A eard consists of ank and sit. A r deck of 52 eards conains 13 different ranks and 4 different suits, Ranks consist of ACE, TW THRE, FOUR, FIVE, SI, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, and KING. Suits consist of HEARTS, CLUBS,DIAMONDS, and SPADES. In our design, every card has a point and a pririty which should be implemcnted in its iherited classes. The CardSwitch Class (scc CardSwitch.java) Write a class specific to the game SWITCH by extending the abstract class Card. Required methods public CardSwitch[Rank rank, Suit suit Create a cand with a give n rank and suit public int geteointso .Returns an integer acconding to the point of this card. ACE-1 point, 2-2 points, 3-3 points 99 points,(10, JACK, QUEEN, KING) 10 ponts ech public boolean equals (Card anotherCard) Returns true if the card is of the same rank and suit of another Card public String toString Rcturn a string,r n of the card in the fom of Rank,Suit with ll capita letters and no

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions