package puke;
public class Cards {
public String name;
public String color;
public Cards(String name,String color){
this.name=name;
this.color=color;
}
}
package puke;
import java.util.HashSet;
import java.util.Set;
public class Players {
public int num;
public String name;
public Set<Cards>playercard;
public Players(int num,String name){
this.num=num;
this.name=name;
this.playercard=new HashSet<Cards>();
}
}
package puke;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class Set {
public static String name[]={"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
public static String color[]={"黑桃","红桃","梅花","方片"};
public static List<Cards>card=new ArrayList<Cards>();
static Scanner input =new Scanner(System.in);
//输入扑克牌中的每一个元素 并输出
public void setcard(){
for(int i=0;i<name.length;i++){
for(int j=0;j<color.length;j++){
Cards a=new Cards(name[i],color[j]);
card.add(a);
}
}
for (Cards cards : card) {
System.out.print(cards.color+cards.name+",");
}
}
public void cardshow(){
for (Cards cards : card) {
System.out.println(cards.color+cards.name+",");
}
}
//输入玩家ID
public int setplayers(){
int nums;
while(true){
System.out.println("请输入玩家ID");
nums=inputCommand();
if(nums==-1){
System.out.println("输入类型错误请输入整型!");
continue;
}
break;
}
return nums;
}
//输入玩家姓名
public String playername(){
System.out.println("请输入玩家姓名:");
String name=input.next();
return name;
}
//从控制台输入命令,用于判断输入玩家ID是否为整数
private static int inputCommand(){
int command;
try {
command = input.nextInt();
return command;
} catch (Exception e) {
//若输入字符型或者字符串,则抛出异常,捕获该异常,抛出”错误命令异常“
input=new Scanner(System.in);
//返回-1
return -1;
}
}
//比较扑克牌面大小
public Cards Whobigger(String name[],String color[],Cards one1,Cards one2){
int l=0;
int m=0;
int n=0;
int o=0;
for(int i=0;i<name.length;i++){
if(name[i].equals(one1.name)){
l=i;
}
if(name[i].equals(one2.name)){
m=i;
}
}
for(int j=0;j<color.length;j++){
if(color[j].equals(one1.color)){
n=j;
}
if(color[j].equals(one2.color)){
o=j;
}
}
if(l<m){
return one2;
}
if(l>m){
return one1;
}
if(l==m){
if(n<o){
return one1;
}
if(n>o){
return one2;
}
}
return one2;
}
public static void main(String[] args) {
Set t=new Set();
System.out.println("------开始创建扑克牌------");
System.out.println("------扑克牌创建成功------");
System.out.print("为");
t.setcard();
System.out.println("---------开始洗牌---------");
Collections.shuffle(card);
System.out.println("---------洗牌结束---------");
System.out.println("---------创建玩家---------");
System.out.println("创建第一位玩家");
int b=t.setplayers();
Players player1=new Players(b,t.playername());
Players player2;
while(true){
System.out.println("创建第二位玩家");
int a=t.setplayers();
if(a==b){
System.out.println("ID重复!");
continue;
}
player2=new Players(a,t.playername());
break;
}
System.out.println("欢迎玩家:"+player1.name);
System.out.println("欢迎玩家:"+player2.name);
System.out.println("---------开始发牌---------");
Cards player1card1=card.get(0);
player1.playercard.add(player1card1);
System.out.println("玩家"+player1.name+"拿牌");
Cards player2card1=card.get(1);
player2.playercard.add(player2card1);
System.out.println("玩家"+player2.name+"拿牌");
Cards player1card2=card.get(2);
player1.playercard.add(player1card2);
System.out.println("玩家"+player1.name+"拿牌");
Cards palyer2card2=card.get(3);
player2.playercard.add(palyer2card2);
System.out.println("玩家"+player2.name+"拿牌");
System.out.println("---------开始游戏---------");
Cards player1big=t.Whobigger(name, color, player1card1,player1card2 );
Cards player2big=t.Whobigger(name, color, player2card1, palyer2card2);
Cards big=t.Whobigger(name, color, player1big, player2big);
if(big.name.equals(player2big.name)){
System.out.println("玩家"+player2.name+"获胜");
}else{
System.out.println("玩家"+player1.name+"获胜");
}
System.out.println("---------双方手牌---------");
System.out.println("玩家"+player1.name);
for(Cards one1:player1.playercard){
System.out.println(one1.color+one1.name);
}
System.out.println("玩家"+player2.name);
for(Cards one1:player2.playercard){
System.out.println(one1.color+one1.name);
}
}
}