首页 > IT职业 > IT图书 > JAVA入门第三季实战:简易扑克牌
本程序使用了ArrayList 、HashMap,简单应用了Java第三季的集合框架
首先创建一个卡牌类
/**
* 扑克类
*/
public class Poker {
public Poker(String size,String number){
this.size=size;
this.number=number;
}
public String getSize(){
return this.size;
}
public String getNumber(){
return this.number;
}
private String size;
private String number;
}
然后创建一个卡牌玩家类
import java.util.ArrayList;
import java.util.List;
/**
* 打牌人员类
*/
public class PokerPlayer {
public PokerPlayer(String id,String name){
this.id=id;
this.name=name;
this.playerpoker = new ArrayList();
}
public String getId(){
return this.id;
}
public String getName(){
return this.name;
}
private String id;
private String name;
public List playerpoker;
public String a;
}
最后一个就大杂烩了,流程如下:创建卡牌--遍历卡牌--创建玩家--发牌--比较卡牌;
创建卡牌用ArrayList
遍历卡牌用foreach方法
创建玩家用到了HashMap
发牌过程较简单,仅为随机不重复四个数字
卡牌比较可以扩展一下,我写的比较简单了
import java.util.*;
public class PokerList {
private List poker;
private Map player ;
private int a1,a2,b1,b2;//用来比较
Scanner in = new Scanner(System.in);
public PokerList (){
this.poker = new ArrayList();
this.player = new HashMap();
}
public void addPoker(){
System.out.println("正在创建卡牌......");
String[] size={"黑桃","红桃","梅花","方块"};
String[] number={"A","2","3","4","5","6","7","8","9","10","J","Q","k"};
for (String a:size) {
for (String b:number) {
Poker apoker = new Poker(a,b);
poker.add(apoker);
}
}
}
public void pokerforeach(){
System.out.print("已创建卡牌: ");
for (Poker pok:poker) {
System.out.print(pok.getSize()+pok.getNumber()+", ");
}
System.out.println("n正在洗牌......");
}
public void creatPlayer(){
int i=0;
while (ia2)
amax=a1;
else amax=a2;
if (b1>b2)
bmax=b1;
else bmax=b2;
if (amax>bmax)
System.out.println("玩家一胜利");
else System.out.println("玩家二胜利");
}
public static void main(String[] args){
PokerList pl= new PokerList();
pl.addPoker();//创建卡牌
pl.pokerforeach();//遍历卡牌
pl.creatPlayer();//创建玩家
pl.shuttle();//发牌
pl.comparison();
}
} 来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/3276/viewspace-2799248/ ,如需转载,请注明出处,否则将追究法律责任。