package PlayingCards;
/**
*
*
* 定义扑克牌类
* @author Administration
*
*/
public class Card {
private int pvalue; //花色大小 ,从大到小分别赋值为int型的4,3,2,1便于比较大小
private int nvalue; //数字大小,从大到小分别赋值为int型的13~1,便于比较大小
private String picture; //扑克牌花色
private String number; //扑克牌数字
//有参构造函数
public Card(int pvalue,int nvalue,String picture,String number){
setPvalue(pvalue);
setNvalue(nvalue);
setPicture(picture);
setNumber(number);
}
//下为set get方法
public String getPicture() {
return picture;
}
public void setPicture(String picture) {
this.picture = picture;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public int getPvalue() {
return pvalue;
}
public void setPvalue(int pvalue) {
this.pvalue = pvalue;
}
public int getNvalue() {
return nvalue;
}
public void setNvalue(int nvalue) {
this.nvalue = nvalue;
}
}
----------
package PlayingCards;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
/**
*
*
* 定义玩家类
* @author Administration
*
*/
public class Player {
private String ID; //玩家ID
private String Name; //玩家姓名
public List<Card> hcards; //玩家手牌
//创建有参构造函数
public Player(String ID,String Name){
setID(ID);
setName(Name);
hcards = new ArrayList<Card>();
}
//set get方法
public String getID() {
return ID;
}
public void setID(String iD) {
ID = iD;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
}
----------
package PlayingCards;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
/**
*
* 该类包含生成扑克牌,洗牌,创建玩家,玩家获取手牌,比较手牌大小,展示玩家手牌等方法
* @author Administration
*
*/
public class Playing {
//定义一个一副扑克牌的List
List<Card> cards = new ArrayList<Card>();
//定义玩家List
List<Player> players = new ArrayList<Player>();
/**
*
* 生成一副扑克牌
*
*/
public void addCards(){
System.out.println("---------------创建扑克牌。。。--------------");
String num = "";
for(int i=0;i<13;i++){
num = "";
if(i==0) num ="A";
else if(i==10) num ="J";
else if(i==11) num ="Q";
else if(i==12) num ="K";
else num +=(i+1);
cards.add(new Card(4,i,"黑桃",num));
cards.add(new Card(3,i,"红桃",num));
cards.add(new Card(2,i,"梅花",num));
cards.add(new Card(1,i,"方片",num));
}
System.out.println("---------------扑克牌创建成功!-----------------");
System.out.print("为:");
}
public void printCards(){
System.out.print("[ ");
for(Card cd:cards){
System.out.print(cd.getPicture()+cd.getNumber()+" ");
}
System.out.print("]");
System.out.println();
}
/**
*
* 洗牌
*
*/
public void randomCards(){
System.out.println("---------------开始洗牌。。。----------------");
List<Card> newCards = new ArrayList<Card>();
for(int i=0;i<cards.size();i++){
//随机生成插牌位置
Random r = new Random();
int n = r.nextInt(i+1);
newCards.add(n,cards.get(i));
}
cards = newCards;
System.out.println("---------------洗牌结束!--------------------");
}
/**
*
* 创建玩家
*
*/
public void CreatePlayers(){
System.out.println("---------------创建玩家:--------------------");
Scanner sc = new Scanner(System.in);
for(int i=0;i<2;i++){
System.out.println("请输入第"+(i+1)+"玩家的ID和姓名:");
System.out.println("输入ID:");
String ID = sc.next();
System.out.println("输入姓名:");
String Name = sc.next();
players.add(new Player(ID,Name));
}
System.out.println("-----欢迎玩家:"+players.get(0).getName());
System.out.println("-----欢迎玩家:"+players.get(1).getName());
}
/**
*
* 玩家获取扑克牌
*
*/
public void gethandCards(){
System.out.println("---------------开始发牌。。。-----------------");
for(int i=0;i<4;i++){
if(i%2==0){
System.out.println("玩家:"+players.get(0).getName()+"-拿牌");
Card oneCard = cards.get(i);
players.get(0).hcards.add(oneCard);
}
else{
System.out.println("玩家:"+players.get(1).getName()+"-拿牌");
Card oneCard = cards.get(i);
players.get(1).hcards.add(oneCard);
}
}
System.out.println("----------------发牌结束!-------------------");
}
/**
*
* 调用CompareMethod方法比较手牌大小
*
*/
public void CompareCards(){
List<Card> cd = new ArrayList<Card>();
Card p1Max = CompareMethod(players.get(0).hcards);
System.out.println("玩家"+players.get(0).getName()+"的最大手牌为:"+p1Max.getPicture()+p1Max.getNumber());
Card p2Max = CompareMethod(players.get(1).hcards);
System.out.println("玩家"+players.get(1).getName()+"的最大手牌为:"+p2Max.getPicture()+p2Max.getNumber());
cd.add(p1Max);
cd.add(p2Max);
Card Max = CompareMethod(cd);
System.out.println();
if(Max.equals(p1Max)) System.out.println("-----玩家"+players.get(0).getName()+"获胜!");
else System.out.println("-----玩家"+players.get(1).getName()+"获胜!");
System.out.println();
}
/**
*
* 手牌大小比较方法
*
*/
public Card CompareMethod(List<Card> cards){
//记录值最大的牌
Card maxCard = cards.get(0);
for(int i=1;i<cards.size();i++){
//比较数值大小
if(maxCard.getNvalue()<cards.get(i).getNvalue()){
maxCard = cards.get(i);
}
//若数值相等,比较花色大小
else if(maxCard.getNvalue()==cards.get(i).getNvalue()){
if(maxCard.getPvalue()<cards.get(i).getPvalue()){
maxCard = cards.get(i);
}
}
}
return maxCard;
}
/**
*
* 展示玩家手牌
*
*/
public void displayCards(){
System.out.println("玩家各自的手牌为:");
for(int i=0;i<2;i++){
System.out.print(players.get(i).getName()+":[ ");
for(Card cd : players.get(i).hcards){
System.out.print(cd.getPicture()+cd.getNumber()+" ");
}
System.out.print("]");
System.out.println();
}
}
}
----------
package PlayingCards;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/**
*
*
* 主函数,调用Playing中的方法完成功能
* @author Administration
*
*/
public class testMain {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Playing play = new Playing();
play.addCards(); //生成扑克牌
play.printCards(); //输出扑克牌
play.randomCards(); //打乱扑克牌
//play.printCards(); //输出打乱的扑克牌
play.CreatePlayers(); //添加玩家
play.gethandCards(); //玩家获得手牌
play.CompareCards(); //比较玩家手牌
play.displayCards(); //展示玩家手牌
}
}
``
点击查看更多内容
38人点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦