java程式:簡易撲克牌遊戲

wangsys發表於2021-09-09
package lun.cardgame;
//單張撲克牌模板
public class Card implements Comparable{
      String cardColor;
      String cardPoints;
      String color="'方塊''梅花''黑桃''紅桃'";
      String points="345678910JQKA2";
      public Card(String cardColor,String cardPoints){
          this.cardColor=cardColor;
          this.cardPoints=cardPoints;
      }
      public Card(){

      }
    @Override
    public int compareTo(Card o) {
        // TODO Auto-generated method stub
        if(points.indexOf(this.cardPoints)==points.indexOf(o.cardPoints)){
            if(color.indexOf(this.cardColor)==color.indexOf(o.cardColor))
               return 0;
            else
                return color.indexOf(this.cardColor)-color.indexOf(o.cardColor);
        }
        else
            return points.indexOf(this.cardPoints)-points.indexOf(o.cardPoints);
    }//比較Card型別大小
}

package lun.cardgame;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
//存放撲克牌
public class Cards {
       public List cards;
       String[] colorAll={"黑桃","紅桃","梅花","方塊"};
       String[] pointsAll={"3","4","5","6","7","8","9","10","J","Q","K","A","2"};
       public Cards(){
           this.cards=new ArrayList();
       }
       //建立撲克牌
       public void build(){
           System.out.println("開始建立撲克牌");
           for(String s1:colorAll){
               for(String s2:pointsAll ){
                   Card c=new Card();
                  c.cardColor=s1;c.cardPoints=s2;
                  cards.add(c);
               }
           }
           System.out.println("********");
           System.out.println("********");
           System.out.println("建立完畢");
       }
       //輸出建立好的撲克牌
       public void display(){
           System.out.println("列印輸出建立好的撲克牌");
           for(Card c:cards){
               System.out.print("["+c.cardColor+c.cardPoints+"]"+" ");
           }
       }
       //洗牌
       public void shuffle(){
           System.out.println();
           System.out.println("開始洗牌!");
         Collections.shuffle(cards);
         System.out.println("*******");
        System.out.println("洗牌結束!");

}
}

package lun.cardgame;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
//玩家模板構建
public class Player {
       int playerId;      
       String playerName;
       List handCard;
       Scanner input=new Scanner(System.in);
       public Player(int playerId,String playerName){
           this.playerId=playerId;
           this.playerName=playerName;
           this.handCard=new ArrayList();
       }
       public Player(){
           this.handCard=new ArrayList();
       }

       //輸入玩家id和姓名
       public void inputPlayer(){
       while(true){
       try{    

           System.out.println("輸入玩家id");
           int id=input.nextInt();
           System.out.println("輸入玩家姓名");
           String name=input.next();
           playerId=id;
           playerName=name;
           break;
       }catch(Exception e){

           String s=input.nextLine();//清楚快取區非int型資料
           System.out.println("請輸入整型資料!");
       }
       }
       }

       //發一張到玩家手上
       public void addCard(Card e){
           handCard.add(e);
       }
       //展示玩家手上的牌
       public void showHandCard(){
           System.out.println(playerName+"手中的牌:");
           for(Card c:handCard){
               System.out.print("["+c.cardColor+c.cardPoints+"]"+" ");
           }
       }
       //將玩家手上的牌從小到大排序,方便比較大小
       public void handCardSort(){
           Collections.sort(handCard);
       }

}

package lun.cardgame;

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

import org.omg.CORBA.SystemException;

public class Startplay {

    public static void main(String[] args){
        Cards c=new Cards();
        c.build();//建立撲克牌
        c.display();//列印輸出整幅撲克牌
        c.shuffle();//洗牌
        Player p1=new Player();
        Player p2=new Player();
        p1.inputPlayer();//輸入一號玩家ID和姓名
        p2.inputPlayer();//輸入二號玩家ID和姓名
        System.out.println("開始發牌");//開始發牌
        for(int i=0;i cardCompare=new ArrayList();
        cardCompare.add(p1max);
        cardCompare.add(p2max);
        Collections.sort(cardCompare);//比較大小,調整順序,從小到大
        if(cardCompare.get(1).equals(p1max))
            System.out.println("一號玩家:"+p1.playerName+"勝出");
        else
            System.out.println("二號玩家:"+p2.playerName+"勝出");
    }
}

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

相關文章