import java.util.Scanner; |
/** |
* java赌博程序 |
* |
*/ |
public class CardGame { |
static int [] twoHandsCards = new int [ 6 ]; |
static int userAMoney = 0 ; |
static int userBMoney = 0 ; |
public static void main(String[] args) { |
System.out.println( "欢迎光临【挨踢赌场】 ***马无夜草不肥,人无横财不富!*** " ); |
Scanner s = new Scanner(System.in); |
System.out.println( "请输入总赌本:" ); |
userAMoney = s.nextInt(); |
userBMoney = userAMoney; |
int currentMoney = 0 ; |
while (userAMoney > 0 && userBMoney > 0 ) { |
System.out.println( "请输入本局赌注:" ); |
currentMoney = s.nextInt(); |
if (currentMoney <= 0 || currentMoney > userAMoney |
|| currentMoney > userBMoney) { |
System.out.println( "电脑或你的赌注不足,请重新输入!" ); |
continue ; |
} |
dispatchCards(); |
printCards(twoHandsCards); |
if (compare() > 0 ) { |
userAMoney += currentMoney; |
userBMoney -= currentMoney; |
System.out.println( "玩家获胜!" ); |
} else if (compare() < 0 ) { |
userAMoney -= currentMoney; |
userBMoney += currentMoney; |
System.out.println( "电脑获胜!" ); |
} else { |
System.out.println( "平局!" ); |
} |
System.out.println( "玩家赌本:" + userAMoney + ";电脑赌本:" + userBMoney); |
} |
if (userAMoney <= 0 ) { |
System.out.println( "你输光老本了,请下次再来!" ); |
} else { |
System.out.println( "电脑已经输光老本了,你行行好吧!" ); |
} |
} |
/** |
* 分牌 |
*/ |
public static void dispatchCards() { |
int i = 0 ; |
while (i < twoHandsCards.length) { |
int h = ( int ) (Math.random() * 4 ) + 1 ; |
int d = ( int ) (Math.random() * 13 ) + 2 ; |
int j = 0 ; |
for (; j < i; j++) { |
if (twoHandsCards[j] == h * 100 + d) { |
break ; |
} |
} |
if (j < i) { |
continue ; |
} else { |
twoHandsCards[i++] = h * 100 + d; |
} |
} |
} |
/** |
* 打印双方的牌 |
* |
* @param oneHandCards |
*/ |
public static void printCards( int [] oneHandCards) { |
for ( int i = 0 ; i < oneHandCards.length; i++) { |
if (i == 0 ) { |
System.out.print( "你的牌:" ); |
} |
if (i == 3 ) { |
System.out.print( "\t电脑的牌:" ); |
} |
switch (oneHandCards[i] / 100 ) { |
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 (oneHandCards[i] % 100 <= 10 ) { |
System.out.print(oneHandCards[i] % 100 ); |
} else { |
switch (oneHandCards[i] % 100 ) { |
case 11 : |
System.out.print( "J" ); |
break ; |
case 12 : |
System.out.print( "Q" ); |
break ; |
case 13 : |
System.out.print( "K" ); |
break ; |
case 14 : |
System.out.print( "A" ); |
break ; |
} |
} |
System.out.print( " " ); |
} |
System.out.println(); |
} |
public static int compare() { |
int [] aCards = new int [ 3 ]; |
int [] bCards = new int [ 3 ]; |
for ( int i = 0 ; i < 3 ; i++) { |
aCards[i] = twoHandsCards[i]; |
} |
for ( int i = 3 ; i < 6 ; i++) { |
bCards[i - 3 ] = twoHandsCards[i]; |
} |
return fromCardsToNumber(aCards) - fromCardsToNumber(bCards); |
} |
public static int fromCardsToNumber( int [] oneHandCards) { |
for ( int i = 0 ; i < oneHandCards.length - 1 ; i++) { |
for ( int j = i + 1 ; j < oneHandCards.length; j++) { |
if (oneHandCards[i] % 100 < oneHandCards[j] % 100 ) { |
int temp = oneHandCards[i]; |
oneHandCards[i] = oneHandCards[j]; |
oneHandCards[j] = temp; |
} |
} |
} |
int num = 0 ; |
if (oneHandCards[ 0 ] % 100 == oneHandCards[ 1 ] % 100 |
&& oneHandCards[ 1 ] % 100 == oneHandCards[ 2 ] % 100 ) { |
num = 6 * 1000000 + oneHandCards[ 0 ] % 100 * 10000 + oneHandCards[ 1 ] |
% 100 * 100 + oneHandCards[ 2 ] % 100 ; |
} else if (oneHandCards[ 0 ] / 100 == oneHandCards[ 1 ] / 100 |
&& oneHandCards[ 1 ] / 100 == oneHandCards[ 2 ] / 100 |
&& oneHandCards[ 0 ] % 100 == oneHandCards[ 1 ] % 100 + 1 |
&& oneHandCards[ 1 ] % 100 == oneHandCards[ 2 ] % 100 + 1 ) { |
num = 5 * 1000000 + oneHandCards[ 0 ] % 100 * 10000 + oneHandCards[ 1 ] |
% 100 * 100 + oneHandCards[ 2 ] % 100 ; |
} else if (oneHandCards[ 0 ] / 100 == oneHandCards[ 1 ] / 100 |
&& oneHandCards[ 1 ] / 100 == oneHandCards[ 2 ] / 100 ) { |
num = 4 * 1000000 + oneHandCards[ 0 ] % 100 * 10000 + oneHandCards[ 1 ] |
% 100 * 100 + oneHandCards[ 2 ] % 100 ; |
} else if (oneHandCards[ 0 ] % 100 == oneHandCards[ 1 ] % 100 + 1 |
&& oneHandCards[ 1 ] % 100 == oneHandCards[ 2 ] % 100 + 1 ) { |
num = 3 * 1000000 + oneHandCards[ 0 ] % 100 * 10000 + oneHandCards[ 1 ] |
% 100 * 100 + oneHandCards[ 2 ] % 100 ; |
} else if (oneHandCards[ 0 ] % 100 == oneHandCards[ 1 ] % 100 ) { |
num = 2 * 1000000 + oneHandCards[ 0 ] % 100 * 10000 + oneHandCards[ 1 ] |
% 100 * 100 + oneHandCards[ 2 ] % 100 ; |
} else if (oneHandCards[ 1 ] % 100 == oneHandCards[ 2 ] % 100 ) { |
num = 2 * 1000000 + oneHandCards[ 1 ] % 100 * 10000 + oneHandCards[ 2 ] |
% 100 * 100 + oneHandCards[ 0 ] % 100 ; |
} else { |
num = 1 * 1000000 + oneHandCards[ 0 ] % 100 * 10000 + oneHandCards[ 1 ] |
% 100 * 100 + oneHandCards[ 2 ] % 100 ; |
} |
return num; |
} |
} |
高级设计师
by: 神马 发表于:2012-10-03 20:50:56 顶(0) | 踩(0) 回复
电脑已经输光老本了,你行行好吧!

回复评论