java封装扑克牌代码 java封装扑克牌代码是多少
JAVA扑克牌编程
1.定义扑克的对象 有两个参数a。花色,b数字Poke{ int number, String color, Poker(String color,int number) { this.color=color; this.number=number; }} 2.每一张牌都实例化一个扑克对象。 poke1= new Poke("红桃","10");poke2=new Poke("黑桃","9"); 3.定义游戏规则类,类里定义一个方法,用来比较大小。public comparePoke(Poke poke1,Poke poke2){ if(花色比较){ return 结果; } if(数字比较){ return 结果; }}。调用方法就能比较
让客户满意是我们工作的目标,不断超越客户的期望值来自于我们对这个行业的热爱。我们立志把好的技术通过有效、简单的方式提供给客户,将通过不懈努力成为客户在信息化领域值得信任、有价值的长期合作伙伴,公司提供的服务项目有:空间域名、雅安服务器托管、营销软件、网站建设、陕州网站维护、网站推广。
JAVA 扑克牌
package justforjoke.pkGame;
//花色
public class CardsType {
private static final String [] ct={
"黑桃",
"红桃",
"梅花",
"方块"
};
public static String getType(int i){
return ct[i];
}
public static int com(String s,String c){
int s1=find(s);
int s2=find(c);
if(s1==-1)return 100;
if(s2==-1)return -100;
return s2-s1;
}
private static int find(String s){
int i=-1;
for(String st:ct ){
i++;
if(st.equals(s))return i;
}
return i;
}
}
package justforjoke.pkGame;
//牌码
public class Num {
private static final String[]n={
"0","2","3","4","5","6","7","8","9","10","J","Q","K","A"
};
public static String getN(int i){
if(i1||i13)return null;
return n[i];
}
}
package justforjoke.pkGame;
//每张牌
public class Cards implements Comparable{
private String type;
private int num;
public Cards(){}
public Cards(String s,int n){
this.type=s;
this.num=n;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public boolean equals(Object o){
if(!(o instanceof Cards))return false;
Cards c=(Cards)o;
if(this.num==c.getNum()this.type.equals(c.getType()))return true;
return false;
}
public int compareTo(Object o) {
if(!(o instanceof Cards))return 1;
Cards c=(Cards)o;
int bjjg;
if((bjjg=CardsType.com(this.type,c.getType()))!=0)return bjjg;
return c.getNum()-this.num;
}
public String toString(){
return type+" : "+Num.getN(num);
}
}
package justforjoke.pkGame;
//一副新扑克
public class PkC {
public static Cards[] getNewCards(){
Cards[] nc=new Cards[52];
int k=0;
for(int i=0;i4;i++){
String t=CardsType.getType(i);
for(int j=1;j14;j++){
nc[k++]=new Cards(t,j);
}
}
return nc;
}
}
package justforjoke.pkGame;
import java.util.Arrays;
import java.util.Random;
//游戏,包含两幅扑克
public class Game {
private Cards[]gc=new Cards[104];
private int[] num=new int[104];
private int length=103;
public Game(){
int i=0;
for(Cards c:PkC.getNewCards()){
gc[i++]=c;
}
for(Cards c:PkC.getNewCards()){
gc[i++]=c;
}
for(int j=0;j104;j++)num[j]=j;
}
//发牌
public Cards[] fp(){
Cards[] pf=new Cards[5];
Random r=new Random();
for(int i=0;i5;i++){
if(length1)break;
int t=r.nextInt(length);
int tem=num[t];
num[t]=num[length];
num[length--]=tem;
pf[i]=gc[tem];
}
return pf;
}
//去掉函数中的注释会打印游戏的中间过程
public static String cp(Cards[] c){
String result1="同花";
String result2="顺子";
String result3="";
for(int i=0;i4;i++){
if(!c[i].getType().equals(c[i+1].getType()))result1="";
if(c[i+1].getNum()-c[i].getNum()!=1)result2="";
if(c[i].equals(c[i+1]))result3="对子";
//System.out.print(c[i]+" || ");
}
//System.out.println(c[4]);
return result1+result2+result3;
}
public static void main(String []args){
Game g=new Game();
while(true){
Cards[] pf=g.fp();
if(pf[4]==null){
System.out.println("游戏失败!!");
break;
}
Arrays.sort(pf);
String r=Game.cp(pf);
if(!r.equals("")){
System.out.println(r);
for(Cards cs:pf)System.out.print(cs+" || ");
break;
}
}
}
}
····上面有5个类。Came类就是用来游戏的。它会打印游戏结果。
题目所要求的都解决了,不过有一点,那就是排序时又大到小排的。
这个在写代码的时候没有注意到。
代码只会打印游戏的结果,如果你想打印其他的可以在代码中添加。也可以去掉一些注释查看中间过程
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的问题(扑克牌)
代码仅供参考,如有疑问,欢迎追问:
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 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封装扑克牌代码 java封装扑克牌代码是多少
链接地址:http://scjbc.cn/article/doseooo.html