撲克遊戲代碼
package com.imooc.collection;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Scanner;
public class Poker {
public Map<Integer, String> manMap;
public List lCards;
public Integer[][] lCards2,lCards3;//打乱前后牌的顺序和位置,先示数字,再是花色
// public Integer[] sCard1,sCard2;
public Scanner scanner = new Scanner(System.in);
public Poker() {
this.manMap = new HashMap<Integer, String>();
this.lCards = new ArrayList<Card>();
this.lCards2 = new Integer[52][2];
this.lCards3 = new Integer[52][2];
// this.sCard1 = new Integer[2];
// this.sCard2 = new Integer[2];
}
public static void main(String[] args) {
Poker p = new Poker();
String[] pokers = p.getPoker();
// System.out.println(Arrays.toString(pokers));
List pList = p.wash(pokers);
System.out.println("----------------------------------------------------------");
p.getMan();
p.setCard();
}
/**
* 創建撲克牌
*/
public String[] getPoker(){
String[] pStrings = {"方塊","梅花","紅桃","黑桃"};
String[] numStrings = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
String[] pokers = new String[52];
int k = 0;
for(int i = 0;i<pStrings.length;i++){
for (int j = 0; j < numStrings.length; j++) {
pokers[k] = pStrings[i]+numStrings[j];
// lCards.add(new Card(j+1, i+1));
lCards2[k][0] = j+1;
lCards2[k][1] = i+1;
k++;
}
}
System.out.println("--------------創建一副撲克牌------------------");
System.out.println(pokers.length);
System.out.println(Arrays.toString(pokers));
System.out.println("--------------撲克牌創建成功,開始洗牌------------------");
return pokers;
}
/**
* 洗牌
*/
public List wash(String[] poker){
Random random = new Random();
int i = 0;
for (int j = 0; j < 52; j++) {
do{
i= random.nextInt(52);
}while(lCards.contains(i));
lCards.add(poker[i]);//牌打亂后的順序
lCards3[j] = lCards2[i];// 打亂后的牌的數字和花色的位置
}
// System.out.println(lCards3[0][0]);
System.out.println(lCards.size());
System.out.println(lCards);
System.out.println("-------------------完成洗牌----------------");
return lCards;
}
/**
* 生成兩位牌友
*/
public void getMan(){
System.out.println("請輸入第一位賽者的ID:");
int id = Integer.parseInt(scanner.next());
System.out.println("請輸入第一位賽者的姓名:");
String name = scanner.next();
manMap.put(id, name);
System.out.println("第一位賽的信息是:"+id+":"+name);
System.out.println("--------------------------------------------");
System.out.println("請輸入第二位賽者的ID:");
int id2 = Integer.parseInt(scanner.next());
System.out.println("請輸入第二位賽者的姓名:");
String name2 = scanner.next();
manMap.put(id2, name2);
System.out.println("第二位賽的信息是:"+id2+":"+name2);
System.out.println("--------------------------------------------");
}
/*
* 發牌:
*/
public String setCard(){
String lString = "";
// String[] pokers = new String[4];
// 0 是數字,1是花色
Integer[][] integers1 = new Integer[2][2],integers2 = new Integer[2][2],iMax1 = new Integer[1][1],iMax2 = new Integer[1][1];
integers1[0] = lCards3[0];
integers1[1] = lCards3[2];
integers2[0] = lCards3[1];
integers2[1] = lCards3[3];
// 第一個人的最大那張牌
iMax1[0] = (integers1[0][0]>integers1[1][0])?integers1[0]:
integers1[0][0]<integers1[1][0]?integers1[1]://比較數字
(integers1[0][1]>integers1[1][1])?integers1[0]:integers1[1];//數字相同就比較花色
// 第二個人的最大那張牌
iMax2[0] = (integers2[0][0]>integers2[1][0])?integers2[0]:
integers2[0][0]<integers2[1][0]?integers2[1]://比較數字
(integers2[0][1]>integers2[1][1])?integers2[0]:integers2[1];//數字相同就比較花色
//最大的牌
iMax1[0] = iMax1[0][0]>iMax2[0][0]?iMax1[0]:iMax1[0][0]<iMax2[0][0]?iMax2[0]:
iMax1[0][1]>iMax2[0][1]?iMax1[0]:iMax2[0];
//獲取最大的牌所在位置
for (int i = 0; i < 52; i++) {
if(iMax1[0][0]==lCards3[i][0]&&iMax1[0][1]==lCards3[i][1]){
lString =(String)lCards.get(i);//最大那張牌
if(i==0||i==2){
System.out.println("獲勝者是:"+manMap.get(1));
}else{
System.out.println("獲勝者是:"+manMap.get(2));
}
break;
}
}
System.out.println("--------------------------------------------");
System.out.println(manMap.get(1)+"的牌是:"+(String)lCards.get(0)+"、"+(String)lCards.get(2));
System.out.println(manMap.get(2)+"的牌是:"+(String)lCards.get(1)+"、"+(String)lCards.get(3));
System.out.println("最大的牌是:"+lString);
return lString;
}
}