Java 简易扑克游戏
- 创建扑克牌组包括4种花色和13种大小
- 随后进行随机洗牌
- 创建两名玩家
- 分别输入id和姓名,id输入不是整型抛出异常并要求重新输入
- 从洗牌后的扑克牌的第一张开始,发给每个玩家,按照一人一张的方式,每人发两张
- 游戏规则为:取两人各自手中点数最大的牌进行比较,点数大的赢;若两人各自的点数最大的牌相等,比较两人较小的牌点数;若两人各自的点数最大的牌相等并且两人各自的点数最小的牌也相等,则再按照最大的牌花色比较
package com.xk;
public class Poker {
public String color;
public String points;
public Poker(String color,String points){
this.color=color;
this.points=points;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((points == null) ? 0 : points.hashCode());
result = prime * result + ((color == null) ? 0 : color.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Poker other = (Poker) obj;
if (points == null) {
if (other.points != null)
return false;
} else if (!points.equals(other.points))
return false;
if (color == null) {
if (other.color != null)
return false;
} else if (!color.equals(other.color))
return false;
return true;
}
}
Player.java
package com.xk;
import java.util.ArrayList;
import java.util.List;
public class Player {
public int id;
public String name;
public List<Poker> handCards=new ArrayList<>();
public Player(int id,String name){
this.id=id;
this.name=name;
}
}
CompareToPoker.java
package com.xk;
import java.util.Comparator;
public class CompareToPoker implements Comparator<Poker> {
@Override
public int compare(Poker o1, Poker o2) {
// TODO Auto-generated method stub
String[] color = {"方片", "梅花", "红桃", "黑桃"};
String[] point = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "J","Q","K", "A"};
int temp1 = 0;
int temp2 = 0;
for(int i=0;i<point.length;i++){
if(o1.points.equals(point[i])) temp1 += i*10;
if(o2.points.equals(point[i])) temp2 += i*10;
}
for(int i=0;i<color.length;i++){
if(o1.color.equals(color[i])) temp1 += i;
if(o2.color.equals(color[i])) temp2 += i;
}
if( temp1 > temp2) return -1;
if( temp1 < temp2 ) return 1;
return 0;
}
}
GameStart.java
package com.xk;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class GameStart{
String[] point=new String[]{"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
String[] color=new String[]{"黑桃","红桃","梅花","方块"};
Scanner input = new Scanner(System.in);
List<Poker> pokerList=new ArrayList<>();
List<Player> playerList=new ArrayList<>();
public void initialize(){
for(int i=0;i<4;i++) {
for(int j=0;j<13;j++) {
pokerList.add(new Poker(color[i],point[j]));
}
}
System.out.println("------------创建扑克牌-------------");
int i=0;
for(Poker poker : pokerList){
System.out.print(poker.color+poker.points+" ");
i++;
if(i%13==0){
System.out.println("");
}
}
}
public void shuffle(){
System.out.println("-------------开始洗牌-------------");
Collections.shuffle(pokerList);//shuffle List随机排序
System.out.println("-------------洗牌完成-------------");
}
public void sort(){
Collections.sort(playerList.get(0).handCards,new CompareToPoker());
Collections.sort(playerList.get(1).handCards,new CompareToPoker());
System.out.println(playerList.get(0).name+"最大手牌:"+playerList.get(0).handCards.get(0).color+playerList.get(0).handCards.get(0).points);
System.out.println(playerList.get(1).name+"最大手牌:"+playerList.get(1).handCards.get(0).color+playerList.get(1).handCards.get(0).points);
}
public void print(){
System.out.println("----------玩家各自手牌-------------");
for(Player player : playerList){
System.out.print(player.name+":");
for(Poker poker : player.handCards){
System.out.print("["+poker.color+poker.points+"]");
}
System.out.println("");
}
}
public void compareToPonint(){
System.out.println("--------------获胜方--------------");
List<Poker> maxPoker = new ArrayList<Poker>();
List<Poker> minPoker = new ArrayList<Poker>();
maxPoker.add(playerList.get(0).handCards.get(0));
maxPoker.add(playerList.get(1).handCards.get(0));
minPoker.add(playerList.get(0).handCards.get(1));
minPoker.add(playerList.get(1).handCards.get(1));
Collections.sort(maxPoker,new CompareToPoker());
Collections.sort(minPoker,new CompareToPoker());
/*
* 判断最大牌点数是否相等
* 判断最小牌点数是否相等
* 判断最大牌花色
* 判断最小牌花色
*
*/
if(maxPoker.get(0).points.equals(maxPoker.get(1).points)){
if(minPoker.get(0).points.equals(minPoker.get(1).points)){
if(maxPoker.get(0).equals(playerList.get(0).handCards.get(0))){
System.out.println("红方获胜\tWinner:"+playerList.get(0).name);
}else{
System.out.println("蓝方获胜\tWinner:"+playerList.get(1).name);
}
}else{
if(minPoker.get(0).equals(playerList.get(0).handCards.get(1))){
System.out.println("红方获胜\tWinner:"+playerList.get(0).name);
}else{
System.out.println("蓝方获胜\tWinner:"+playerList.get(1).name);
}
}
}else if(maxPoker.get(0).equals(playerList.get(0).handCards.get(0))){
System.out.println("红方获胜\tWinner:"+playerList.get(0).name);
}else{
System.out.println("蓝方获胜\tWinner:"+playerList.get(1).name);
}
}
public void playerSet(){
System.out.println("-----------创建两名玩家------------");
for(int i=1;i<=2;i++){
System.out.println("输入玩家"+i+"的id:");
int id=0;
try{
id=scanf();
}catch(Exception e){
System.out.println(e.getMessage());
i--;
continue;
}
System.out.println("输入玩家"+i+"的姓名:");
String name=input.next();
playerList.add(new Player(id,name));
}
input.close();
System.out.println("----------Game Start!-----------\n红方玩家\t蓝方玩家");
for(Player p : playerList){
System.out.print(p.name+"\t");
}
}
public int scanf() throws Exception{
try{
int in=input.nextInt();
return in;
}catch(Exception e){
input=new Scanner(System.in);
throw new Exception("输入异常,请输入整数类型的ID!");
}
}
public void deal(){
System.out.println("\n-------------开始发牌-------------");
System.out.println(playerList.get(0).name+"拿牌");
playerList.get(0).handCards.add(pokerList.get(0));
System.out.println(playerList.get(1).name+"拿牌");
playerList.get(1).handCards.add(pokerList.get(1));
System.out.println(playerList.get(0).name+"拿牌");
playerList.get(0).handCards.add(pokerList.get(2));
System.out.println(playerList.get(1).name+"拿牌");
playerList.get(1).handCards.add(pokerList.get(3));
System.out.println("------------发牌结束--------------");
}
public static void main(String[] args) {
System.out.println("----------欢迎进入纸牌大战----------");
GameStart gs=new GameStart();
gs.initialize();
gs.shuffle();
gs.playerSet();
gs.deal();
gs.sort();
gs.compareToPonint();
gs.print();
System.out.println("-------------游戏结束-------------");
}
}
运行效果
点击查看更多内容
36人点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦