JAVA入門第三季——最後練習題撲克牌
玩家類:
package playCards;
import java.util.ArrayList;
import java.util.List;
//建立物件玩家,玩家至少要有ID、姓名、手牌等屬性,手牌為撲克牌的集合
public class Players {
public int id;
public String name;
public List handCards;
public Players(Integer id,String name) {
this.id = id;
this.name = name;
this.handCards = new ArrayList();
}
}
卡牌類
package playCards;
//建立物件撲克牌,不考慮大小王
public class Cards {
public String num;
public String color;
public Cards(String num, String color) {
this.num=num;
this.color=color;
}
public Cards() {
}
@Override
public boolean equals(Object arg0) {
// TODO 自動生成的方法存根
return super.equals(arg0);
}
}
遊戲類(含main方法)
package playCards;
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
public class PlayGame {
public static List cardsToSelect;
public static List cardsToSelectNew;
private Scanner console;
public Players player1;
public Players player2;
public PlayGame() {
cardsToSelect = new ArrayList();
cardsToSelectNew = new ArrayList();
console = new Scanner(System.in);
}
//新增撲克牌
public void addCards() {
System.out.println("建立撲克牌");//預設花色大小:黑桃》紅桃》梅花》方片
Cards[] cd = {new Cards("2","方片"),new Cards("2","梅花"),new Cards("2","紅桃"),new Cards("2","黑桃"),
new Cards("3","方片"),new Cards("3","梅花"),new Cards("3","紅桃"),new Cards("3","黑桃"),
new Cards("4","方片"),new Cards("4","梅花"),new Cards("4","紅桃"),new Cards("4","黑桃"),
new Cards("5","方片"),new Cards("5","梅花"),new Cards("5","紅桃"),new Cards("5","黑桃"),
new Cards("6","方片"),new Cards("6","梅花"),new Cards("6","紅桃"),new Cards("6","黑桃"),
new Cards("7","方片"),new Cards("7","梅花"),new Cards("7","紅桃"),new Cards("7","黑桃"),
new Cards("8","方片"),new Cards("8","梅花"),new Cards("8","紅桃"),new Cards("8","黑桃"),
new Cards("9","方片"),new Cards("9","梅花"),new Cards("9","紅桃"),new Cards("9","黑桃"),
new Cards("10","方片"),new Cards("10","梅花"),new Cards("10","紅桃"),new Cards("10","黑桃"),
new Cards("J","方片"),new Cards("J","梅花"),new Cards("J","紅桃"),new Cards("J","黑桃"),
new Cards("Q","方片"),new Cards("Q","梅花"),new Cards("Q","紅桃"),new Cards("Q","黑桃"),
new Cards("K","方片"),new Cards("K","梅花"),new Cards("K","紅桃"),new Cards("K","黑桃"),
new Cards("A","方片"),new Cards("A","梅花"),new Cards("A","紅桃"),new Cards("A","黑桃")
,};
for (Cards cards : cd) {
cardsToSelect.add(cards);
}
System.out.println("建立撲克牌成功!");
}
//遍歷所有撲克牌
public void cardsForEach(List cardsToSelect) {
System.out.print("所有的牌為:");
for (Cards cards : cardsToSelect) {
System.out.print(cards.color+cards.num+",");
}
System.out.println();
}
//洗牌
public List shuffle(List cardsToSelect) {
Cards cdNew = new Cards();
Random random = new Random();
List cardsToSelectNew = new ArrayList();
for(int i=0;icardsToSelect.indexOf(max2)) {
System.out.println("-----玩家"+player1.name+"獲勝-----");
System.out.println("玩家各自的手牌為:");
System.out.println(player1.name+":["+player1.handCards.get(0).color+player1.handCards.get(0).num
+player1.handCards.get(1).color+player1.handCards.get(1).num+"]");
System.out.println(player2.name+":["+player2.handCards.get(0).color+player2.handCards.get(0).num
+player2.handCards.get(1).color+player2.handCards.get(1).num+"]");
}
else {
System.out.println("-----玩家"+player1.name+"獲勝-----");
System.out.println("玩家各自的手牌為:");
System.out.println(player1.name+":["+player1.handCards.get(0).color+player1.handCards.get(0).num
+player1.handCards.get(1).color+player1.handCards.get(1).num+"]");
System.out.println(player2.name+":["+player2.handCards.get(0).color+player2.handCards.get(0).num
+player2.handCards.get(1).color+player2.handCards.get(1).num+"]");
}
}
public static void main(String[] args) {
// TODO 自動生成的方法存根
PlayGame game = new PlayGame();
game.addCards();
game.cardsForEach(cardsToSelect);
cardsToSelectNew=game.shuffle(cardsToSelect);
System.out.println("洗牌成功!");
game.cardsForEach(cardsToSelectNew);
game.addPlayer();
game.deal();
game.gameStart();
}
}
第一次寫手記,個人基礎也不好,高階的方法用的很少,比大小的時候也是隻有用原來按大小建立的卡牌的位置來比大小,希望各位大佬多多指教。
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/4830/viewspace-2808267/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Java入門第三季撲克牌小遊戲Java遊戲
- java入門最後一章 簡易撲克牌比較大小Java
- Java入門第三季7-1簡易撲克牌作業Java
- Python程式設計基礎練習——撲克牌發牌問題Python程式設計
- Java第三季—簡易撲克牌遊戲Java遊戲
- JAVA入門第三季 簡易撲克牌遊戲原始碼 可以自定義玩家人數 有詳細注Java遊戲原始碼
- 每日一練(33):撲克牌中的順子
- java程式:簡易撲克牌遊戲Java遊戲
- JAVA入門第三季第一章第九節課後練習題!Java
- java入門第三季6-5練習Java
- Java 簡單實現撲克牌抽象類Java抽象
- Java第三季撲克牌作業 參考了幕友的排序方法 非常感謝Java排序
- 撲克牌速算24 -窮舉(JavaScript)JavaScript
- JZ-045-撲克牌順子
- 劍指Offer 撲克牌順子
- [每日一題] 第十二題:撲克牌中的順子每日一題
- 簡易撲克牌遊戲JAVA原始碼_新手上路遊戲Java原始碼
- 使用Java語言寫的一個簡單的撲克牌發牌程式碼Java
- CAD入門級練習題
- SwiftUI:看我展示52張撲克牌,“很快啊!”SwiftUI
- sql查詢入門練習題SQL
- 三次答題判題程式練習讓你入門Java。Java
- 慕課網Python入門練習題---Python
- JAVA入門第三季(第一章練習)模擬借書系統Java
- Java第三季6-5 Collections.sort() 課後練習Java
- bzoj1816: [Cqoi2010]撲克牌(二分)
- 編譯器背後的故事(入門練習)編譯
- 【YOLOv5】實現撲克牌的點數識別YOLO
- python實現撲克遊戲 - 抽鬼牌 和 21點Python遊戲
- python練習冊-第0000題Python
- python練習冊-第0002題Python
- 系統入門課後習題1
- 簡易撲克牌遊戲簡單實現,歡迎指正遊戲
- 【躍遷之路】Java基礎練習(異常)(最後更新:2018.05.03)Java
- python例項:解決經典撲克牌遊戲 -- 四張牌湊24點 (二)Python遊戲
- python例項:解決經典撲克牌遊戲 -- 四張牌湊24點 (一)Python遊戲
- 牛客SQL練習第21題SQL
- java基礎第二季最後一節課練習Java