package com.imooc;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
import java.util.Set;
/**
* 控制器,主函数
* @author jelly
*
*/
public class Controller {
List<Poker> poker = new ArrayList<Poker>();
List<Player> player = new ArrayList<Player>();
Set<Poker> newPoker;
//创建poker牌
public void build() {
System.out.println("-----------开始创建扑克牌-----------");
String[] suits = { "方片", "梅花", "红桃", "黑桃" };
String[] numbers = { "2", "3", "4", "5", "6", "7", "8", "9", "10", "J",
"Q", "K", "A", };
System.out.println("-----------扑克创建成功----------");
System.out.print("为:[");
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 13; j++) {
poker.add(new Poker(suits[i], numbers[j]));
}
}
for (Poker pk : poker) {
System.out.print(pk + ",");
}
System.out.println("]");
}
// 洗牌
public void shuffle() {
System.out.println("----------开始洗牌----------");
this.newPoker = new HashSet<>(poker);
System.out.println("----------洗牌结束----------");
}
// 创建玩家
public void buildPlayer() {
System.out.println("----------创建玩家----------");
int i = 0;
while (i < 2) {
System.out.println("请输入第" + (i + 1) + "位玩家的ID和姓名");
try {
Scanner in = new Scanner(System.in);
System.out.println("输入ID:");
int id = in.nextInt();
System.out.println("输入姓名:");
String name = in.next();
player.add(new Player(id, name));
i++;
} catch (Exception e) {
System.out.println("请输入整数类型的ID");
}
}
for (int k = 0; k < player.size(); k++) {
System.out.println("欢迎玩家:" + player.get(k).getName());
}
}
// 发牌
public void deal() {
System.out.println("----------开始发牌----------");
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
Iterator<Poker> it = newPoker.iterator();
Poker poker = it.next();
player.get(j).handPoker.add(poker);
System.out.println("玩家" + player.get(j).getName() + "拿牌");
newPoker.remove(poker);
}
}
System.out.println("----------发牌结束----------");
}
public void game() {
System.out.println("----------开始游戏----------");
Compare compare = new Compare();
for (int i = 0; i < 2; i++) {
Collections.sort(player.get(i).handPoker, compare);
System.out.println("玩家" + player.get(i).getName() + "最大的手牌为"
+ player.get(i).handPoker.get(1));
}
if (compare.compare(player.get(0).handPoker.get(0),
player.get(1).handPoker.get(1)) > 0) {
System.out.println("玩家" + player.get(0).getName() + "获胜");
} else {
System.out.println("玩家" + player.get(1).getName() + "获胜");
}
System.out.println("玩家各自的手牌为");
for (int j = 0; j < 2; j++) {
System.out.println("玩家"+player.get(j).getName());
for (Poker poker : player.get(j).handPoker) {
System.out.println(poker+" ");
}
}
}
public static void main(String[] args) {
Controller cr = new Controller();
cr.build();
cr.shuffle();
cr.buildPlayer();
cr.deal();
cr.game();
}
}