为了账号安全,请及时绑定邮箱和手机立即绑定

简易纸牌游戏:两个玩家每人两张牌比较大小

标签:
Java

Cards.java:
package com.cardsgame;
import java.util.HashSet;
import java.util.Set;
public class Cards {
public String[] type = {"ablack","bred","cflower","dsquare"};
public String[] num = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
public Set<String> card = new HashSet<String>();
public void GetCards(){
// for(int i=0;i<type.length;i++)
// for(int j=0;j<num.length;j++){
// card.add(type[i]+num[j]);
// }
for(String m:type)
for(String n:num){
card.add(m+"—"+n);
}
}
public void PrintCards(){
for(String car:card){
System.out.println(car);
}
}

public static void main(String[] args) {
// TODO Auto-generated method stub
Cards c = new Cards();
c.GetCards();
c.PrintCards();
}
}

ComparatorForUsers.java:
package com.cardsgame;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
public class ComparatorForUsers implements Comparator<Users>{
@Override
public int compare(Users o1, Users o2) {
// TODO Auto-generated method stub
Set<String> user1card = o1.userCardsSet;
Set<String> user2card = o2.userCardsSet;
int res = GetMax(user1card,user2card);
if(res>0){
return 1;
}else{
return 0;
}
}

public int GetMax(Set<String> ss1,Set<String> ss2){
List<String> s1 = new ArrayList<String>();
List<String> s2 = new ArrayList<String>();
Iterator<String> s1t = ss1.iterator();
Iterator<String> s2t = ss2.iterator();
while(s1t.hasNext()){
s1.add(s1t.next());
}
while(s2t.hasNext()){
s2.add(s2t.next());
}
String[] s1_1 = s1.get(0).split("—");
String[] s1_2 = s1.get(1).split("—");
String[] s2_1 = s2.get(0).split("—");
String[] s2_2 = s2.get(1).split("—");
String[] s1Max = s1_1[1].compareTo(s1_2[1])>0?s1_1:s1_2;
String[] s2Max = s2_1[1].compareTo(s2_2[1])>0?s2_1:s2_2;
String s1numMax = s1Max[1];
String s2numMax = s2Max[1];
if(s1numMax.equals("A")){
s1numMax = "S"+s1numMax;
}
if(s1numMax.equals("K")){
s1numMax = "R"+s1numMax;
}
if(s2numMax.equals("A")){
s2numMax = "S"+s2numMax;
}
if(s2numMax.equals("K")){
s2numMax = "S"+s2numMax;
}
if(s1numMax.compareTo(s2numMax)>0){
System.out.println("当前最大的牌是:"+s1Max[0]+"—"+s1Max[1]);
return 1;
}else if(s1numMax==s2numMax){
if(s1Max[0].compareTo(s2Max[0])>0){
System.out.println("当前最大的牌是:"+s1Max[0]+"—"+s1Max[1]);
return 1;
}
else {
System.out.println("当前最大的牌是:"+s2Max[0]+"—"+s2Max[1]);
return 0;}
}else{
System.out.println("当前最大的牌是:"+s2Max[0]+"—"+s2Max[1]);
return 0;
}
}
}

Users.java:(主类)
package com.cardsgame;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
public class Users {
public int id;
public String name;
Random random = new Random();
public Set<String> userCardsSet = new HashSet<String>();
public static List<String> cardtmp = new ArrayList<String>();
public List<String> cardList = new ArrayList<String>();
public Users(int id,String name) {
this.id = id;
this.name = name;
}

public List<String> GetListCards(Cards card){
Iterator<String> it = card.card.iterator();
while(it.hasNext()){
cardtmp.add(it.next());
}
return cardtmp;
}
public void Get1Cards(Users user,Cards card){
for (int i=0;i<1;i++){
System.out.println(user.name +"get card:"+cardList.get(i));
userCardsSet.add(cardList.get(i));
Users.cardtmp.remove(cardList.get(i));
}
}

public void ShowUserCard(Users user){
for(String set:userCardsSet){
System.out.println(user.name +"current cards:"+set);
}
}
public static void main(String[] args) {
Cards c = new Cards();
c.GetCards();
//c.PrintCards();
Users user1 = new Users(1,"Tom");
System.out.println("开始发牌...");
user1.cardList = user1.GetListCards(c);
Collections.shuffle(user1.cardList);
System.out.println("完成洗牌");
Users user2 = new Users(2,"Jack");
user2.cardList = cardtmp;
user1.Get1Cards(user1,c);
user2.Get1Cards(user2,c);
user1.Get1Cards(user1,c);
user2.Get1Cards(user2,c);
ComparatorForUsers cf = new ComparatorForUsers();
int res = cf.compare(user1, user2);
if(res==1){
System.out.println("玩家"+user1.name+ " 获胜!");
}else{
System.out.println("玩家"+user2.name+ " 获胜");
}
System.out.println("游戏结束:将展示玩家手中所有卡牌");
user1.ShowUserCard(user1);
user2.ShowUserCard(user2);
// Users.cardtmp = null;
// c.card = null;
}
}

点击查看更多内容
1人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消