package pukeGame;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
public class Player {
Scanner input = new Scanner(System.in);//输入类
List<Puke> Pukecontainer = new ArrayList<Puke>();//相当于玩家手的拿牌的功能
int Id;//玩家的Id
String name;//玩家的姓名
public Player(String name, int Id) {
this.name = name;
this.Id = Id;
}
public Player() {//构造方法
}
public Puke takePoker(List<Puke> l) {//模拟的发牌功能,玩家摸牌
Random random = new Random();
int i = random.nextInt(l.size());
Puke p = l.get(i);
l.remove(i);//移出已经抽出去的扑克牌
Pukecontainer.add(p);
return p;
}
public Puke PickMaxPoker() {// 挑选出玩家手上最大的扑克牌
Puke MaxPoker = null;
Puke poker[] = new Puke[Pukecontainer.size()];// 取出手上牌并存放在poker数组中
for (Puke Poker : Pukecontainer) {
int i = 0;
while (i < Pukecontainer.size()) {
poker[i] = Poker;// Pukecontainer.get(i);
i++;
}
}
for (int j = 0; j < poker.length; j++) {
MaxPoker = poker[0];
if (poker[j].shunxu > MaxPoker.shunxu)
MaxPoker = poker[j];
}
return MaxPoker;
}
}
package pukeGame;
import java.util.ArrayList;
import java.util.List;
public class Puke {//扑克类赋予colorline(花色序列属性)以及数字大小numline属性用来区别扑克牌的大小
List<String> colorline=new ArrayList<String>();
List<String> numline=new ArrayList<String>();
@Override
public String toString() {
return color + num;
}
String color;
String num;
int shunxu;
public Puke(String color,String num,int shunxu)
{
this.color=color;
this.num=num;
this.shunxu=shunxu;
}
public Puke()
{}
public void compare()
{
}
}
package pukeGame;
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
public class Test {
List<Puke> container = new ArrayList<Puke>();
List<Integer> pukelocation = new ArrayList<Integer>();
List<Puke> washedpuke = new ArrayList<Puke>();
//static Player p1;
//static Player p2;
public static void main(String[] args) {
// TODO Auto-generated method stub
Test t = new Test();
List<Puke> originalpuke = t.PukeCreate();
System.out.println(originalpuke);
System.out.println("--------扑克牌创建成功---------");
System.out.println("--------开始洗牌---------");
List<Puke> washedpoker = t.PukeWash(originalpuke);
System.out.println("--------洗牌成功---------");
System.out.println(washedpoker);
System.out.println("创建第一个玩家");
Player p1=joinPlayer();// Player p1 = fuzhu();//在这使用fuzhu()函数的时候出现的NullPointException
//System.out.println("请输入整形id和String类型姓名");
System.out.println("创建第二个玩家");
Player p2=joinPlayer();// Player p2 = fuzhu();
System.out.println("玩家1抽到" + p1.takePoker(washedpoker));//System.out.println(washedpoker.size());
System.out.println("玩家2抽到" + p2.takePoker(washedpoker));//System.out.println(washedpoker.size());
System.out.println("玩家1抽到" + p1.takePoker(washedpoker));//System.out.println(washedpoker.size());
System.out.println("玩家2抽到" + p2.takePoker(washedpoker));//System.out.println(washedpoker.size());
System.out.println("玩家" + p1.name + "手上的牌是" + p1.Pukecontainer);
System.out.println("玩家" + p2.name + "手上的牌是" + p2.Pukecontainer);
Puke p1Maxpoker = p1.PickMaxPoker();
Puke p2Maxpoker = p2.PickMaxPoker();
if (p1Maxpoker.shunxu > p2Maxpoker.shunxu) {
System.out.println(p1.name + "赢了!!!");
}else{System.out.println(p2.name+"赢了!!!");}
}
public List<Puke> PukeCreate() {// 创建一副有序的扑克牌
String color[] = { "红桃", "梅花", "方片", "黑桃" };
String num[] = { "A", "2", "3", "4", "5", "6", "7", "8", "9", "10",
"J", "Q", "K" };
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 13; j++) {
Puke p = new Puke(color[i], num[j], i * 13 + j);
container.add(p);
}
}
return container;
}
public List<Puke> PukeWash(List<Puke> p) {//将有序的扑克牌洗成无序的
// int location;
int count = 0;
Random random = new Random();
// List washedpuke = new Puke[51];
while (count < 52) {
boolean flag = pukelocation.add(random.nextInt(52));
if (flag)
count++;
}
for (int j = 0; j < 52; j++) {
Puke poker = p.get(pukelocation.get(j));
washedpuke.add(poker);
}
return washedpuke;
}
public static Player joinPlayer(){//加入玩家的函数,执行一次加入一个玩家
int id=0;
Scanner input = new Scanner(System.in);
System.out.println("请输入玩家Id");
try{id = input.nextInt();}
catch(InputMismatchException I)
{
System.out.println("请输入整型Id!!!");
joinPlayer();
}
System.out.println("请输入玩家姓名");
String name = input.next();
// input.close();//不能关闭input因为还需要创建第二个玩家关闭了就会出现异常
Player p1 = new Player(name, id);
return p1;
}
/*
* *
* 没什么用主要是为了解决输入不匹配异常的时候瞎想出来的函数,使用的时候老是提示什么NullPionterException
* public static Player fuzhu()
{Player p=null;
try{p=joinPlayer();}
catch(InputMismatchException I)
{
System.out.println("请输入整形id和String类型的名字");
fuzhu();
}
return p;
}*/
}
相关分类