package com.imooc;
import java.util.ArrayList;
import java.util.List;
public class Player {
private int id;
private String name;
private List<Card> handCards;
public Player(int id,String name){
this.id=id;
this.name=name;
this.handCards=new ArrayList<Card>();
}
public Player(){
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public List<Card> getHandCards() {
return handCards;
}
}
package com.imooc;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Card implements Comparable<Card>{
private String color;
private String value;
private static final List<String> Colors=Arrays.asList("方片","梅花","红桃","黑桃");
private static final List<String> Values=Arrays.asList("2","3","4","5","6","7","8","9","10","J","Q","K","A");
public Card(String color,String value){
this.color=color;
this.value=value;
}
public Card(){
this.color="";
this.value="";
}
public String getColor() {
return color;
}
public String getValue() {
return value;
}
public int compareTo(Card o) {
if(Values.indexOf(this.value)==Values.indexOf(o.value))
return Integer.valueOf(Colors.indexOf(this.color)).compareTo(Integer.valueOf(Colors.indexOf(o.color)));
else
return Integer.valueOf(Values.indexOf(this.value)).compareTo(Integer.valueOf(Values.indexOf(o.value)));
}
public static List<Card> generateCard(){
List<Card> poker=new ArrayList<Card>();
for(int i=0;i<3;i++){
String color=Colors.get(i);
for(int j=0;j<13;j++){
String value=Values.get(j);
poker.add(new Card(color,value));
}
}
return poker;
}
@Override
public String toString() {
return color+value;
}
}
package com.imooc;
import java.util.*;
public class PokerGame {
private List<Card> cards;
private List<Player> players;
Scanner scanner;
private static final int HandCardNum=2;
private static final int PlayerNum=2;
public PokerGame(){
this.cards=Card.generateCard();
scanner=new Scanner(System.in);
this.players=new ArrayList<Player>();
}
public void showCards(){
System.out.println(cards);
}
public void createPlayers(){
int id;
String name;
for(int i=0;i<PlayerNum;){
try{
System.out.println("创建第"+(i+1)+"位玩家");
System.out.println("请输入玩家编号:");
id=scanner.nextInt();
}
catch(Exception e){
System.out.println("请输入正整数:");
scanner.next(); //吸收缓冲区
continue;
}
System.out.println("请输入玩家姓名:");
name=scanner.next();
players.add(new Player(id,name));
i++;
}
}
public void shuffleCards(){
Collections.shuffle(cards);
}
public void showPlayers(){
for(Player p:players)
System.out.println("欢迎玩家"+p.getName());
}
public void play(){
System.out.println("-----------------开始发牌----------------");
for(int i=0;i<HandCardNum*PlayerNum;i++){
players.get(i%PlayerNum).getHandCards().add(cards.get(i));
System.out.println("玩家:"+players.get(i%PlayerNum).getName()+"拿牌");
}
System.out.println("-----------------发牌结束----------------");
System.out.println("-----------------开始游戏----------------");
int winner=0;
Card max=new Card();
for(int i=0;i<PlayerNum;i++){
Collections.sort(players.get(i).getHandCards());
Collections.reverse(players.get(i).getHandCards());
Card maxCard=players.get(i).getHandCards().get(0);
System.out.println("玩家:"+players.get(i).getName()+"的最大手牌为:"+maxCard);
if (max.compareTo(maxCard) < 0) {
max=maxCard;
winner = i;
}
}
System.out.println("-------------玩家"+players.get(winner).getName()+"获胜---------");
System.out.println("玩家各自的手牌为:");
for (Player p:players){
System.out.println(p.getName()+":"+p.getHandCards());
}
}
public static void main(String[] args) {
System.out.println("---------------创建扑克牌------------------");
PokerGame pg=new PokerGame();
System.out.println("--------------创建扑克牌成功----------------------");
pg.showCards();
System.out.println("--------------开始洗牌----------------------");
pg.shuffleCards();
System.out.println("--------------洗牌结束----------------------");
System.out.println("--------------创建玩家----------------------");
pg.createPlayers();
pg.showPlayers();
pg.play();
}
}
参考了很多慕友的代码,希望对大家有帮助
对于初学者来说,每一次写程序真的很痛苦,坚持下来就好,一定要多练,大家一起加油!
热门评论
我想问一下,conllection.sort的排序还是默认的吗?花色是按字母,牌数是按大小,那为什么我运行出A大于K呢?字母排序不应该是A,B,C.......K。希望得到好的回复,谢谢!
谢谢大家建议 个人感觉没有注视更清晰一点 以后会加的
感觉你在代码里应该多写点注释