package collection_map_demo.Game;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Player {
public String id;
public String name;
public List pokers;
public Player(String id, String name){
this.id = id;
this.name = name;
this.pokers = new ArrayList();
}
public void dropPokers(){
pokers.clear();
}
public Poker selectMaxPoker(){
Collections.sort(pokers);
return (Poker) pokers.get(0);
}
}
package collection_map_demo.Game;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class Poker implements Comparable<Poker> {
public String point;
public String color;
public Poker(String point,String color){
this.point = point;
this.color = color;
}
public Poker() {
}
@Override
public int compareTo(Poker o) {
//比当前的小返回正整数
//比当前的大返回副整数
//相等返回0
if(this.point.compareTo(o.point) == 0) {
return this.color.compareTo(o.color);
}else{
return this.point.compareTo(o.point);
}
}
}
package collection_map_demo.Game;
import java.util.*;
public class PokerGame {
private String[] pointsToSelect;
private String[] colorsToSelect;
private static List<Poker> combination;
public List<Player> players;
private Scanner scanner;
public PokerGame(){
this.scanner = new Scanner(System.in);
this.players = new ArrayList<Player>();
this.pointsToSelect = new String[]{"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"};
this.colorsToSelect = new String[]{"黑桃", "红桃", "梅花", "方片"};
this.combination = new ArrayList<Poker>();
}
public static void main(String[] args) {
PokerGame pockerGame = new PokerGame();
pockerGame.createPokers();
pockerGame.intermingle(combination);
pockerGame.createPlayer();
pockerGame.startGame();
}
public void createPokers(){
for(String color : colorsToSelect){
for(String point : pointsToSelect){
Poker poker = new Poker(point,color);
System.out.println(poker.color + poker.point);
combination.add(poker);
}
}
}
public void intermingle(List<Poker> pokers){
Collections.shuffle(pokers);
}
public void createPlayer(){
int i = 1;
while (i < 3){
System.out.println("请输入玩家" + i + "ID");
String id = scanner.next();
System.out.println("请输入玩家" + i +"姓名");
String name = scanner.next();
Player player = new Player(id,name);
System.out.println(player.id + player.name);
players.add(player);
i++;
}
Integer player_num = players.size();
System.out.println("玩家组有" + player_num + "名玩家");
for(Player player : players){
System.out.println("玩家" + player.id + player.name);
}
}
public void startGame(){
System.out.println("发牌");
Player player1 = players.get(0);
Player player2= players.get(1);
int i = 1;
while ( i <= 4){
Poker poker = combination.get(i);
if( i % 2 != 0){
//发给玩家1
player1.pokers.add(poker);
}else{
//发给玩家2
player2.pokers.add(poker);
}
i++;
}
System.out.println("玩家1 手牌");
for(Object poker : player1.pokers){
Poker new_poker = (Poker) poker;
System.out.println(new_poker.point + new_poker.color);
}
System.out.println("玩家2 手牌");
for(Object poker : player2.pokers){
Poker new_poker = (Poker) poker;
System.out.println(new_poker.point + new_poker.color);
}
Poker player_1_max = player1.selectMaxPoker();
Poker player_2_max = player2.selectMaxPoker();
if(player_1_max.point.compareTo(player_2_max.point) == 0){
if(player_1_max.color.compareTo(player_2_max.color) < 0){
System.out.println("玩家 2 赢");
}else{
System.out.println("玩家 1 赢");
}
}else if(player_1_max.point.compareTo(player_2_max.point) < 0){
System.out.println("玩家 2 赢");
}else{
System.out.println("玩家 1 赢");
}
player1.dropPokers();
player2.dropPokers();
}
}