手记

入门Java简易的扑克牌

创建 玩家
package com.pukepai;

public class Man {
private Integer id;
private String name;

public Man(Integer id,String name) {
    this.id=id;
    this.name=name;
}

public Integer getId() {
    return id;
}
public void setId(Integer id) {
    this.id = id;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}

}

package com.pukepai;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.Random;

public class PuKe {
public static void main(String[] args) {
System.out.println("--------------------创建扑克牌--------------------");
/*

  • 利用集合来创建扑克牌 文字和数字分开
    /
    List<String> Wenzi = new ArrayList<>();
    Wenzi.add("方块");Wenzi.add("梅花");Wenzi.add("红桃");Wenzi.add("黑桃");
    List<String> Shuzi = new ArrayList<>();
    Shuzi.add("1");Shuzi.add("2");Shuzi.add("3");Shuzi.add("4");Shuzi.add("5");Shuzi.add("6");Shuzi.add("7");
    Shuzi.add("8");Shuzi.add("9");Shuzi.add("10");Shuzi.add("J");Shuzi.add("Q");Shuzi.add("K");Shuzi.add("A");
    System.out.println("------------------扑克创建成功!-------------------");
    System.out.println("扑克牌为:");
    /
  • 打印出所有的扑克牌
    /
    for(int i=0;i<4;i++)
    {
    for(int j=1;j<14;j++)
    {
    System.out.print(Wenzi.get(i));
    System.out.print(Shuzi.get(j));
    }
    System.out.println();
    }
    System.out.println("\n----------------------开始洗牌--------------------");
    System.out.println("\n----------------------洗牌结束--------------------");
    System.out.println("\n----------------------创建玩家--------------------");
    System.out.println("请输入第一位玩家的ID&&姓名:\n请输入ID:");
    /

    • 获取用户输入
      */
      Scanner oneId=new Scanner(System.in);
      Integer oneid=oneId.nextInt();
      Scanner oneName = new Scanner(System.in);
      System.out.println("请输入玩家一姓名:");
      String onename = oneName.nextLine();
      System.out.println("请输入第二位玩家的ID&&姓名:\n请输入ID:");
      Scanner twoId=new Scanner(System.in);
      int twoid=twoId.nextInt();
      Scanner twoName = new Scanner(System.in);
      System.out.println("请输入玩家二姓名:");
      String twoname = twoName.nextLine();
      Man one = new Man(oneid,onename);
      Man two = new Man(twoid,twoname);
      System.out.println("-------欢迎玩家:"+one.getName()+"\n-------欢迎玩家:"+two.getName());
      System.out.println("--------------------开始发牌--------------------");
      System.out.println("-------玩家:"+one.getName()+"-拿牌");
      System.out.println("-------玩家:"+two.getName()+"-拿牌");
      System.out.println("-------玩家:"+one.getName()+"-拿牌");
      System.out.println("-------玩家:"+two.getName()+"-拿牌");
      System.out.println("---------------------发牌结束!-------------------"
      • "\n---------------------开始游戏-------------------");

    /*

    • 获取随机数
      */
      List<Integer> arr1 = new ArrayList<>();
      for(int i=0;i<4;i++)
      {

      do {
      Random random = new Random();
      int t=random.nextInt(52)%(52-1+1) + 1;
      arr1.add(t);
      }
      while( !arr1.contains(arr1.get(i)));
      }

// for (Integer integer1 : arr1) {
// System.out.println(integer1);
// } 测试牌的张数1-52

/*
 * 先判断自己手中的牌 再玩家之间比较
 */
int max1;
int max2;
int max;

if(arr1.get(0)>arr1.get(2))
    max1=0;
else
    max1=2;
if(arr1.get(1)>arr1.get(3))
    max2=1;
else
    max2=3;
if(arr1.get(max1)>arr1.get(max2))
    max=max1;
else
    max=max2;

/*
 * 将牌翻译为 0-3  0-13 通过集合输出牌
 */
int[] arr2 = {-1,-1,-1,-1};
int[] arr3 = {-1,-1,-1,-1};
for(int i=0;i<4;i++)
{
    if(arr1.get(i)<14)
    {
        arr2[i]=0;
        arr3[i]=arr1.get(i);
    }
    else if(arr1.get(i)<27)
    {
        arr2[i]=1;
        arr3[i]=arr1.get(i)-13;
    }
    else if(arr1.get(i)<40)
    {
        arr2[i]=2;
        arr3[i]=arr1.get(i)-26;
    }
    else
    {
        arr2[i]=3;
        arr3[i]=arr1.get(i)-39;
    }   
}

System.out.println("玩家:"+one.getName()+"最大的牌为:"+Wenzi.get(arr2[max1])+Shuzi.get(arr3[max1]));
System.out.println("玩家:"+two.getName()+"最大的牌为:"+Wenzi.get(arr2[max2])+Shuzi.get(arr3[max2]));
System.out.println("最大的牌为    "+Wenzi.get(arr2[max])+Shuzi.get(arr3[max]));

if(max==0||max==2)
{
    System.out.println("---------------玩家"+one.getName()+"获胜---------------");
}
else
{

    System.out.println("---------------玩家"+two.getName()+"获胜---------------");
}

/*
 * 自己手中的拍
 */

System.out.println("玩家:"+one.getName()+"的牌为:"+Wenzi.get(arr2[0])+Shuzi.get(arr3[0])+"   "+Wenzi.get(arr2[2])+Shuzi.get(arr3[2]));
System.out.println("玩家:"+two.getName()+"的牌为:"+Wenzi.get(arr2[1])+Shuzi.get(arr3[1])+"   "+Wenzi.get(arr2[3])+Shuzi.get(arr3[3]));

}

}

1人推荐
随时随地看视频
慕课网APP