Zha Jinhua is a small game, I want to be a programmer. Most of them have played it when they were children! Now let’s take a look at this Sohu interview question! See how to use code to implement Zha Jinhua.
两个搜狐的程序员加了一个月班,终于放假了,于是他们决定扎金花渡过愉快的假期 。
There are 52 ordinary cards in total. The cards are one of 2, 3, 4, 5, 6, 7, 8, 9, 10, J, Q, K, and A. The size is increasing. Four; each player draws three cards. The two people compare the three cards in their hands, and the person with the bigger card wins.
1. Three cards that are the same are leopards
2. Three consecutive cards form a straight (A23 does not count as a straight)
3. There are only two cards that are the same pair. Leopard > Straight > Pair > Ordinary card types. When the card types are the same, compare the numerical values of the card types (such as AAA>KKK, QAK>534, QQ2> ;10104) When neither player has a special card type, compare the highest among the three cards in turn. The person with the bigger card wins. If the highest card is the same, the second highest card will be compared, and so on (such as 37K>89Q). If the two cards are the same, it will be a draw.
输入两个字符串代表两个玩家的牌(如”10KQ” “354”), 先输入的作为玩家1,后输入的作为玩家2
1 代表 玩家1赢 0 代表 平局 -1 代表 玩家2赢 -2 代表不合法的输入
KQ3 3Q9 10QA 6102 5810 7KK 632 74J 10102 K77 JKJ 926 68K 27A
1 1 -1 -1 1 1 -1
(1) Get the strings input by players 1 and 2 and determine whether they are legal
(2) After it is legal, split the string into a string array
(3) Convert the string array into an int array and sort
( 4) Determine the equality of 3 cards
(5) Compare the big and small, who loses and who wins
When there is 10, the problem of string splitting: you can judge the splitting according to the length of the string
Convert the letters For numbers: First convert all the strings you get to uppercase, so that the lowercase and uppercase letters are the same, and then directly use if to judge the return
Compare who loses and who wins : Use the method from big to small to compare, first judge whether there is a leopard, then judge the straight, then judge the pair, and finally judge the problem of handling the straight with no card type
package 搜狐面试2016; import java.util.Arrays; import java.util.Scanner; public class Test1 { public static void main(String[] args) { // 2,3,4,5,6,7,8,9,10,J,Q,K,A Scanner scanner = new Scanner(System.in); boolean isContinue=true; while (isContinue) { //1.游戏规则 System.out.println("游戏规则:共52张普通牌,牌面为2,3,4,5,6,7,8,9,10,J,Q,K,A之一,大小递增,各四张; 每人抓三张牌。两人比较手中三张牌大小,大的人获胜。"); System.out.println("对于牌型的规则如下:"); System.out.println("1.三张牌一样即为豹子"); System.out.println("2.三张牌相连为顺子(A23不算顺子)"); System.out.println("3.有且仅有两张牌一样为对子 豹子>顺子>对子>普通牌型 在牌型一样时,比较牌型数值大小"); System.out.println("谁输谁赢:1 --代表玩家1赢;0 --代表 平局 ;-1 --代表玩家2赢 ;-2 --代表不合法的输入"); //2.分别出牌 System.out.println("请玩家1出牌:"); String num1 = scanner.next(); System.out.println("请玩家2出牌:"); String num2 = scanner.next(); //3.判断是否合法 boolean flag=isValid(num1, num2); if(!flag){ //不合法 System.out.println("-2"); }else { //输入合法---先拆分字符串---再转化为int数组 //4.拆分字符串 String[] nums1=getStrArray(num1); String[] nums2=getStrArray(num2); System.out.println("拆分后的字符串数组A:"+Arrays.toString(nums1)); System.out.println("拆分后的字符串数组B:"+Arrays.toString(nums2)); //5.转化为int数组 int[] nums11=strToNumber(nums1); int[] nums22=strToNumber(nums2); System.out.println("转化为int后的数组A:"+Arrays.toString(nums11)); System.out.println("转化为int后的数组B:"+Arrays.toString(nums22)); //6.获得三张牌的相等情况 int[] equalNum11=equalNum(nums11); int[] equalNum22=equalNum(nums22); System.out.println("三张牌的相等情况--数组A:"+Arrays.toString(equalNum11)); System.out.println("三张牌的相等情况--数组B:"+Arrays.toString(equalNum22)); //7.判断输赢 int whoWin=whoWin(equalNum11, nums11, equalNum22, nums22); System.out.println(""+whoWin); } //是否继续 System.out.println("是否继续?输入N或n退出,其他任意键继续!"); String string = scanner.next(); string=string.toUpperCase(); if("N".equals(string)){ isContinue=false; } } } /*1.判断输入的内容是否合法 * 不合法两种情况:(1)出现的字符不是2,3,4,5,6,7,8,9,10,J,Q,K,A (2)每种牌只有4张,超过4张则不合法了 *方法说明: *该方法只处理情况(1),情况(2)放在判断输赢的时候处理,因为第二种情况涉及牌面转化后计算的问题*/ public static boolean isValid(String num1, String num2) { String reg = "([2-9JQKA]|10){3}";// 正则匹配,只能出现2,3,4,5,6,7,8,9,10,J,Q,K,A,并且一共只能出现3次 boolean a = num1.matches(reg); boolean b = num2.matches(reg); // 有一方不合法就返回false if (a == false || b == false) { return false; } else { // 都合法 return true; } } // 1.拆分字符串,得到三个数字 public static String[] getStrArray(String num) { // 字符串的长度和拆分后的数组 int length = num.length(); String[] nums = new String[3]; // 无论输入的J,Q,K,A是否为大写,都改为大写 num.toUpperCase(); // 字符串不含10时,长度都为3 if (length == 3) { // nums=num.split("");//使用该方法拆分会多出一个空格位--比如33a-->[,3,3,1] for (int i = 0; i 2) { nums[0] = nums[2] = "10"; nums[1] = num.substring(2, 3); } else { // 两个1距离等于2时,说明两个10是挨在一起的 if (first == 0) { nums[0] = nums[1] = "10"; nums[2] = num.substring(4); } else { nums[0] = num.substring(0, 1); nums[1] = nums[2] = "10"; } } } else { // 字符串为3个10 for (int i = 0; i b[1]) { return 1; } else if (a[1] primaryB[0]) { return 1; } else if (primaryA[0] b[1]) { return 1; } else if (a[1] thirdB) { return 1; } else if (thirdA primaryB[2]) { return 1; } else if (primaryA[2] primaryB[1]) { return 1; } else if (primaryA[1] primaryB[0]) { return 1; } else if (primaryA[0] <h2><strong>Run the test</strong></h2><p><strong>The length is illegal</strong><br><img src="https://img.php.cn/upload/article/000/000/001/383c341ff337c87b48f4468a5b9909cf-0.png" alt="How to implement the Zha Jinhua game with code"></p><p><strong>A single card 6 appears 5 times, which is illegal</strong><br><img src="https://img.php.cn/upload/article/000/000/001/112731fc5aabcfefb7340343c06be2b1-1.png" alt="How to implement the Zha Jinhua game with code"></p><p><strong>Leopard</strong><br><img src="https://img.php.cn/upload/article/000/000/001/112731fc5aabcfefb7340343c06be2b1-2.png" alt="How to implement the Zha Jinhua game with code"></p><p><strong>Shunzi and Pair</strong><br><img src="https://img.php.cn/upload/article/000/000/001/112731fc5aabcfefb7340343c06be2b1-3.png" alt="How to implement the Zha Jinhua game with code"></p><p><strong> are all letters, and Shunzi and Pair</strong><br><img src="https://img.php.cn/upload/article/000/000/001/4d65838ea7c1c524e17fdc04439ab32c-4.png" alt="How to implement the Zha Jinhua game with code"></p><p><strong> appears 10, two straights</strong><br><img src="https://img.php.cn/upload/article/000/000/001/4d65838ea7c1c524e17fdc04439ab32c-5.png" alt="How to implement the Zha Jinhua game with code"></p><p><strong> have no card type, directly compare the big and small</strong> <br><img src="https://img.php.cn/upload/article/000/000/001/4d65838ea7c1c524e17fdc04439ab32c-6.png" alt="How to implement the Zha Jinhua game with code"></p>
The above is the detailed content of How to implement the Zha Jinhua game with code. For more information, please follow other related articles on the PHP Chinese website!