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

终于完成作业了

package com.pokergame;


import java.util.ArrayList;


public class Player {

public int id;

public String name;

public ArrayList<Card> cards;

public Player(int id, String name) {

this.id = id;

this.name = name;

this.cards = new ArrayList<Card>();

}

}


package com.pokergame;


public class Card implements Comparable<Card> {

public String type;

public String value;

public Card(String type, String value){

this.type = type;

this.value = value;

}

public int eqType(){

        int flower = 0;

   switch(type){

        case "黑桃": flower = 4;break;

        case "红桃": flower = 3;break;

        case "梅花": flower = 2;break;

        case "方片": flower = 1;break;

   }       

        return flower;

    }


@Override

public int compareTo(Card o) {

int flag = this.value.compareTo(o.value);

if ( flag == 0) {

return this.eqType() - o.eqType();

} else {

return flag;

}

}

}



package com.pokergame;


import java.util.ArrayList;

import java.util.Collections;

import java.util.InputMismatchException;

import java.util.Random;

import java.util.Scanner;


public class PlayGame {

public ArrayList<Card> pokers = new ArrayList<Card>();

public ArrayList<Card> newpokers = new ArrayList<Card>();

public ArrayList<Player> players = new ArrayList<Player>();

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

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


//创建扑克牌

public ArrayList<Card> createPokerSet(){

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

for ( String i : type ){

for ( String j : size ){

pokers.add(new Card(i, j));

System.out.print(i+j+",");

}

}

System.out.println("\n扑克牌创建完成!共有"+pokers.size()+"张牌。");

return pokers;

}


//创建玩家

public void createPlayer(int playernum){

for (int i = 1; i <= playernum; i++){

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

int id = 0;

String name = "";

while (true){

try {

//scanner对象发生异常之后必须重置才可使用,故必须放在循环内,否则循环将变成死循环

Scanner input = new Scanner(System.in);

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

id = input.nextInt();

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

name = input.next();

break;

} catch (InputMismatchException e) {

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

}

}

players.add(new Player(id, name));

}

for (Player player : players){

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

}

}

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

public void shuffle(){

Random random = new Random();

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

Card e = pokers.get(random.nextInt(pokers.size()));

newpokers.add(e);

}

}

//给玩家发牌

public void getCards(int cardsnum){

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

int idx = 0;

for (int i = 1; i <= cardsnum; i++){

for (Player player : players) {

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

// System.out.println(newpokers.get(idx).type+newpokers.get(idx).value);

player.cards.add(newpokers.get(idx));

idx++;

}

}

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

}

public void show(ArrayList<Card> cards){

for (Card card:cards){

System.out.print(card.type+card.value+",");

}

System.out.println();

}

   

public void play(){

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

ArrayList<Card> mc = new ArrayList<Card>();

//选出每个玩家手上的最大牌

for (Player player:players) {

Collections.sort(player.cards);

int num = player.cards.size();

System.out.println("玩家:"+player.name+"最大的手牌为:"+player.cards.get(num-1).type+player.cards.get(num-1).value);

mc.add(player.cards.get(num-1));

}

//选出最大牌,宣布赢家

Collections.sort(mc);

Card max = mc.get(mc.size()-1);

for (Player player:players) {

if (player.cards.contains(max)) {

System.out.println("----------玩家"+player.name+"获胜----------");

}

}

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

for (Player player:players){

System.out.print(player.name+":");

show(player.cards);

}

}

public static void main(String[] args) {

PlayGame pg = new PlayGame();

pg.createPokerSet();

pg.createPlayer(2);

pg.shuffle();

pg.getCards(2);

pg.play();

}

}


正在回答

1 回答

举报

0/150
提交
取消

终于完成作业了

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