扑克牌游戏-交作业
package com.PkCardGame;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Scanner;
import java.util.Set;
public class Init {
List<Card> cardList = new ArrayList<Card>();//扑克牌集合
List<Member> memberList = new ArrayList<Member>();//玩家集合
/**
* 生成一副打乱的扑克牌
*/
public void createCardList() {
for(int i=1;i<=4;i++) {
for(int j=1;j<=13;j++) {
String cardName = "";
switch(i) {
case 1:
cardName = "黑桃";
break;
case 2:
cardName = "红桃";
break;
case 3:
cardName = "梅花";
break;
case 4:
cardName = "方块";
break;
}
switch(j) {
case 1:
cardName = cardName + "A";
break;
case 11:
cardName = cardName + "J";
break;
case 12:
cardName = cardName + "Q";
break;
case 13:
cardName = cardName + "K";
break;
default:
cardName = cardName + j;
}
Card card = new Card(cardName,i,j);
cardList.add(card);
}
}
System.out.println("--------------------扑克牌--------------------");
Collections.shuffle(cardList);
for(Card card : cardList) {
System.out.println(card.cardName+":"+card.color+"-"+card.count);
}
}
/**
* 创建两个玩家
* @param args
* @throws Exception
*/
public void createMember(){
Scanner console = new Scanner(System.in);
for(int i=1;i<=2;i++) {
try {
System.out.println("请输入第"+i+"位玩家编号");
int id = console.nextInt();
System.out.println("请输入玩家姓名");
String name = console.next();
memberList.add(new Member(id,name,new ArrayList()));
}catch(Exception e) {
System.out.println("玩家编号必须为整型");
createMember();
}
}
System.out.println("玩家如下:");
for(Member member : memberList) {
System.out.println(member.id+":"+member.name);
}
}
/**
* 发牌并比较大小
* @param args
*/
public void sendAndCompareCard() {
Member mem1 = memberList.get(0);
Member mem2 = memberList.get(1);
for(int i = 0;i<4;i++) {
Card card = cardList.get(i);
if(i%2 != 0) {
mem1.cards.add(cardList.get(i));
}else{
mem2.cards.add(cardList.get(i));
}
}
System.out.println("------------------发牌完成------------------");
System.out.println("第一位玩家:"+mem1.id+":"+mem1.name+"拿到以下手牌:");
for(Card card : mem1.cards) {
System.out.println(card.cardName);
}
System.out.println("第二位玩家:"+mem2.id+":"+mem2.name+"拿到以下手牌:");
for(Card card : mem2.cards) {
System.out.println(card.cardName);
}
//玩家1
Card mem1MaxCard;
Card mem1Card1 = mem1.cards.get(0);
Card mem1Card2 = mem1.cards.get(1);
if(mem1Card1.count == mem1Card2.count) {
if(mem1Card1.color < mem1Card2.color)
mem1MaxCard = mem1Card1;
else
mem1MaxCard = mem1Card2;
}else {
if(mem1Card1.count > mem1Card2.count)
mem1MaxCard = mem1Card1;
else
mem1MaxCard = mem1Card2;
}
System.out.println("玩家"+mem1.name+"的最大手牌为"+mem1MaxCard.cardName);
//玩家2
Card mem2MaxCard;
Card mem2Card1 = mem2.cards.get(0);
Card mem2Card2 = mem2.cards.get(1);
if(mem2Card1.count == mem2Card2.count) {
if(mem2Card1.color < mem2Card2.color)
mem2MaxCard = mem2Card1;
else
mem2MaxCard = mem2Card2;
}else {
if(mem2Card1.count > mem2Card2.count)
mem2MaxCard = mem2Card1;
else
mem2MaxCard = mem2Card2;
}
System.out.println("玩家"+mem2.name+"的最大手牌为"+mem2MaxCard.cardName);
//比较大小
Member Winner;
if(mem1MaxCard.count == mem2MaxCard.count) {
if(mem1MaxCard.color < mem2MaxCard.color)
Winner = mem1;
else
Winner = mem2;
}else {
if(mem1MaxCard.count > mem2MaxCard.count)
Winner = mem1;
else
Winner = mem2;
}
System.out.println("----------本次的赢家为"+Winner.name+"-------------");
}
public static void main(String[] args){
// TODO Auto-generated method stub
Init it = new Init();
it.createCardList();
it.createMember();
it.sendAndCompareCard();
}
}
package com.PkCardGame;
public class Card {
String cardName;
int color;
int count;
public Card(String cardName,int color,int count) {
this.cardName = cardName;
this.color = color;
this.count = count;
}
}
package com.PkCardGame;
import java.util.ArrayList;
import java.util.List;
public class Member {
int id;
String name;
List<Card> cards = new ArrayList();
public Member(int id,String name,List<Card> cards) {
this.id = id;
this.name = name;
this.cards = cards;
}
}