Added some checks for error handling and typesafety

master
sgoudham 4 years ago
parent 5a01203177
commit 805472afb3
No known key found for this signature in database
GPG Key ID: EF51A29A50FB754C

@ -4,6 +4,7 @@ import java.util.Scanner;
public class Sevens extends Deck { public class Sevens extends Deck {
public Sevens() { public Sevens() {
super();
} }
public void play(List<Player> allPlayers, int numOfPlayers) { public void play(List<Player> allPlayers, int numOfPlayers) {
@ -15,7 +16,7 @@ public class Sevens extends Deck {
while (!winner) { while (!winner) {
Player player = allPlayers.get(playerCounter); Player player = allPlayers.get(playerCounter);
tableHand.display(); tableHand.display("Current Deck:");
System.out.println("\nPlayer " + (playerCounter + 1) + "'s Deck:\n"); System.out.println("\nPlayer " + (playerCounter + 1) + "'s Deck:\n");
player.displayHand(); player.displayHand();
@ -29,26 +30,55 @@ public class Sevens extends Deck {
playerCounter = (playerCounter + 1) % numOfPlayers; playerCounter = (playerCounter + 1) % numOfPlayers;
} }
Player winningPlayer = allPlayers.get(playerCounter); System.out.println("\nPlayer " + (playerCounter) + " Is The Winner!");
System.out.println("Player " + (playerCounter + 1) + " Is The Winner!"); tableHand.display("Winning Deck:");
System.out.println("Winning Deck: ");
winningPlayer.displayHand();
} }
public String[] askPlayer(int playerCounter, Player player, TableHand tableHand, Scanner in) { public String[] askPlayer(int playerCounter, Player player, TableHand tableHand, Scanner in) {
System.out.println("\nEnter In The Card That You Want To Play: \nPress S To Skip"); System.out.println("\nEnter In The Card That You Want To Play: \nPress S To Skip");
String userCard = in.nextLine(); String userCard = in.nextLine();
if (userCard.toLowerCase().charAt(0) == 's') { if (userCard.toLowerCase().charAt(0) == 's') {
System.out.println("Player has skipped their turn!");
return new String[0]; return new String[0];
} }
String[] splitCard = userCard.split(" "); String[] splitCard = userCard.split(" ");
if (splitCard.length != 2) if (splitCard.length == 2)
return new String[0]; validatePlayerInput(splitCard, player, tableHand, in);
return splitCard; return splitCard;
} }
public void validatePlayerInput(String[] splitCard, Player player, TableHand tableHand, Scanner in) {
// Check if the first element is of type integer
try {
Integer.parseInt(splitCard[0]);
} catch (NumberFormatException e) {
System.out.println("Card Face Value Not Valid! Please Enter A Suitable Face");
while (!in.hasNextInt()) {
in.nextLine();
System.out.println("Card Face Value Not Valid! Please Enter A Suitable Face");
}
splitCard[0] = Integer.toString(in.nextInt());
}
boolean validSuit = false;
while (!validSuit) {
for (Suits suits : Suits.values()) {
if (suits.getSuit().equalsIgnoreCase(splitCard[1])) {
validSuit = true;
break;
}
}
if (validSuit == false) {
System.out.println("Card Suit Not Valid! Please Enter A Suitable Suit");
splitCard[1] = in.nextLine();
}
}
}
public void validateCard(String[] splitCard, Player player, TableHand tableHand) { public void validateCard(String[] splitCard, Player player, TableHand tableHand) {
int userCardFace = Integer.parseInt(splitCard[0]); int userCardFace = Integer.parseInt(splitCard[0]);
@ -58,22 +88,21 @@ public class Sevens extends Deck {
Hand userHand = player.getHand(); Hand userHand = player.getHand();
Card cardToBePlayed = userHand.sevensCheckCardExists(userCardFace, userCardSuit); Card cardToBePlayed = userHand.sevensCheckCardExists(userCardFace, userCardSuit);
if (cardToBePlayed == null) { if (cardToBePlayed == null) {
System.out.println("Your Card Doesn't Exist Within Your Hand! Try again!"); System.out.println("Your Card Doesn't Exist Within Your Hand! Turn Skipped!");
return; return;
} }
Hand userSuitHand = tableHand.getSevensTableHand().get(userSuitInTableHand); Hand userSuitHand = tableHand.getSevensTableHand().get(userSuitInTableHand);
canBePlayed(cardToBePlayed, userSuitHand, userSuitInTableHand, tableHand, player); canBePlayed(cardToBePlayed, userSuitHand, player);
} }
public void canBePlayed(Card cardToBePlayed, Hand userSuitHand, Suits userSuitInTableHand, TableHand tableHand, public void canBePlayed(Card cardToBePlayed, Hand userSuitHand, Player player) {
Player player) {
if (userSuitHand.getListOfCards().size() == 0) { if (userSuitHand.getListOfCards().size() == 0) {
if (cardToBePlayed.getFace() == 7) { if (cardToBePlayed.getFace() == 7) {
playACard(userSuitHand, cardToBePlayed, 0, tableHand, userSuitInTableHand, player); playACard(userSuitHand, cardToBePlayed, 0, player);
} else { } else {
System.out.println("Card Played Was Not A Face of 7 in Empty Suit! Try again!"); System.out.println("Card Played Was Not A Face of 7 in Empty Suit! Turn Skipped!");
return; return;
} }
} else { } else {
@ -82,19 +111,19 @@ public class Sevens extends Deck {
int maximumValueInUserSuitHand = userSuitHand.getListOfCards().get(sizeOfUserSuitHand - 1).getFace(); int maximumValueInUserSuitHand = userSuitHand.getListOfCards().get(sizeOfUserSuitHand - 1).getFace();
if (cardToBePlayed.getFace() == (minimumValueInUserSuitHand - 1)) { if (cardToBePlayed.getFace() == (minimumValueInUserSuitHand - 1)) {
playACard(userSuitHand, cardToBePlayed, 0, tableHand, userSuitInTableHand, player); playACard(userSuitHand, cardToBePlayed, 0, player);
} else if (cardToBePlayed.getFace() == (maximumValueInUserSuitHand + 1)) { } else if (cardToBePlayed.getFace() == (maximumValueInUserSuitHand + 1)) {
playACard(userSuitHand, cardToBePlayed, sizeOfUserSuitHand, tableHand, userSuitInTableHand, player); playACard(userSuitHand, cardToBePlayed, sizeOfUserSuitHand, player);
} else {
System.out.println("Card Plyaed Was Not +1 or +1 Of Any Cards In The Suit! Turn Skipped!");
return;
} }
} }
} }
public void playACard(Hand userSuitHand, Card playingCard, int indexToInsertCard, TableHand tableHand, public void playACard(Hand userSuitHand, Card playingCard, int indexToInsertCard, Player player) {
Suits userSuitInTableHand, Player player) {
userSuitHand.addCard(indexToInsertCard, playingCard); userSuitHand.addCard(indexToInsertCard, playingCard);
player.getHand().removeCard(playingCard); player.getHand().removeCard(playingCard);
} }
} }

Loading…
Cancel
Save