//思路:定义一个PK类,定义属性花色和牌面大小,再定一个庄家类,主要进行发牌和洗牌,再定义一个玩牌规则类.
import java.util.Scanner; import java.util.Vector; public class PK_game { public static void main(String[] args) { Vector i = Banker.xiPai(); while (true) { Pk[] arr = new Pk[2]; for (int j = 1; j < 3; j++) { System.out.println("玩家" + j + "手上的牌"); Pk p = Banker.faPai(i); arr[j - 1] = p; System.out.println(p); } System.out.println(); int n = Retus.biMin(arr[0], arr[1]); System.out.println(n == 0 ? "平局" : n > 0 ? "玩家1胜" : "玩家2胜"); Scanner sc = new Scanner(System.in); String s3=sc.nextLine(); } } } package nx.road.game; public class Pk { private String color; private String pm; public Pk() { } public Pk(String color, String pm) { super(); this.color = color; this.pm = pm; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } public String getPm() { return pm; } public void setPm(String pm) { this.pm = pm; } public String toString() { StringBuffer buffer = new StringBuffer(); buffer.append(color).append(pm); return buffer.toString(); } }
package nx.road.game; import java.util.Random; import java.util.Vector; public class Banker { static String[] arr_col = { "红桃", "方块", "黑桃", "梅花" }; static String[] arr_pm = { "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A" }; public static Vector xiPai() {//洗牌方法 Vector vc = new Vector(); Pk p = null; for (int i = 0; i < arr_col.length; i++) { for (int j = 0; j < arr_pm.length; j++) { p = new Pk(arr_col[i], arr_pm[j]); vc.add(p); } } return vc; } public static Pk faPai(Vector o) {//发牌方法 Random r = new Random(); int temp = r.nextInt(o.size()); Pk p = (Pk) o.get(temp); o.remove(temp); return p; } } class Retus {//规则类 public static int biMin(Pk x, Pk y) { String s = x.getPm(); String s1 = y.getPm(); int n=biJiao(s, s1); return n; } public static int biJiao(String a, String b) { int x = 0; int y = 0; for (int j = 0; j < Banker.arr_pm.length; j++) { if (a.equals(Banker.arr_pm[j])) { x = j; } } for (int j = 0; j < Banker.arr_pm.length; j++) { if (b.equals(Banker.arr_pm[j])) { y = j; } } return x == y ? 0 : x > y ? 1 : -1; } }
|