qq_星雨_0
2015-06-12 17:04
public class Card implements Comparable<Card>{
private String cardColor;
private String cardNum;
public Card(String color,String num){
this.setCardColor(color);
this.setCardNum(num);
}
public String getCardColor() {
return cardColor;
}
public void setCardColor(String cardColor) {
this.cardColor = cardColor;
}
public String getCardNum() {
return cardNum;
}
public void setCardNum(String cardNum) {
this.cardNum = cardNum;
}
@Override
public int compareTo(Card o) {
// TODO Auto-generated method stub
String colorstr = "方块,梅花,红桃,黑桃";
String numstr = "2,3,4,5,6,7,8,9,10,J,Q,K,A";
int k;
k = numstr.indexOf(this.getCardNum())-numstr.indexOf(o.getCardNum());
if(k==0){
k= colorstr.indexOf(this.getCardColor())-colorstr.indexOf(o.getCardColor());
}
return k;
}
}
===========================================
import java.util.ArrayList;
import java.util.List;
public class Porker {
private List<Card> cardList;
public List<Card> getCardList() {
return cardList;
}
public Porker(){
cardList = new ArrayList<Card>();
String[] colorlist = {"黑桃","红桃","梅花","方块"};
String[] numlist = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
for(int i=0;i<colorlist.length;i++){
for(int j=0;j<numlist.length;j++){
cardList.add(new Card(colorlist[i],numlist[j]));
}
}
}
}
==================================================
import java.util.ArrayList;
import java.util.List;
public class Person {
private int id;
private String name;
private List<Card> handcards;
public Person(int id,String name){
this.setId(id);
this.setName(name);
handcards = new ArrayList<Card>();
}
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;
}
public List<Card> getHandcards() {
return handcards;
}
}
=====================================
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class TestPaly {
public Porker porker;
public TestPaly(){
porker = new Porker();
}
public void showPorker(List<Card> cards){
System.out.print("扑克牌包含:[");
for(int i=0;i<cards.size();i++){
Card card = cards.get(i);
if(i==(cards.size()-1)){
System.out.print(card.getCardColor()+card.getCardNum());
}else{
System.out.print(card.getCardColor()+card.getCardNum()+",");
}
}
System.out.println("]");
}
public static void main(String[] args){
System.out.println("开始玩扑克牌了,创建一付扑克牌");
TestPaly tp = new TestPaly();
System.out.println("扑克牌创建成功!");
List<Card> cards = tp.porker.getCardList();
tp.showPorker(cards);
System.out.println("开始洗牌");
Collections.shuffle(cards);
System.out.println("洗牌结束");
System.out.println("创建2名玩家");
Scanner scanner = new Scanner(System.in);
List<Person> plist = new ArrayList<Person>();
int i=0;
while(i<2){
try{
System.out.println("请输入用户ID:");
int id = scanner.nextInt();
System.out.println("请输入用户名:");
String name = scanner.next();
plist.add(new Person(id,name));
i++;
}catch(Exception e){
System.out.println("输入有误,请重新输入");
scanner.next();
}
}
System.out.println("开始发牌");
for(int j=0;j<2;j++){
for(Person p:plist){
Card card = cards.get(0);
p.getHandcards().add(card);
cards.remove(0);
}
}
System.out.println("发牌结束");
System.out.println("开始比较大小:");
Collections.sort(plist.get(0).getHandcards());
Collections.sort(plist.get(1).getHandcards());
List<Card> comparelist = new ArrayList<Card>();
for(Person p:plist){
Card card = p.getHandcards().get(p.getHandcards().size()-1);
comparelist.add(card);
System.out.println("玩家:"+p.getId()+":"+p.getName()+"最大的牌:["+card.getCardColor()+card.getCardNum()+"]");
}
Collections.sort(comparelist);
Card max = comparelist.get(comparelist.size()-1);
for(Person p:plist){
if(p.getHandcards().contains(max)){
System.out.println("获胜的是:"+p.getId()+":"+p.getName());
}
}
System.out.println("玩家的牌为:");
for(Person p:plist){
System.out.print("玩家:"+p.getId()+":"+p.getName());
tp.showPorker(p.getHandcards());
}
}
}
card的compareTo()方法貌似有误,只是阐述了数值相同的情况
赞一个
Java入门第三季
409792 学习 · 4340 问题
相似问题
回答 3
回答 3