package com.imooc;
public class Card implements Comparable<Card>{
public String color;
public int num;
public Card(){}
public Card(String color,int num){
this.color=color;
this.num=num;
}
@Override
public int compareTo(Card o) {
String color="方片梅花红桃黑桃";
//大小不同,比较大小
if(this.num!=o.num){
return this.num-o.num;
}
//大小相同,比较花色
else{
//花色相同
if(this.color.equals(o.color)){
return 0;
}
//花色不同
else{
return (color.indexOf(this.color)-color.indexOf(o.color));
}
}
}
}
//----------------------------------------------------------------------------------------------------
package com.imooc;
import java.util.ArrayList;
import java.util.List;
public class Player {
public int id;
public String name;
public List<Card> cards=new ArrayList<Card>();
public Player(){}
public Player(int id,String name){
this.id=id;
this.name=name;
}
}
//----------------------------------------------------------------------------------------------------
package com.imooc;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
import java.util.Set;
public class Game {
public Scanner input=new Scanner(System.in);
public List<Card> cardList=new ArrayList<Card>();//用来创建一副扑克牌
public Set<Card> refreshCardList=new HashSet<Card>();//用来存放洗过的牌
public Random random=new Random();//用于洗牌
public Player[] players=new Player[2];//创建两名VAN家,作为全局变量使用
//创建52张牌,并洗牌,因为Set是无序,不重复的,所以向cardList中添加元素时,等于洗牌
//但是再次运行是同样的乱序,所以再创建一个set来洗牌
//其实可以创建完对象直接加在set中.但是我怕我写的太麻烦和啰嗦(其实是懒)
public void creatPoker(){
String[] colors={"黑桃","红桃","梅花","方片"};
int[] numbers={1,2,3,4,5,6,7,8,9,10,11,12,13};
Card[] cards=new Card[52];
for(int i=0;i<4;i++){
for(int j=0;j<13;j++){
cardList.add(new Card(colors[i],numbers[j]));
}
}
System.out.println("一副扑克创建完成");
}
//洗牌,根据老师的CollectionsTest.java里的方法写的,哈哈哈哈
public void refreshCard(){
Card card;
for(int i=0;i<52;i++){
do{
card=cardList.get(random.nextInt(52));
}while(refreshCardList.contains(card));
refreshCardList.add(card);
}
}
//遍历创建的牌(用来测试,无视我吧)
public void cardForEach0(){
for(Card c:cardList){
System.out.print("["+c.color+","+c.num+"]");
}
System.out.println();
}
//遍历refreshcardList(用来测试,无视我吧)
public void cardForEach(){
for(Card c:refreshCardList){
System.out.print("["+c.color+","+c.num+"]");
}
System.out.println();
}
//输入VAN家信息,创建VAN家
public void addTwoPlayer(){
System.out.println("请输入两名VAN家的信息:");
for(int i=0;i<2;i++){
System.out.print("请输入第"+(i+1)+"名VAN家的id:");
int id=input.nextInt();
System.out.print("请输入第"+(i+1)+"名VAN家的名字:");
String name=input.next();
players[i]=new Player(id,name);
System.out.println("创建成功!");
}
for (Player player : players) {
System.out.println("欢迎VAN家:"+player.name);
}
}
//向VAN家发牌,轮流发,每人两张,发完牌后,立即亮牌
public void sendCards(){
System.out.println("向VAN家发牌");
players[0].cards.add((Card)refreshCardList.toArray()[0]);
players[1].cards.add((Card)refreshCardList.toArray()[1]);
players[0].cards.add((Card)refreshCardList.toArray()[2]);
players[1].cards.add((Card)refreshCardList.toArray()[3]);
System.out.println("发牌结束");
//亮牌
for(int i=0;i<2;i++){
System.out.print(players[i].name+"的牌为:");
for (Card c : players[i].cards) {
System.out.print("["+c.color+","+c.num+"]");
}
System.out.println();
}
//比较一个选手两张牌的大小
Card[] playerMaxCard=new Card[2];//存放单个player的最大牌
for(int i=0;i<2;i++){
if(((Card)players[i].cards.get(0)).compareTo((Card)players[i].cards.get(1))>0){
playerMaxCard[i]=(Card)players[i].cards.get(0);
}else{
playerMaxCard[i]=(Card)players[i].cards.get(1);
}
}
Card[] maxCard=new Card[1];
if(playerMaxCard[0].compareTo(playerMaxCard[1])>0){
System.out.println("胜者:"+players[0].name);
}
else if(playerMaxCard[0].compareTo(playerMaxCard[1])<0){
System.out.println("胜者:"+players[1].name);
}
else if(playerMaxCard[0].compareTo(playerMaxCard[1])==0){
System.out.println("平局!!!!!!!!");
}
}
public static void main(String[] args) {
System.out.println("--------------开始游戏--------------");
Game g=new Game();
System.out.println("已创建一副扑克牌");
g.creatPoker();
System.out.println("创建的牌顺序:");
g.cardForEach0();
g.refreshCard();
System.out.println("洗牌后的顺序:");
g.cardForEach();
g.addTwoPlayer();
g.sendCards();
}
}
点击查看更多内容
3人点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦