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

好有成就感啊!分享一下我的代码

写了三个类

--------------------------------------Cards类------------------------------------------

package com.imooc;


import java.util.ArrayList;

import java.util.List;

import java.util.Random;


public class Cards{

final String[] Types= {"黑桃","红桃","梅花","方片"};

final String[] Numbers= {"2","3","4","5","6","7","8","9","10","J","Q","K","A"};

private String cardType;

private String cardNumber;

Cards[] cards=new Cards[Types.length*Numbers.length];  //注意!!!这里只是声明自定义类数组,对于每个元素需要分别初始化

List<Cards> shuffleCards=new ArrayList<Cards>();

public void createCards() {

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

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

cards[i]=new Cards();

}

int k=0;

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

for(int j=0;j<Numbers.length;j++) {

cards[k].setCardType(Types[i]);

cards[k].setCardNumber(Numbers[j]);

k++;

}

}

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

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

for(Cards c:cards) {

System.out.print(c.cardType+c.cardNumber+", ");

}

System.out.print("]");

System.out.println();

}

public void shuffle() {

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

Random random=new Random();

int cardIndex;

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

do{

cardIndex=random.nextInt(cards.length);

}while(shuffleCards.contains(cards[cardIndex]));

shuffleCards.add(cards[cardIndex]);

}

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

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

for(Cards c:shuffleCards) {

System.out.print(c.cardType+c.cardNumber+", ");

}

System.out.print("]");

System.out.println();

}

public void deal(Player p1,Player p2) {

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

System.out.println("----玩家:"+p1.getName()+"-拿牌");

Cards ownCards1=shuffleCards.get(0);

p1.ownCards.add(ownCards1);

System.out.println("----玩家:"+p2.getName()+"-拿牌");

Cards ownCards2=shuffleCards.get(1);

p2.ownCards.add(ownCards2);

System.out.println("----玩家:"+p1.getName()+"-拿牌");

Cards ownCards3=shuffleCards.get(2);

p1.ownCards.add(ownCards3);

System.out.println("----玩家:"+p2.getName()+"-拿牌");

Cards ownCards4=shuffleCards.get(3);

p2.ownCards.add(ownCards4);

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

}


public String getCardType() {

return cardType;

}


public void setCardType(String cardType) {

this.cardType = cardType;

}


public String getCardNumber() {

return cardNumber;

}


public void setCardNumber(String cardNumber) {

this.cardNumber = cardNumber;

}


@Override

public int hashCode() {

final int prime = 31;

int result = 1;

result = prime * result + ((cardNumber == null) ? 0 : cardNumber.hashCode());

result = prime * result + ((cardType == null) ? 0 : cardType.hashCode());

return result;

}


@Override

public boolean equals(Object obj) {

if (this == obj)

return true;

if (!(obj instanceof Cards))

return false;

Cards other = (Cards) obj;

if (cardNumber == null) {

if (other.cardNumber != null)

return false;

} else if (!cardNumber.equals(other.cardNumber))

return false;

if (cardType == null) {

if (other.cardType != null)

return false;

} else if (!cardType.equals(other.cardType))

return false;

return true;

}

}

----------------------------------------Player类-----------------------------------------

package com.imooc;


import java.util.HashSet;

import java.util.InputMismatchException;

import java.util.Scanner;

import java.util.Set;


public class Player {

private int id;

private String name;

public Set<Cards> ownCards=new HashSet<Cards>();

public Player[] players=new Player[2];

public void createPlayers() {

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

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

players[i]=new Player();

}

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

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

int ID;

while(true) {

try {

Scanner console=new Scanner(System.in);

ID=console.nextInt();

players[0].setId(ID);

break;

}catch(InputMismatchException e) {

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

}

}

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

Scanner console2=new Scanner(System.in);

String name=console2.nextLine();

players[0].setName(name);

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

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

int ID2;

while(true) {

try {

Scanner console3=new Scanner(System.in);

ID2=console3.nextInt();

players[1].setId(ID2);

break;

}catch(InputMismatchException e) {

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

}

}

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

Scanner console4=new Scanner(System.in);

String name2=console4.nextLine();

players[1].setName(name2);

System.out.println("----欢迎玩家:"+players[0].getName());

System.out.println("----欢迎玩家:"+players[1].getName());

}

public void showOwnCards() {

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

System.out.print(this.getName()+":[");

for(Cards cards:ownCards) {

System.out.print(cards.getCardType()+cards.getCardNumber()+" ");

}

System.out.println("]");

}


public int getId() {

return id;

}


public void setId(int id) {

this.id = id;

}


public String getName() {

return name;

}


public void setName(String name) {

this.name = name;

}



}

---------------------------------------主类-----------------------------------------

package com.imooc;


import java.util.Arrays;

import java.util.List;


public class MainPlayingGame {

public Cards compareTwoCards(Cards c1,Cards c2) {

final String[] Types= {"黑桃","红桃","梅花","方片"};

final String[] Numbers= {"2","3","4","5","6","7","8","9","10","J","Q","K","A"};

List<String> arrayTypes=Arrays.asList(Types);

List<String> arrayNumbers=Arrays.asList(Numbers);

if(arrayNumbers.indexOf(c1.getCardNumber()) < arrayNumbers.indexOf(c2.getCardNumber())) {

return c2;

}else if(arrayNumbers.indexOf(c1.getCardNumber()) > arrayNumbers.indexOf(c2.getCardNumber())) {

return c1;

}else {

if(arrayTypes.indexOf(c1.getCardType()) < arrayTypes.indexOf(c2.getCardType()))

return c2;

else

return c1;

}

}

public void playingGame(Player p1,Player p2) {

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

Cards p1MaxCard=compareTwoCards((Cards)p1.ownCards.toArray()[0],(Cards)p1.ownCards.toArray()[1]);

System.out.println("玩家:"+p1.getName()+"最大的手牌为:"+p1MaxCard.getCardType()+p1MaxCard.getCardNumber());

Cards p2MaxCard=compareTwoCards((Cards)p2.ownCards.toArray()[0],(Cards)p2.ownCards.toArray()[1]);

System.out.println("玩家:"+p2.getName()+"最大的手牌为:"+p2MaxCard.getCardType()+p2MaxCard.getCardNumber());

Cards winnerCards=compareTwoCards(p1MaxCard,p2MaxCard);

if(winnerCards.equals(p1MaxCard))

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

else

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

}


public static void main(String[] args) {

Cards cd=new Cards();

cd.createCards();

cd.shuffle();

Player pl=new Player();

pl.createPlayers();

cd.deal(pl.players[0], pl.players[1]);

MainPlayingGame pg=new MainPlayingGame();

pg.playingGame(pl.players[0], pl.players[1]);

pl.players[0].showOwnCards();

pl.players[1].showOwnCards();

}


}


提问者:qq_Pinocchio_1 2019-10-17 10:25

个回答

  • 慕斯卡7563590
    2020-03-27 11:17:03

    运行错了,对不起。。。

  • 慕斯卡7563590
    2020-03-27 10:40:56

    洗牌结束后边的那个syso怎么没有打印输出呢

  • 幕布斯3136447
    2020-01-06 12:07:23

    嘿嘿,我发现不输入玩家姓名也可以运行,两个玩家姓名都不输入的话就无法辨别谁获胜了。。。

  • qq_慕设计4252141
    2019-11-07 21:38:57

    牛逼啊