For the code in java below it shows a deck of 52 cards and asks the name of the two players and makes both players draw five cards from the deck. What I want to be added into the code is that both Players are human that manuelly pick which cards they pick and Player A starts. Player A picks a card in his/her hand. Player B gets to choose the two cards which add to the value of Player A’s card, if player B does not have two cards whose value adds to the value of Player A’s card, then no one gets a point, if Player B has two cards that equal the value of Player A's card then Player B gets a point. Player’s A card (if selected) and the two cards from Player B are discarded both players draw back to 5 cards from the deck. A new round starts and with Player B picking a card that Player A has to match. Main class code: import java.util.ArrayList; import java.util.Scanner; import java.util.List; import java.util.Random; public class Main { public static void main(String[] args) { // card game, two players, take turns. String[] suits = {"Hearts", "Clubs", "Spades", "Diamonds"}; String[] numbers = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"}; for(String oneSuit : suits){ for(String num : numbers){ System.out.println(oneSuit + " " + num); } } List listOfPlayers = new ArrayList<>(); Scanner sc = new Scanner(System.in); System.out.println("Name of Player 1"); //changed the name of input string variable to remove the error String PlayerName = sc.nextLine(); Player newPlayer = new Player(PlayerName); listOfPlayers.add(newPlayer); //adding another player to the list System.out.println("Name of Player 2"); PlayerName = sc.nextLine(); Player newPlayer2 = new Player(PlayerName); listOfPlayers.add(newPlayer2); //now choosing Random cards for both players and then store it in the array. List Player1Cards = new ArrayList<>(); List Player2Cards = new ArrayList<>(); for(int i=1;i<=5;i++){ //a card is "suitName" + "number" int suitIndex1,suitIndex2; int numbersIndex1,numbersIndex2; Random random = new Random(); //suitIndex ranges from 0 to 3. // numbersIndex ranges from 0 to 12. suitIndex1 = random.nextInt(3 - 0) + 0; numbersIndex1= random.nextInt(12 - 0) + 0; suitIndex2 = random.nextInt(3 - 0) + 0; numbersIndex2= random.nextInt(12 - 0) + 0; Player1Cards.add(suits[suitIndex1]+numbers[numbersIndex1]); Player2Cards.add(suits[suitIndex2]+numbers[numbersIndex2]); } //displaying cards choosen System.out.print("Player "+newPlayer.getName()+" cards are: "); for(String card:Player1Cards){ System.out.print(card+" "); } System.out.print("\nPlayer "+newPlayer2.getName()+" cards are: "); for(String card:Player2Cards){ System.out.print(card+" "); } } }
For the code in java below it shows a deck of 52 cards and asks the name of the two players and makes both players draw five cards from the deck.
What I want to be added into the code is that both Players are human that manuelly pick which cards they pick and Player A starts. Player A picks a card in his/her hand. Player B gets to choose the two cards which add to the value of Player A’s card, if player B does not have two cards whose value adds to the value of Player A’s card, then no one gets a point, if Player B has two cards that equal the value of Player A's card then Player B gets a point. Player’s A card (if selected) and the two cards from Player B are discarded both players draw back to 5 cards from the deck. A new round starts and with Player B picking a card that Player A has to match.
Main class code:
import java.util.ArrayList;
import java.util.Scanner;
import java.util.List;
import java.util.Random;
public class Main {
public static void main(String[] args) {
// card game, two players, take turns.
String[] suits = {"Hearts", "Clubs", "Spades", "Diamonds"};
String[] numbers = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
for(String oneSuit : suits){
for(String num : numbers){
System.out.println(oneSuit + " " + num);
}
}
List<Player> listOfPlayers = new ArrayList<>();
Scanner sc = new Scanner(System.in);
System.out.println("Name of Player 1");
//changed the name of input string variable to remove the error
String PlayerName = sc.nextLine();
Player newPlayer = new Player(PlayerName);
listOfPlayers.add(newPlayer);
//adding another player to the list
System.out.println("Name of Player 2");
PlayerName = sc.nextLine();
Player newPlayer2 = new Player(PlayerName);
listOfPlayers.add(newPlayer2);
//now choosing Random cards for both players and then store it in the array.
List<String> Player1Cards = new ArrayList<>();
List<String> Player2Cards = new ArrayList<>();
for(int i=1;i<=5;i++){
//a card is "suitName" + "number"
int suitIndex1,suitIndex2;
int numbersIndex1,numbersIndex2;
Random random = new Random();
//suitIndex ranges from 0 to 3.
// numbersIndex ranges from 0 to 12.
suitIndex1 = random.nextInt(3 - 0) + 0;
numbersIndex1= random.nextInt(12 - 0) + 0;
suitIndex2 = random.nextInt(3 - 0) + 0;
numbersIndex2= random.nextInt(12 - 0) + 0;
Player1Cards.add(suits[suitIndex1]+numbers[numbersIndex1]);
Player2Cards.add(suits[suitIndex2]+numbers[numbersIndex2]);
}
//displaying cards choosen
System.out.print("Player "+newPlayer.getName()+" cards are: ");
for(String card:Player1Cards){
System.out.print(card+" ");
}
System.out.print("\nPlayer "+newPlayer2.getName()+" cards are: ");
for(String card:Player2Cards){
System.out.print(card+" ");
}
}
}
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 2 images