Card
import java.util.Arrays;
import java.util.List;
public class Card implements Comparable<Card> {
public String cColor;
public String point;
public static final List<String> cColors = Arrays.asList("方片", "梅花", "红桃", "黑桃");
public Card(String cColor,String point){
this.cColor=cColor;
this.point=point;
}
@Override
public String toString() {
return cColor + point;
}
@Override
public int compareTo(Card o) {
// TODO 自动生成的方法存根
if(o.point.equals(this.point))
return Integer.valueOf(cColors.indexOf(cColor)).compareTo(Integer.valueOf(cColors.indexOf(o.cColor)));
else
return this.point.compareTo(o.point);
}
}
Player
import java.util.ArrayList;
import java.util.List;
public class Player {
public int id;
public String name;
public List<Card> twoCards;
public Player(int id,String name){
this.id = id;
this.name = name;
twoCards = new ArrayList<Card>();
}
}
Game
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class Game {
private final String[] points = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
private final String[] cColors = { "黑桃", "红桃", "梅花", "方片" };
private Scanner console;
private final int num = 2; //玩家的数量
private final int cardNum = 2;//每人2张牌
private List<Card> cardList;
private List<Player> playerList;
private Game(){
cardList = new ArrayList<Card>();
playerList = new ArrayList<Player>();
}
/**
* 初始化cardList并且打乱
*/
private void cards(){
Card newCard;
System.out.println("---------------创建扑克牌-----------------");
for(int i = 0;i < cColors.length;i++){
for(int j = 0;j < points.length;j++){
newCard = new Card(cColors[i],points[j]);
// System.out.println(newCard.toString());
cardList.add(newCard);
}
}
System.out.println("---------------扑克牌创建成功-----------------");
System.out.println(cardList.toString());
System.out.println("---------------开始洗牌-----------------");
Collections.shuffle(cardList);
System.out.println("---------------洗牌结束-----------------");
// for(Card card:cardList){
// System.out.println(card.toString());
// }
}
/**
* 创建玩家
*/
public void twoPlays(){
for(int i = 0;i < num;i++){
System.out.println("请输入第"+(i+1)+"位玩家的ID和姓名:");
console = new Scanner(System.in);
Integer newID;
while(true){
System.out.println("输入ID:");
try{
newID = console.nextInt();
break;
}catch(Exception e){
console.next();
System.out.println("输入错误,请输入正整数!!");
continue;
}
}
System.out.println("输入姓名:");
String newName = console.next();
Player newPlayer = new Player(newID,newName);
playerList.add(newPlayer);
}
for(Player player:playerList){
System.out.println("----欢迎玩家:"+player.name);
}
}
/**
* 开始发放
*/
public void playerHasCards(){
System.out.println("---------------开始发牌-----------------");
for(int i = 0;i < cardNum*2;i++){
if(i%2 == 0){
playerList.get(0).twoCards.add(cardList.get(i));
System.out.println("----玩家:"+playerList.get(0).name+"-拿牌");
//System.out.println(playerList.get(0).twoCards);
}else{
playerList.get(1).twoCards.add(cardList.get(i));
System.out.println("----玩家:"+playerList.get(1).name+"-拿牌");
//System.out.println(playerList.get(1).twoCards);
}
}
System.out.println("---------------发牌结束!-----------------");
}
public void startGame(){
System.out.println("---------------开始游戏!-----------------");
Card[] cards = new Card[cardNum];
for(int i = 0;i < num;i++){
if(playerList.get(i).twoCards.get(0).compareTo(playerList.get(i).twoCards.get(1)) > 0){
System.out.println("玩家:"+playerList.get(i).name+"最大的手牌为:"+playerList.get(i).twoCards.get(0));
cards[i] = playerList.get(i).twoCards.get(0);
}else{
System.out.println("玩家:"+playerList.get(i).name+"最大的手牌为:"+playerList.get(i).twoCards.get(1));
cards[i] = playerList.get(i).twoCards.get(1);
}
}
if(cards[0].compareTo(cards[1]) > 0){
System.out.println("----------玩家:"+playerList.get(0).name+"获胜!----------");
}else{
System.out.println("----------玩家:"+playerList.get(1).name+"获胜!----------");
}
System.out.println("玩家各自的手牌为:");
System.out.println(playerList.get(0).name+":"+playerList.get(0).twoCards.toString());
System.out.println(playerList.get(1).name+":"+playerList.get(1).twoCards.toString());
}
public static void main(String[] args){
Game game = new Game();
game.cards();
game.twoPlays();
game.playerHasCards();
game.startGame();
}
}
点击查看更多内容
10人点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦