紙牌小游戲
??? package cn.com.group;
?? ?
?? ?public class PuKe {
?? ??? ?public String huase;
?? ??? ?public String value;
?? ??? ?
?? ??? ?public PuKe() {
?? ??? ??? ?
?? ??? ?}
?? ??? ?
?? ??? ?public PuKe(String huase,String value) {
?? ??? ??? ?this.huase= huase;
?? ??? ??? ?this.value = value;
?? ??? ?}
?? ??? ?
?? ??? ?public String getHuase() {
?? ??? ??? ?return this.huase;
?? ??? ?}
?? ?
?? ?public PuKe myCompare(PuKe p) {?? ?
?? ??? ?if (this.value.compareTo(p.value) > 0) {
?? ??? ??? ?return this;
?? ??? ?}?? ?
?? ??? ?if (this.value.compareTo(p.value) < 0) {?? ?
?? ??? ??? ?return p;
?? ??? ?}
?? ??? ?if (this.value.compareTo(p.value) == 0) {
?? ??? ??? ?if (this.huase.equals("黑桃")) {
?? ??? ??? ??? ??? ?return this;
?? ??? ??? ??? ?} else if (p.huase.equals("黑桃")) {
?? ??? ??? ??? ??? ?return p;
?? ??? ??? ??? ?} else if (this.huase.equals("紅桃")) {
?? ??? ??? ??? ??? ?return this;
?? ??? ??? ??? ?} else if (p.huase.equals("紅桃")) {
?? ??? ??? ??? ??? ?return p;
?? ??? ??? ??? ?} else if (this.huase.equals("梅花")) {
?? ??? ??? ??? ??? ?return this;
?? ??? ??? ??? ?}
?? ??? ??? ?else {
?? ??? ??? ??? ?return p;
?? ??? ??? ??? ?}
?? ??? ??? ?}
?? ??? ??? ??? ?return new PuKe(); // 這行代碼必須要有,否則報錯
?? ??? ??? ?}
}
package cn.com.group;
import java.util.List;
public class Player {
?? ?public String name;
?? ?public int id;
?? ?public List<PuKe> list;
?? ?public Player() {
?? ??? ?
?? ?}
?? ?public Player(String name,int id) {
?? ??? ?this.name=name;
?? ??? ?this.id=id;
?? ?}
}
package cn.com.group;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class Game {
?? ?
?? ?public List<PuKe> puke;
?? ?public Player[] player;
?? ?
?? ?public void testGame() {
?? ??? ?
?? ??? ?int tempid=0;
?? ??? ?String tempName="";
?? ??? ?
?? ??? ?
?? ??? ?System.out.println("歡迎來到游戲");
?? ??? ?System.out.println("---洗牌--");
?? ??? ?puke = new ArrayList();
?? ??? ?for(int i=0;i<13;i++) {
?? ??? ??? ?if(i==0) {
?? ??? ??? ??? ?puke.add(new PuKe("方片","A"));
?? ??? ??? ??? ?puke.add(new PuKe("梅花","A"));
?? ??? ??? ??? ?puke.add(new PuKe("紅桃","A"));
?? ??? ??? ??? ?puke.add(new PuKe("黑桃","A"));
?? ??? ??? ?}
?? ??? ??? ?else if(i>1&&i<10) {
?? ??? ??? ??? ?puke.add(new PuKe("方片",String.valueOf(i)));
?? ??? ??? ??? ?puke.add(new PuKe("梅花",String.valueOf(i)));
?? ??? ??? ??? ?puke.add(new PuKe("紅桃",String.valueOf(i)));
?? ??? ??? ??? ?puke.add(new PuKe("黑桃",String.valueOf(i)));
?? ??? ??? ?}
?? ??? ??? ?else if(i==10){
?? ??? ??? ??? ?puke.add(new PuKe("方片","J"));
?? ??? ??? ??? ?puke.add(new PuKe("梅花","J"));
?? ??? ??? ??? ?puke.add(new PuKe("紅桃","J"));
?? ??? ??? ??? ?puke.add(new PuKe("黑桃","J"));
?? ??? ??? ?}
?? ??? ??? ?else if(i==11) {
?? ??? ??? ??? ?puke.add(new PuKe("方片","Q"));
?? ??? ??? ??? ?puke.add(new PuKe("梅花","Q"));
?? ??? ??? ??? ?puke.add(new PuKe("紅桃","Q"));
?? ??? ??? ??? ?puke.add(new PuKe("黑桃","Q"));
?? ??? ??? ?}
?? ??? ??? ?else if(i==12) {
?? ??? ??? ??? ?puke.add(new PuKe("方片","K"));
?? ??? ??? ??? ?puke.add(new PuKe("梅花","K"));
?? ??? ??? ??? ?puke.add(new PuKe("紅桃","K"));
?? ??? ??? ??? ?puke.add(new PuKe("黑桃","K"));
?? ??? ??? ?}
?? ??? ?}
?? ??? ?System.out.println("創(chuàng)建成功");
?? ??? ?for(PuKe puke1:puke) {
?? ??? ??? ?System.out.println(puke1.huase+puke1.value);
?? ??? ?}
?? ??? ?System.out.println("開始洗牌");
?? ??? ?Collections.shuffle(puke);
?? ??? ?System.out.println("洗牌完成");
?? ??? ?System.out.println("選擇玩家");
?? ??? ?player = new Player[2];
?? ??? ?Scanner console = new Scanner(System.in);
?? ??? ?for(int i=0;i<2;i++) {
?? ??? ??? ?System.out.println("請輸入第"+(i+1)+"個玩家的游戲名稱");
?? ??? ??? ?try {
?? ??? ??? ??? ?tempName = console.next();
//?? ??? ??? ??? ?tempid=i;
?? ??? ??? ?}catch(Exception e) {
?? ??? ??? ??? ?e.printStackTrace();
?? ??? ??? ??? ?System.out.println("請輸入String類型的字符串");
?? ??? ??? ?}
?? ??? ??? ?player[i]=new Player(tempName,tempid);
?? ??? ?}
?? ??? ?System.out.println("歡迎玩家:" + player[0].name);
?? ??? ?System.out.println("歡迎玩家:" + player[1].name);
?? ??? ?
?? ??? ?// 接下來每人發(fā)兩張牌
?? ??? ?player[0].list = new ArrayList<PuKe>();
?? ??? ?player[1].list = new ArrayList<PuKe>();
?? ??? ?System.out.println("--------開始發(fā)牌------------------");
?? ??? ?System.out.println("玩家:" + player[0].name + "拿牌");
?? ??? ?player[0].list.add(puke.get(0));
?? ??? ?System.out.println("玩家:" + player[1].name + "拿牌");
?? ??? ?player[1].list.add(puke.get(1));
?? ??? ?System.out.println("玩家:" + player[0].name + "拿牌");
?? ??? ?player[0].list.add(puke.get(2));
?? ??? ?System.out.println("玩家:" + player[1].name + "拿牌");
?? ??? ?player[1].list.add(puke.get(3));
?? ??? ?System.out.println("---------發(fā)牌結(jié)束----------------");
?? ??? ?// 開始pk
?? ??? ?PuKe p1max = player[0].list.get(0).myCompare(player[0].list.get(1));
?? ??? ?PuKe p2max = player[1].list.get(0).myCompare(player[1].list.get(1));
?? ??? ?if (p1max.myCompare(p2max).equals(p1max)) {
?? ??? ?System.out.println("恭喜玩家:" + player[0].name + "獲勝");
?? ??? ?} else {
?? ??? ?System.out.println("恭喜玩家:" + player[1].name + "獲勝");
?? ??? ?}
?? ??? ?System.out.println("玩家:" + player[0].name + "的牌是:"+
?? ??? ?player[0].list.get(0).huase+player[0].list.get(0).value+","+player[0].list.get(1).huase+player[0].list.get(1).value);
?? ??? ?System.out.println("玩家:" + player[1].name + "的牌是:"+
?? ??? ?player[1].list.get(0).huase+player[1].list.get(0).value+","+player[1].list.get(1).huase+player[1].list.get(1).value);
//?? ??? ?for (PuKe i : puke) {
//?? ??? ?System.out.println(i.huase + i.value);
//?? ??? ?}
?? ?}
?? ?public static void main(String[] args) {
?? ??? ?Game game = new Game();
?? ??? ?game.testGame();
?? ?}
}
2019-10-08
為什么用compare比較下呢??
public PuKe myCompare(PuKe p) {?? ?
?? ??? ?if (this.value.compareTo(p.value) > 0) {
?? ??? ??? ?return this;