耗时4小时,参考了一些其他慕友的代码,因为整体思路都差不多,所以我着重体现了ComparePoker类的compare方法,并且和其他慕友的比较方法都不一样,自认为我的比较大小的方法更简洁方便。因为刚开始学,很多东西都不懂,还请各路大神多多指教!
运行结果展示:
扑克牌类Poker
/**
* 创建扑克牌
* 共有4种花色13种大小
*/
package com.lsjt;
public class Poker
{
public String colors;
public String numbers;
public Poker(String colors, String numbers)
{
this.colors = colors;
this.numbers = numbers;
}
}
玩家类Player
/**
* 创建两名玩家
*/
package com.lsjt;
import java.util.ArrayList;
import java.util.List;
public class Player
{
public int id;
public String name;
List<Poker> handCards = new ArrayList<Poker>();
public Player(int id, String name)
{
this.id = id;
this.name = name;
}
}
主类GameStart
/**
* Java简易扑克游戏
* 1. 创建扑克牌组包括4种花色和13种大小
* 2. 创建两名玩家分别输入id和姓名,id输入不是整型抛出异常并要求重新输入
* 3. 随机洗牌
* 4. 从洗牌后的扑克牌的第一张开始,发给每个玩家,按照一人一张的方式,每人发两张
* 5. 游戏规则为:取两人各自手中点数最大的牌进行比较,点数大的赢;若两人各自的点数最大的牌相等,则再按照花色比较,按花色顺序比较大小。
* 开发日期:2017.05.05
* 开发者:慕容1444579
*/
package com.lsjt;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class GameStart
{
String[] color = {"","","",""};//创建一副牌(52张)
String[] number = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
Scanner input = new Scanner(System.in);
List<Poker> pokerList = new ArrayList<Poker>();
List<Player> playerList = new ArrayList<Player>();
public static void main(String[] args)
{
System.out.println("- - - - - - - - - - - -\nJava简易扑克游戏(林氏集团出品)\n");
System.out.println("-----欢迎进入纸牌大战游戏----");
GameStart gs = new GameStart();
gs.setPayer(); //创建玩家
gs.initializeCard(); //初始化牌库
gs.shuffleCard(); //洗牌
gs.getCard(); //发牌(拿到手牌)
gs.compare(); //比较大小,判定胜负
gs.showCard(); //展示玩家手牌
System.out.println("\n--------游戏结束--------");
}
//创建两名玩家
private void setPayer()
{
System.out.println("-------创建两名玩家-------");
for(int i=1; i<=2; i++)
{
System.out.println("请输入玩家"+i+"的id:\n");
int id = 0;
try{
id = scanf();
}catch(Exception e){
System.out.println(e.getMessage());
i--;
continue;
}
System.out.println("请输入玩家"+i+"的姓名:\n");
String name ;
String namex = input.next();
if(namex == null)
{
System.out.println("姓名输入不合理,请重新输入!\n");
name = input.next();
}
else{
name = namex;
}
playerList.add(new Player(id, name));
}
input.close();
System.out.println("玩家创建完毕");
}
//判断输入id是否合理,处理异常
private int scanf() throws Exception
{
try{
int inner = input.nextInt();//输入一个整数
return inner;
}catch(Exception e){
input = new Scanner(System.in);
throw new Exception("id输入不合理,请重新输入!");
}
}
//初始化牌
private void initializeCard()
{
System.out.println("\n-------创建扑克牌--------");
for(int i=0; i<4; i++)
{
for(int j=0; j<13; j++)
{
pokerList.add(new Poker(color[i], number[j]));
}
}
int n = 0 ;
for(Poker poker : pokerList)
{
//将创建的牌依次打印出来
if(n%13 == 0)
{
System.out.println("");
}
System.out.print(poker.colors+poker.numbers+" ");
n++;
}
System.out.println("\n------扑克牌创建完毕-------");
}
//洗牌
private void shuffleCard()
{
System.out.println("\n---------开始洗牌--------");
Collections.shuffle(pokerList);//List shuffle随机排序
System.out.println("---------洗牌完毕--------");
}
//发牌
private void getCard()
{
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("--------发牌结束--------\n");
}
//比较大小,判定胜负
private void compare()
{
System.out.println("---------获胜方---------");
ComparePoker comparehPoker = new ComparePoker();
List<Poker> hPoker1 = new ArrayList<Poker>();
List<Poker> hPoker2 = new ArrayList<Poker>();
//获取玩家1的第一张手牌
hPoker1.add(0, playerList.get(0).handCards.get(0));
//获取玩家1的第二张手牌
hPoker1.add(1, playerList.get(0).handCards.get(1));
//获取玩家2的第一张手牌
hPoker2.add(0, playerList.get(1).handCards.get(0));
//获取玩家2的第二张手牌
hPoker2.add(1, playerList.get(1).handCards.get(1));
//调用方法比较大小,先确定玩家各自手牌中较大的一张
int a = comparehPoker.compare(hPoker1.get(0), hPoker1.get(1));
int b = comparehPoker.compare(hPoker2.get(0), hPoker2.get(1));
if(a>0)//玩家1的第一张手牌大
{
if(b>0)//玩家2的第一张手牌大
{
int ab1 = comparehPoker.compare(hPoker1.get(0), hPoker2.get(0));
if(ab1>0)//玩家1赢了
System.out.println("玩家"+ playerList.get(0).name + "获胜!");
else//玩家2赢了
System.out.println("玩家"+ playerList.get(1).name + "获胜!");
}
else//玩家2的第二张手牌大
{
int ab2 = comparehPoker.compare(hPoker1.get(0), hPoker2.get(1));
if(ab2>0)//玩家1赢了
System.out.println("玩家"+ playerList.get(0).name + "获胜!");
else//玩家2赢了
System.out.println("玩家"+ playerList.get(1).name + "获胜!");
}
}
else//玩家1的第二张手牌大
{
if(b>0)//玩家2的第一张手牌大
{
int ab1 = comparehPoker.compare(hPoker1.get(1), hPoker2.get(0));
if(ab1>0)//玩家1赢了
System.out.println("玩家"+ playerList.get(0).name + "获胜!");
else//玩家2赢了
System.out.println("玩家"+ playerList.get(1).name + "获胜!");
}
else//玩家2的第二张手牌大
{
int ab2 = comparehPoker.compare(hPoker1.get(1), hPoker2.get(1));
if(ab2>0)//玩家1赢了
System.out.println("玩家"+ playerList.get(0).name + "获胜!");
else//玩家2赢了
System.out.println("玩家"+ playerList.get(1).name + "获胜!");
}
}
}
//展示玩家手牌
private void showCard()
{
System.out.print("\n-------玩家各自手牌-------");
for(Player player : playerList)
{
System.out.print("\n玩家"+player.name+":");
for(Poker hPoker : player.handCards)
{
System.out.print(hPoker.colors+hPoker.numbers+" ");
}
}
}
}
比较牌的大小类ComparePoker
/**
* 创建扑克牌比较类
* 比较玩家手牌大小
*/
package com.lsjt;
import java.util.Comparator;
public class ComparePoker implements Comparator<Poker>
{
//比较玩家1和2的手牌大小
public int compare(Poker o1, Poker o2)
{
String[] color = {"","","",""};
String[] number = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
int n1 = 0, n2 = 0;
for(int i=0; i<number.length; i++)
{
if(o1.numbers == number[i])
n1 = i;
if(o2.numbers == number[i])
n2 = i;
}
if(n1 == n2)//数字相等
{
int c1 = 0, c2 = 0;
for(int j=0; j<color.length; j++)
{
if(o1.colors == color[j])
c1 = j ;
if(o2.colors == color[j])
c2 = j ;
}
if(c1 > c2)//比较花色
return -1;
else
return 1;
}
if(n1 > n2)
return 1;
else
return -1;
}
}
这个程序我主要侧重于比较类,我只用了一个compare方法,在Poker类中有很多方法都省去了,个人觉得还是算比较简单的一个程序。
不过我对于创建玩家时输入的信息异常处理方面并没有过于完善,所以输入的时候需要规范输入,因为比较懒,所以写完了跑起来了也就懒得继续解决了。
后续我准备用这种方法再写一个炸金花的程序,当然还是会侧重于比较牌的大小以及各种规则,后续。。。
点击查看更多内容
28人点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦