懒的写注释了,就这样吧

来源:7-1 简易扑克牌游戏

瑟瑟发抖萌新

2018-07-19 19:14

    games.java

package muke.Collection.poker;
import java.util.*;
public class game {
    public user user1;
    public user user2;
    public List pokerList;
    public String[] flowerList={"黑桃","红桃","方块","梅花"};
    public int[] numberList={2,3,4,5,6,7,8,9,10};
    public game(){
        this.pokerList=new ArrayList();
    }
    public  static void main(String[] args){
        game game=new game();
        System.out.println("正在生成牌组");
        for(String flower:game.flowerList){
            for(int number:game.numberList){
                poker poker=new poker(flower,number);
                game.pokerList.add(poker);
            }
        }
        String arr="牌组:";
        for(Object obj:game.pokerList){
            poker poker=(poker) obj;
            arr+=poker.getFlowerColor()+poker.getSize()+"  ";
        }
        System.out.println(arr);
        System.out.println("正在洗牌");
        Collections.shuffle(game.pokerList);
        arr="洗牌后的牌组:";
        for(Object obj:game.pokerList){
            poker poker=(poker) obj;
            arr+=poker.getFlowerColor()+poker.getSize()+"  ";
        }
        System.out.println(arr);
        System.out.println("开始创建人物");
        game.user1=new user(game.saveUserId(1),game.saveUserName(1));
        game.user2=new user(game.saveUserId(2),game.saveUserName(2));
        System.out.println("正在生成人物");
        System.out.println("人物生成完成");
        System.out.println("游戏开始");
        System.out.println("开始发牌");
        Licensing(game.user1,game.pokerList);
        Licensing(game.user2,game.pokerList);
        Licensing(game.user1,game.pokerList);
        Licensing(game.user2,game.pokerList);
        System.out.println("正在计算点数大小");
        size(game.user1);
        size(game.user2);
        System.out.println("正在生成比赛结果");
        result(game.user1,game.user2);
    }
    public int saveUserId(int num){
        int gameId = 0;
        try {
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入第"+num+"个玩家id");
            gameId = scanner.nextInt();
        } catch (Exception e) {
            this.saveUserId(num);
        }
        return gameId;
    }
    public String saveUserName(int num){
        String gameName="";
        try {
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入第"+num+"个玩家名字");
            gameName = scanner.next();
        } catch (Exception e) {
            this.saveUserName(num);
        }
        return gameName;
    }
    public static void Licensing(user user, List pokerList){
        poker poker=(poker) pokerList.get(0);
        System.out.println("为玩家"+user.getName()+"发放"+poker.getFlowerColor()+poker.getSize());
        user.addPokerList(poker);
        pokerList.remove(0);
    }
    public static void size(user user){
        int size=0;
        for(Object obj: user.getPokerList()){
            poker poker=(poker) obj;
            size+=poker.getSize();
        }
        System.out.println("玩家"+user.getName()+"的点数为"+size);
        user.setSize(size);
    }
    public static void result(user user1, user user2){
        if(user1.getSize()>user2.getSize()){
            System.out.println("玩家"+user1.getName()+"以点数"+user1.getSize()+"的点数获胜");
        }else if(user2.getSize()>user1.getSize()){
            System.out.println("玩家"+user2.getName()+"以点数"+user2.getSize()+"的点数获胜");
        }else{
            System.out.println("平局");
        }
    }
}

----------------------------------------------------

user.java

package muke.Collection.poker;

import java.util.ArrayList;
import java.util.List;

public class user {
    private int id;
    private String name;
    private List pokerList;
    private int size;
    public user(int id,String name){
        this.id=id;
        this.name=name;
        this.pokerList=new ArrayList();
    }

    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;
    }

    public List getPokerList() {
        return pokerList;
    }

    public void setPokerList(List pokerList) {
        this.pokerList = pokerList;
    }

    public void addPokerList(poker poker) {
        this.pokerList.add(poker);
    }

    public int getSize() {
        return size;
    }

    public void setSize(int size) {
        this.size = size;
    }
}

------------------------------------------------------

poker.java

package muke.Collection.poker;

public class poker {
    private String flowerColor;
    private int size;
    public poker(String flowerColor,int size){
        this.flowerColor=flowerColor;
        this.size=size;
    }

    public String getFlowerColor() {
        return flowerColor;
    }

    public void setFlowerColor(String flowerColor) {
        this.flowerColor = flowerColor;
    }

    public int getSize() {
        return size;
    }

    public void setSize(int size) {
        this.size = size;
    }
}


写回答 关注

2回答

  • 老丶斑鸠
    2018-07-26 11:40:18

    你这个只有2-10啊?

    瑟瑟发抖萌新

    是的,因为A不是数字,也懒的弄成1了

    2018-07-26 14:01:36

    共 1 条回复 >

  • Passionate1
    2018-07-20 07:36:12

    我怎么一点头绪都没有呢 难过

    瑟瑟发抖萌新

    先创建好list集合-循环添加扑克牌到数组中-打乱数组-键盘输入创建用户-将集合的前4张牌发给用户,计算大小-比较大小-完成

    2018-07-20 10:20:42

    共 1 条回复 >

Java入门第三季

Java中你必须懂得常用技能,不容错过的精彩,快来加入吧

409792 学习 · 4340 问题

查看课程

相似问题