简易扑克牌游戏
功能描述
1、创建一副扑克牌
包括四种花色
十三个点数,不考虑大小王
2、创建两名玩家
玩家至少要有ID、姓名、手牌等属性,手牌为扑克牌的集合
3、洗牌
将之前创建的一副扑克牌打乱顺序
4、发牌
将洗牌之后的扑克牌集合,从第一张开始,发给两名玩家,按照一人一张的方式,没人发两张
5、游戏
比较两名玩家手中的扑克牌,规则为:取两人各自手中点数最大的牌进行比较,点数大的赢,若两人各自的点数最大的牌相等,则按花色比较
package poker;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
public class Player {
public int id;
public String name;
public List<Poker> myPo = new ArrayList<Poker>();
public Player(int id,String name)
{
this.id = id;
this.name = name;
}
}
package poker;
public class Poker implements Comparable<Poker> {
public String flower;
public String num;
public Integer k;//定义点数大小
public Integer f;//定义花色大小
public Poker(String flower,String num){
this.flower = flower;
this.num = num;
if(num.equals("J"))
{
this.k = 11;
}
else if(num.equals("Q"))
{
this.k = 12;
}
else if(num.equals("K"))
{
this.k = 13;
}
else if(num.equals("A"))
{
this.k = 14;
}
else
{
this.k = Integer.parseInt(num);
}
if(flower.equals("方块"))
{
this.f = 1;
}
else if(flower.equals("梅花"))
{
this.f = 2;
}
else if(flower.equals("红心"))
{
this.f = 3;
}
else
{
this.f = 4;
}
}
public int compareTo(Poker arg0){
if(!(this.k==arg0.k))
{
return this.k.compareTo(arg0.k);
}
else
{
return this.f.compareTo(arg0.f);
}
}
public String toString(){
return (this.flower+this.num);
}
}
package poker;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class PlayGame {
List<Poker> allPokers = new ArrayList<Poker>();
List<Player> players = new ArrayList<Player>();
public void createPokers(){
System.out.println("----------创建扑克牌----------");
String[] flowers = {"方块","梅花","红心","黑桃"};
String[] nums = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
for(int i=0;i<4;i++)
{
for(int j=0;j<13;j++)
{
allPokers.add(new Poker(flowers[i],nums[j]));
}
}
System.out.println("----------扑克牌创建成功!----------");
System.out.print("为:"+allPokers.toString());
}
public void shuf(){
System.out.println("----------开始洗牌...----------");
Collections.shuffle(allPokers);
System.out.println("----------洗牌结束!----------");
}
public void createPlayers(int num){
System.out.println("----------创建玩家...----------");
Scanner console = new Scanner(System.in);
int id = 0;
boolean isError = true;
for(int i=0;i<num;i++)
{
System.out.println("请输入第"+(i+1)+"位玩家id和姓名:");
while(isError)
{
System.out.println("输入id:");
try{
console = new Scanner(System.in); //所输入的非整型一直在扫描器,因此会陷入死循环
id = console.nextInt();
break;
}catch(Exception e){
System.out.println("请输入整数类型的id!");
// String s = console.next();
}
}
System.out.println("输入姓名:");
String name = console.next();
players.add(new Player(id,name));
}
for(Player p:players)
{
System.out.println("欢迎玩家:"+p.name);
}
}
public void deal(int numOfPokes){
System.out.println("----------开始发牌--------------");
int j = 0;
for(int i =0;i<numOfPokes;i++)
{
for(Player p:players)
{
System.out.println("玩家:"+p.name+"-拿牌");
p.myPo.add(allPokers.get(j++));
}
}
System.out.println("----------发牌结束!----------");
}
public void startGame(){
System.out.println("----------开始游戏----------");
for(Player p:players)
{
// Collections.sort(p.myPo);
System.out.println("玩家:"+p.name+
"最大的手牌为:"+Collections.max(p.myPo).toString());
}
}
public void winOrLose(){
Player winner = players.get(0);
for(int i=1; i<players.size()-1;i++)
{
Poker p = Collections.max(players.get(i).myPo);
if(p.compareTo(Collections.max(winner.myPo))>1)
{
winner = players.get(i);
}
}
System.out.println("----------玩家:"+winner.name+"获胜!----------");
}
public void pokersOfPlayer(){
System.out.println("玩家各自的手牌为:");
for(Player p:players)
{
System.out.println(p.name+":"+p.myPo.toString());
}
}
public static void main(String[] args) {
PlayGame pg = new PlayGame();
pg.createPokers();
pg.shuf();
pg.createPlayers(3);
pg.deal(3);
pg.startGame();
pg.winOrLose();
pg.pokersOfPlayer();
}
}
网友评论