這篇文章主要講解了Java如何實現(xiàn)簡單的抽牌游戲,內(nèi)容清晰明了,對此有興趣的小伙伴可以學習一下,相信大家閱讀完之后會有幫助。
成都創(chuàng)新互聯(lián)主營劍閣網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,重慶App定制開發(fā),劍閣h5微信小程序搭建,劍閣網(wǎng)站營銷推廣歡迎劍閣等地區(qū)企業(yè)咨詢
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"; } }
看完上述內(nèi)容,是不是對Java如何實現(xiàn)簡單的抽牌游戲有進一步的了解,如果還想學習更多內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
分享文章:Java如何實現(xiàn)簡單的抽牌游戲
分享路徑:http://bm7419.com/article16/jcshgg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計公司、外貿(mào)網(wǎng)站建設(shè)、品牌網(wǎng)站設(shè)計、動態(tài)網(wǎng)站、外貿(mào)建站、網(wǎng)站維護
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)