卡牌类
package pukepai;
public class Cards implements Comparable<Cards>{
private String huase;
private String shuzi;
//定义扑克的所有属性
public static String[] HUASE = new String[]{"黑桃", "红桃", "梅花","方块"};
public static String[] DAXIAO = new String[]{"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
//Cards 构造函数 对属性进行赋值
public Cards(String huase, String shuzi) {
super();
this.huase = huase;
this.shuzi = shuzi;
}
public Cards() {
// TODO 自动生成的构造函数存根
}
public String getHuase() {
return huase;
}
public void setHuase(String huase) {
this.huase = huase;
}
public String getShuzi() {
return shuzi;
}
public void setShuzi(String shuzi) {
this.shuzi = shuzi;
}
@Override
public String toString() {
return this.huase+this.shuzi;
}
public int compareTo(Cards o) {
// TODO 自动生成的方法存根
if(getIntDaxiao(this)==getIntDaxiao(o)){
if(getintHuase(this)<getintHuase(o))
return 1;
}else if(getIntDaxiao(this)<getIntDaxiao(o)){
return -1;
}else
{
return 0;
}
return 0;
}
//获得花色的数组索引号
int getintHuase(Cards card){
for(int i = 0;i<4;i++){
if(card.getHuase().equals(HUASE[i])){
return i;
}
}
return -1;
}
//获得卡牌大小的数组索引号
int getIntDaxiao(Cards card){
for(int i = 0;i <13; i++){
if(card.getShuzi().equals(DAXIAO[i])){
return i;
}
}
return -1;
}
//比较较大的卡牌
Cards bigCard(Cards ca){
if(this.getIntDaxiao(this)==ca.getIntDaxiao(ca)){
if(this.getintHuase(this)==ca.getintHuase(ca)){
return ca;
}
}else if(this.getIntDaxiao(this)<ca.getIntDaxiao(ca)){
return ca;
}else{
return this;
}
return ca;
}
}
用户类
package pukepai;
import java.util.ArrayList;
import java.util.List;
public class Users implements Comparable<Users>{
private int ID;
private String name;
// 每个用户手中的手牌
public List<Cards> cards = new ArrayList<Cards>();
public Users(int iD, String name) {
this.ID = iD;
this.name = name;
}
public Users() {
super();
}
public int getID() {
return ID;
}
public void setID(int iD) {
this.ID = iD;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
//重写比较方法
public int compareTo(Users o) {
// TODO 自动生成的方法存根
int compare = this.cards.get(1).compareTo(o.cards.get(1));
return compare;
}
}
主类
package pukepai;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.InputMismatchException;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
public class Main {
static int k=0;
public static void main(String[] args) {
// TODO 自动生成的方法存根
//创建一个card 的引用
Cards card = new Cards();
//创造一副新扑克牌
Cards[] cards = new Cards[52];
//用于保存洗乱后的扑克牌
Cards[] newCards = new Cards[52];
//对扑克牌进行初始化
for(int i = 0; i<52; i++){
cards[i] = new Cards(card.HUASE[i/13],card.DAXIAO[i%13]);
}
System.out.println("----------创建扑克牌----------");
System.out.println("---------初始化扑克牌---------");
for(int i =0;i<52;i++){
System.out.print(cards[i].toString()+" ");
}
//生成一组1-52的乱序数
List<Integer> cardlist = new ArrayList<Integer>();
List<Users> user = new ArrayList<Users>();
Users user1 = new Users();
Users user2 = new Users();
user.add(user1);
user.add(user2);
for(int i = 0; i < 52; i++){
cardlist.add(i);
}
//开始洗牌
Collections.shuffle(cardlist);
//洗牌结束
System.out.println("\n--------------洗牌结束------------");
for(int i = 0; i<52; i++){
newCards[i]=cards[cardlist.get(i)];
}
// for(int i = 0; i<52; i++){
// System.out.print(newCards[i].toString()+" ");
// }
Scanner input = new Scanner(System.in);
System.out.println("\n--------------创建玩家------------");
try{
System.out.println("请输入第一个玩家的Id和姓名:");
System.out.println("输入ID:");
user1.setID(input.nextInt());
System.out.println("输入姓名:");
user1.setName(input.next());
System.out.println("请输入第二个玩家的Id和姓名:");
System.out.println("输入ID:");
user2.setID(input.nextInt());
while(user2.getID()==user1.getID()){
System.out.println("输入错误!请重新输入:");
user2.setID(input.nextInt());
}
System.out.println("输入姓名:");
user2.setName(input.next());
}catch(InputMismatchException e){
System.out.println("输入类型不匹配!");
}
System.out.println("------欢迎玩家:"+user1.getName());
System.out.println("------欢迎玩家:"+user2.getName());
System.out.println("---------开始发牌---------");
for(int i = 0; i < 4; i++){
if(i%2==0){
System.out.println("--------玩家:"+user1.getName()+"拿牌");
user1.cards.add(newCards[k++]);
}else{
System.out.println("--------玩家:"+user2.getName()+"拿牌");
user2.cards.add(newCards[k++]);
}
}
System.out.println("-----------------发牌结束------------------");
System.out.println("-----------------开始游戏------------------");
//对手牌进行排序
Collections.sort(user1.cards);
Collections.sort(user2.cards);
Collections.sort(user);
System.out.println("---------恭喜玩家:"+user.get(1).getName()+"获胜");
System.out.println("---------玩家:"+user1.getName()+"最大手牌为:"+user1.cards.get(1));
System.out.println("---------玩家:"+user2.getName()+"最大手牌为:"+user2.cards.get(1));
System.out.println("---------玩家:"+user1.getName()+"手牌为"+user1.cards.get(0)+" "+user1.cards.get(1));
System.out.println("---------玩家:"+user2.getName()+"手牌为"+user2.cards.get(0)+" "+user2.cards.get(1));
System.out.println("---------恭喜玩家:"+user.get(1).getName()+"获胜");
}
}
共同学习,写下你的评论
评论加载中...
作者其他优质文章