package com.test.card;
/**
* 然后: 学渣团提供
* ZCG 提供
*/
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Random;
import java.util.Scanner;
public class Card {
public static void main(String[] args) {
Card game = new Card();
Card.cardSort();
game.shuru();
game.sendCard();
game.end();
}
/**
* 取得牌的位置,比较牌的大小
*/
public void end() {
int i = sizeCard(listCard1);
int j = sizeCard(listCard2);
System.out.println("--------------------比较开始----------------------------------");
if (i > j) {
System.out.println(listCard1.get(0) + "胜利了 ,最大的牌为:" + list1.get(i));
} else {
System.out.println(listCard2.get(0) + "胜利了 ,最大的牌为:" + list1.get(j));
}
System.out.println("----------------------比较结束--------------------------------");
}
public int sizeCard(List<String> list) {
for (String string : list) {
System.out.print(string + " ");
}
System.out.println("");
// 取得牌的位置
int i = list1.indexOf(list.get(1));
int j = list1.indexOf(list.get(2));
// 比较牌的大小
String cardMax = list1.get((i > j) ? i : j);
int cardMax1 = (i > j) ? i : j;
System.out.println(list.get(0) + "的最大牌为:" + cardMax);
return cardMax1;
}
/**
* 发牌,每人一张,共二张
*/
public static List<String> listCard1;
public static List<String> listCard2;
public void sendCard() {
Card.listCard1 = new ArrayList<String>();
Card.listCard2 = new ArrayList<String>();
int i = 0;
// 把玩家放进数组
String s1 = "ID为:" + list3.get(1).getId() + " " + "姓名为:" + list3.get(1).getName() + "的玩家";
String s2 = "ID为:" + list3.get(2).getId() + " " + "姓名为:" + list3.get(2).getName() + "的玩家";
listCard1.add(s1);
listCard2.add(s2);
// 发牌到对应的玩家去
System.out.println("----------------发牌开始----------------------------");
do {
System.out.println(list3.get(1).getName() + "拿牌");
listCard1.add(list2.get(i));
i++;
System.out.println(list3.get(2).getName() + "拿牌");
listCard2.add(list2.get(i));
i++;
} while (i < 3);
System.out.println("----------------发牌结束------------------------------");
}
/**
* 创建玩家id和姓名,id唯一,名字可以相同
*
*/
public Map<Integer, Player> list3;
public void shuru() {
this.list3 = new HashMap<Integer, Player>();
// 输入要创建的玩家id,判断id是否正确或者重复
int i = 0, j = 1;
int id = 0;
@SuppressWarnings("resource")
Scanner scanner = new Scanner(System.in);
try {
while (i < 2) {
System.out.println("*******************************");
System.out.print("请输入第" + j + "位玩家的ID:");
id = scanner.nextInt();
false1(id);
if (list3.containsKey(id)) {
System.out.println("ID已存在,再输入");
continue;
} else {
System.out.print("输入第" + j + "位玩家姓名:");
String name = scanner.next();
Player player = new Player(id, name);
list3.put(j, player);
i++;
j++;
}
}
System.out.println("*******************************");
System.out.println("----------------------------");
allPlayers(list3);
System.out.println("----------------------------");
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println("输入类型错误");
shuru();
}
}
public static int false1(int i) throws Exception {
return i;
}
/**
* 遍历玩家
*
* @param <V>
* @param <K>
*/
public static void allPlayers(Map<Integer, Player> list3) {
Collection<Entry<Integer, Player>> allPl = list3.entrySet();
for (Entry<Integer, Player> cr : allPl) {
System.out.println(cr.getKey() + ":ID为" + cr.getValue().getId() + "名字为:" + cr.getValue().getName());
}
}
/**
* 创建卡牌,和洗牌
*/
public static List<String> list2, list1;
public static void cardSort() {
list1 = new ArrayList<String>();
list2 = new ArrayList<String>();
String[] cardType = { "方块", "梅花", "红桃", "黑桃" };
String[] cardSize = { "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2" };
for (String string : cardSize) {
for (String string1 : cardType) {
String card = string1 + string;
list1.add(card);
}
}
System.out.println("------------------创建卡牌--------------------------");
for (String string : list1) {
System.out.print(string + " ");
}
System.out.println("");
System.out.println("-----------------------洗牌-------------------------");
Random random = new Random();
String k = "";
for (int i = 0; i < list1.size(); i++) {
do {
k = list1.get(random.nextInt(list1.size()));
} while (list2.contains(k));
list2.add(k);
}
for (String string : list2) {
System.out.print(string + " ");
}
System.out.println("------------------------洗牌结束------------------");
}
}
package com.test.card;
/**
* 然后: 学渣团提供 ZCG 提供
*/
public class Player {
private int id;
private String name;
public Player(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
非常感谢,我一定要借鉴学习