大神 帮忙看下 为啥两个玩家取出的牌都是一样的 我感觉第二次没有运行

package base;


import java.util.ArrayList;

import java.util.Arrays;

import java.util.Comparator;

import java.util.HashSet;

import java.util.List;

import java.util.Random;

import java.util.Set;


public class Card {

private static List<String> CardList;


public Card() {

super();

CardList = new ArrayList<String>();

}


/**

* 创建扑克牌

* @param void

*/

public void createCard(){

String[] c = new String[52];

for (int i = 0; i < c.length; i++) {

if(i>=0&&i<=12){

switch(i){

case 0:c[i]="黑桃A";break;

case 10:c[i]="黑桃J";break;

case 11:c[i]="黑桃Q";break;

case 12:c[i]="黑桃K";break;

default:c[i]="黑桃"+(i+1);break;

}

}else if(i>12&&i<=25){

switch(i){

case 0+13:c[i]="红心A";break;

case 10+13:c[i]="红心J";break;

case 11+13:c[i]="红心Q";break;

case 12+13:c[i]="红心K";break;

default:c[i]="红心"+(i-13+1);break;

}

}else if(i>25&&i<=38){

switch(i){

case 0+13*2:c[i]="梅花A";break;

case 10+13*2:c[i]="梅花J";break;

case 11+13*2:c[i]="梅花Q";break;

case 12+13*2:c[i]="梅花K";break;

default:c[i]="梅花"+(i-13*2+1);break;

}

}else{

switch(i){

case 0+13*3:c[i]="方块A";break;

case 10+13*3:c[i]="方块J";break;

case 11+13*3:c[i]="方块Q";break;

case 12+13*3:c[i]="方块K";break;

default:c[i]="方块"+(i-13*3+1);break;

}

}

}

CardList.addAll(Arrays.asList(c));

}

/**

* 重洗扑克牌

* @param void

*/

public void washThePoker(){

Random random = new Random();

Set<Integer> set = new HashSet<Integer>();

boolean flag = true;

for (int i = 0; i < CardList.size(); i++) {

flag = true;

while(flag){

int a = random.nextInt(52);

while(set.add(a)){

CardList.add(a,CardList.get(i));

CardList.remove(i);

flag=false;

}

}

}

}

/**

* 迭代扑克牌

* @param List<String>

*/

public List<String> forCard(){

for (int i = 0; i < CardList.size(); i++) {

if(i==12){

System.out.println(CardList.get(i));

}else if(i==25){

System.out.println(CardList.get(i));

}else if(i==38){

System.out.println(CardList.get(i));

}else if(i==51){

System.out.println(CardList.get(i));

}else{

System.out.print(CardList.get(i)+"\t");

}

}

return CardList;

}

public List<String> initCard(){

System.out.println("创建扑克牌");

createCard();

System.out.println("循环遍历扑克牌");

forCard();

System.out.println("洗扑克牌");

washThePoker();

System.out.println("再循环遍历扑克牌");

List<String> CardList = forCard();

return CardList;

}

}



package base;


import java.util.ArrayList;

import java.util.HashSet;

import java.util.List;

import java.util.Random;

import java.util.Scanner;

import java.util.Set;


public class Person {

private int Id;

private String name;

private static Set<Integer> set ;

static{

Person.set = new HashSet<Integer>();

}

public Person() {

super();

// TODO Auto-generated constructor stub

}

public int getId() {

return Id;

}

public void setId(int id) {

Id = id;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

/**

* 从52张扑克牌中挑选2张 

* @param CardList

* @return

*/

public List<String> openHandTo(List<String> CardList,Set<Integer> set){

List<String> drawAHand = new ArrayList<String>();

System.out.println(drawAHand+"\t"+this.getName());

boolean flag = true;

Random random = new Random();

int b = 0;

for (int i = 0; i < CardList.size(); i++) {

if(b<2){

flag = true;

while(flag){

int a = random.nextInt(52);

while(set.add(a)){

drawAHand.add(CardList.get(i));

System.out.print(CardList.get(i)+"\t");

flag=false;

}

}

}

b++;

}

System.out.println(set.size());

for (Integer integer : set) {

System.out.print(integer+"\t");

}

for (String string : drawAHand) {

System.out.print(string+"\t");

}

return drawAHand;

}


/**

* 展示手中牌

* @param drawAHand

*/

public void forCard(List<String> drawAHand){

for (int i = 0; i < drawAHand.size(); i++) {

if(i==drawAHand.size()-1){

System.out.println(drawAHand.get(i));

}else{

System.out.print(drawAHand.get(i)+"\t");

}

}

}


/**

* 比较手中的牌的大小选出最大的

* @param drawAHand

*/

public void compareOwnCard(List<String> drawAHand){

String a  = drawAHand.get(0).substring(2);

switch(a){

case "A":a="1";break;

case "J":a="11";break;

case "Q":a="12";break;

case "K":a="13";break;

}

System.out.println(a);

String a1  = drawAHand.get(0).substring(0,2);

switch(a1){

case "黑桃":a1="4";break;

case "红心":a1="3";break;

case "梅花":a1="2";break;

case "方块":a1="1";break;

}

System.out.println(a1);

String b = drawAHand.get(1).substring(2);

switch(b){

case "A":b="1";break;

case "J":b="11";break;

case "Q":b="12";break;

case "K":b="13";break;

}

System.out.println(b);

String b1 = drawAHand.get(1).substring(0,2);

switch(b1){

case "黑桃":b1="4";break;

case "红心":b1="3";break;

case "梅花":b1="2";break;

case "方块":b1="1";break;

}

System.out.println(b1);

if(Integer.parseInt(a)==Integer.parseInt(b)){

if(Integer.parseInt(a1)>Integer.parseInt(b1)){

drawAHand.add(drawAHand.get(0)+","+a+","+a1);

}else{

drawAHand.add(drawAHand.get(1)+","+b+","+b1);

}

}else if(Integer.parseInt(a)>Integer.parseInt(b)){

drawAHand.add(drawAHand.get(0)+","+a+","+a1);

}else{

drawAHand.add(drawAHand.get(1)+","+b+","+b1);

}

}


/**

* 第一个玩家开始玩游戏

* @param drawAHand

*/

public List<String> initPlayerAndCard(int id,List<String> CardList){


Scanner scanner = new Scanner(System.in);

System.out.println("请输入第"+id+"个玩家ID");

int a = scanner.nextInt();

this.setId(a);

System.out.println("请输入第"+id+"个玩家NAME");

String b =scanner.next();

this.setName(b);

System.out.println("玩家"+b+"开始选牌");

List<String> drawAHand = this.openHandTo(CardList,Person.set);

System.out.println("玩家"+b+"开始看自己的牌");

this.forCard(drawAHand);

System.out.println("玩家"+b+"开始从自己的牌中挑出最大的牌");

this.compareOwnCard(drawAHand);

System.out.println("玩家"+b+"手中最大的牌是"+drawAHand.get(2).split(",")[0]);

return drawAHand;

}

}




package base;


import java.util.List;


public class Test {

public Integer TwoPersonCompareCard(List<String> player1,List<String> player2){

String[] p1 = player1.get(2).split(",");

int p1_1 = Integer.parseInt(p1[1]);

int p1_2 = Integer.parseInt(p1[2]);

String[] p2 = player1.get(2).split(",");

int p2_1 = Integer.parseInt(p1[1]);

int p2_2 = Integer.parseInt(p1[2]);

if(p1_1==p2_1){

if(p1_1>p1_2){

return 1;

}else{

return 2;

}

}else if(p1_1>p2_1){

return 1;

}

return 2;



}

public void win(Person person1,Person person2,int a,List<String> player1,List<String> player2){

if(a==1){

System.out.println("玩家"+person1.getName()+"获得胜利,手中的拍是"+

player1.get(2).split(",")[0]);

}else{

System.out.println("玩家"+person2.getName()+"获得胜利,手中的拍是"+

player2.get(2).split(",")[0]);

}

}

public static void main(String[] args) {

Card card = new Card();

List<String> CardList = card.initCard();

Person person1 = new Person();

List<String> player1 = person1.initPlayerAndCard(1, CardList);

Person person2 = new Person();

List<String> player2 = person2.initPlayerAndCard(2, CardList);

Test test = new Test();

int a = test.TwoPersonCompareCard(player1, player2);

test.win(person1, person2, a, player1, player2);

}


}


qq__6307
浏览 1142回答 0
0回答
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java