本文目录一览:
- 1、JAVA斗地主如何实现发牌和出牌功能
- 2、java 52张 扑克牌 问题
- 3、扑克牌实现洗牌发牌,显示各家手上的牌并统计各家的牌中有几组对子,几组三条(例如三个A),几组同花
- 4、java52张扑克牌问题
- 5、用java程序编写一个发牌的程序。。。跪求了。。。。
JAVA斗地主如何实现发牌和出牌功能
用随机的方式生成54张牌(即数字或者其他方式),按顺时针方向,依次给每个人一个数字。直到剩最后三张。
出牌:先判断出牌方选中的牌是否符合规则。是的话。则该人的数值列表中去掉相应的牌。桌面上显示它出示的牌。出完牌还得判断游戏是否结束。如果他的牌下完了。游戏就结束了
java 52张 扑克牌 问题
识别类:
一张牌:public class Card{};
一副牌:public class Poker{};
带main方法的类用于实现洗牌和切牌:public class Shuffle{};
识别属性:
class Card:
一张牌应该具有花色和点数这两个属性:
private color; //花色
private value; //点数
class Poker:
一副牌有54张牌,四种花色,每种花色都有2~A的点数:
static Card[] cards=new Card[54];// 每副扑克都有54张牌;
static String[] colors={"红桃","黑桃","方块","梅花"}; //每副牌都有四种花色;
static String values[]={"2","3","4","5","6","7","8","9","10","J","Q","K","A"};//每个花
色的点数
定义类:
public class Card{
String color;
String value;
//定义一个构造函数用于初始化点数2~A的牌
public Card(String color,String value){
this.color = color;
this.value = value;
}
//定义一个构造函数用于初始化大王和小王
public Card(String value){
this.color=""; //如果不给color赋值的话,洗牌和切牌后的大小王前面会显示“null”
this.value = value;
}
// 取一张牌的花色
public String getcolor() {
return color;
}
// 取一张牌的值
public String getvalue() {
return value;
}
}
public class Poker{
static Card[] cards=new Card[54];
static String[] colors={"红桃","黑桃","方块","梅花"};
static String values[]={"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
//构造方法Poker()用于初始化这副扑克
public Poker() {
for (int i = 0; i colors.length; i++) {
for (int j = 0; j values.length; j++) {
cards[i * 13 + j] = new Card(colors[i], values[j]);
}
}
cards[52] = new Card("小王");
cards[53] = new Card("大王");
}
//方法getCard()用于获取一张牌的花色和点数
public Card[] getCard() {
return Poker.cards;
}
//方法Show()用于显示一副新的扑克
public void Show() {
……………………
}
public class Shuffle {
static Poker poker = new Poker();
static Card[] cards = new Card[54];
//方法random用于返回一个不重复的数组
public static int[] random(int length) {
int[] a = new int[length];
for (int i = 0; i length; i++) {
a[i] = (int) (Math.random() * length);//产生一个0到length的随机数
for (int j = 0; j i; j++) {
if (a[j] == a[i]) {//比较是否相等,如果相等则重新产生
i--;
break;
}
}
}
return a;
}
//方法xipai()用于将所得的随机数组的值赋值给牌的下标
public static void xipai() {
int[] b = new int[54];
b = random(54);//调用random方法产生一个长度为54的不重复的随机数组
cards = poker.getCard();
Card temp = null;
for(int i = 0; i b.length; i++){ //开始遍历数组实现重新排列
int k = b[i];
temp = cards[k];
cards[i] = cards[k];
cards[k] = temp;}
//输出洗牌后的扑克
……………………
……………………
}
//方法qiepai()实现扑克的切牌操作
public static void qiepai(int length) {
int number = (int) (Math.random() * length);//产生一个随机数
System.out.println("随机数为:" + number);//显示随机数是多少
Card[] temp = new Card[length];
int start = number;//设置切牌后的起点位置
for (int i = 0; i length - number; i++) {
temp[i] = cards[start++];//将切点后面的牌暂时放入temp数组中
}
for (int j = 0; j number; j++) {
temp[length - number + j] = cards[j];//将切点前面的牌加入temp数组中
}
for (int k = 0; k length; k++) {
cards[k] = temp[k];}
//输出切牌后的扑克
……………………
……………………
}
//main方法
public static void main(String[] args) {
Poker newpoker = new Poker();
newpoker.Show();//调用Show()方法显示一副新扑克
xipai();//调用xipai()方法显示洗牌过后的扑克
qiepai(54);//调用qiepai()方法显示切牌以后的扑克
}
}
参考资料:
扑克牌实现洗牌发牌,显示各家手上的牌并统计各家的牌中有几组对子,几组三条(例如三个A),几组同花
1500行。。。。搞笑。200行左右。
这个对对子的处理,存在一个问题,就是有一个三条的存在,那么该算成是几对呢?
按排列算法的话,是一共有三对,在这里只算出了两对,想要算出三对,会麻烦一些。
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Random;
public class Cards {
public static void main(String[] args) {
ListCard list = new ArrayListCard();
for (int i = 0; i 4; i++) {
for (int j = 1; j = 13; j++) {
Card card = new Card();
card.type = i;
card.serial = j;
card.execute();
list.add(card);
}
}
//
Card[] west = licensing(list, 13);
Card[] east = licensing(list, 13);
Card[] north = licensing(list, 13);
Card[] south = licensing(list, 13);
System.out.println("玩家[west]手中的牌是" + Arrays.toString(west));
System.out.println("玩家[east]手中的牌是" + Arrays.toString(east));
System.out.println("玩家[north]手中的牌是" + Arrays.toString(north));
System.out.println("玩家[south]手中的牌是" + Arrays.toString(south));
assort(west, "west");
assort(east, "east");
assort(north, "north");
assort(south, "south");
}
// 发牌,随机取出,相当于洗牌
public static Card[] licensing(ListCard list, int len) {
Random random = new Random();
Card[] cards = new Card[len];
int cardIndex = 0;
for (int i = 0; i len; i++) {
int index = random.nextInt(list.size());
cards[cardIndex++] = list.remove(index);
}
// 排序
Arrays.sort(cards, new ComparatorCard() {
@Override
public int compare(Card o1, Card o2) {
if (o1.type != o2.type) {
return o1.type - o2.type;
}
return o1.serial - o2.serial;
}
});
return cards;
}
public static void assort(Card[] cards, String username) {
System.out.print("玩家[" + username + "]");
ListCard[] towList = new ArrayListCard[]();
ListCard[] threeList = new ArrayListCard[]();
Card[] tow = null;
Card[] three = null;
for (int i = 0; i cards.length - 1; i++) {
tow = new Card[2];
three = new Card[3];
tow[0] = cards[i];
three[0] = cards[i];
for (int j = i + 1; j cards.length; j++) {
if (cards[i].type == cards[j].type) {
continue;
}
if (cards[i].serial == cards[j].serial) {
tow[1] = cards[j];
towList.add(tow);
three[1] = cards[j];
for (int k = 0; k cards.length; k++) {
if (three[0].type == cards[k].type
|| three[1].type == cards[k].type) {
continue;
}
if (three[1].serial == cards[k].serial) {
three[2] = cards[k];
threeList.add(three);
}
}
break;
}
}
}
ListCard[] flower = new ArrayListCard[]();
// 三张连续的即为同花
for (int i = 0; i cards.length - 2; i++) {
three = new Card[3];
three[0] = cards[i];
three[1] = cards[i + 1];
three[2] = cards[i + 2];
if (three[0].type != three[1].type
|| three[2].type != three[1].type) {
continue;
}
if (three[1].serial - three[0].serial != 1
|| three[2].serial - three[1].serial != 1) {
continue;
}
flower.add(three);
}
System.out.println();
System.out.print("\t有对子" + towList.size() + "组");
outList(towList);
System.out.println();
System.out.print("\t有三条" + threeList.size() + "组");
outList(threeList);
System.out.println();
System.out.print("\t有同花" + threeList.size() + "组");
outList(flower);
System.out.println();
}
public static void outList(ListCard[] list) {
for (int i = 0; i list.size(); i++) {
Card[] cards = list.get(i);
System.out.print(Arrays.toString(cards));
}
}
}
class Card {
String[] types = { "黑桃", "红桃", "梅花", "方块" };
int type = 0;
int serial = 0;
String name = null;
public void execute() {
name = types[type];
switch (serial) {
case 1:
name += "A";
break;
case 11:
name += "J";
break;
case 12:
name += "Q";
break;
case 13:
name += "K";
break;
default:
name += serial;
break;
}
}
public String toString() {
return name;
}
}
java52张扑克牌问题
使用JAVA编写以下程序:
1、请定义一个名为Card的扑克牌类,该类有两个private访问权限的字符串变量face和suit;分别描述一张牌的牌面值(如:A、K、Q、J、10、9、……3、2等)和花色(如:“黑桃”、“红桃”、“梅花”、“方块”);定义Card类中的public访问权限的构造方法,为类中的变量赋值;定义protected访问权限的方法getFace(),得到扑克牌的牌面值;定义protected访问权限的方法getSuit(),得到扑克牌的花色;定义方法toString(),返回表示扑克牌的花色和牌面值字符串(如“红桃A”、“梅花10”等)。
2、若应用程序的main方法中,定义字符串数组f和s;分别表示扑克牌的牌面值和花色;定义52个元素的Card类型数组deck,用来存放4个花色的52张牌。如下所示。
String f[] = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
String s[] = {"黑桃","红桃","梅花","方块"};
Card deck = new Card[52];
(1)使用Card类的构造方法给deck数组的52张牌赋值,要求数组中先存放黑桃花色的A、2、3、……、K;然后是红桃花色的A、2、3、……、K;梅花花色的A、2、3、……、K;方块花色的A、2、3、……、K。请写出实现上述功能的程序段。
(2)请编写模拟洗牌的完整程序,即把数组deck中的扑克牌随机打乱存放顺序。
原题如下:
引用内容
private String face; // A, K, Q, J, 10, ... 2
private String suit; // Spade, Heart, Club, Diamond
public Card(String suit, String face) {
this.face = face;
this.suit = suit;
}
protected String getFace() {
return face;
}
protected String getSuit() {
return suit;
}
public String toString() {
return suit + " " + face;
}
解题,完成程序解:
程序代码
package poker;
public class Card {
private String face; // A, K, Q, J, 10, ... 2
private String suit; // Spade, Heart, Club, Diamond
public Card(String suit, String face) {
this.face = face;
this.suit = suit;
}
protected String getFace() {
return face;
}
protected String getSuit() {
return suit;
}
public String toString() {
return suit + " " + face;
}
public static void shuffle(Card[] deck, int startIndex, int size,
int splitIndex) { //shuffle洗牌
System.out.println("startIndex:" + startIndex);
if (splitIndex * 2 size) { //if rand is 30
Card.swap(deck, startIndex, splitIndex, size - splitIndex); //[52]、0、30、22
shuffle(deck, size - splitIndex, splitIndex, size - splitIndex); //[52]、22、30、22
} else if (splitIndex * 2 size) { //if rnd is 10
Card.swap(deck, startIndex, size - splitIndex, splitIndex); //[52]、0、42、10
shuffle(deck, startIndex, size - splitIndex, splitIndex); //[52]、0、42、10
} else {
Card.swap(deck, startIndex, splitIndex, splitIndex);
}
}
public static void swap(Card[] deck, int srcIndex, int dstIndex, int size) { //交换
//SRC是在本位置显示:source的缩写,源的意思 HREF是点击后连接的目标:HyperlinkReference,超链接引用
String face = "";
String suit = "";
for (int i = 0; i size; i++) {
face = deck[srcIndex + i].face;
suit = deck[srcIndex + i].suit;
deck[srcIndex + i].face = deck[dstIndex + i].face;
deck[srcIndex + i].suit = deck[dstIndex + i].suit;
deck[dstIndex + i].face = face;
deck[dstIndex + i].suit = suit;
}
}
/**
* @param args
*/
public static void main(String[] args) {
//第一问解答
Card[] deck = new Card[52];
String f[] = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J",
"Q", "K"};
String s[] = {"Spade", "Heart", "Club", "Diamond"};
for (int i = 0; i s.length; i++) {
for (int j = 0; j f.length; j++) {
deck[i * 13 + j] = new Card(s[i], f[j]); //依次存入数组之中,每13张一回合
}
}
//第二问解答
int rnd = 0;
int numOfShuffle = 10;
for (int i = 0; i numOfShuffle; i++) {
rnd = (int) Math.abs(Math.random() * 52); //Math.random()返回大于或等于 0.0 但小于 1.0 的伪随机 double 值。
Card.shuffle(deck, 0, deck.length, rnd);
}
// Test
/*
for (int i=0; ideck.length; i++)
{
System.out.println(deck[i]);
} */
}
}
用java程序编写一个发牌的程序。。。跪求了。。。。
1、设计二维数组a[13][4],13代表2,3,4,5,...,K,A , 4代表红黑方梅 13*4=52
2、这样a[0][0]就是红桃2,a[12][3]就是梅花A
按照这么来随机发牌就很简单了,直接随机个一个a[i][j]就可以了。i和j都分别在13和4以内
思路明确了,代码还是自己写比较好