本帖最后由 精诚所至 于 2017-3-24 22:22 编辑
这是个扑克牌游戏的代码,大家没事可以玩玩.[AppleScript] 纯文本查看 复制代码 package com.itheima.tenhalf;
import java.util.Random;
import java.util.Scanner;
/*
* 扑克牌游戏10.5,规则如下:
* 一副扑克,大于等于10的记为0.5,其他的按照其点数记.
* 经过洗牌后,各玩家依次取一张牌,根据自己牌点数的大小决定是否继续要牌,最后由玩家牌的点数大小来决定胜负,点数大的则赢,如果某个玩家点数总和大于10.5,则称为炸掉,炸掉后成绩为0
*
*/
/*
* 思路:
* 1.创建扑克牌类
* 2.洗牌
* 3.显示扑克牌
* 4.10点半的算法
*/
public class TenHalf {
public static void main(String[] args) {
System.out.println("10点半游戏!\r\n");
Card[] oneCard = new Card[52];//保存每张牌的花色和数字
Shuffle(oneCard);
tenhalf(oneCard);
}
/*
* 显示扑克牌
*/
public static void showCard(Card[] oneCard) {
String s = "";
//遍历52张牌
for (int i = 0, j = 0; i < oneCard.length; i++, j++) {
if (j % 13 == 0) {
System.out.println();
}
switch (oneCard[i].getSuit()) {
case 1:
s = "黑桃";
break;
case 2:
s = "红桃";
break;
case 3:
s = "梅花";
break;
case 4:
s = "方块";
break;
default:
break;
}
System.out.print(" " + s + oneCard[i].getNumber());
}
}
/*
* 洗牌
* 按照顺序生成牌和花色,然后显示生成的扑克牌.
* 初始化随机种子,并随机换牌,将牌的的顺序打乱
*/
public static void Shuffle(Card[] oneCard) {
Card tempCard = new Card();
int suitNumber = 0;
//生成52张牌
for (int i = 0; i < oneCard.length; i++) {
if (i % 13 == 0) {
suitNumber++;//改变花色
}
Card t = new Card();
t.setSuit(suitNumber);//保存花色
int temp = i % 13;//生成牌数
switch (temp) {
case 0:
t.setNumber('A');
break;
case 9:
t.setNumber('0');
break;
case 10:
t.setNumber('J');
break;
case 11:
t.setNumber('Q');
break;
case 12:
t.setNumber('K');
break;
default:
t.setNumber((char)(temp + '1'));
}
if (temp >= 10) {
t.setNum(0.5);
}else {
t.setNum((double)(temp + 1));
}
oneCard[i] = t;
}
System.out.println("一副新牌的初始排列如下:");
showCard(oneCard);//调用显示扑克牌的方法
System.out.println();
Random r = new Random();
//将扑克牌中随机两张牌进行互换位置,共焕52次
for (int i = 0 ; i < oneCard.length; i++) {
int j = r.nextInt(oneCard.length);
Card tempcard = oneCard[j];
oneCard[j] = oneCard[i];
oneCard[i] = tempcard;
}
}
/*
* 十点半算法
*/
public static void tenhalf(Card[] oneCard) {
int count = 0;//牌的计数器
int iUser = 0;//为游戏者牌的数量
int iComputer = 0;//为计算机牌的数量
int flag = 1, flagc = 1;
String jixu, s = "";
Card[] user = new Card[20], computer = new Card[20];//保存游戏者和计算机手中的牌
double uTotal = 0, cTotal = 0;//统计计算机和游戏者的总点数
Scanner input = new Scanner(System.in);
while (flag == 1 && count < 52) {//还有牌继续发牌
//游戏者取牌
user[iUser++] = oneCard[count++];//发牌给游戏者
uTotal += user[iUser - 1].getNum();//累加游戏者总点数
//计算机进行取牌
if (count >= 52) {
flag = 0;
}else if (uTotal > 10.5) {
flagc = 0;
}else {
if ((cTotal < 10.5 && cTotal < uTotal) || cTotal < 7) {
computer[iComputer++] = oneCard[count++];
cTotal += computer[iComputer-1].getNum();
}
}
System.out.println("用户的总点数为:" + uTotal +"\t");
System.out.print("用户的牌为:");
for (int i = 0; i < iUser; i++) {
switch(user[i].getSuit()) {
case 1:
s = "黑桃";
break;
case 2:
s = "红桃";
break;
case 3:
s = "梅花";
break;
case 4:
s = "方块";
break;
default:
break;
}
System.out.print(" " + s + user[i].getNumber());
}
System.out.println();
System.out.println("计算机的总点数为:" + cTotal + "\t");
System.out.print("计算机的牌为:");
for (int i = 0; i < iComputer; i++) {
switch(user[i].getSuit()) {
case 1:
s = "黑桃";
break;
case 2:
s = "红桃";
break;
case 3:
s = "梅花";
break;
case 4:
s = "方块";
break;
default:
break;
}
System.out.print(" " + s + computer[i].getNumber());
}
System.out.println();
if (uTotal < 10.5) {
System.out.println("还要牌吗(y/n)?");
jixu = input.nextLine();
if (jixu.equalsIgnoreCase("y")) {
flag = 1;
}else {
flag = 0;
}
if (count == 52) {
System.out.println("牌已经发完了");
break;
}
}else {
break;
}
}
while (flagc == 1 && cTotal < uTotal) {//游戏者不要牌
if (uTotal > 10.5) {
break;
}else {
if (cTotal < 10.5 && cTotal < uTotal) {//计算机点数小于游戏者点数
computer[iComputer++] = oneCard[count++];
cTotal += computer[iComputer-1].getNum();
}else {
break;
}
}
}
System.out.println("用户的总点数为:" + uTotal +"\t");
System.out.print("用户的牌为:");
for (int i = 0; i < iUser; i++) {
switch(user[i].getSuit()) {
case 1:
s = "黑桃";
break;
case 2:
s = "红桃";
break;
case 3:
s = "梅花";
break;
case 4:
s = "方块";
break;
default:
break;
}
System.out.print(" " + s + user[i].getNumber());
}
System.out.println();
System.out.println("计算机的总点数为:" + cTotal + "\t");
System.out.print("计算机的牌为:");
for (int i = 0; i < iComputer; i++) {
switch(user[i].getSuit()) {
case 1:
s = "黑桃";
break;
case 2:
s = "红桃";
break;
case 3:
s = "梅花";
break;
case 4:
s = "方块";
break;
default:
break;
}
System.out.print(" " + s + computer[i].getNumber());
}
System.out.println();
//输出游戏结果
if(cTotal == uTotal || (uTotal > 10.5 && cTotal > 10.5)) {
System.out.println("用户和计算机打成了平手!");
}else if (uTotal > 10.5) {
System.out.println("你输了,请继续努力吧!");
}else if (cTotal >10.5) {
System.out.println("恭喜,你赢了!");
}else if (cTotal > uTotal) {
System.out.println("你输了,请继续努力吧!");
}else {
System.out.println("恭喜,你赢了!");
}
}
}
//下面是Card类
package com.itheima.tenhalf;
public class Card {
private int suit;//花色
private char number;//牌数
private double num;//游戏中的点数
public Card() {
}
public Card(int suit, char number, double num) {
this.suit = suit;
this.number = number;
this.num = num;
}
public int getSuit() {
return suit;
}
public void setSuit(int suit) {
this.suit = suit;
}
public char getNumber() {
return number;
}
public void setNumber(char number) {
this.number = number;
}
public double getNum() {
return num;
}
public void setNum(double num) {
this.num = num;
}
}
|