package day1Today5;
import java.util.Scanner;/*
 * 4,砸金花游戏(控制台版本) 
 * 描述:编写程序实现砸金花的扑克游戏,程序允许用户和电脑对玩,
 * 程序开始时要求用户输入总赌本,电脑和玩家的赌本一样多。
 * 每次开局前,提示用户输入本次赌注,并在一副牌中随机发两手牌(去掉大小王),
 * 每手牌三张,然后比较这两手牌的大小,若A比B大,则用户赢得赌注,
 * 反之,则输掉赌注,直到有一方(用户或电脑)的赌注为0,则退出游戏。
 * 每手牌的大小比较规则:
 * 1)三条>同花顺>同花>顺子>对子>单张
 * 2)处于同一级别的两手牌比较最大的一张牌的大小
 * 3)若两手牌同为对子,则应比较成对的那张牌的大小
 */
//本题有两个方法特别重要,打印出牌面的方法 printpai(),比牌的大小的方法compara()
public class CardGame {
static int userMoney = 0;
static int computerMoney = 0;
static int[] handcards = new int[6]; public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入赌本:");
userMoney = sc.nextInt();
computerMoney = userMoney;
while (userMoney > 0 && computerMoney > 0) {
System.out.println("请输入本局赌注:");
int duzhu = sc.nextInt();
if (duzhu <= 0 || duzhu > userMoney || duzhu > computerMoney) {
System.out.println("赌注不正确,请重新输入:");
continue;
}
printpai();
System.out.println();
if (compara() > 0) {
System.out.println("玩家获胜!");
userMoney += duzhu;
computerMoney -= duzhu;
} else if (compara() < 0) {
System.out.println("电脑获胜!");
userMoney -= duzhu;
computerMoney += duzhu;
} else {
System.out.println("平局!");
}
System.out
.println("玩家赌本为:" + userMoney + " 电脑赌本为:" + computerMoney);
}
} public static void printpai() {
for (int j = 0; j < handcards.length; j++) {
int huase = (int) (Math.random() * 4 + 1);
int paimian = (int) (Math.random() * 13 + 2);
handcards[j] = (huase * 100 + paimian);
switch (huase) {
case 1:
System.out.print("黑桃");
break;
case 2:
System.out.print("红桃");
break;
case 3:
System.out.print("梅花");
break;
case 4:
System.out.print("方块");
break;
}
if (handcards[j] % 100 <= 10) {
System.out.print(handcards[j] % 100 + "  ");
} else {
switch (handcards[j] % 10) {
case 1:
System.out.print("J  ");
break;
case 2:
System.out.print("Q  ");
break;
case 3:
System.out.print("K  ");
break;
case 4:
System.out.print("A  ");
break;
}
}
}
} // 比较两手牌时不能用数字的大小sum+(handcards[j] = (huase * 100 + paimian))
// 的加以判断,那样判断的结果会令 黑桃A<小于梅花8,红桃9>黑桃Q的情况,不符合现实
public static int compara() {
int[] userCard = new int[handcards.length / 2];
int[] computerCard = new int[handcards.length / 2];
// 打印数字类型的牌面值,数组
// System.out.println(Arrays.toString(handcards));
// 定义玩家的牌,取数组元素的前三个
for (int i = 0; i < userCard.length; i++) {
userCard[i] = handcards[i];
}
// 定义电脑的牌,取数组元素的后三个
for (int i = handcards.length / 2; i < handcards.length; i++) {
computerCard[i - handcards.length / 2] = handcards[i];
}
// 比较两玩家的牌的大小值
return fromCardToNumber(userCard) - fromCardToNumber(computerCard);
} public static int fromCardToNumber(int[] temp) {
// 下面是一段 冒泡排序
for (int i = 0; i < temp.length; i++) {
// 比牌面值的大小,即现实中红桃5(代表205)永远是比 黑桃(K113)要小的,对牌的大小顺序进行排序
for (int j = 0; j < temp.length - i - 1; j++) {
if (temp[j] % 100 < temp[j + 1] % 100) {
int tmp = temp[j];
temp[j] = temp[j + 1];
temp[j + 1] = tmp;
}
}
// 输出冒泡排序的两组数字,前面一半的数组值代表玩家的,后一半的数组值代表式电脑的
// System.out.print(temp[i] + " ");
}
// System.out.println();
int num = 0;
// 判断三条的情况
if (temp[0] % 100 == temp[1] % 100 && temp[1] % 100 == temp[2] % 100) {
num = 6 * 1000000 + temp[0] % 100 * 10000 + temp[1] % 100 * 100
+ temp[2] % 100;
} else if (temp[0] / 100 == temp[1] / 100
&& temp[1] / 100 == temp[2] / 100
&& temp[0] % 100 == temp[1] % 100 + 1
&& temp[1] % 100 == temp[2] % 100 + 1) {
num = 5 * 1000000 + temp[0] % 100 * 10000 + temp[1] % 100 * 100
+ temp[2] % 100;
} else if (temp[0] / 100 == temp[1] / 100
&& temp[1] / 100 == temp[2] / 100) {
num = 4 * 1000000 + temp[0] % 100 * 10000 + temp[1] % 100 * 100
+ temp[2] % 100;
} else if (temp[0] % 100 == temp[1] % 100 + 1
&& temp[1] % 100 == temp[2] % 100 + 1) {
num = 3 * 1000000 + temp[0] % 100 * 10000 + temp[1] % 100 * 100
+ temp[2] % 100;
// 数组是从大到小排列的,因此在比较时 要注意
} else if (temp[0] % 100 == temp[1] % 100
|| temp[1] % 100 == temp[2] % 100) {
num = 2 * 1000000 + temp[0] % 100 * 10000 + temp[1] % 100 * 100
+ temp[2] % 100;
} else {
num = 1 * 1000000 + temp[0] % 100 * 10000 + temp[1] % 100 * 100
+ temp[2] % 100;
}
return num; }}
--------------------------------------------------------------------------------------------------------------
/*
 * 用面向对象的方式重写扑克牌游戏程序,定义以下类型:
1,花色,枚举类型,包含枚举值:黑,红,梅,方。
2,点数,枚举类型,包含枚举值:2-A
3,一张扑克牌(Card):包含属性:花色,点数
4,一手扑克牌(OneHandCards):
      包含属性:Card[] cards;//三张扑克牌
      方法: public int  compareTo(OneHandCards c);
      该方法返回某两手扑克牌大小比较的结果:
    如果当前这手牌比c大,则返回>0的值
    如果当前这手牌比c小,则返回<0的值
    如果当前这手牌和c相等,则返回0
5,玩家类(Player) :包含属性:姓名,赌本,当前一手牌6,游戏类(Game):包含属性:两个玩家
包含方法:void 发牌();为该游戏中的所有玩家发牌。
    void startGame();开始游戏。
 */