我写的这个扑克牌小游戏是一个很基础的java控制台程序。这个扑克牌游戏主要的游戏过程是:首先创建一副扑克牌,创建完扑克牌之后要进行洗牌,牌洗好了,需要玩家来玩,接下来就创建玩家。有洗好的牌,也有玩家了,那么就开始发牌,每一位玩家发两张牌,发完牌后,比较玩家中手牌的大小,大的那一位获胜。(忽略大小王)
第一步:首先我们需要创建一个扑克牌类,扑克牌主要有两个属性,一个是扑克牌的点数(point),一个是扑克牌的花色(color),需要重载构造函数,一个无参的,一个有参的。在最后还重写了equals方法,判断两个扑克牌是否相等。
第二步:我们创建玩家类,玩家拥有三个属性,一个是id,一个name,另一个是手牌,手牌需要用集合存储。在这个游戏中,我们给每名玩家会发两张牌,所以用List来存放手牌。同样是创建无参构造方法和有参构造方法。
第三步:创建游戏的主类了。
public class Poker {
private String point;
private String color;
public void setPoint(String point) {
this.point=point;
}
public String getPoint() {
return point;
}
public void setColor(String color) {
this.color=color;
}
public String getColor() {
return color;
}
public Poker(String color,String point) {
// TODO 自动生成的构造函数存根
this.point=point;
this.color=color;
}
@Override
public String toString() {
return color +" "+ point;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (!(obj instanceof Poker))
return false;
Poker other = (Poker) obj;
if (color == null) {
if (other.color != null)
return false;
} else if (!color.equals(other.color))
return false;
if (point == null) {
if (other.point != null)
return false;
} else if (!point.equals(other.point))
return false;
return true;
}
}
import java.util.ArrayList;
import java.util.List;
public class GamePlayer {
private String id;
private String name;
private List pokers;
public GamePlayer(String id, String name) {
// TODO 自动生成的构造函数存根
this.id = id;
this.name = name;
this.pokers = new ArrayList();
}
public void setId(String id) {
this.id = id;
}
public String getId() {
return id;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public List getPokers() {
return pokers;
}
public void setPokers(List pokers) {
this.pokers = pokers;
}
}
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
public class ControlCenter implements Comparator {
private List pokerList;
private List playerList;
private List pokerListShuffle;
private String playerId;
private String playerName;
private Scanner sc;
private Poker getPokers;
public ControlCenter() {
// TODO 自动生成的构造函数存根
this.pokerList = new ArrayList();
this.playerList = new ArrayList();
this.pokerListShuffle = new ArrayList();
this.sc = new Scanner(System.in);
}
/*
* 第一步:创建扑克牌
*/
public void poker() {
String arrayPoint[] = { "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A" };
String arrayColor[] = { "方块", "梅花", "红桃", "黑桃" };
for (String colors : arrayColor) {
for (String points : arrayPoint) {
Poker poker = new Poker(colors, points);
pokerList.add(poker);
}
}
System.out.print("扑克牌:[");
for (Poker pokerlist : pokerList) {
System.out.print(pokerlist + " ");
}
System.out.println("]");
}
/*
* 第二步:洗牌
*/
public void shufflePoker() {
System.out.println("-----洗牌开始-----");
Collections.shuffle(pokerList);
for (Poker shuffle : pokerList) {
pokerListShuffle.add(shuffle);
}
System.out.println("-----洗牌完毕-----");
}
/*
* 第三步:创建玩家
*/
public void player() {
for (int i = 1; i < 3; i++) {
System.out.println("有请第" + i + "位玩家输入Id和Name:");
System.out.println("请输入Id:");
playerId = sc.next();
System.out.println("请输入Name:");
playerName = sc.next();
GamePlayer gp = new GamePlayer(playerId, playerName);
playerList.add(gp);
}
System.out.println("玩家:" + playerList.get(0).getName() + "和玩家" + playerList.get(1).getName() + "登录成功");
}
/*
* 第四步:发牌
*/
public void Licensing() {
Random r = new Random();
for (int i = 0; i < 2; i++) {
for (int j = 0; j 1) {
return 1;
} else if (o2.getColor().charAt(0) - o1.getColor().charAt(0) 1) {
return 1;
} else if (o2.getPoint().charAt(0) - o1.getPoint().charAt(0) < 1) {
return -1;
} else {
return 0;
}
}
}
/*
* 第六步:扑克牌比大小
*/
public void gameStart() {
Collections.sort(playerList.get(0).getPokers(), new ControlCenter());
Collections.sort(playerList.get(1).getPokers(), new ControlCenter());
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
System.out.println(
"玩家:" + playerList.get(i).getName() + "手中的牌:" + playerList.get(i).getPokers().get(j).getColor()
+ playerList.get(i).getPokers().get(j).getPoint());
}
}
System.out.println("玩家" + playerList.get(0).getName() + "的牌:" + playerList.get(0).getPokers().get(0));
System.out.println("玩家" + playerList.get(1).getName() + "的牌:" + playerList.get(1).getPokers().get(0));
List lastList = new ArrayList();
lastList.add(playerList.get(0).getPokers().get(0));
lastList.add(playerList.get(1).getPokers().get(0));
Collections.sort(lastList, new ControlCenter());
if (lastList.get(0).equals(playerList.get(0).getPokers().get(0))) {
System.out.println("玩家:" + playerList.get(0).getName()+"胜利");
} else {
System.out.println("玩家:" + playerList.get(1).getName()+"胜利");
}
}
public static void main(String[] args) {
// TODO 自动生成的方法存根
ControlCenter cc = new ControlCenter();
cc.poker();
cc.shufflePoker();
cc.player();
cc.Licensing();
cc.gameStart();
}
}
游戏结果
原创文章 1获赞 3访问量 84
关注
私信
展开阅读全文
作者:Java菜鸟~