import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Human<Cards> {
/**
- 创建玩家;
- 玩家要有:1、id 2、姓名;3、手牌;(手牌有涉及到学生选课,一并将学生选课完善)
- 利用Map;
- 洗牌与发牌利用随机数来实现;
- 要用到switch语句来实现JQK的转换;
- 选手id的异常处理;
- 进行排序输出最大的(所以就要利用comparator借口,还要利用collections.sort方法来实现compare方法)
-
*/
public List <Cards>cards;
private int id;
private String name;public Human(int id, String name) {
// TODO 自动生成的构造函数存根
this.id = id;this.name = name; this.cards = new ArrayList<Cards>();
}
public int getId() {
return id;
}public void setId(int id) {
this.id = id;
}public String getName() {
return name;
}public void setName(String name) {
this.name = name;
}
}
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
public class Squeezer implements Comparable<Squeezer>{
/**
- 创建扑克牌;
- 扑克牌的属性有:1、花色;2、点数;3、id;"因为出现JQK三个数无法在程序中直接体现"
- 所以要用到switch语句来实现JQK的转换;
- 尝试运用循环来插入扑克牌集合中的内容;
-
利用List;
*/private List<Squeezer>squeezer;
private String flower;
private int number;
private int id;
public Squeezer(int id,int number,String flower){
this.id = id;
this.flower = flower;
this.number = number;
this.squeezer = new ArrayList<Squeezer>();
}public String getFlower() {
return flower;
}
public void setFlower(String flower) {
this.flower = flower;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}@Override
public int compareTo(Squeezer o) {
// TODO 自动生成的方法存根
if(this.number == o.number){
if(this.flower.equals("黑桃")){
return -1;
}
else if(this.flower.equals("红桃")){
if(o.flower.equals("黑桃")){
return 1;
}else{
return -1;
}
}else if(this.flower.equals("梅花")){
if(o.flower.equals("黑桃")||o.flower.equals("红桃")){
return 1;
}else{
return -1;
}
}else{
return -1;
}} else if(this.number ==1){ return 1; } else{ if(this.getNumber()>o.getNumber()){ return -1; }else{ return 1; } }
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime result + ((flower == null) ? 0 : flower.hashCode());
result = prime result + id;
result = prime result + number;
result = prime result + ((squeezer == null) ? 0 : squeezer.hashCode());
return result;
}@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Squeezer other = (Squeezer) obj;
if (flower == null) {
if (other.flower != null)
return false;
} else if (!flower.equals(other.flower))
return false;
if (id != other.id)
return false;
if (number != other.number)
return false;
if (squeezer == null) {
if (other.squeezer != null)
return false;
} else if (!squeezer.equals(other.squeezer))
return false;
return true;
}
}
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
import javax.naming.NameNotFoundException;
public class Test {
/**
- 用来实现各种方法的调用;
-
@param args
*/
private List<Squeezer>squ;
private Map<Integer,Human>hum;
private Scanner sc = new Scanner(System.in);
public Test(){this.hum = new HashMap<Integer,Human>(); this.squ = new ArrayList<Squeezer>();
}
public void creatsqu(){
System.out.println("创建扑克牌");
int j = 0;
for(int i = 1;i <= 13;i++){
Squeezer sq = new Squeezer(j,i,"黑桃");
squ.add(sq);
Squeezer sq1 = new Squeezer((j+13),i,"红桃");
squ.add(sq1);
Squeezer sq2 = new Squeezer((j+26),i,"方片");
squ.add(sq2);
Squeezer sq3 = new Squeezer((j+39),i,"梅花");
squ.add(sq3);
j = i;
}}
public void foreach(){
System.out.println("构建的扑克牌为:");
for (Squeezer sq : squ) {
switch (sq.getNumber()) {
case 11:
System.out.print(sq.getFlower()+"J"+" ");
break;
case 12:
System.out.print(sq.getFlower()+"Q"+" ");
break;
case 13:
System.out.print(sq.getFlower()+"k"+" ");
break;default: System.out.print(sq.getFlower()+sq.getNumber()+" "); break; } }
}
public void creat(){
int id ; for(int i = 0;i < 2;i++) { System.out.println("请输入玩家ID"); while(true) { try{ id = Integer.parseInt(sc.nextLine().trim());; if(hum.get(id)==null){ break; }else{ System.out.println("此ID已被占用请重新输入"); continue; } } catch (NumberFormatException e) { System.out.println("请输入整数类型的ID!"); System.out.println("重新输入ID:"); continue; } } System.out.println("输入姓名:"); String name = sc.nextLine(); Human player = new Human(id, name); hum.put(id, player); } }
public void humforeach(){
Set<Integer>keyset = hum.keySet();
for (int st : keyset) {
Human sr = hum.get(st);
if(sr !=null){
System.out.println("-------欢迎玩家-------"+sr.getName());
}
}
}public void shuffle(){
System.out.println("---------开始洗牌--------"); //随机打乱顺序用List方法
Collections.shuffle(squ);
System.out.println("---------洗牌结束----------");
}public void dealCard(){
int i = 0;
int in[] = new int[2];
String str[] = new String[2];
Set<Integer>keyset = hum.keySet();
for (int st : keyset) {
Human sr = hum.get(st);
if(sr !=null){
in[i] = sr.getId();
str[i] = sr.getName() ;
i++;
}
}
System.out.println("给"+str[0]+"发牌");
hum.get(in[0]).cards.add(squ.get(0));
System.out.println("给"+str[1]+"发牌");
hum.get(in[1]).cards.add(squ.get(2));
System.out.println("给"+str[0]+"发牌");
hum.get(in[0]).cards.add(squ.get(1));
System.out.println("给"+str[1]+"发牌");
hum.get(in[1]).cards.add(squ.get(3));}
public void Startgame(){
int i = 0;
int in[] = new int[2];
String str[] = new String[2];
Set<Integer>keyset = hum.keySet();
for (int st : keyset) {
Human sr = hum.get(st);
if(sr !=null){
in[i] = sr.getId();
str[i] = sr.getName() ;
i++;
}
}
System.out.println("------开始游戏--------");
Collections.sort(hum.get(in[0]).cards);
Squeezer maxCard= (Squeezer) hum.get(in[0]).cards.get(1);System.out.print("玩家:"+hum.get(in[0]).getName()+"最大的手牌是:"+maxCard.getFlower()); switch (maxCard.getNumber()) { case 11: System.out.println("J"); break; case 12: System.out.println("Q"); break; case 13: System.out.println("k"); break; default: System.out.println(maxCard.getNumber()); break; } Collections.sort(hum.get(in[1]).cards); Squeezer maxCard1= (Squeezer) hum.get(in[1]).cards.get(1); System.out.print("玩家:"+hum.get(in[1]).getName()+"最大的手牌是:"+maxCard1.getFlower()); switch (maxCard1.getNumber()) { case 11: System.out.println("J"); break; case 12: System.out.println("Q"); break; case 13: System.out.println("k"); break; default: System.out.println(maxCard1.getNumber()); break; } List <Squeezer>list = new ArrayList<Squeezer>(); list.add(maxCard1); list.add(maxCard); Collections.sort(list); if(list.get(0).getNumber() == maxCard.getNumber()&&list.get(0).getFlower().equals(maxCard.getFlower())){ System.out.println("-------玩家"+hum.get(in[0]).getName()+"赢了--------"); }else { System.out.println("-------玩家"+hum.get(in[1]).getName()+"赢了--------"); }
}
public static void main(String[] args) {
//
System.out.println("欢迎游戏");
Test te = new Test();
te.creatsqu();
te.foreach();
System.out.println("");te.creat(); te.humforeach(); te.shuffle(); te.dealCard(); te.Startgame();
}
}
共同学习,写下你的评论
评论加载中...
作者其他优质文章