温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Java如何实现简单的抽牌游戏

发布时间:2020-08-01 10:27:00 来源:亿速云 阅读:131 作者:小猪 栏目:编程语言

这篇文章主要讲解了Java如何实现简单的抽牌游戏,内容清晰明了,对此有兴趣的小伙伴可以学习一下,相信大家阅读完之后会有帮助。

Main类

package com.company;
 
import java.util.*;
 
public class Main
{
 
  public static void main(String[] args)
  {
    Poke p = new Poke();
    p.shuffle();
    System.out.println("您想抽几张牌?");
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
 
 
    System.out.println("抽取了"+n+"张牌,分别为:");
    Card[] c = p.draw(n);
    for (Card g :c ) System.out.print(g);
    System.out.println();
    p.sortOut(c);
    System.out.println("理牌完成!");
    for (Card g :c ) System.out.print(g);
  }
}

Poke类

package com.company;
 
import java.util.Arrays;
 
/**
 * Created by ttc on 16-11-2.
 */
public class Poke
{
  Card[] m_card = null;
  int[] values = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
  String[] colors = {"♡", "♠", "♢", "♧"};
 
  public Poke()
  {
    m_card = new Card[52];
    for (int i = 0; i < colors.length; i++)
    {
      for (int j = 0; j < values.length; j++)
      {
        m_card[i * values.length + j] = new Card(values[j], colors[i]);
      }
    }
  }
 
  public void outPut()
  {
    //展示当前牌序
    for (int i = 0; i < m_card.length; i++)
    {
      if (i % 13 == 0) System.out.println();
      System.out.print(m_card[i]);
    }
  }
 
  public void shuffle()
  {
    //洗牌
    Card tempC = null;
    for (int i = 0; i < 52; i++)
    {
      tempC = m_card[i];
      int j = (int) (Math.random() * 51);
      m_card[i] = m_card[j];
      m_card[j] = tempC;
    }
    System.out.print("洗牌完成!");
  }
 
  public Card[] draw(int n)
  {
    //抽N张牌
    Card[] c = new Card[n];
    for (int i = 0; i < n ; i++) c[i] = m_card[i];
    return c;
  }
 
  public void sortOut(Card[] c)
  {
    //理牌
    Arrays.sort(c);
  }
}

Card类

package com.company;
 
/**
 * Created by ttc on 16-11-2.
 */
public class Card implements Comparable
{
  private int m_values;
  private String m_colors;
 
  public Card(int m_values, String m_colors)
  {
    this.m_values = m_values;
    this.m_colors = m_colors;
  }
 
  @Override
  public int compareTo(Object o)
  {
    if (this.m_values > ((Card)o).m_values) return 1;
    else if(this.m_values == ((Card)o).m_values) return 0;
    else return -1;
  }
 
  @Override
  public String toString()
  {
    String strtmp;
    switch (m_values)
    {
      case 1:
        strtmp = "A";
        break;
      case 11:
        strtmp = "J";
        break;
      case 12:
        strtmp = "Q";
        break;
      case 13:
        strtmp = "K";
        break;
      default:
        strtmp = String.valueOf(m_values);
    }
    return m_colors + strtmp + "\t";
  }
}

看完上述内容,是不是对Java如何实现简单的抽牌游戏有进一步的了解,如果还想学习更多内容,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI