继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

简易扑克牌游戏

qq_e琴_0
关注TA
已关注
手记 1
粉丝 0
获赞 0

//创建玩家类

package com.project.game;


import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;


/*

 * 玩家

*/

public class Player {

private int id;

private String name;

public List<Type> playerToSelectCards;

public Player(int id,String name) {

this.id = id;

this.name = name;

this.playerToSelectCards = new ArrayList<Type>();

}


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.project.game;


public class Type implements Comparable<Type> {

private Integer id;//根据id值去判断那种牌型大

private String name;//牌型

private String num;

public Type(int id,String name,String num) {

this.id = id;

this.name = name;

this.num = num;

}

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 String getNum() {

return num;

}

public void setNum(String num) {

this.num = num;

}



//@Override

public int compareTo(Type obj) {

// TODO Auto-generated method stub

if(obj.getNum()=="A" && this.getNum()!="A") {

return 1;//arg1>arg0

}

if(this.getNum()=="A" && obj.getNum()!="A") {

return -1;//arg1<arg0

}

if(obj.getNum().compareTo(this.getNum())==0) {

return (obj.getId()+"").compareTo(this.getId()+"");

}else {

return obj.getNum().compareTo(this.getNum());

}

}

}


//通过 Comparator实现自定义排序

package com.project.game;


import java.util.Comparator;


public class TypeComparator implements Comparator<Type> {


@Override

public int compare(Type arg0, Type arg1) {

//升序

// TODO Auto-generated method stub

if(arg1.getNum()=="A" && arg0.getNum()!="A") {

return 1;//arg1>arg0

}

if(arg0.getNum()=="A" && arg1.getNum()!="A") {

return -1;//arg1<arg0

}

if(arg1.getNum().compareTo(arg0.getNum())==0) {

return (arg1.getId()+"").compareTo(arg0.getId()+"");

}else {

return arg1.getNum().compareTo(arg0.getNum());

}

}


}


package com.project.game;


import java.util.ArrayList;

import java.util.Collections;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import java.util.Scanner;


/*

 * 玩牌

*/

public class Game {

public List<Type> cards;//扑克牌

public List<Player> players;//玩家

public String[] num;

public List<Type> type ;

public Game() {

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

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

this.num = new String[] {"2","3","4","5","6","7","8","9","10","J","Q","K","A"};

this.type = new ArrayList<Type>();

type.add(new Type(0,"方片","1"));

type.add(new Type(1,"梅花","2"));

type.add(new Type(2,"红桃","3"));

type.add(new Type(3,"黑桃","4"));

}

/*

* 创建扑克牌

*/

public void getCardsList() {

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

int add = 0;

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

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

add++;

Type newType = new Type(add,type.get(i).getName(),num[j]);

cards.add(newType);

}

}

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

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

List<String> temp  = makeList(cards);

System.out.println("为:"+temp);

}

/*

* 扑克牌生成List打印出来

*/

public List<String> makeList(List<Type> cards) {

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

for(Type card: cards) {

temp.add(card.getName()+card.getNum());

}

return temp;

}

/*

* 洗牌

*/

public void getRandomCards() {

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

Collections.shuffle(cards);

System.out.println("洗牌后的扑克牌:"+makeList(cards));

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

}

/*

* 创建玩家

*/

public void getPlayer() {

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

Scanner console = new Scanner(System.in);

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

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

Boolean isTrue = false;

do {

try {

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

int id = console.nextInt();

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

String name = console.next();

Player player = new Player(id,name);

players.add(player);

isTrue = false;

}catch(Exception e) {

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

String clear = console.next();//处理死循环的关键,程序一直保存错误类型的id的值,所有一直死循环

isTrue = true;

}

}while(isTrue);

}

/*

* 遍历players

*/

for(Player p:players) {

System.out.println("欢迎玩家:"+p.getName());

}

}

/*

* 发牌

*/

public void sendCards() {

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

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

for(Player p:players) {

p.playerToSelectCards.add(cards.get(0));

cards.remove(0);

//

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

}

}

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

}

/*

* 决出玩家最终赢家

*/

public void getWinner() {

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

List<Type> winner = new ArrayList<Type>();

for(Player p:players) {

Collections.sort(p.playerToSelectCards);

System.out.println("排序后的"+makeList(p.playerToSelectCards));

System.out.println("玩家:"+p.getName()+"最大的手牌为:"+p.playerToSelectCards.get(0).getName()+p.playerToSelectCards.get(0).getNum());

winner.add(p.playerToSelectCards.get(0));

}

//获取Map的值集合

Collections.sort(winner);

System.out.println(makeList(winner));

for(Player p:players) {

if(p.playerToSelectCards.contains(winner.get(0))) {

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

}

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

System.out.println(p.getName()+":"+makeList(p.playerToSelectCards));

}

//System.out.println("获胜的牌为:"+winner.get(0).getName()+winner.get(0).getNum());

}

public void test() {

List<Type> card = new ArrayList<Type>();

card.add(new Type(1,"方片","A"));

card.add(new Type(2,"红桃","A"));

//未排序前的元素列表

List<String> temp  = makeList(card);

System.out.println("为:"+temp);

//排序后的元素

Collections.sort(card);

//Collections.sort(card,new TypeComparator());

List<String> temp1  = makeList(card);

System.out.println("为:"+temp1);

}

public static void main(String[] args) {

Game game = new Game();

//game.test();

game.getCardsList();

game.getRandomCards();//洗牌

game.getPlayer();//加入玩家

game.sendCards();//发牌

game.getWinner();//得出赢家

}

}


运行结果:

spacer.gif

https://img3.mukewang.com/5b84af57000152d216241020.jpg

请大神指导







打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP