package firstSaample;
import java.util.ArrayList;
public class PlayingCards implements Comparable <PlayingCards>{
String cardName,cardValue;
ArrayList<PlayingCards> cardList=new ArrayList<PlayingCards>();
public PlayingCards()
{
}
public PlayingCards(String cardName ,String cardValue)
{
this.cardName=cardName;
this.cardValue=cardValue;
}
public ArrayList< PlayingCards> getCard()
{
int mark=0;
String s="";
while(mark!=4)
{
switch(mark)
{
case 0: s="Spade";break;
case 1: s="Heart";break;
case 2: s="Diamand";break;
case 3: s="Club";break;
}
for(int i=2;i<15;i++)
{
if(i<11)
cardList.add(new PlayingCards(s,""+i));
else
{
switch(i)
{
case 11:cardList.add(new PlayingCards(s,"J"));break;
case 12:cardList.add(new PlayingCards(s,"Q"));break;
case 13:cardList.add(new PlayingCards(s,"K"));break;
case 14:cardList.add(new PlayingCards(s,"A"));break;
}
}
}
mark++;
}
return cardList;
}
@Override
public int compareTo(PlayingCards o) {
// TODO Auto-generated method stub
if(0==(this.cardName.compareTo(o.cardName)))
return this.cardValue.compareTo(o.cardValue);
else
return this.cardName.compareTo(o.cardName);
}
}
package firstSaample;
import java.util.ArrayList;
import java.util.Random;
public class Player <E>{
private String playerName;
private int playerID;
public void setName(String name)
{
this.playerName=name;
}
public void setId(int ID)
{
this.playerID=ID;
}
public ArrayList<E> getCard(ArrayList<E> playerCard)
{
int i,j;
ArrayList<E> getCard=new ArrayList<E>(5);
Random random=new Random();
i=random.nextInt(51);
j=random.nextInt(51);
while(i==j)
j=random.nextInt(51);
getCard.add(playerCard.get(i));
getCard.add(playerCard.get(j));
return getCard;
}
public void printInfor()
{
System.out.print(" "+"玩家"+playerID+playerName+"获得手牌为:");
}
}
package firstSaample;
import java.util.ArrayList;
import java.util.Collections;
import java.util.InputMismatchException;
import java.util.Scanner;
public class demo {
public static void main (String[]args)
{
ArrayList<PlayingCards>lin=new ArrayList<PlayingCards>();
ArrayList<PlayingCards>temp=new ArrayList<PlayingCards>();
ArrayList<PlayingCards>temp1=new ArrayList<PlayingCards>();
PlayingCards card=new PlayingCards();
Player<PlayingCards> player1=new Player<PlayingCards>();
Player <PlayingCards>player2=new Player<PlayingCards>();
Scanner scanner=new Scanner(System.in);
while(true)
{
try
{
System.out.println("请输入玩家1ID");
player1.setId(scanner.nextInt());
System.out.println("请输入玩家1姓名");
player1.setName(scanner.next());
System.out.println("请输入玩家2ID");
player2.setId(scanner.nextInt());
System.out.println("请输入玩家2姓名");
player2.setName(scanner.next());
break;
}catch(InputMismatchException e){
System.out.println("请输入正确的玩家ID");
scanner.next(); //吸收缓冲区
//e.printStackTrace();
}
}
scanner.close();
lin=card.getCard();
for(int i=0;i<lin.size();i++)
System.out.println(lin.get(i).cardName+lin.get(i).cardValue+",");
temp=player1.getCard(lin);
lin.removeAll(temp);
temp1=player2.getCard(lin);
player1.printInfor();
for(int i=0;i<temp.size();i++)
System.out.print(temp.get(i).cardName+temp.get(i).cardValue+" ");
player2.printInfor();
for(int i=0;i<temp1.size();i++)
System.out.print(temp1.get(i).cardName+temp1.get(i).cardValue+" ");
lin.clear();
lin.addAll(temp);
lin.addAll(temp1);
Collections.sort(lin);
if(temp.contains(lin.get(0)))
System.out.print("获胜者为玩家一他的获胜手牌为"+lin.get(0).cardName+lin.get(0).cardValue+" ");
else
System.out.print("获胜者为玩家二他的获胜手牌为"+lin.get(0).cardName+lin.get(0).cardValue+" ");
}
}
共同学习,写下你的评论
评论加载中...
作者其他优质文章