建立了四個類和一個接口(主要是用一下接口的知識),基本實現了游戲功能,可能有一點冗余,歡迎各位大佬指出寶貴意見?。?!
/*
建立了四個類和一個接口(主要是用一下接口的知識),基本實現了游戲功能,可能有一點冗余,歡迎各位大佬指出寶貴意見!??!
*/
package com.jiang.youxi;
public class Main
{
/**
* @param args
*/
public static void main(String[] args)
{
Work work = new WorkImpl();
// 創建撲克牌
work.add1();
// 洗牌
work.xiPai();
// 創建玩家
work.wanJia();
// 發牌
work.faPai();
// 決勝負
work.panduan();
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////
package com.jiang.youxi;
public class Puke
{
private String huaSe;
private String shuZi;
public String getHuaSe()
{
return huaSe;
}
public void setHuaSe(String huaSe)
{
this.huaSe = huaSe;
}
public String getShuZi()
{
return shuZi;
}
public void setShuZi(String shuZi)
{
this.shuZi = shuZi;
}
public Puke()
{
super();
// TODO Auto-generated constructor stub
}
public Puke(String huaSe, String shuZi)
{
super();
this.huaSe = huaSe;
this.shuZi = shuZi;
}
public void tianjia()
{
}
@Override
public String toString()
{
return "Puke [huaSe=" + huaSe + ", shuZi=" + shuZi + "]";
}
}
/////////////////////////////////////////////////////////
package com.jiang.youxi;
/*
?* 玩家類
?*/
public class Wanjia
{
private String name;
private Integer id;
public Puke[] shouPai = new Puke[2];
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public Integer getId()
{
return id;
}
public void setId(Integer id)
{
this.id = id;
}
public Wanjia(String name, Integer id)
{
super();
this.name = name;
this.id = id;
}
public Wanjia()
{
super();
// TODO Auto-generated constructor stub
}
}
///////////////////////////////////////////////////////////////////
package com.jiang.youxi;
public interface Work
{
void add1();
void xiPai();
void faPai();
void wanJia();
void panduan();
}
//////////////////////////////////////
package com.jiang.youxi;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Scanner;
import java.util.Set;
public class WorkImpl implements Work
{
Scanner scanner = new Scanner(System.in);
List<Puke> list = new ArrayList<Puke>();
Puke puke[] = new Puke[52];
Wanjia[] w = new Wanjia[2];
// 創建撲克牌
@Override
public void add1()
{
System.out.println("◇◇◇創建撲克牌◇◇◇");
String[] color = new String[] { "黑桃", "紅桃", "方塊", "梅花" };
String[] number = new String[] { "2", "3", "4", "5", "6", "7", "8",
"9", "10", "J", "Q", "K", "A" };
int k = 0;
for (int i1 = 0; i1 < color.length; i1++)
{
for (int i2 = 0; i2 < number.length; i2++)
{
puke[k] = new Puke();
puke[k].setHuaSe(color[i1]);
puke[k].setShuZi(number[i2]);
k++;
}
}
// 遍歷輸出
for (Puke p : puke)
{
System.out.print(p.getHuaSe() + p.getShuZi() + " ");
}
System.out.println("\n◇◇◇創建撲克牌成功!◇◇◇\n");
}
// 分發撲克牌
@Override
public void faPai()
{
System.out.println("◇◇◇開始發牌:(一人兩張)");
// 定義的對象數組必須說明大小,否則這里會出錯
w[0].shouPai[0] = new Puke();
w[0].shouPai[0] = list.get(0);
w[1].shouPai[0] = new Puke();
w[1].shouPai[0] = list.get(1);
w[0].shouPai[1] = new Puke();
w[0].shouPai[1] = list.get(2);
w[1].shouPai[1] = new Puke();
w[1].shouPai[1] = list.get(3);
System.out.println("玩家 " + w[0].getName() + "的手牌為:"
+ w[0].shouPai[0].getHuaSe() + w[0].shouPai[0].getShuZi()
+ "? " + w[0].shouPai[1].getHuaSe()
+ w[0].shouPai[1].getShuZi());
System.out.println("玩家 " + w[1].getName() + "的手牌為:"
+ w[1].shouPai[0].getHuaSe() + w[1].shouPai[0].getShuZi()
+ "? " + w[1].shouPai[1].getHuaSe()
+ w[1].shouPai[1].getShuZi());
}
@Override
public void xiPai()
{
System.out.println("◇◇◇開始洗牌◇◇◇");
Set<Puke> set1 = new HashSet<Puke>();
// 利用set集合元素的無序性,將對象數組里面的每個對象放在set集合中
for (int i = 0; i < 52; i++)
{
set1.add(puke[i]);
}
// System.out.println("洗牌過后:");
// 然后再依次遍歷set集合,賦值給List集合,方便發牌的時候順序發牌
for (Puke puke1 : set1)
{
// 吧洗完牌的排序賦值給一個List
list.add(puke1);
System.out.print(puke1.getHuaSe() + puke1.getShuZi() + " ");
}
System.out.println("\n◇◇◇洗牌成功◇◇◇\n");
}
@Override
public void wanJia()
{
System.out.println("◇◇◇創建玩家◇◇◇");
w[0] = new Wanjia();// 創建玩家對象
System.out.println("請輸入第一個玩家 的ID:(數字)");
String idsString = scanner.nextLine();
Integer id = err(idsString);// 轉換為數字
while (id < 0)
{
System.out.println("你的輸入有問題(只能是數字)");
System.out.println("請重新輸入第一個玩家 的ID:(數字)");
idsString = scanner.nextLine();
id = err(idsString);
}
w[0].setId(id);
System.out.println("請輸入第一個玩家的姓名:");
String nameString = scanner.nextLine();
w[0].setName(nameString);
System.out.println("請輸入第二個玩家 的ID:(數字)");
w[1] = new Wanjia();// 創建玩家對象
String idsString3 = scanner.nextLine();
Integer id2 = err(idsString3);
// 判斷輸入的是否是數字
while (id2 < 0)
{
System.out.println("你的輸入有問題(只能是數字)");
System.out.println("請重新輸入第二個玩家 的ID:(數字)");
idsString3 = scanner.nextLine();
id2 = err(idsString3);
}
while (id2.equals(w[0].getId()))
{
System.out.println("玩家已經存在?。?);
System.out.println("請重新輸入第二個玩家 的ID:(數字)");
idsString3 = scanner.nextLine();
id2 = err(idsString3);
}
w[1].setId(id2);
System.out.println("請輸入第二個玩家的姓名:");
String nameString2 = scanner.next();
w[1].setName(nameString2);
System.out.println("創建玩家成功:");
System.out.println("第一個玩家 :" + "id: " + w[0].getId() + " 姓名:"
+ w[0].getName() + "\n第二個玩家 :" + "id: " + w[1].getId() + " 姓名:"
+ w[1].getName() + "\n");
}
@Override
public void panduan()
{
// 比較大小的邏輯是:花色相同的玩家手牌大于花色不同的。如果來兩個玩家的花色情況相同,則比較手牌數字。
// 每個玩家的最大手牌數字相比,較大的那個勝出。如果最大手牌相同,繼續比較小哪張手牌的數字。否則平手。
// 第一個玩家的手牌花色和數字
//
String w01 = w[0].shouPai[0].getHuaSe();
String w011 = w[0].shouPai[0].getShuZi();
String w02 = w[0].shouPai[1].getHuaSe();
String w012 = w[0].shouPai[1].getShuZi();
// 第二個玩家的手牌花色和數字
String w11 = w[1].shouPai[0].getHuaSe();
String w111 = w[1].shouPai[0].getShuZi();
String w12 = w[1].shouPai[1].getHuaSe();
String w112 = w[1].shouPai[1].getShuZi();
// 首先判斷每個玩家的手牌花色是否相同
if ((w01.equals(w02) && w11.equals(w12))
|| (!w01.equals(w02) && !w11.equals(w12)))
{
// 在此比較數字大小
// 將第一個玩家手牌的數字大小轉換為數字類型
Integer num01 = maxNumber(w011);
Integer num02 = maxNumber(w012);
// 玩家一的手牌最大的那個數字
Integer maxNum01 = Math.max(num01, num02);
Integer num11 = maxNumber(w111);
Integer num12 = maxNumber(w112);
// 玩家二的手牌最大的那個數字
Integer maxNum02 = Math.max(num11, num12);
if (maxNum01 > maxNum02)
{
System.out.println("恭喜玩家一 " + w[0].getName() + " 勝出!游戲結束!");
} else
{
System.out.println("恭喜玩家二 " + w[1].getName() + " 勝出!游戲結束!");
}
} else
{
if (w01.equals(w02) && !w11.equals(w12))
{
System.out.println("恭喜玩家一 " + w[0].getName() + " 勝出!游戲結束!");
}
if (!w01.equals(w02) && w11.equals(w12))
{
System.out.println("恭喜玩家二 " + w[1].getName() + " 勝出!游戲結束!");
}
}
}
// 自定義方法。將手牌的的數字轉換為數字類型
private Integer maxNumber(String a)
{
Integer num = 0;
if (a.equals("J"))
{
num = 11;
return num;
}
if (a.equals("Q"))
{
num = 12;
return num;
}
if (a.equals("K"))
{
num = 13;
return num;
}
if (a.equals("A"))
{
num = 1;
return num;
}
return Integer.valueOf(a);
}
// 自定義方法,判斷輸入的ID字符串是否正確
private Integer err(String num)
{
char[] ch = new char[num.length()];
ch = num.toCharArray();
boolean flag = true;
for (int i = 0; i < ch.length; i++)
{
if (ch[i] >= '1' && ch[i] <= '9')
{
flag = true;
} else
{
flag = false;
}
}
if (flag)
{
return Integer.valueOf(num);
}
return -1;
}
}
2018-10-11
我也是菜鳥一個,寫代碼每個人都有自己的思路,我不大好評價你的邏輯和語法(其實也不大懂),但是就是有一個建議,就是你的變量名或者類名,最好不要用中文的拼音,因為這樣不符合行業規范,最好是用英文,比如玩家不是用"wanjia”,而是用“player”,實在不懂得如何用英文表示,那就百度一下。。。
2018-08-30
忘了注釋掉洗牌過后的打印。