当前位置: 移动技术网 > IT编程>开发语言>Java > Java实现简单的抽牌游戏

Java实现简单的抽牌游戏

2020年05月13日  | 移动技术网IT编程  | 我要评论

黑衣人3种子,唐正东老婆,政府部门工作总结

本文实例为大家分享了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";
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网