继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

简易扑克牌游戏(没有用集合框架)

qq_只是冬天饮雪水_0
关注TA
已关注
手记 2
粉丝 1
获赞 2

package com.SmallExeDemo;

import java.util.Random;
import java.util.Scanner;
class Demo
{
public static void main(String[] args)
{

    System.out.print("牌堆初始化:");
    DeskOfCard cc = new DeskOfCard();
    cc.initcard();//初始化
    System.out.println();
    cc.shufflecard();//洗牌
    System.out.println("洗牌完毕");

    Scanner input=new Scanner(System.in);
    Player player1=new Player();
    Player player2=new Player();
    System.out.println("输入第一位玩家的id:");
    player1.id=input.nextInt();
    System.out.println("输入第一位玩家的name:");
    player1.name=input.next();
    System.out.println("输入第二位玩家的id:");
    player2.id=input.nextInt();
    System.out.println("输入第二位玩家的name:");
    player2.name=input.next();
    System.out.println("开始发牌:");

    System.out.println("第一位玩家的牌是:");
    System.out.println(cc.card[0]+""+cc.card[2]);

    System.out.println("第二位玩家的牌是:");
    System.out.println(cc.card[1]+""+cc.card[3]);

    System.out.println("进行比较");
    Game g=new Game();
    int i=g.CompareCardWithOther(cc.card[g.CompareCardOwn(cc.card,0)],cc.card[g.CompareCardOwn(cc.card,1)]);
    //System.out.println(i);
    //System.out.print(g.CompareCardOwn(cc.card,0));
   // System.out.print(g.CompareCardOwn(cc.card,1));
    if(i==1)
        System.out.println("第一位玩家胜出!");
    else if(i==-1)
        System.out.println("第二位玩家胜出!");

}

}

class Card
{
public String num;
public String suit;
Card(String n,String s)
{
this.num=n;
this.suit=s;
}
public String toString()
{
String ss=suit+":"+num+" ";
return ss;
}
public String getNum()
{
return num;
}
public String getSuit()
{
return suit;
}

}

class DeskOfCard
{
Card card[];
public void initcard()//初始化
{
String num[]={"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
String suit[]={"黑桃","红桃","梅花","方片"};
card = new Card[52];
for(int i=0;i<52;i++)
{
card[i] = new Card(num[i%13],suit[i/13]);
if(i%13==0)System.out.println();
System.out.print(card[i]);
}
}

public void shufflecard()//洗牌
{
    Random rd = new Random();
    for(int i=0;i<52;i++)
    {
        int j = rd.nextInt(52);//生成随机数
        Card temp = card[i];//交换
        card[i]=card[j];
        card[j]=temp;
    }
}

}
class Game
{
int CompareCardOwn(Card[] c,int i)
{
int a=0;int b=0;int m=0;int n=0;
if(c[i].num.equals("A"))a=1;
if(c[i].num.equals("2"))a=2;
if(c[i].num.equals("3"))a=3;
if(c[i].num.equals("4"))a=4;
if(c[i].num.equals("5"))a=5;
if(c[i].num.equals("6"))a=6;
if(c[i].num.equals("7"))a=7;
if(c[i].num.equals("8"))a=8;
if(c[i].num.equals("9"))a=9;
if(c[i].num.equals("10"))a=10;
if(c[i].num.equals("J"))a=11;
if(c[i].num.equals("Q"))a=12;
if(c[i].num.equals("K"))a=13;
if(c[i+2].num.equals("A"))b=1;
if(c[i+2].num.equals("2"))b=2;
if(c[i+2].num.equals("3"))b=3;
if(c[i+2].num.equals("4"))b=4;
if(c[i+2].num.equals("5"))b=5;
if(c[i+2].num.equals("6"))b=6;
if(c[i+2].num.equals("7"))b=7;
if(c[i+2].num.equals("8"))b=8;
if(c[i+2].num.equals("9"))b=9;
if(c[i+2].num.equals("10"))b=10;
if(c[i+2].num.equals("J"))b=11;
if(c[i+2].num.equals("Q"))b=12;
if(c[i+2].num.equals("K"))b=13;
if(c[i].suit.equals("黑桃"))m=4;
if(c[i].suit.equals("红桃"))m=3;
if(c[i].suit.equals("梅花"))m=2;
if(c[i].suit.equals("方片"))m=1;
if(c[i+2].suit.equals("黑桃"))n=4;
if(c[i+2].suit.equals("红桃"))n=3;
if(c[i+2].suit.equals("梅花"))n=2;
if(c[i+2].suit.equals("方片"))n=1;
if(a>b)
{return i;}
if(a<b)
{return i+2;}
if(a==b)
{
if(m>n)return i;
if(m<n)return i+2;

     }
      return -1111;

}
int CompareCardWithOther(Card c1,Card c2)
{
    int a=0;int b=0;int c=0;int d=0;
    if(c1.num.equals("A"))a=1;
    if(c1.num.equals("2"))a=2;
    if(c1.num.equals("3"))a=3;
    if(c1.num.equals("4"))a=4;
    if(c1.num.equals("5"))a=5;
    if(c1.num.equals("6"))a=6;
    if(c1.num.equals("7"))a=7;
    if(c1.num.equals("8"))a=8;
    if(c1.num.equals("9"))a=9;
    if(c1.num.equals("10"))a=10;
    if(c1.num.equals("J"))a=11;
    if(c1.num.equals("Q"))a=12;
    if(c1.num.equals("K"))a=13;
    if(c2.num.equals("A"))b=1;
    if(c2.num.equals("2"))b=2;
    if(c2.num.equals("3"))b=3;
    if(c2.num.equals("4"))b=4;
    if(c2.num.equals("5"))b=5;
    if(c2.num.equals("6"))b=6;
    if(c2.num.equals("7"))b=7;
    if(c2.num.equals("8"))b=8;
    if(c2.num.equals("9"))b=9;
    if(c2.num.equals("10"))b=10;
    if(c2.num.equals("J"))b=11;
    if(c2.num.equals("Q"))b=12;
    if(c2.num.equals("K"))b=13;
    if(c1.suit.equals("黑桃"))c=4;
    if(c1.suit.equals("红桃"))c=3;
    if(c1.suit.equals("梅花"))c=2;
    if(c1.suit.equals("方片"))c=1;
    if(c2.suit.equals("黑桃"))d=4;
    if(c2.suit.equals("红桃"))d=3;
    if(c2.suit.equals("梅花"))d=2;
    if(c2.suit.equals("方片"))d=1;
    if(a>b)
    {return 1;}
     if(a<b)
        {return -1;}
     if(a==b)
        {
         if(c>d)return 1;
         if(c<d)return -1;
         }

        return 0;

}

}
class Player
{
int id;
String name;
Player()
{}
Player(int id,String name)
{
this.id=id;
this.name=name;
}
void printPlayer()
{
System.out.println("玩家id"+id+"玩家游戏名"+name);
}

}

打开App,阅读手记
1人推荐
发表评论
随时随地看视频慕课网APP