package com.xiaoyuexiaos;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
public class TestGame {
//定义一个List来存储扑克牌
public List<Pukepai> aPuKePai;
public List<Player> aPlayer;
//初始化List
public TestGame(){
this.aPuKePai = new ArrayList<Pukepai>();
this.aPlayer = new ArrayList<Player>();
}
/**
* 添加扑克牌
* */
public void addPuKePai(){
String[] PuKePaitype = {"黑桃","红桃","梅花","方片"};
String[] PuKePaipoints = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
int typeSize = PuKePaitype.length;
int pointsSize = PuKePaipoints.length;
for(int i=0;i<typeSize;i++){
for(int j=0;j<pointsSize;j++){
Pukepai one = new Pukepai(PuKePaitype[i],PuKePaipoints[j]);
aPuKePai.add(one);
//System.out.println("添加了牌:"+ PuKePaitype[i] + PuKePaipoints[j]);
}
}
}
/**
* 添加玩家
* */
public void addPlayer(){
Scanner input = new Scanner(System.in);
for(int i = 0; i<2 ; i++){
System.out.println("请输入玩家id:");
int id = input.nextInt();
System.out.println("请输入玩家姓名:");
String name = input.next();
List<Pukepai> handPukepai = new ArrayList<Pukepai>();
Player p1 = new Player(name,id,handPukepai);
aPlayer.add(p1);
System.out.println("添加了玩家:" + p1.getName());
// for(Player p:aPlayer){
// if(id == p.getId()){
// System.out.println("该ID已被占用!!!");
// i--;
// break;
// }
// else{
//
// }
// }
//System.out.println("请输入玩家姓名:");
//name = input.next();
}
input.close();
}
/**
* 洗牌
* */
public void mixPukepai(){
Random random = new Random();
// System.out.println("洗牌前为:");
// for(Pukepai p2:aPuKePai){
// System.out.println(p2.getType()+p2.getPoints());
// }
for(Pukepai pkp:aPuKePai){
int a = random.nextInt(52);
Pukepai p1 = aPuKePai.get(a);
aPuKePai.set(a, pkp);
int index = aPuKePai.indexOf(pkp);
aPuKePai.set(index, p1);
}
// System.out.println("洗牌后为:");
// for(Pukepai p2:aPuKePai){
// System.out.println(p2.getType()+p2.getPoints());
// }
}
/**
* 发牌
* */
public void givePlayPukepai(){
Player player1 = aPlayer.get(0);
Player player2 = aPlayer.get(1);
System.out.println("给玩家"+player1.getName()+"发第一张牌");
Pukepai p1 = aPuKePai.get(0);
player1.getHandPukepai().add(p1);
System.out.println("给玩家"+player2.getName()+"发第一张牌");
Pukepai p2 = aPuKePai.get(1);
player2.getHandPukepai().add(p2);
System.out.println("给玩家"+player1.getName()+"发第二张牌");
Pukepai p3 = aPuKePai.get(2);
player1.getHandPukepai().add(p3);
System.out.println("给玩家"+player2.getName()+"发第二张牌");
Pukepai p4 = aPuKePai.get(1);
player2.getHandPukepai().add(p4);
System.out.println("发牌结束!!");
}
/**
* 得到玩家手中最大的一张手牌
* 传入一个玩家作为参数
* 取得他的手牌
* 进行比较大小
* */
public Pukepai getMaxHandPukepai(Player p){
List<Pukepai> handPukepaiList = p.getHandPukepai();
Pukepai p1 = handPukepaiList.get(0);
Pukepai p2 = handPukepaiList.get(1);
return getMaxPukepai(p1,p2);
}
/**
* 比较两张牌谁更大
* */
public Pukepai getMaxPukepai(Pukepai p1,Pukepai p2){
String[] PuKePaitype = {"方片","梅花","红桃","黑桃"};
String[] PuKePaipoints = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
int indexType1 = 0;
int indexType2 = 0;
int indexPoint1 = 0;
int indexPoint2 = 0;
//得到手牌Points对应的索引
for(int i =0;i<PuKePaipoints.length;i++){
if(p1.getPoints()==PuKePaipoints[i]){
indexPoint1 = i;
}
if(p2.getPoints()==PuKePaipoints[i]){
indexPoint2 = i ;
}
}
//得到手牌type对应的索引
for(int i =0;i<PuKePaitype.length;i++){
if(p1.getType()==PuKePaitype[i]){
indexType1 = i;
}
if(p2.getType()==PuKePaitype[i]){
indexType2 = i ;
}
}
//先进行大小的比较如果大小一样就进行类型的比较
if(indexPoint1>indexPoint2){
return p1;
}
else if(indexPoint1<indexPoint2){
return p2;
}
else//类型的比较
{
if(indexType1>indexType2){
return p1;
}
else{
return p2;
}
}
}
/**
* 比较大小游戏开始
* */
public void playGame(){
System.out.println("开始游戏!");
//得到添加的两个玩家
Player player1 = aPlayer.get(0);
Player player2 = aPlayer.get(1);
//输出他们手中的扑克牌
System.out.println(player1.getName()+"获得了"+aPuKePai.get(0).getTypeAndPoints()+
"和"+aPuKePai.get(2).getTypeAndPoints());
System.out.println(player2.getName()+"获得了"+aPuKePai.get(1).getTypeAndPoints()+
"和"+aPuKePai.get(3).getTypeAndPoints());
//获得他们手中最大的一张
Pukepai max1 = getMaxHandPukepai(player1);
Pukepai max2 = getMaxHandPukepai(player2);
//分别输出最大的一张
System.out.println("玩家"+player1.getName()+"最大的手牌为:"+
max1.getType()+max1.getPoints());
System.out.println("玩家"+player2.getName()+"最大的手牌为:"+
max2.getType()+max2.getPoints());
Pukepai max12 = getMaxPukepai(max1,max2);
if(max12.getPoints()==max1.getPoints()&& max12.getType()==max1.getType()){
System.out.println("-----恭喜玩家"+player1.getName()+"获得胜利!!!-----");
}
else{
System.out.println("-----恭喜玩家"+player2.getName()+"获得胜利!!!-----");
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
TestGame tg = new TestGame();
tg.addPuKePai();
tg.addPlayer();
tg.mixPukepai();
tg.givePlayPukepai();
tg.playGame();
}
}
//玩家类
package com.xiaoyuexiaos;
import java.util.ArrayList;
import java.util.List;
public class Player {
private String name;
private int id;
private List<Pukepai> handPukepai;
public Player(String name,int id,List<Pukepai> handPukepai){
this.name = name;
this.id = id;
this.handPukepai = new ArrayList<Pukepai>();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public List<Pukepai> getHandPukepai() {
return handPukepai;
}
public void setHandPukepai(List<Pukepai> handPukepai) {
this.handPukepai = handPukepai;
}
}
//扑克牌类
package com.xiaoyuexiaos;
public class Pukepai implements Comparable<Pukepai>{
private String type;
private String points;
public Pukepai(){}
public Pukepai(String type,String points){
this.type = type;
this.points =points;
}
public int compareTo(Pukepai o){
return this.getPoints().compareTo(o.getPoints());
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getPoints() {
return points;
}
public void setPoints(String points) {
this.points = points;
}
public String getTypeAndPoints(){
return type + points;
}
}
1 回答
MR帽子先生
TA贡献245条经验 获得超106个赞
如果你值得是一次运行的话是因为玩家一先运行的缘故,所以导致提前获取数字,虽然两者获取同一个数的概率是一样的,倒是由于程序中的优先执行顺序导致第一位玩家获取的是最大值。第二位玩家执行的时候应为数字已经不够了所以就没有了。
添加回答
举报
0/150
提交
取消