
import java.util.Scanner
public class Poker {
private static final int NUM = 1
private static int SUM = 52
private static final String P = "A,2,3,4,5,6,7,8,9,10,J,Q,K"
private static String[][] POKER = {
P.replaceAll("([^,]+)", "黑桃$1").split(","),
P.replaceAll("([^,]+)", "红桃$1").split(","),
P.replaceAll("([^,]+)", "梅花$1").split(","),
P.replaceAll("([^,]+)", "方片$1").split(",")
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System. in )
int count = NUM
String[] hand = new String[0]
System.out.println("随机发 " + count + " 张牌")
SUM -= count
for (int i = 0 i < count i++) {
int row = (int)(Math.random() * POKER.length)
int col = (int)(Math.random() * POKER[row].length)
String[] css = new String[hand.length + 1]
System.arraycopy(hand, 0, css, 0, hand.length)
css[css.length - 1] = POKER[row][col]
hand = css
}
System.out.println("然后看手里的牌:")
System.out.println(Arrays.toString(hand))
scanner.close()
}
}
package zhidaoimport java.util.Arrays
import java.util.Collections
import java.util.Comparator
import java.util.Scanner
/**
* 发牌游戏
*/
public class PokerGame
{
/**
* 共有1幅扑克牌,不包括大王和小王共52张牌。
*/
public static final int POKER_NUMBER = 52
/**
* 特殊分隔符 _
*/
private static final String T = "_"
/**
* 52张牌<br />
* 顺序为黑桃、红心、草花、方块
*/
public static final String[] POKERS =
{
"A" + T + "0", "A" + T + "1", "A" + T + "2", "A" + T + "3",
"2" + T + "0", "2" + T + "1", "2" + T + "2", "2" + T + "3",
"3" + T + "0", "3" + T + "1", "3" + T + "2", "3" + T + "3",
"4" + T + "0", "4" + T + "1", "4" + T + "2", "4" + T + "3",
"5" + T + "0", "5" + T + "1", "5" + T + "2", "5" + T + "3",
"6" + T + "0", "6" + T + "1", "6" + T + "2", "6" + T + "3",
"7" + T + "0", "7" + T + "1", "7" + T + "2", "7" + T + "3",
"8" + T + "0", "8" + T + "1", "8" + T + "2", "8" + T + "3",
"9" + T + "0", "9" + T + "1", "9" + T + "2", "9" + T + "3",
"10" + T + "0", "10" + T + "1", "10" + T + "2", "10" + T + "3",
"J" + T + "0", "J" + T + "1", "J" + T + "2", "J" + T + "3",
"Q" + T + "0", "Q" + T + "1", "Q" + T + "2", "Q" + T + "3",
"K" + T + "0", "K" + T + "1", "K" + T + "2", "K" + T + "3"
}
/**
* 原始的扑克牌
*/
public static final String[] ORIAL_POKERS = POKERS.clone ()
/**
* 发到哪张牌 0
*/
public static int currentIndex = 0
/**
* 四种花色
*/
public static final String POKER_COLOR = "黑桃" + T + "红心" + T + "草花" + T + "方块"
/**
* 发牌
* @param n int 一共几个人玩牌
* @param results 储存发牌结果的数组
*/
public static void drawPoker (int n, String[][] results)
{
int m = n - 1
if (n == 0)
{
return
}
int number = (POKER_NUMBER - currentIndex) / n
while (number > 0)
{
String current = POKERS[currentIndex++]
String pk = current.split ("\\" + T)[0].trim ()
int col = Integer.parseInt (current.split ("\\" + T )[1].trim ())
if (null == results[m][col])
{
results[m][col] = pk + " "
}
else
{
results[m][col] += pk + " "
}
number--
}
for ( int i = 0 i < results.length i++ )
{
for ( int j = 0 j < results[i].length j++ )
{
if (null == results[i][j])
{
results[i][j] = ""
}
}
}
drawPoker (--n, results)
}
/**
* 给牌排序
* @param orialPokers String 原始扑克牌
* @param temp String[] 每张花色的扑克牌数组
*/
private static void sortPokers ( final String orialPokers, String[] temp )
{
Arrays.sort (temp, new Comparator<String> ()
{
@Override
public int compare ( String o1, String o2 )
{
int index1 = orialPokers.indexOf (o1 + T)
int index2 = orialPokers.indexOf (o2 + T)
if (index1 > index2)
{
return -1
}
else if (index1 < index2)
{
return 1
}
else
{
return 0
}
}
})
}
/**
* 洗牌
*/
private static void shufflePokers ( String[] POKERS )
{
Collections.shuffle (Arrays.asList (POKERS))
}
public static void main ( String[] args )
{
System.out.print ("输入人数:")
Scanner scanner = new Scanner (System.in)
int n = scanner.nextInt ()
scanner.close ()
String[] pColors = POKER_COLOR.split ("\\" + T )
String[][] results = new String[n][pColors.length]
shufflePokers (POKERS)
drawPoker (n, results)
final String poker = Arrays.toString (ORIAL_POKERS)
System.out.println ("\n输出如下:\n")
for (int i = 0 i < results.length i++)
{
System.out.println ("第" + ( i + 1 ) + "个人:")
for ( int j = 0 j < results[i].length j++ )
{
String[] temp = results[i][j].trim ().split ("\\s+")
sortPokers (poker, temp)
System.out.println (pColors[j] + ":" + Arrays.toString (temp).replaceAll ("[\\,\\[\\]]", ""))
}
System.out.println ()
}
}
}
存储时可以用数字,最后显示时可以装换为字母import java.util.Random
public class Poker {
public static void main(String[] args) {
Random random = new Random()
int[] computer = new int[5]
int[] player = new int[5]
for (int i = 0i <5i++) {// 发牌
computer[i] = random.nextInt(13) + 2// 2到14的随机数,14表示A
player[i] = random.nextInt(13) + 2// 2到14的随机数,14表示A
}
int result = Judge(player, computer)
Show(player, computer, result)
}
// 比较
private static int Judge(int[] player, int[] computer) {
int p = 0, c = 0
for (int i = 0i <5i++) {
if (14 == player[i])
p++
if (14 == computer[i])
c++
}
if (p >0 &&0 == c)
return 1
if (p == 0 &&c >0)
return 2
return 0
}
private static void Show(int[] player, int[] computer, int result) {
System.out.print("Your cards are:")
for (int i = 0i <5i++) {
PrintCard(player[i])
}
System.out.print("\nThe computer's cards are:")
for (int i = 0i <5i++) {
PrintCard(computer[i])
}
switch (result) {
case 0:
System.out.print("\nYou and the computer are tied for the highest single card.")
break
case 1:
System.out.print("\nYou has the highest single card.")
break
case 2:
System.out.print("\nThe computer has the highest single card.")
break
}
}
private static void PrintCard(int card) {
switch (card) {
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
default:
System.out.print(card + " ")
}
}
void m() {
int[] personCard = new int[5]
int[] computerCard = new int[5]
int personCount = 0
int computerCount = 0
System.out.print("Your cards are: ")
for (int i = 0i <5i++) {
personCard[i] = (int) (Math.random() * 13) + 2
if (personCard[i] == 11) {
System.out.print("J" + "\t")
} else if (personCard[i] == 12) {
System.out.print("Q" + "\t")
} else if (personCard[i] == 13) {
System.out.print("K" + "\t")
} else if (personCard[i] == 14) {
personCount++
System.out.print("A" + "\t")
} else {
System.out.print(personCard[i] + "\t")
}
}
System.out.println("\n")
System.out.print("The computer's cards are:")
for (int i = 0i <5i++) {
computerCard[i] = (int) (Math.random() * 13) + 1
if (computerCard[i] == 11) {
System.out.print("J" + "\t")
} else if (computerCard[i] == 12) {
System.out.print("Q" + "\t")
} else if (computerCard[i] == 13) {
System.out.print("K" + "\t")
} else if (computerCard[i] == 14) {
computerCount++
System.out.print("A" + "\t")
} else {
System.out.print(computerCard[i] + "\t")
}
}
System.out.println("\n")
if (personCount >0 &&computerCount == 0) {
System.out.println("You has the highest single card")
} else if (personCount == 0 &&computerCount >0) {
System.out.println("The computer has the highest single card")
} else if (personCount == 0 &&computerCount == 0) {
System.out.println("You and the computer are tied for the highest single card")
} else if (personCount >0 &&computerCount >0) {
System.out.println("Both you and the computer have the highest single card")
}
}
}
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)