(续)
本篇手记续我的上一篇手记《简易扑克游戏(续)—— 炸金花(中)》链接描述
因篇幅限制,上一篇主类GameStart中还有几个方法,在这里补贴上:
//创建两名玩家
private void setPayer()
{
System.out.println("-------创建两名玩家-------");
int id = 0;
String name;
id = 1;
String name1 = "电脑玩家";
name = name1;
playerList.add(new Player(id, name));
id = 2;
String name2 = "游戏玩家";
name = name2;
playerList.add(new Player(id, name));
System.out.println("玩家1:电脑玩家 创建成功!");
System.out.println("玩家2:游戏玩家 创建成功!");
}
//初始化牌
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(playerList.get(0).name+"拿牌...");
playerList.get(0).handCards.add(pokerList.get(4));
System.out.println(playerList.get(1).name+"拿牌...");
playerList.get(1).handCards.add(pokerList.get(5));
System.out.println("--------发牌结束--------\n");
}
注意!这几个方法是类GameStart中的。
还有最后一个类,扑克牌比较类ComparePoker
/**
* 创建扑克牌比较类
* 按照炸金花游戏规则
* 比较玩家手牌大小
*/
package com.lsjt_2;
import java.util.Comparator;
public class ComparePoker implements Comparator<Poker>
{
String[] color = {"","","",""};
String[] number = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
//比较两张牌的数字及花色大小
public int compare(Poker o1, Poker o2)
{
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;
}
//比较两张牌的数字大小
public int compareCN(Poker o1, Poker o2)
{
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)
return 0;//相等
else if(n1 > n2)
return 1;//数字1大
else
return -1;//数字2大
}
//对三个整型数进行排序(升序)并判断是否为三个连续的数(顺子)
boolean sort_3n(int a, int b, int c)
{
if (a > b && a > c)//a最大
{
if (b > c)//c最小
{
int temp = a;
a = c;
c = temp;
}
else//b最小
{
int temp = a;
a = b;
b = c;
c = temp;
}
}
else if (b > a && b > c)// b最大
{
if (a > c)// c最小
{
int temp = a;
a = c;
c = b;
b = temp;
}
else// a最小
{
int temp = b;
b = c;
c = temp;
}
}
else if (c > a && c > b)// c最大
{
if (a > b)// b最小
{
int temp = a;
a = b;
b = temp;
}
else// a最小
{
}
}
else
{
return false;
}
if((b-a)==1 && (c-b)==1)
{
return true;
}
return false;
}
}
对比之前写的那篇手记《Java第三季-简易扑克牌游戏》链接描述,在类ComparePoker中新增了对三个整型数进行排序(升序)并判断是否为三个连续的数(顺子)方法,然后取消了创建玩家时用户输入及输入的异常处理。
代码整体感觉写的不是很好,希望各路大神勿喷。也欢迎各位不吝赐教,谢谢!
再贴几张运行结果展示:
点击查看更多内容
1人点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦