问答详情
源自:7-1 简易扑克牌游戏

这是我写好的代码,希望大家共同进步,哪里有不足的希望大家指点

以下是我写的代码,希望和大家共同进步(可以创建多名玩家和给每位玩家发多张牌)

*******************Player.java****************

package bean;

import java.util.ArrayList;

import java.util.List;

public class Player {

private int playerId;

private String playerName;

private List<Poker>playerPokers;

public Player(int playerId,String playerName){

this.playerId=playerId;

this.playerName=playerName;

this.playerPokers=new ArrayList<Poker>();

}

public int getPlayerId() {

return playerId;

}

public void setPlayerId(int playerId) {

this.playerId = playerId;

}

public String getPlayerName() {

return playerName;

}

public void setPlayerName(String playerName) {

this.playerName = playerName;

}

public List<Poker> getPlayerPokers() {

return playerPokers;

}

public void setPlayerPokers(List<Poker> playerPokers) {

this.playerPokers = playerPokers;

}

}

*******************Poker.java****************

package bean;


public class Poker {

private String pokerId;

private String pokerClass;

public Poker(String pokerid,String pokerClass){

this.pokerId=pokerid;

this.pokerClass=pokerClass;

}

public String getPokerId() {

return pokerId;

}

public void setPokerId(String pokerId) {

this.pokerId = pokerId;

}

public String getPokerClass() {

return pokerClass;

}

public void setPokerClass(String pokerClass) {

this.pokerClass = pokerClass;

}

}

*******************PokerGame.java****************

package function;


import java.util.ArrayList;

import java.util.Arrays;

import java.util.Collections;

import java.util.HashSet;

import java.util.InputMismatchException;

import java.util.List;

import java.util.Random;

import java.util.Scanner;

import java.util.Set;


import bean.Player;

import bean.Poker;


public class PokerGame {

private List<Poker> pokersList;

private List<Player> playersList;

private List<Poker> eachOtherCompPokers;

private Set<Poker> pokersSet;


// private Scanner input;

public PokerGame() {

this.pokersList = new ArrayList<Poker>();

this.playersList = new ArrayList<Player>();

this.eachOtherCompPokers=new ArrayList<Poker>();

this.pokersSet = new HashSet<Poker>();

// this.input=new Scanner(System.in);

}


/*

* 1.创建扑克牌

*/

public void createPoker() {

List<String> pokerClassStrings = new ArrayList<String>();

String[] pokerClass = { "黑桃", "红心", "梅花", "方块" };

pokerClassStrings.addAll(Arrays.asList(pokerClass));


List<String> pokerIdStrings = new ArrayList<String>();

String[] pokerId = { "2", "3", "4", "5", "6", "7", "8", "9", "10", "J",

"Q", "K", "A" };

pokerIdStrings.addAll(Arrays.asList(pokerId));


// pokersList=new ArrayList<Poker>();

for (int i = 3; i >= 0; i--) {

String Class = pokerClassStrings.get(i);

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

String Id = pokerIdStrings.get(j);

Poker poker = new Poker(Id, Class);

pokersList.add(poker);

}

}

}


/*

* 2.展示扑克牌

*/

public String displayPoker() {

String[] string = new String[52];

for (int i = 0; i < string.length; i++) {

String pokerAll = new String(pokersList.get(i).getPokerClass()

+ pokersList.get(i).getPokerId());

string[i] = pokerAll;

}

return Arrays.toString(string);

}


/*

* 3.创建两名玩家

*/

public void createPlayer() {

for (int i = 0; i < 3; i++) {  //这里可以设置创建的人数

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

while (true) {

try {

Scanner input = new Scanner(System.in);

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

int ID = input.nextInt();

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

String name = input.next();

Player player = new Player(ID, name);

playersList.add(player);

break;

} catch (InputMismatchException e) {

// TODO Auto-generated catch block

System.out.println("请输入整数类型的ID!");

}

}

}

for (Player player : playersList) {

System.out.println("-----欢迎玩家:" + player.getPlayerName());

}


}


/*

* 4.洗牌

*/

// public String massPoker(){

// //pokersSet=new HashSet<Poker>();

// //System.out.println(pokersList.size());

// for (Poker pokerBefore : pokersList) {

// pokersSet.add(pokerBefore);

// }

// for (Poker test : pokersSet) {

// String pokerAll=new String(test.getPokerClass()+test.getPokerId());

// System.out.println("扑克为:"+pokerAll);

// }

// int i=0;

// String[]string=new String[52];

// //System.out.println(pokersSet.size());

// for (Poker pokerAfter : pokersSet) {

// String pokerAll=new

// String(pokerAfter.getPokerClass()+pokerAfter.getPokerId());

// string[i]=pokerAll;

// i++;

// }

// return Arrays.toString(string);

//

// }


public String massPoker() {

Collections.shuffle(pokersList);//Collections的Shuffle方法可以打乱List列表中的顺序

String[] string = new String[52];

for (int i = 0; i < string.length; i++) {

String pokerAll = new String(pokersList.get(i).getPokerClass()

+ pokersList.get(i).getPokerId());

string[i] = pokerAll;

}

return Arrays.toString(string);

}


/*

* 给玩家发牌

*/

// public void deliverPoker() {

// Random random = new Random();

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

// int a = random.nextInt(pokersList.size());

// Poker poker = pokersList.get(a);

// playersList.get(0).getPlayerPokers().add(poker);

// pokersList.remove(a);

// System.out.println("----玩家:" + playersList.get(0).getPlayerName()

// + "-拿牌");

// int b = random.nextInt(pokersList.size());

// Poker pokertwo = pokersList.get(b);

// playersList.get(1).getPlayerPokers().add(pokertwo);

// pokersList.remove(b);

// System.out.println("----玩家:" + playersList.get(1).getPlayerName()

// + "-拿牌");

// }

// }

public void deliverPoker() {

Random random = new Random();

for (int i = 0; i < 3; i++) {//这里可以设置给每个人发牌的数量

for (int j = 0; j < playersList.size(); j++) {

int randomNumber=random.nextInt(pokersList.size());

Poker poker=pokersList.get(randomNumber);

playersList.get(j).getPlayerPokers().add(poker);

pokersList.remove(randomNumber);

System.out.println("----玩家:" + playersList.get(j).getPlayerName()

+ "-拿牌");

}

}

// for (Player player : playersList) {

// System.out.println("玩家"+player.getPlayerName()+"的牌有:");

// for (Poker poker :player.getPlayerPokers() ) {

// System.out.println("卡牌:"+poker.getPokerClass()+poker.getPokerId());

// }

// }

}

/*

 * 展示玩家手牌

 */

// public void displayPlarersPoker() {

// System.out.println("玩家各自的手牌为:");

// for (int i = 0; i < playersList.size(); i++) {

// String[]strings=new String[playersList.get(0).getPlayerPokers().size()];

// for(int j=0;j<playersList.get(0).getPlayerPokers().size();j++){

// String userPoker = playersList.get(i).getPlayerPokers().get(j)

// .getPokerClass()

// + playersList.get(i).getPlayerPokers().get(j).getPokerId();

// strings[j]=userPoker;

// }

// System.out.println(playersList.get(i).getPlayerName() + ":"

// + Arrays.toString(strings));

// }

// }

public void displayPlarersPoker() {

System.out.println("玩家各自的手牌为:");

for (Player player : playersList) {

String[]strings=new String[player.getPlayerPokers().size()];

for (Poker poker :player.getPlayerPokers() ) {

String userPoker=poker.getPokerClass()+poker.getPokerId();

int index =player.getPlayerPokers().indexOf(poker);

strings[index]=userPoker;

}

System.out.println(player.getPlayerName() + ":"

+ Arrays.toString(strings));

}

}

/*

* 判断自己手牌中的最大扑克是那一张

*/

public void maxSelfPoker(){

int length=playersList.get(0).getPlayerPokers().size();

for (Player player : playersList) {

// System.out.println("玩家:"+player.getPlayerName()+"最大的手牌为:"+player.getPlayerPokers().get(length-1).getPokerClass()+

// player.getPlayerPokers().get(0).getPokerId());

Collections.sort(player.getPlayerPokers(), new PokerComparator());

System.out.println("玩家:"+player.getPlayerName()+"最大的手牌为:"+player.getPlayerPokers().get(length-1).getPokerClass()+

player.getPlayerPokers().get(length-1).getPokerId());

}

}


/*

* 两者最大的排相互比较

*/

public void eachOtherComparePoker() {

int length = playersList.get(0).getPlayerPokers().size();// 的出每个玩家的扑克列表属性中有多少张牌

//List<Poker> eachOtherCompPokers = new ArrayList<Poker>();

for (Player player : playersList) {

eachOtherCompPokers.add(player.getPlayerPokers().get(length - 1));

}

// for (Poker pokertest : eachOtherCompPokers) {

// System.out.println("该列表元素有:" + pokertest.getPokerClass()

// + pokertest.getPokerId());

// }

Collections.sort(eachOtherCompPokers, new PokerComparator());

// for (Poker pokertest : eachOtherCompPokers) {

// System.out.println("该列表元素有:" + pokertest.getPokerClass()

// + pokertest.getPokerId());

// }

int playerNumber=playersList.size();

System.out.println("结果:经过比较最大的牌为:"+eachOtherCompPokers.get(playerNumber-1).getPokerClass()

+eachOtherCompPokers.get(playerNumber-1).getPokerId());

}

/*

* 通过最大的牌是谁的来判断赢家

*/

public void judgeWinner(){

int playerNumber=playersList.size();

for (Player player : playersList) {

if(player.getPlayerPokers().contains(eachOtherCompPokers.get(playerNumber-1))){

System.out.println("-------------------玩家:"+player.getPlayerName()+"获胜!-------------");

}

}

}

public static void main(String[] args) {

// TODO Auto-generated method stub

PokerGame pGame = new PokerGame();

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

pGame.createPoker();

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

System.out.println("为:" + pGame.displayPoker());

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

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

System.out.println("为:" + pGame.massPoker());

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

pGame.createPlayer();

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

pGame.deliverPoker();

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

pGame.displayPlarersPoker();

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

pGame.maxSelfPoker();

pGame.eachOtherComparePoker();

pGame.judgeWinner();


}

}

*******************PokerComparator.java****************

package function;


import java.util.Comparator;


import bean.Poker;


public class PokerComparator implements Comparator<Poker> {


@Override

public int compare(Poker o1, Poker o2) {

// TODO Auto-generated method stub

String pokerClass="方块,梅花,红心,黑桃";

String PokerID="3,4,5,6,7,8,9,10,J,Q,K,A,2";

int userOneClassIndex=pokerClass.indexOf(o1.getPokerClass());

int userOneIDIndex=PokerID.indexOf(o1.getPokerId());

int userTwoClassIndex=pokerClass.indexOf(o2.getPokerClass());

int userTwoIDIndex=PokerID.indexOf(o2.getPokerId());

if(userOneIDIndex==userTwoIDIndex){

if (userOneClassIndex>userTwoClassIndex) 

return 1;

else

return -1;

}else if (userOneIDIndex>userTwoIDIndex) 

return 1;

else 

return -1;

}


}

都是学过的知识,反复的使用 ,一起加油。

提问者:慕田峪6247734 2018-08-01 11:46

个回答

  • qq_木_31
    2018-08-03 10:32:49
    已采纳

    public void createPlayer() {

    for (int i = 0; i < 3; i++) { // 这里可以设置创建的人数

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

    while (true) {

    try {

    boolean flag = true;

    while (flag) {

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

    input = new Scanner(System.in);

    ID = input.nextInt();

    if (playersList.size() == 0) {

    flag = false;

    break;

    }

    for (int i1 = 0; i1 < playersList.size(); i1++) {

    if (ID == playersList.get(i1).getPlayerId()) {

    System.out.println("ID重复,请重新输入");

    flag = true;

    break;

    } else

    flag = false;

    }

    }

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

    String name = input.next();

    playersList.add(new Player(ID, name));

    break;

    } catch (InputMismatchException e) {

    System.out.println("请输入整数类型的ID!");

    }

    }

    }

    for (Player player1 : playersList) {

    System.out.println("-----欢迎玩家:" + player1.getPlayerName());

    }


    }


  • 神影天初
    2018-08-12 22:21:14

    你注释里写想创建两名玩家,但是你i<3,这就是三名玩家了。

  • qq_木_31
    2018-08-02 17:49:26

    玩家ID相同情况,没有处理