为了账号安全,请及时绑定邮箱和手机立即绑定

不知道思路对不对,感觉代码有点多

Card类:定义扑克属性

Player类:定义玩家属性

CompareCard类:定义扑克牌比较规则,定义compare方法

CardGame类:程序主入口


package com.game;

public class Card {

public String points;

public String colour;

public Card(String colour,String points) {

this.points=points;

this.colour=colour;

}

public Card() {

}

/**

* @return the points

*/

public String getPoints() {

return points;

}

/**

* @param points the points to set

*/

public void setPoints(String points) {

this.points = points;

}

/**

* @return the colour

*/

public String getColour() {

return colour;

}

/**

* @param colour the colour to set

*/

public void setColour(String colour) {

this.colour = colour;

}

public int GetColourWeight(String colour) {

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

return 4;

}

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

return 3;

}

if(colour.equals("梅花")) {

return 2;

}

if(colour.equals("方块")) {

return 1;

}

return 0;

}

public int GetPointsWeight(String points) {


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

return 2;

}

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

return 3;

}

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

return 4;

}

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

return 5;

}

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

return 6;

}

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

return 7;

}

if(points.equals("8")) {

return 8;

}

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

return 9;

}

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

return 10;

}

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

return 11;

}

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

return 12;

}

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

return 13;

}

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

return 14;

}

return 0;

}

}


package com.game;

public class Player {

public int id;

public String name;

public Player(int id,String name) {

this.id=id;

this.name=name;

}

}


package com.game;

import java.util.Comparator;

public class CompareCard implements Comparator<Card> {

@Override

public int compare(Card o1, Card o2) {

// TODO Auto-generated method stub

if(o1.GetPointsWeight(o1.getPoints())>o2.GetPointsWeight(o2.getPoints())) {

return 1;

}

if(o1.GetPointsWeight(o1.getPoints())<o2.GetPointsWeight(o2.getPoints())) {

return -1;

}

if(o1.GetPointsWeight(o1.getPoints())==o2.GetPointsWeight(o2.getPoints())) {

if(o1.GetColourWeight(o1.getColour())>o2.GetColourWeight(o2.getColour())) {

return 1;

}

else if(o1.GetColourWeight(o1.getColour())<o2.GetColourWeight(o2.getColour())) {

return -1;

}

else if(o1.GetColourWeight(o1.getColour())==o2.GetColourWeight(o2.getColour())){

return 0;

}

}

return 0;

}

}


package com.game;

import java.util.ArrayList;

import java.util.Arrays;

import java.util.Collections;

import java.util.InputMismatchException;

import java.util.List;

import java.util.Random;

import java.util.Scanner;

public class CardGame {

/**

* 1、创建一副扑克牌

* 包括四种花色:黑桃、红桃、梅花、方片

* 十三中点数:2-10,J,Q,K,A,不考虑大小王

* 2、创建两名玩家

* 玩家至少要有ID、姓名、手牌等属性,手牌为扑克牌的集合

* 3、洗牌

* 将之前创建的"一副扑克牌"打乱顺序

* 4、发牌

* 将洗牌之后的扑克牌集合,从第一张开始,发给两名玩家,按照一人一张的方式,每人发两张

* 5、游戏

* 比较两名玩家手中的扑克牌,规则为:取两人各自手中点数最大的牌进行比较,点数大的赢;

* 若两人各自的点数最大的牌相等,则按花色比较

* @param args

*/

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

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

public List<Card> cardList=new ArrayList<>();

public List<Player> playerList=new ArrayList<>();

public List<Card> player1CardList=new ArrayList<>();

public List<Card> player2CardList=new ArrayList<>();

//创建一副扑克牌

public void createCard() {

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

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

cardList.add(new Card(colours[i],points[j]));

}

}

}

//创建两名玩家,玩家ID必须是整数类型且捕获异常

public void createPlayer() {

Scanner sc=new Scanner(System.in);

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

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

int playerId;

String playerName;

while(true){

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

try {

playerId=sc.nextInt();

break;

}catch(Exception e) {

System.out.println("您应该输入整数!");

sc.next();

}

}

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

playerName=sc.next();

playerList.add(new Player(playerId,playerName));

}

}

//foreach玩家名单

public void foreachPlayerList() {

for (Player player : playerList) {

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

}

}

//洗牌,打乱扑克牌顺序

public void changeCard() {

System.out.println("------------洗牌------------:");

Collections.shuffle(cardList);

}

//foreach扑克牌

public void foreachCard() {

for (Card card : cardList) {

System.out.println("元素:"+card.colour+card.points);

}

}

//发牌,将洗牌之后的扑克牌集合,从第一张开始,发给两名玩家,按照一人一张的方式,每人发两张

public void takeCard() {


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

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

if(i%2==0) {

player1CardList.add(cardList.get(i));

System.out.println("---玩家:"+playerList.get(0).name+"-拿牌");

//System.out.println(playerList.get(0).name+"拿牌:"+player1CardList.get(i/2).colour+player1CardList.get(i/2).points);

continue;

}

if(i%2!=0) {

player2CardList.add(cardList.get(i));

System.out.println("---玩家:"+playerList.get(1).name+"-拿牌");

//System.out.println(playerList.get(1).name+"拿牌:"+player2CardList.get((i-1)/2).colour+player2CardList.get((i-1)/2).points);

continue;

}

}

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

}

//开始游戏,比较两名玩家手中的扑克牌,规则为:取两人各自手中点数最大的牌进行比较,点数大的赢

//若两人各自的点数最大的牌相等,则按花色比较

public void comparePlayerCarList() {

CompareCard cc=new CompareCard();

Card maxCardPlayer1=new Card();

Card maxCardPlayer2=new Card();

int resultPlayer1=cc.compare(player1CardList.get(0), player1CardList.get(1));

int resultPlayer2=cc.compare(player2CardList.get(0), player2CardList.get(1));

if(resultPlayer1==1) {

maxCardPlayer1=player1CardList.get(0);

}else {

maxCardPlayer1=player1CardList.get(1);

}

System.out.println("玩家:"+playerList.get(0).name+"最大的手牌为:"+maxCardPlayer1.colour+maxCardPlayer1.points);

if(resultPlayer2==1) {

maxCardPlayer2=player2CardList.get(0);

}else {

maxCardPlayer2=player2CardList.get(1);

}

System.out.println("玩家:"+playerList.get(1).name+"最大的手牌为:"+maxCardPlayer2.colour+maxCardPlayer2.points);

if(cc.compare(maxCardPlayer1, maxCardPlayer2)==1) {

System.out.println("------------玩家:"+playerList.get(0).name+"获胜");

}

if(cc.compare(maxCardPlayer1, maxCardPlayer2)==-1) {

System.out.println("------------玩家:"+playerList.get(1).name+"获胜");

}

}

public void showHandCard() {

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

System.out.println(playerList.get(0).name+":["+player1CardList.get(0).colour+player1CardList.get(0).points

+","+player1CardList.get(1).colour+player1CardList.get(1).points+"]");

System.out.println(playerList.get(1).name+":["+player2CardList.get(0).colour+player2CardList.get(0).points

+","+player2CardList.get(1).colour+player2CardList.get(1).points+"]");

}

public static void main(String[] args) {

CardGame cg=new CardGame();

cg.createPlayer();

cg.foreachPlayerList();

cg.createCard();

cg.changeCard();

cg.foreachCard();

cg.takeCard();

cg.comparePlayerCarList();

cg.showHandCard();

}

}

http://img1.sycdn.imooc.com//5e0dd32f0001d8f807771423.jpg

http://img1.sycdn.imooc.com//5e0dd32f0001862907881360.jpg


正在回答

2 回答

为什么在getPointsWeight和getColorWeight方法中最后要加return 0

我知道不加的话会报错,但是如果最后面写个return 0,难道不是意思为:不管是if哪种情况,最后都将0返回吗,求教

0 回复 有任何疑惑可以回复我~

牛逼哦

1 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

不知道思路对不对,感觉代码有点多

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信