这是我写的简易扑克牌游戏,希望能帮到大家,欢迎建议,共同进步
******player.java**************************************************
package com.PokeGame;
import java.util.ArrayList;
import java.util.List;
public class Player {
public int id;
public String name;
public List<Poke> handpoke;//用来存放玩家手牌
public Player(int id,String name) {
this.id = id;
this.name = name;
this.handpoke = new ArrayList<Poke>();
}
}
******poke.java******************************************************
package com.PokeGame;
public class Poke {
public String pointsort;//用于游戏点数排序比较
public String flowercolorsort;//用于游戏花色排序
public String flowercolor;
public String point;
public Poke(String flowercolor, String point, String pointsort, String flowercolorsort){
this.flowercolor = flowercolor;
this.point = point;
this.pointsort = pointsort;
this.flowercolorsort = flowercolorsort;
}
@Override
public String toString() {
return flowercolor + point ;
}
}
******Shuffle.java******************************************************
package com.PokeGame;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Shuffle {
//用于存放扑克牌的List
public List<Poke> pokes;
public Shuffle(){
pokes = new ArrayList<>();
}
/**
* 创建扑克牌并添加到扑克牌List
*/
public void pokeCreate(){
String[] flowercolor = {"方块","梅花","红桃","黑桃"};
String[] point = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
for(int i = 0; i<flowercolor.length; i++){
for(int j =0; j<point.length; j++){
Poke poke = new Poke(flowercolor[i],point[j],j+"",i+"");
this.pokes.add(poke);
}
}
System.out.println("------------------创建扑克牌成功----------------");
System.out.println(this.pokes.toString());
}
/**
* 使用Collections.shuffle()随机打乱List顺序;
*/
public void pokeShuffle(){
System.out.println("------------------开始洗牌----------------");
Collections.shuffle(pokes);
System.out.println("------------------洗牌结束----------------");
}
}
******Game.java******************************************************
package com.PokeGame;
import java.util.ArrayList;
import java.util.Collections;
import java.util.InputMismatchException;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
public class Game {
//定义一个玩家List
public List<Player> players;
public Game() {
//初始化玩家List
players = new ArrayList<Player>();
}
/**
* 创建两位玩家进行游戏
* 通过输入玩家ID和姓名进行创建
*/
public void createplayer(){
System.out.println("------------------创建玩家..---------------");
for(int i = 1; i < 3; i++){
System.out.println("请输入第"+i+"位玩家的ID和姓名:");
try {
System.out.println("请输入ID:");
Scanner console = new Scanner(System.in);
int playerid = console.nextInt();
System.out.println("请输入姓名:");
String name = console.next();
Player player = new Player(playerid,name);
players.add(player);
} catch (InputMismatchException e) {
System.out.println("请输入整数类型的ID!");
i--;
continue;
}
}
}
/**
* 通过For Each遍历玩家List
*/
public void forEachPlayers(){
for (Player player : players) {
System.out.println("------欢迎玩家:" + player.name);
}
}
/**
*传入创建扑克牌并洗牌的Shuffle类的实例s,
*才能使用创建存放扑克牌名为pokes的List,
*并随机生成52以内的整数(一共52张牌)作为
*添加进玩家手牌handpoke中的poke在List中的位置
*/
public void faPai(Shuffle s){
System.out.println("------------------开始发牌----------------");
Random random = new Random();
for(int i = 0; i < 4; i++){
if(i==0||i==2){
int a = random.nextInt(52);
players.get(0).handpoke.add(s.pokes.get(a));
//往玩家手牌(handpoke)List中添加完一张牌,将这张牌从(Pokes)List中删除
s.pokes.remove(a);
System.out.println("玩家:" + players.get(0).name + "--拿牌");
}else{
int b = random.nextInt(51);
players.get(1).handpoke.add(s.pokes.get(b));
System.out.println("玩家:" + players.get(1).name + "--拿牌");
}
}
System.out.println("------------------发牌结束----------------");
}
/**
* 通过调用Collectoins的sort()方法对玩家手牌List(handpoke)进行排序,
* 因为每个玩家只有两张手牌,排序之后输入位置为1的元素就是最大手牌
* 同过实现Comparator接口,用手牌中的pointsort和flowercolorsort,
* 分别是点数排序和花色排序
*/
public void handPokeSort(){
Collections.sort(players.get(0).handpoke, new PokeComparator());
System.out.println("玩家:" + players.get(0).name + "最大的手牌为:"+
players.get(0).handpoke.get(1).toString());
Collections.sort(players.get(1).handpoke, new PokeComparator());
System.out.println("玩家:" + players.get(1).name + "最大的手牌为:"+
players.get(1).handpoke.get(1).toString());
//定义一个新的List用于存放两名玩家最大的手牌
List<Poke> compare = new ArrayList<Poke>();
compare.add(players.get(0).handpoke.get(1));
compare.add(players.get(1).handpoke.get(1));
Collections.sort(compare, new PokeComparator());
//通过输出的w值来判断哪名玩家获胜
int w;
if(compare.get(1).equals(players.get(0).handpoke.get(1))){
w = 0;
}else{
w = 1;
}
System.out.println("------------------玩家"+players.get(w).name+"获胜!----------------");
System.out.println("玩家各自的手牌为:");
System.out.println(players.get(0).name+":"+players.get(0).handpoke.toString());
System.out.println(players.get(1).name+":"+players.get(1).handpoke.toString());
}
public static void main(String[] args) {
Shuffle s = new Shuffle();
s.pokeCreate();
s.pokeShuffle();
Game g = new Game();
g.createplayer();
g.forEachPlayers();
g.faPai(s);
g.handPokeSort();
}
}
******PokeComparator.java******************************************************
package com.PokeGame;
import java.util.Comparator;
public class PokeComparator implements Comparator<Poke> {
@Override
public int compare(Poke o1, Poke o2) {
if(o1.pointsort.equals(o2.pointsort)){
return o1.flowercolorsort.compareTo(o2.flowercolorsort);
}else{
return o1.pointsort.compareTo(o2.pointsort);
}
}
}