java三张扑克牌,java第三季综合练习之扑克牌游戏

初学Java,看完入门前三季,终于把最后的综合练习实现功能了!有待后续继续优化。代码顺序是先分类,最后部分是测试类。

import java.util.ArrayList;

import java.util.Arrays;

import java.util.Collections;

import java.util.List;

/**

* 扑克牌类

*

* @author Dell

*

*/

public class Card {

private List cardToSelect;//用于存放扑克牌的List

String[] cardArray={"黑桃2","黑桃3","黑桃4","黑桃5","黑桃6","黑桃7","黑桃8","黑桃9","黑桃10","黑桃J","黑桃Q",

"黑桃K","黑桃A","红桃2","红桃3","红桃4","红桃5","红桃6","红桃7","红桃8","红桃9","红桃10","红桃J","红桃Q",

"红桃K","红桃A","梅花2","梅花3","梅花4","梅花5","梅花6","梅花7","梅花8","梅花9","梅花10","梅花J","梅花Q",

"梅花K","梅花A","方片2","方片3","方片4","方片5","方片6","方片7","方片8","方片9","方片10","方片J","方片Q",

"方片K","方片A"};

/**

* 该方法用于初始化扑克牌类

*/

public Card(){

System.out.println("------------------创建扑克牌--------------------");

cardToSelect = new ArrayList();

cardToSelect.addAll(Arrays.asList(cardArray));

System.out.println("------------------扑克牌创建成功!----------------");

}

/**

* 该方法用于遍历输出所有扑克牌

*/

public void outputCard(){

System.out.print("为:");

for(String card:cardToSelect){

System.out.print(card+" ");

}

System.out.println();

}

/*

* 该方法用于打乱扑克牌

*/

public void messCard(){

System.out.println("------------------开始洗牌----------------------");

Collections.shuffle(cardToSelect);

System.out.println("------------------洗牌结束!---------------------");

}

/*

* 该方法用于输出指定位置的扑克牌

*/

public String outCard(int i){

return cardToSelect.get(i);

}

}

import java.util.ArrayList;

import java.util.List;

/**

* 玩家类

* 属性:编号、姓名和存放自己手牌的List

* 对私有属性进行访问的共有方法

* 对自己手牌进行排序的方法

* 添加手牌的方法

* 输出相应手牌的方法

* @author Dell

*

*/

public class Player{

private String id;

private String name;

private List cardList;

/*

* 该方法用于初始化玩家

*/

public Player(String id,String name){

this.id=id;

this.name=name;

cardList=new ArrayList();

}

/*

* 对私有属性进行读写操作的一系列共有方法

*/

public void setid(String id){

this.id=id;

}

public void setname(String name){

this.name=name;

}

public String getid(){

return this.id;

}

public String getname(){

return this.name;

}

/*

* 该方法用于向玩家手牌集合中添加扑克牌

*/

public void putCard(String card){

cardList.add(card);

}

/*

* 该方法用于对玩家自己的手牌进行排序

* 返回最大牌在手牌List中的索引位置

* 比较规则是:先比较点数,按照2-10,J-A的递增顺序排列大小,若点数相同,则按照黑红梅方递增顺序进行排序

* 每个玩家手中最多有两张牌

*/

public int sortMyCard(){

int indexCard=0;//最大手牌的索引位置

//得到玩家手中的两张牌

String c1=cardList.get(0);

String c2=cardList.get(1);

//将牌面拆分成花色和点数两部分

String number1;

String number2;

String color1=c1.substring(0,1);

String color2=c2.substring(0,1);

if(c1.length()==3)

number1=c1.substring(2);

else

number1=c1.substring(2,3);

if(c2.length()==3)

number2=c2.substring(2);

else

number2=c2.substring(2,3);

if(number1.equals(number2)){

int i1=colorNumber(color1);

int i2=colorNumber(color2);

if(i1

indexCard=1;

}else{

int i1=numNumber(number1);

int i2=numNumber(number2);

if(i1

indexCard=1;

}

return indexCard;

}

/*

* 该方法用于按照花色比较,返回响应花色对应的数字;

*/

public int colorNumber(String color){

int i=0;

if(color.equals("黑桃")){

i=1;

}

if(color.equals("红桃")){

i=2;

}if(color.equals("梅花")){

i=3;

}if(color.equals("方片")){

i=4;

}

return i;

}

/*

* 该方法用于比较点数,返回相应点数对应的数字

*/

public int numNumber(String number){

int i=0;

if(number.equals("2")){

i=1;

}

if(number.equals("3")){

i=2;

}

if(number.equals("4")){

i=3;

}

if(number.equals("5")){

i=4;

}

if(number.equals("6")){

i=5;

}

if(number.equals("7")){

i=6;

}if(number.equals("8")){

i=7;

}

if(number.equals("9")){

i=8;

}

if(number.equals("10")){

i=9;

}

if(number.equals("J")){

i=10;

}

if(number.equals("Q")){

i=11;

}

if(number.equals("K")){

i=12;

}

if(number.equals("A")){

i=13;

}

return i;

}

/*

* 取得玩家手牌集合中相应位置上的扑克牌

*/

public String getMyCard(int i){

return cardList.get(i);

}

}

import java.util.ArrayList;

import java.util.List;

/*

* 玩家集合类

* 存放所有玩家

* 该游戏只有两个玩家

*/

public class PlayerMap {

private List players;//用于存放玩家的集合

/*

* 初始化玩家集合

*/

public PlayerMap(){

players=new ArrayList();

}

/*

* 添加玩家

*/

public void addPlayers(int i,Player play){

players.add(i, play);

}

/*

* 遍历输出玩家姓名

*/

public void getPlayers(){

for(Player py:players){

System.out.println("欢迎玩家:"+py.getname());

}

}

/*

* 输出相应索引位置上的玩家姓名

*/

public String getPlayersName(int i){

return players.get(i).getname();

}

/*

* 输出相应位置上的玩家类型

*/

public Player getPlayers(int i){

return players.get(i);

}

}

import java.util.ArrayList;

import java.util.List;

/**

* 比较两张最大手牌的大小,并返回最大手牌的索引位置

* @author Dell

*

*/

public class compareTwoCard {

private List twoCard;

public compareTwoCard(){

twoCard=new ArrayList();

}

public void addPlayers(int i,String play){

twoCard.add(i, play);

}

public int sortMyCard(){

int indexCard=0;

String c1=twoCard.get(0);

String c2=twoCard.get(1);

String number1;

String number2;

String color1=c1.substring(0,1);

String color2=c2.substring(0,1);

if(c1.length()==3)

number1=c1.substring(2);

else

number1=c1.substring(2,3);

if(c2.length()==3)

number2=c2.substring(2);

else

number2=c2.substring(2,3);

if(number1.equals(number2)){

int i1=colorNumber(color1);

int i2=colorNumber(color2);

if(i1

indexCard=1;

}else{

int i1=numNumber(number1);

int i2=numNumber(number2);

if(i1

indexCard=1;

}

return indexCard;

}

public int colorNumber(String color){

int i=0;

if(color.equals("黑桃")){

i=1;

}

if(color.equals("红桃")){

i=2;

}if(color.equals("梅花")){

i=3;

}if(color.equals("方片")){

i=4;

}

return i;

}

public int numNumber(String number){

int i=0;

if(number.equals("2")){

i=1;

}

if(number.equals("3")){

i=2;

}

if(number.equals("4")){

i=3;

}

if(number.equals("5")){

i=4;

}

if(number.equals("6")){

i=5;

}

if(number.equals("7")){

i=6;

}if(number.equals("8")){

i=7;

}

if(number.equals("9")){

i=8;

}

if(number.equals("10")){

i=9;

}

if(number.equals("J")){

i=10;

}

if(number.equals("Q")){

i=11;

}

if(number.equals("K")){

i=12;

}

if(number.equals("A")){

i=13;

}

return i;

}

}

import java.util.Scanner;

/**

* 测试游戏类

* 创建一副扑克牌、创建两个玩家,洗牌、按照顺序发牌(每个玩家两张扑克牌)、比较两个玩家手中的最大牌,牌数最大的玩家获胜

* @author Dell

*

*/

public class PlayTest {

public static void main(String[] args) {

Scanner input=new Scanner(System.in);

compareTwoCard comTwo=new compareTwoCard();

Card myCard=new Card();

PlayerMap playersList=new PlayerMap();

myCard.outputCard();

myCard.messCard();

System.out.println("-----------创建玩家-----------");

for(int n=0;n<2;n++){

System.out.println("请输入第"+(n+1)+"位玩家的ID和姓名:");

System.out.println("输入ID:");

String id=input.next();

//player.setid(id);

System.out.println("输入姓名:");

String name=input.next();

//player.setname(name);

Player player=new Player(id,name);

playersList.addPlayers(n, player);

}

playersList.getPlayers();

System.out.println("-------------开始发牌····-------------");

for(int i=0;i<2;i++){

for(int j=0;j<2;j++){

System.out.println("玩家:"+playersList.getPlayersName(j)+"-"+"拿牌");

if(i==0)

playersList.getPlayers(j).putCard(myCard.outCard(i+j));

else

playersList.getPlayers(j).putCard(myCard.outCard(i+j+1));

}

}

System.out.println("--------------发牌结束!---------------");

System.out.println("--------------开始游戏····---------------");

int indexCard1=playersList.getPlayers(0).sortMyCard();

int indexCard2=playersList.getPlayers(1).sortMyCard();

System.out.println("玩家:"+playersList.getPlayers(0).getname()+"最大的手牌为:"+playersList.getPlayers(0).getMyCard(indexCard1));

System.out.println("玩家:"+playersList.getPlayers(1).getname()+"最大的手牌为:"+playersList.getPlayers(1).getMyCard(indexCard2));

comTwo.addPlayers(0, playersList.getPlayers(0).getMyCard(indexCard1));

comTwo.addPlayers(1, playersList.getPlayers(1).getMyCard(indexCard2));

int indexT=comTwo.sortMyCard();

System.out.println("------玩家:"+playersList.getPlayers(indexT).getname()+"获胜");

System.out.println("玩家个字的手牌为:");

System.out.println(playersList.getPlayers(0).getname()+":"+playersList.getPlayers(0).getMyCard(0)+" "+playersList.getPlayers(0).getMyCard(1));

System.out.println(playersList.getPlayers(1).getname()+":"+playersList.getPlayers(1).getMyCard(0)+" "+playersList.getPlayers(1).getMyCard(1));

}

}

你可能感兴趣的:(java三张扑克牌)