Please do this in java Stacks are really useful for things like playing cards. I
ID: 3834010 • Letter: P
Question
Please do this in java
Stacks are really useful for things like playing cards. In this program, I want you create a Stack of 52 different cards that you can shuffle and then “deal” out the players. You can read the cards in from a file, shuffle them and then deal them out. You are to ask the user how many players there are and then deal 5 cards to each of them. After you deal them, you should have each player display their cards.
The players should then be able to reshuffle and re-deal as many times as they want. This can be done graphically or in the console.
Explanation / Answer
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Random;
class Player{
String playerCards[]=new String[5];
}
public class Cards {
public static void main(String[] args) throws FileNotFoundException,IOException{
int choice;
do{
BufferedReader in=new BufferedReader(new FileReader("<Enter the file path>"));
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
LinkedList<String> card=new LinkedList<>();
String str;
while((str=in.readLine())!=null){//reading card from file
card.add(str);
}
Random ran=new Random();
LinkedList<String> suffledCard=new LinkedList<>();
while(card.size()!=0){//suffling the card
suffledCard.add(card.remove(ran.nextInt(card.size())));
}
System.out.println("Enter the number of player");
int n=Integer.parseInt(br.readLine());
Cards c=new Cards();
LinkedList<Player> players=new LinkedList<>();
for(int i=1;i<=n;i++){ //dealing the cards to player
Player p=new Player();
for(int j=0;j<5;j++){
p.playerCards[j]=suffledCard.removeFirst();
}
players.add(p);
}
//displaying cards of player
int i=1;
for(Player p:players){
System.out.println("Player "+(i++));
for(int j=0;j<5;j++){
System.out.print(p.playerCards[j]+" ");
}
System.out.println(" ");
}
System.out.println("Do you wish to continue?? Enter 1 for YES and 2 for NO");
choice=Integer.parseInt(br.readLine());
}while(choice==1);
}
}