綜合練習——洗牌發牌Easy版
Card程式碼,裡面是卡牌的屬性,重寫了equals方法,並且實現了Comparable介面。這裡因為要求只是比較大小,所以我在屬性裡多新增了一條,用多加的這條屬性來進行比較,由數字10,20,30,40,50,60,70,80,90,91,92,93,94還有顏色用A-D表示,組合而成的一個字串,這樣去排序看大小的話會非常方便。
因為一般的時候2是最大的牌。2用94,A93,K92,Q91,J90,10用80……3用10。後面跟著如果數字相同則比較花色,如黑紅梅方,用DCBA來表示,比如說當都是2時,前兩位都是94,方片2是94A,黑桃2就是94D。這樣升序會排在對方後面。直接排序然後取最大的值就可以了。再用最大的值和當時的手牌去比較,相同的那位玩家獲勝。
遊戲執行的類
import java.util.ArrayList;
import java.util.Collections;
/**
* Created by Administrator on 2017/4/4.
*/
public class PokerGameTest {
public static void main(String[] args) {
System.out.println("--------歡迎來到撲克牌遊戲--------");
System.out.println("------------建立撲克牌------------");
System.out.println("----------撲克牌建立成功----------");
System.out.print("撲克牌為:");
//建立卡牌集合類,例項化,呼叫方法建立52張的卡組
PokerCardsList pcl=new PokerCardsList();
pcl.pokerCardsListAdd();
System.out.println("------------開始洗牌-------------");
System.out.println("------------洗牌結束!------------");
System.out.println("------------建立玩家--------------");
PokerPlayerList pm=new PokerPlayerList();
//新建一個PokerPlayerList集合把這兩個玩家儲存進來
System.out.println("請輸入第1位玩家的ID和姓名");
pm.playerIdNameAdd();
Player player1=pm.playerList.get(0);
System.out.println("請輸入第2位玩家的ID和姓名");
pm.playerIdNameAdd();
Player player2=pm.playerList.get(1);
System.out.println("歡迎玩家:"+player1.playerName);
System.out.println("歡迎玩家:"+player2.playerName);
System.out.println("------------開始發牌-------------");
int[] k=pcl.randomTest();
Card player1Card1=new Card(pcl.pokerCardsList.get(k[0]).colorNum,
pcl.pokerCardsList.get(k[0]).colorSum);
Card player1Card2=new Card(pcl.pokerCardsList.get(k[1]).colorNum,
pcl.pokerCardsList.get(k[1]).colorSum);
Card player2Card1=new Card(pcl.pokerCardsList.get(k[2]).colorNum,
pcl.pokerCardsList.get(k[2]).colorSum);
Card player2Card2=new Card(pcl.pokerCardsList.get(k[3]).colorNum,
pcl.pokerCardsList.get(k[3]).colorSum);
//建立兩個玩家的手牌List,例項化,否則會無法呼叫,空指標異常
player1.playerCardsList=new ArrayList();
player2.playerCardsList=new ArrayList();
player1.playerCardsList.add(player1Card1);
System.out.println("-----玩家:"+player1.playerName+"----拿牌");
player2.playerCardsList.add(player2Card1);
System.out.println("-----玩家:"+player2.playerName+"----拿牌");
player1.playerCardsList.add(player1Card2);
System.out.println("-----玩家:"+player1.playerName+"----拿牌");
player2.playerCardsList.add(player2Card2);
System.out.println("-----玩家:"+player2.playerName+"----拿牌");
System.out.println("------------發牌結束-------------");
System.out.println("------------開始遊戲-------------");
Collections.sort(player1.playerCardsList);
System.out.println("玩家"+player1.playerName+"最大的手牌是:"+
player1.playerCardsList.get(1).colorNum);
Collections.sort(player2.playerCardsList);
System.out.println("玩家"+player2.playerName+"最大的手牌是:"+
player2.playerCardsList.get(1).colorNum);
Player nullPlayer=new Player();
nullPlayer.playerCardsList=new ArrayList();
nullPlayer.playerCardsList.add(player1.playerCardsList.get(1));
nullPlayer.playerCardsList.add(player2.playerCardsList.get(1));
Collections.sort(nullPlayer.playerCardsList);
if(nullPlayer.playerCardsList.get(1).equals(player1.playerCardsList.get(1))){
System.out.println("----------"+"玩家"+player1.playerName+"獲勝"+"----------");
}else{
System.out.println("----------"+"玩家"+player2.playerName+"獲勝"+"-----------");
}
System.out.println("玩家各自的手牌為:");
System.out.println("玩家"+player1.playerName+":"+player1.playerCardsList.get(0).colorNum+
" "+player1.playerCardsList.get(1).colorNum);
System.out.println("玩家"+player2.playerName+":"+player2.playerCardsList.get(0).colorNum+
" "+player2.playerCardsList.get(1).colorNum);
}
}
Card類
package pokergame;
/**
* Created by Administrator on 2017/4/5.
* 定義一個卡牌類
*/
public class Card implements Comparable{
public String colorNum;
public String colorSum;
public Card(String colorNum,String colorSum) {
this.colorSum = colorSum;
this.colorNum=colorNum;
}
@Override
public int compareTo(Card o) {
return this.colorSum.compareTo(o.colorSum);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Card)) return false;
Card card = (Card) o;
return colorNum.equals(card.colorNum);
}
}
遊戲玩家類
import java.util.*;
/**
* Created by Administrator on 2017/4/4.
*/
public class Player{
public Integer playerId;
public String playerName;
public List playerCardsList;
public Player(Integer playerId,String playerName){
this.playerId=playerId;
this.playerName=playerName;
List playerCardsList=new ArrayList();
}
public Player(){
}
}
建立了整副牌的List類
import java.util.*;
/**
* Created by Administrator on 2017/4/5.
* 建立一整副卡牌的集合,由於要打亂卡牌的順序
* 可以建立四個不重複的隨機數,分別取牌發給玩家
*/
public class PokerCardsList {
public List pokerCardsList;
public PokerCardsList() {
this.pokerCardsList = new ArrayList();
}
/**
* 往pokerCardsList中新增卡牌的方法
*/
public void pokerCardsListAdd() {
final String[] Color = {"黑桃", "紅桃", "梅花", "方片"};
final String[] Num = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
final String[] colorTwo={"D","C","A","B"};
final int[] sum={93,94,10,20,30,40,50,60,70,80,90,91,92};
for(int i = 0;i47;j--){
testSet.add(num.nextInt(j));
if(testSet.size()>i){
i++;
}else{
continue;
}
}
//定義一個陣列接收這四個數
int[] k=new int[4];
int i=0;
for(Object x:testSet) {
int y=(int)x;
k[i]=y;
i++;
}
return k;
}
}
玩家資訊儲存的類
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.List;
import java.util.Scanner;
/**
* Created by Administrator on 2017/4/5.
*/
public class PokerPlayerList {
//建立一個List集合存放兩名玩家的資訊
public List playerList;
public PokerPlayerList(){
this.playerList=new ArrayList();
}
/**
* 存放玩家資訊
*/
public void playerIdNameAdd() {
while(true) {
try {
Scanner sc=new Scanner(System.in);
System.out.println("輸入ID:");
Integer playerid = sc.nextInt();
System.out.println("輸入姓名:");
String playername = sc.next();
Player p = new Player(playerid, playername);
this.playerList.add(p);
break;
} catch (InputMismatchException e) {
System.out.println("您輸入的ID非數字");
continue;
}
}
}
}
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/758/viewspace-2807648/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Java基礎 --- 綜合練習Java
- SparkSql與Redis綜合練習SparkSQLRedis
- ADMIN07 - 綜合練習
- 迴圈高階綜合練習
- Day5-SQL綜合練習(Datawhale)SQL
- Java攻城獅第二季綜合練習——初學者好懂版Java
- 程式設計實踐(Pandas)綜合練習1程式設計
- U7-11課綜合練習+12課階段測評練習——複習練習題目
- 物件導向綜合訓練物件
- Python程式設計基礎練習——撲克牌發牌問題Python程式設計
- 『政善治』Postman工具 — 13、Postman介面測試綜合練習Postman
- 二維陣列和函式綜合程式設計練習陣列函式程式設計
- 利用JAVA實現發牌-->洗牌-->理牌的過程(話不多說直接上程式碼)Java
- 知識學習綜合
- Java基礎 --- 物件導向綜合訓練Java物件
- 【綜合複習_網路部分】
- Java入門第二季 6-1綜合練習程式碼Java
- 綜合練習——尋找有潛力的bilibili百大UP主(1)
- C++遠征之起航篇第5章 綜合練習 程式碼參考C++
- 【pandas學習筆記】綜合整理筆記
- Spring Boot(二):Web 綜合開發Spring BootWeb
- Spark學習進度7-綜合案例Spark
- python綜合學習七之TensorFlow初識Python
- 【公告】PSRC 4月榮譽榜單大洗牌,頭牌花落誰家?
- python綜合學習四之Numpy和Pandas(下)Python
- 綜合約束
- 綜合實驗
- 綜合漏洞發現工具蜻蜓使用體驗
- 資料庫系列:MySQL索引優化總結(綜合版)資料庫MySql索引優化
- 全選練習-原生版和jQueryjQuery
- Opencv學習筆記(3)---紙牌數字識別練習實踐專案OpenCV筆記
- Steam教育在新時代中綜合學習論
- JAVA入門第三季——最後練習題撲克牌Java
- OA綜合辦公管理系統功能開發
- 教資 - 綜合(6)
- 教資 - 綜合(4)
- 教資 - 綜合(1)
- 綜合掃描工具