Java第三季—簡易撲克牌遊戲

solution發表於2021-09-09

//看來異常方面還是要好好複習一下!

package com.Puke2;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Scanner;
import java.util.Set;
import java.util.Map.Entry;

public class Pukes {

public static void main(String[]args){
    System.out.println("-------------簡易撲克牌遊戲-------------");
    Puke p=new Puke();
    Scanner inpit = new Scanner(System.in);
    System.out.println("有以下撲克牌:");
    String shu[]={"3","4","5","6","7","8","9","10","J","Q","K","A","2"};
    String hua[]={"","","",""};//花色,可能會出現亂碼或者空白
    for(int i=0;i name=new HashMap();//接收玩家名字
    private ArrayList set = new ArrayList();//接收數字
//  ArrayList list = new ArrayList();//接收撲克牌集合
    HashMap map=new HashMap();
    private String shu[]={"3","4","5","6","7","8","9","10","J","Q","K","A","2"};//撲克牌的大小
    private String hua[]={"","","",""}; //和花色,可能會出現亂碼或者空白
/**
  *玩家方法
*/
    private void name(){
        for(int i=0;i>  entrys=map.entrySet();//如果要輸出鍵值對,一般用這種方法
    for (Entry entry :  entrys) {
    System.out.println(entry.getKey()+"--"+entry.getValue());
        }
*/
    System.out.println();
    int b=0;
    System.out.println("-------------開始洗牌--------------");
    Collections.shuffle(set);//寫在外面,防止隨機生成相同的牌
    System.out.println("-------------洗牌結束--------------");
    for(int i=0;i0){
            System.out.print(map.get(set.get(1)));
        }else{
            System.out.print(map.get(set.get(3)));
        }
        System.out.println();
        System.out.print("玩家"+name.get(2)+"最大的牌是:");
        if(set.get(2)-set.get(4)>0){
            System.out.print(map.get(set.get(2)));
        }else{
            System.out.print(map.get(set.get(4)));
        }
        System.out.println();
        //測試計算是否真確

    //System.out.println(set.get(1)+" "+set.get(2)+" "+set.get(3)+" "+set.get(4)+" ");
        System.out.println("獲勝者是:");
        if(set.get(1)-set.get(3)>0&&set.get(2)-set.get(4)>0){//當都是摸得第一張牌大

            if(set.get(1)-set.get(2)>0){
                System.out.println(name.get(1));
            }else{
                System.out.println(name.get(2));    
            }

        }else if(set.get(1)-set.get(3)>0&&set.get(2)-set.get(4)0){
                System.out.println(name.get(1));
            }else{
                System.out.println(name.get(2));    
            }

        }else if(set.get(1)-set.get(3)0){//與上面的相反

            if(set.get(3)-set.get(2)>0){
                System.out.println(name.get(1));
            }else{
                System.out.println(name.get(2));    
            }

      }else if(set.get(1)-set.get(3)0){    
                System.out.println(name.get(1));
            }else{
                System.out.println(name.get(2));    
            }
}
}
}
class Game{//玩家類
//  HashMap name=new HashMap();

private String name;
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public Game() {

}
public Game(String name) {

    this.name = name;
}
@Override
public String toString() {
    return name;
}

}

執行結果:

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

相關文章