Java第三季撲克牌作業 參考了幕友的排序方法 非常感謝

w39發表於2021-09-09

package com.java1996;

public class GameStart {

public static void main(String[] args){
Game game=new Game();
game.cardCreat();
game.gameRunning();
game.cardSend();

}
}
package com.java1996;


public class Cards {

private String cardNum;//牌
private String color;//花色

public Cards(String color, String cardNum){
this.cardNum=cardNum;
this.color=color;

}

public String getCardNum() {
return cardNum;
}

public void setCardNum(String cardNum) {
this.cardNum = cardNum;
}

public String getColor() {
return color;
}

public void setColor(String color) {
this.color = color;
}

@Override
public String toString() {
return color+cardNum;
}

}
package com.java1996;

import java.util.ArrayList;
import java.util.List;


public class Player {

private int id;//玩家ID
private String name;//玩家名字
private List cardPlayer;//玩家手牌

public Player(int id,String name){
this.id=id;
this.name=name;
this.cardPlayer=new ArrayList();
}

public Player(){
//構造時候new一個ArrayList例項
this.cardPlayer=new ArrayList();
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public List getCardPlayer() {
return cardPlayer;
}

public void setCardPlayer(List cardPlayer) {
this.cardPlayer = cardPlayer;
}
}
package com.java1996;

import java.util.ArrayList;
import java.util.Collections;
import java.util.InputMismatchException;
import java.util.List;
import java.util.Scanner;


public class Game {

Scanner sc=new Scanner(System.in);
//建立兩名玩家物件
Player p1=new Player();
Player p2=new Player();
List maxCards=new ArrayList();//用來比較兩名玩家最大的牌
List allCards=new ArrayList();//全部的牌,建立的撲克牌
String[] color={"黑桃","紅桃","梅花","方塊"};
String[] cardNum={"2","3","4","5","6","7","8","9","10","J","Q","K","A"};


public void cardCreat(){

System.out.println("---------------- 建立撲克牌-------------------------");
System.out.println("---------------- loading---------------------------");
for(String str:color){
for(String str1:cardNum){
//因為重寫了toString方法新增之後以字串顯示
allCards.add(new Cards(str,str1));
}
}

System.out.println("----------------建立撲克牌成功------------------------");
System.out.println("----------------撲克牌為下----------------------------");
System.out.println(allCards);
System.out.println("-----------------開始洗牌------------------------------");
for(int i=0;i //多洗幾次
Collections.shuffle(allCards);
}
System.out.println("-----------------洗牌結束------------------------------");

}


public void gameRunning(){

for(int i=0;i System.out.println("請輸入第"+(i+1)+"位玩家的ID和姓名:");
System.out.println("輸入ID:");
if(i==0){
while(true){
try{
p1.setId(sc.nextInt());
break;
}catch(InputMismatchException e){
System.out.println("老哥,請輸入數字");
sc.next();
}
}
System.out.println("輸入姓名:");
p1.setName(sc.next());
}else{
p2.setId(sc.nextInt());
System.out.println("輸入姓名:");
p2.setName(sc.next());
}

}
System.out.println("----歡迎玩家:"+p1.getName());
System.out.println("----歡迎玩家:"+p2.getName());
}


public void cardSend(){
System.out.println("-------------------開始發牌-------------------------");

p1.getCardPlayer().add(allCards.get(0));//給p1玩家發牌
p1.getCardPlayer().add(allCards.get(10));
System.out.println("玩家:"+p1.getName()+"-拿牌");

p2.getCardPlayer().add(allCards.get(1));//給p2玩家發牌
p2.getCardPlayer().add(allCards.get(9));
System.out.println("玩家:"+p2.getName()+"-拿牌");

p1.getCardPlayer().add(allCards.get(2));

System.out.println("玩家:"+p1.getName()+"-拿牌");

p2.getCardPlayer().add(allCards.get(3));

System.out.println("玩家:"+p2.getName()+"-拿牌");

System.out.println("--------------------發牌結束-----------------------");
//現在發牌結束 兩名玩家都有各自的手牌集合
//把兩個人的牌加入總的比較牌
maxCards.addAll(p1.getCardPlayer());
maxCards.addAll(p2.getCardPlayer());

System.out.println(p1.getName()+":"+p1.getCardPlayer());
System.out.println(p2.getName()+":"+p2.getCardPlayer());
//排序了,把大的牌牌到最後
Collections.sort(p1.getCardPlayer(),new Mycomparator());
Collections.sort(p2.getCardPlayer(), new Mycomparator());
//這是儲存兩個玩家所有手牌的集合,也排序下
Collections.sort(maxCards, new Mycomparator());

System.out.println("玩家:"+p1.getName()+"的最大手牌是:"+p1.getCardPlayer().get(2));
System.out.println("玩家:"+p2.getName()+"的最大手牌是:"+p2.getCardPlayer().get(2));
System.out.println("-----------------玩家"+maxCards()+"獲勝------------------------");

System.out.println("玩家各自的手牌為:");
System.out.println(p1.getName()+":"+p1.getCardPlayer());
System.out.println(p2.getName()+":"+p2.getCardPlayer());

}

public String maxCards(){
//判斷如果maxCards所有玩家牌的集合裡最大的一張
//等於玩家最大的那一張則返回該玩家
if(maxCards.get(maxCards.size()-1)==p1.getCardPlayer().get(2)){
return p1.getName();
}else{
return p2.getName();
}

}

}
package com.java1996;

import java.util.Comparator;

public class Mycomparator implements Comparator{

@Override
public int compare(Cards o1, Cards o2) {
//自定義了一個排序方法 按照下標排序
String color = "黑桃 紅桃 梅花 方塊";
String point = "2 3 4 5 6 7 8 9 10 J Q K A";
//如果o1-o2大於0,說明o1比o2打 返回1,排在後面
int result= point.indexOf(o1.getCardNum())-point.indexOf(o2.getCardNum());
if(result>0){
return 1;
}else if(result return -1;
}else{
//因為color陣列是從大到小排這裡要倒過來
int result1=color.indexOf(o1.getColor())-color.indexOf(o2.getColor());
if(result1>0){
return -1;
}else{
return 1;
}
}

}

}

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/854/viewspace-2807719/,如需轉載,請註明出處,否則將追究法律責任。

相關文章