Java實(shí)現(xiàn)簡單的抽牌游戲
本文實(shí)例為大家分享了Java實(shí)現(xiàn)簡單抽牌游戲的具體代碼,供大家參考,具體內(nèi)容如下
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()
{
//展示當(dāng)前牌序
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";
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot整合Sharding-JDBC實(shí)現(xiàn)MySQL8讀寫分離
本文是一個基于SpringBoot整合Sharding-JDBC實(shí)現(xiàn)讀寫分離的極簡教程,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的可以了解一下2021-07-07
SpringBoot如何基于POI-tl和word模板導(dǎo)出龐大的Word文件
這篇文章主要介紹了SpringBoot如何基于POI-tl和word模板導(dǎo)出龐大的Word文件,poi-tl是一個基于Apache?POI的Word模板引擎,也是一個免費(fèi)開源的Java類庫2022-08-08
idea中一鍵自動生成序列化serialVersionUID方式
這篇文章主要介紹了idea中一鍵自動生成序列化serialVersionUID方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-09-09
SpringBoot如何使用Template請求http接口
在Spring?Boot中,如果你想要通過模板(template)的方式連接HTTP服務(wù),并發(fā)送HTTP請求,有幾種不同的方式可以實(shí)現(xiàn),但最直接和常用的方式之一是使用RestTemplate,這篇文章主要介紹了SpringBoot使用Template請求http接口,需要的朋友可以參考下2024-08-08
利用Socket.io 實(shí)現(xiàn)消息實(shí)時推送功能
這篇文章主要介紹了利用Socket.io 實(shí)現(xiàn)消息實(shí)時推送功能,需要的朋友可以參考下2017-12-12
Mybatis-Plus同時使用邏輯刪除和唯一索引的問題及解決辦法(報(bào)數(shù)據(jù)重復(fù)Duplicate entry的
在開發(fā)中,我們經(jīng)常會有邏輯刪除和唯一索引同時使用的情況,但當(dāng)使用mybatis plus時,如果同時使用邏輯刪除和唯一索引,會報(bào)數(shù)據(jù)重復(fù)Duplicate entry的問題,如何解決這個問題呢,小編給大家分享Mybatis-Plus同時使用邏輯刪除和唯一索引的問題及解決辦法,一起看看吧2023-11-11

