欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java实现简单的抽牌游戏

程序员文章站 2022-03-26 08:07:21
本文实例为大家分享了java实现简单抽牌游戏的具体代码,供大家参考,具体内容如下main类package com.company; import java.util.*; public class m...

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

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