package imooc_collection_map_colletionscomparable;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
Game mg=new Game();
mg.getCard();//初始化牌,创造一副牌
mg.washCard();//洗牌
mg.playGame();//创建玩家并发牌,并游戏
}
}
package imooc_collection_map_colletionscomparable;
import java.util.ArrayList;
import java.util.List;
public class Player {
//玩家拥有ID,姓名,手牌
private String id;
private String name;
private List<Card>cards;
//构造方法
Player(String id,String name){// 在构造方法里不含返回值的概念是不同于 “void” 的,在定义构造方法时加了 “void” ,结果这个方法就创建不了对象。
this.id=id;
this.name=name;
this.cards=new ArrayList<Card>();
}
//重写toString方法
public String toString(){
return "name:"+name;
}
//建立属性访问器和属性更改器
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Card> getCards() {
return cards;
}
public void setCards(List<Card> cards) {
this.cards = cards;
}
}
package imooc_collection_map_colletionscomparable;
public class Card implements Comparable<Card>{
private String color;//扑克牌花色
private String count;//扑克牌点数
//构造方法
Card(String color,String count){
this.color=color;
this.count=count;
}
//重写toString方法
public String toString(){
return color+count;
}
//规定花色顺序,得到花色位置
public int getColorPoint(){
String[] colors={"黑桃","红桃","梅花","方块"};
for(int i=0;i<colors.length;i++){
if(colors[i]==color)
return i;
}
return -1;
}
//规定点数顺序,得到点数位置
public int getCountPoint(){
String[] counts={"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
for(int i=0;i<counts.length;i++){
if(counts[i]==count){
return i;
}
}
return -1;
}
@Override
public int compareTo(Card c) {
// TODO Auto-generated method stub
if(this.getCountPoint()== c.getCountPoint())
return this.getColorPoint()- c.getColorPoint();
else
return this.getCountPoint()- c.getCountPoint();
}
//建立属性访问器和属性更改器
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String getCount() {
return count;
}
public void setCount(String count) {
this.count = count;
}
}
package imooc_collection_map_colletionscomparable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.InputMismatchException;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
public class Game {
private List<Player> players;//玩家数量
private List<Card> cards;//存放卡牌,为了创造一副牌
//构造方法
public Game(){//初始化属性
players=new ArrayList<Player>();
cards=new ArrayList<Card>();
}
//初始化牌,顺序存放牌
public void getCard(){
String[] colors={"黑桃","红桃","梅花","方块"};
String[] counts={"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
for(int i=0;i<colors.length;i++){
for(int j=0;j<counts.length;j++){
cards.add(new Card(colors[i],counts[j]));
}
}
System.out.println("--------------创建了一副牌完成-----------------");
}
//洗牌操作
public void washCard(){
Random random=new Random();
for(int i=0;i<cards.size();i++){
Card c=cards.get(0);
cards.add(random.nextInt(cards.size()),c);
cards.remove(0);
}
System.out.println("---------------洗牌操作完成!----------------------");
}
//创建玩家并发牌,并游戏
public void playGame(){
Scanner in=new Scanner(System.in);
try{
for(int i=0;i<2;i++){
System.out.println("请输入第"+(i+1)+"个玩家的Id:");
int id=in.nextInt();
if(players.contains(new Player(id+"",null)))//??????????
throw new Exception();
System.out.println("请输入第"+(i+1)+"个玩家的姓名:");
String name=in.next();
players.add(new Player(id+"",name));
}
}catch(InputMismatchException e){
in=new Scanner(System.in);
System.out.println("请输入数字!");
}catch(Exception e){
System.out.println("该id已存在!");
}
//发牌
for(int i=0;i<2;i++){//牌数量
for(int j=0;j<players.size();j++){//玩家数量
players.get(j).getCards().add(cards.get(0));
cards.remove(0);
}
}
//游戏
System.out.println("---------游戏开始!-------");
for(int i=0;i<players.size();i++){
Collections.sort(players.get(i).getCards());
System.out.println("玩家:"+players.get(i).getName()+"最大手牌为:"+players.get(i).getCards().get(1));
}
int comparaNum=players.get(0).getCards().get(1).compareTo(players.get(1).getCards().get(1));
String winPlayer=comparaNum>0?players.get(0).getName():players.get(1).getName();
System.out.println("玩家:"+winPlayer+"获得胜利!");
System.out.println("玩家:"+players.get(0).getName()+"的手牌为:"+players.get(0).getCards().get(0)+" "
+players.get(0).getCards().get(1));
System.out.println("玩家:"+players.get(1).getName()+"的手牌为:"+players.get(1).getCards().get(0)+" "
+players.get(1).getCards().get(1));
}
//建立属性访问器和更改器
public List<Player> getPlayers() {
return players;
}
public void setPlayers(List<Player> players) {
this.players = players;
}
public List<Card> getCards() {
return cards;
}
public void setCards(List<Card> cards) {
this.cards = cards;
}
}
点击查看更多内容
为 TA 点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦