本文目录一览:
- 1、java编写扑克牌发牌程序,牌正面的图片无法显示,求高手解答!!
- 2、java的问题(扑克牌)
- 3、java写一个扑克牌程序
- 4、用java ArrayList集合编写扑克牌洗牌与发牌
- 5、java 52张 扑克牌 问题
- 6、java编写扑克牌游戏里,洗牌时发牌时用到了随机分配的功能,请问在那个包中哪个类哪个方法?
java编写扑克牌发牌程序,牌正面的图片无法显示,求高手解答!!
不支持颜色转换的要求 ImageIO兼容的图片格式。JPG也分很多种的。
bmp gif jpeg jpeg-lossless jpeg2000 png pnm raw tiff wbmp
jpg的分类多 建议把图片都改成bmp
java的问题(扑克牌)
代码仅供参考,如有疑问,欢迎追问:
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/**
* 随机扑克牌
* @author STONE
* @version 2015-10-28
*/
public class Poker {
//用于记录丢失的手牌数
private static Integer lostTotal = 0;
//用于存放已下发的扑克
private static Integer[] its1 = new Integer[54];
//用于存放当前的扑克
private static Integer[] its2 = new Integer[54];
private static ListInteger lostList = new ArrayListInteger();
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
for(int i=0;i54;i++){
its1[i] = 0;
its2[i] = 0;
}
System.out.println("系统开始发牌...");
givePoker(5);
System.out.println("发牌完毕,当前手牌为:");
printPoker(its2);
Integer gameTotal = 0;
while(gameTotal10){//循环跑10次,请注意此处发牌没做限制,如果循环次数过大,会导致死循环
System.out.println("请输入需要丢弃的手牌(多张牌格式如下:黑桃1,黑桃2,黑桃3):");
String lost = scn.nextLine();
checkLost(lost);
System.out.println("丢失手牌完毕,当前手牌:");
printPoker(its2);
System.out.println("系统开始重发手牌...");
givePoker(lostTotal);
System.out.println("重发发牌完毕,当前手牌为:");
printPoker(its2);
gameTotal++;
}
scn.close();
}
public static void givePoker(Integer num){//随机分发手牌
int total = 0;
while(totalnum){
int poke = (int)(Math.random()*54);
if(its1[poke] == 0){
its1[poke] = 1;
its2[poke] = 1;
total++;
}
}
lostTotal = 0;
}
public static Boolean checkLost(String lost){//检查需要丢失的手牌
if(lost==null || lost.equals("")){
System.out.println("输入手牌错误,系统无法处理...");
return false;
}
if(lost.indexOf("黑桃")0 lost.indexOf("红心")0 lost.indexOf("梅花")0 lost.indexOf("方块")0){
System.out.println("输入手牌错误,系统无法处理...");
return false;
}
String[] strs = lost.split(",");
Boolean bol = false;
for(String str : strs){
str = str.trim();
if(str==null || str.equals("")) continue;//去掉,防止空格
bol = getPokerNum(str);
}
if(bol){
losePoker();
}
return false;
}
public static void losePoker(){//丢掉手牌
lostTotal = lostList.size();
for(Integer itr : lostList){//丢掉手牌
its2[itr-1] = 0;
}
lostList = new ArrayListInteger();
}
public static Boolean getPokerNum(String str){//获取手牌点数并去掉
try{
Integer itr = 0;
if(str.indexOf("黑桃")==0){
str = str.replace("黑桃", "");
itr = Integer.parseInt(str);
if(itr13){
System.out.println("输入手牌不存在:黑桃"+str);
return false;
}
if(its2[itr-1]==1){
lostList.add(itr);
}
} else if(str.indexOf("红心")==0){
str = str.replace("红心", "");
itr = Integer.parseInt(str);
if(itr13){
System.out.println("输入手牌不存在:红心"+str);
return false;
}
if(its2[itr+12]==1){
lostList.add(itr+13);
}
} else if(str.indexOf("梅花")==0){
str = str.replace("梅花", "");
itr = Integer.parseInt(str);
if(itr13){
System.out.println("输入手牌不存在:梅花"+str);
return false;
}
if(its2[itr+25]==1){
lostList.add(itr+26);
}
} else if(str.indexOf("方块")==0){
str = str.replace("方块", "");
itr = Integer.parseInt(str);
if(itr13){
System.out.println("输入手牌不存在:方块"+str);
return false;
}
if(its2[itr+38]==1){
lostList.add(itr+39);
}
} else if(str.indexOf("小王")==0){
if(its2[52]==1){
lostList.add(53);
}
} else if(str.indexOf("大王")==0){
if(its2[53]==1){
lostList.add(54);
}
}
return true;
} catch( Exception e ){
System.out.println("输入手牌有误...");
return false;
}
}
public static void printPoker(Integer[] its){//打印当前手牌
String result = "";
for(int i=0;iits.length;i++){
if(its[i]==1){//等于1表示当前手牌存在
result+=pukerCheck(i+1)+",";
}
}
System.out.println(result);
}
public static String pukerCheck(Integer itr){//判断扑克类型
/**
* 1~13黑桃花色、14~26红心花色、27~39梅花花色
* 40~52方块花色、53小王、54大王
*/
if(1=itr itr=13){
return "黑桃"+itr;
} else if(14=itr itr=26){
return "红心"+(itr-13);
} else if(27=itr itr=39){
return "梅花"+(itr-26);
} else if(40=itr itr=52){
return "方块"+(itr-39);
} else if(itr==53){
return "小王";
} else if(itr==54){
return "大王";
}
return "";
}
}
测试运行结果:
java写一个扑克牌程序
使用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 ArrayList集合编写扑克牌洗牌与发牌
import java.util.ArrayList;
import java.util.Random;
public class PokerDemo {
ArrayListCard poker = new ArrayListCard();
Player[] players;
int n = 4;
String[] name = {"Wang", "Zhang", "Li", "Yuan"};
public PokerDemo() {
players = new Player[n];
for(int i = 0; i players.length; i++)
players[i] = new Player(name[i]);
for(int i = 0; i 4; i++)
for(int j = 0; j 13; j++)
poker.add(new Card(i, j));
System.out.println("洗牌前: " + poker);
xipai();
System.out.println("洗牌后: " + poker);
fapai();
System.out.println("发牌后: ");
for(int i = 0; i players.length; i++)
System.out.println(players[i]);
}
public void xipai() {
Random r = new Random();
ArrayListCard tmp = new ArrayListCard();
while(!poker.isEmpty()) {
int index = r.nextInt(poker.size());
Card card = poker.get(index);
tmp.add(card);
poker.remove(index);
}
poker = new ArrayListCard(tmp);
}
public void fapai() {
int p = 0;
while(!poker.isEmpty()) {
Card card = poker.get(0);
players[p].add(card);
p = (p + 1) % n;
poker.remove(0);
}
}
public static void main(String[] args) {
new PokerDemo();
}
}
class Card {
int val, sty;
final String[] value = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
final String[] style = {"黑桃", "红心", "梅花", "方块"};
public Card(int s, int v) {
sty = s;
val = v;
}
public String toString() {
return style[sty]+ " " + value[val];
}
}
class Player {
ArrayListCard cards = new ArrayListCard();
String name;
public Player(String n) {
name = n;
}
public void add(Card card) {
cards.add(card);
}
public String toString() {
return name + ":\t" + cards;
}
}
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()方法显示切牌以后的扑克
}
}
参考资料:
java编写扑克牌游戏里,洗牌时发牌时用到了随机分配的功能,请问在那个包中哪个类哪个方法?
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
/**
* Author:danagao
* java 发牌程序,
* 不含大王,小王
* 可设定发牌人数
*/
public class Pai {
HashMapString, String map = new HashMapString, String();
private static final String[] colors = {"红桃", "方块", "梅花", "黑桃"};
private static final String[] values = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"};
private String[] newpai; private int len;
//初始化52张牌
public Pai() {
len = colors.length * values.length;
newpai = new String[len];
int k = 0;
for (int i = 0; i colors.length; i++) {
for (int j = 0; j values.length; j++) {
newpai[k] = colors[i] + values[j];
k++;
}
}
}
//洗牌(把牌打乱)
private void getNums() {
Random r = new Random();
int i = r.nextInt(len);
String s;
if (i = 0 !map.containsKey(String.valueOf(i))) {
s = String.valueOf(i);
map.put(s, newpai[i]);
} else {
getNums();
}
}
//发牌
public void showResult(int p) {
for (int i = 0; i len; i++) {
getNums();
}
int l = len/p; //每人发几张牌
int j=1;//计人数
int k=0;//计牌数
for (Map.EntryString, String entry : map.entrySet()) {
if(k%l==0 j=p){
System.out.println("第"+j+"个人的牌:");
j++;
} else if(len-k = len%p){
System.out.println("剩余的牌:");
}
k++;
System.out.println(entry.getValue());
} }
public static void main(String[] args) {
DbColorBall dbColorBall = new DbColorBall();
dbColorBall.showResult(4);
}
}