阅读量:0
今天遇到一个很有意思的游戏–斗地主,那么下面就开始实现斗地主啦!!!
package Demo_YY; public class PockerIndex { public static void main(String[] args) { start(); } public static void start(){ String pocker[] = new String[54]; String colors[] = {"♠","♣","♥","♦"}; String nums[] = {"3","4","5","6","7","8","9","10","J","Q","K","A","2"}; int index = 0; for (int i = 0; i < nums.length; i++){ for (int j = 0; j < colors.length; j++){ pocker[index++] = colors[j] + nums[i]; } } // 大小王🃏🂿 pocker[index++] ="\uD83C\uDCCF"; pocker[index] ="\uD83C\uDCBF"; // 看牌 for (int i = 0; i < pocker.length; i++){ System.out.print(pocker[i]+"\t"); if((i+1) % 4 == 0 ) { System.out.println(); } } } }
int index = 0; for (int i = 0; i < colors.length; i++){ for (int j = 0; j < nums.length; j++){ pocker[index++] = colors[i] + nums[j]; } }
修改了这里之后发现什么了呢?就是顺序不一样而已啦!
这里只是存储了54张牌,下面我们开始洗牌啦!
package Demo_YY; public class PockerIndex { public static void main(String[] args) { start(); } public static void start(){ String pocker[] = new String[54]; String colors[] = {"♠","♣","♥","♦"}; String nums[] = {"3","4","5","6","7","8","9","10","J","Q","K","A","2"}; int index = 0; for (int i = 0; i < nums.length; i++){ for (int j = 0; j < colors.length; j++){ pocker[index++] = colors[j] + nums[i]; } } // 大小王🃏🂿 pocker[index++] ="\uD83C\uDCCF"; pocker[index] ="\uD83C\uDCBF"; System.out.println("\t\t\t看牌"); // 看牌 for (int i = 0; i < pocker.length; i++){ System.out.print(pocker[i]+"\t"); if((i+1) % 8 == 0 ) { System.out.println(); } } System.out.println(); System.out.println(); // 洗牌 for(int i = 0; i < pocker.length; i++){ int index1 = (int)(Math.random()*pocker.length); int index2 = (int)(Math.random()*pocker.length); String temp = pocker[index1]; pocker[index1] = pocker[index2]; pocker[index2] = temp; } System.out.println("\t\t\t洗牌后\t\t"); for (int i = 0; i < pocker.length; i++){ System.out.print(pocker[i]+"\t"); if((i+1) % 8 == 0 ) { System.out.println(); } } } }
完结,moyu_ing