提交作业,不足之处请大家指正
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Start {
CaiPan cp;
List<Pai> pailist,pailist2;
public Start(){
cp=new CaiPan();
System.out.println("----------创建扑克牌----------");
cp.getpai();
System.out.println("\n----------开始洗牌----------");
pailist=cp.xipai();
System.out.println("----------创建玩家----------");
System.out.println("请输入第一位玩家的ID和姓名:");
WanJia wj1=new WanJia();
System.out.println("请输入第二位玩家的ID和姓名:");
WanJia wj2=new WanJia();
System.out.println("-----欢迎玩家:"+wj1.name);
System.out.println("-----欢迎玩家:"+wj2.name);
System.out.println("----------开始发牌----------");
System.out.println("-----玩家"+wj1.name+"拿牌...");
wj1.getPai(pailist);
System.out.println("-----玩家"+wj2.name+"拿牌...");
wj2.getPai(pailist);
System.out.println("-----玩家"+wj1.name+"拿牌...");
wj1.getPai(pailist);
System.out.println("-----玩家"+wj2.name+"拿牌...");
wj2.getPai(pailist);
System.out.println("----------发牌结束----------\n----------开始游戏----------");
Collections.sort(wj1.shoupai);
System.out.println("玩家"+wj1.name+"最大的手牌为:"+wj1.shoupai.get(1).name);
Collections.sort(wj2.shoupai);
System.out.println("玩家"+wj2.name+"最大的手牌为:"+wj2.shoupai.get(1).name);
pailist2=new ArrayList<Pai>();
pailist2.add(wj1.shoupai.get(1));
pailist2.add(wj2.shoupai.get(1));
cp.bipai(pailist2,wj1,wj2);
System.out.print("玩家1的手牌为:");
for (Pai pai : wj1.shoupai) {
System.out.print(pai.name+"、");
}
System.out.print("\n玩家2的手牌为:");
for (Pai pai : wj2.shoupai) {
System.out.print(pai.name+"、");
}
}
public static void main(String[] args) {
Start st=new Start();
}
}
//------------------------------------------------------------------------------------------------------------------------
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.Collections;
import java.util.Map.Entry;
public class CaiPan {
Map<Integer,Pai> paimap;
List<Pai> pailist;
Random sjs;
public CaiPan(){
paimap=new HashMap<Integer,Pai>();
pailist=new ArrayList<Pai>();
sjs=new Random();
}
public void getpai(){
Integer key=0;
Pai pai;
String[] nums={"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
String[] huas={"红桃","黑桃","梅花","方块"};
for(int i=0;i<13;i++){
for(int j=3;j>=0;j--){
pai=new Pai(nums[i],huas[j]);
pai.daxiao=key;
paimap.put(key, pai);
key++;
}
}
System.out.print("----------扑克牌创建成功----------\n为:[");
for (int i=0;i<paimap.size();i++) {
System.out.print(paimap.get(i).name+"、");
}
System.out.print("]");
}
public List<Pai> xipai(){
Set<Entry<Integer,Pai>> jianzhi=paimap.entrySet();
for (Entry<Integer,Pai> entry: jianzhi) {
pailist.add(entry.getValue());
}
for(int i=0;i<pailist.size();i++){
pailist.add(sjs.nextInt(pailist.size()),pailist.get(0));
pailist.remove(0);
}
System.out.println("----------洗牌结束!----------");
return pailist;
}
public void bipai(List<Pai> pailist2,WanJia wj1,WanJia wj2){
Collections.sort(pailist2);
if(wj1.shoupai.contains(pailist2.get(1))){
System.out.println("玩家"+wj1.name+"获胜!");
}else{
System.out.println("玩家"+wj2.name+"获胜!");
}
}
}
//-----------------------------------------------------------------------------------------------------------------------
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class WanJia {
int id;
String name;
List<Pai> shoupai;
Scanner sc;
public WanJia(){
shoupai=new ArrayList<Pai>();
sc=new Scanner(System.in);
while(true){
System.out.println("请输入ID:");
try{
this.id=sc.nextInt();
}catch(Exception e){
System.out.println("ID无效,请重新输入!");
String s=sc.next(); //把scanner中的数据吐掉,不然会一直循环
continue;
}
break;
}
System.out.println("请输入姓名:");
this.name=sc.next();
}
public void getPai(List<Pai> pailist){
Pai pai=pailist.get(0);
this.shoupai.add(pai);
pailist.remove(0);
}
}
//---------------------------------------------------------------------------------------------------------------
public class Pai implements Comparable<Pai>{
public String name;
public String number;
public String hua;
public Integer daxiao;
public Pai(String num,String hua){
this.number=num;
this.hua=hua;
this.name=hua+num;
}
public int compareTo(Pai pai){
return this.daxiao.compareTo(pai.daxiao);
}
}