diff --git a/pic/1m.png b/pic/1m.png new file mode 100644 index 0000000..dee54d0 Binary files /dev/null and b/pic/1m.png differ diff --git a/pic/1p.png b/pic/1p.png new file mode 100644 index 0000000..d74a0de Binary files /dev/null and b/pic/1p.png differ diff --git a/pic/1s.png b/pic/1s.png new file mode 100644 index 0000000..4844b48 Binary files /dev/null and b/pic/1s.png differ diff --git a/pic/1z.png b/pic/1z.png new file mode 100644 index 0000000..bd79f92 Binary files /dev/null and b/pic/1z.png differ diff --git a/pic/2m.png b/pic/2m.png new file mode 100644 index 0000000..94f75a7 Binary files /dev/null and b/pic/2m.png differ diff --git a/pic/2p.png b/pic/2p.png new file mode 100644 index 0000000..0a60ac4 Binary files /dev/null and b/pic/2p.png differ diff --git a/pic/2s.png b/pic/2s.png new file mode 100644 index 0000000..32cd465 Binary files /dev/null and b/pic/2s.png differ diff --git a/pic/2z.png b/pic/2z.png new file mode 100644 index 0000000..06b70d9 Binary files /dev/null and b/pic/2z.png differ diff --git a/pic/3m.png b/pic/3m.png new file mode 100644 index 0000000..0f6578b Binary files /dev/null and b/pic/3m.png differ diff --git a/pic/3p.png b/pic/3p.png new file mode 100644 index 0000000..19c2fda Binary files /dev/null and b/pic/3p.png differ diff --git a/pic/3s.png b/pic/3s.png new file mode 100644 index 0000000..8dd899d Binary files /dev/null and b/pic/3s.png differ diff --git a/pic/3z.png b/pic/3z.png new file mode 100644 index 0000000..ac0f188 Binary files /dev/null and b/pic/3z.png differ diff --git a/pic/4m.png b/pic/4m.png new file mode 100644 index 0000000..8097356 Binary files /dev/null and b/pic/4m.png differ diff --git a/pic/4p.png b/pic/4p.png new file mode 100644 index 0000000..79cd9d1 Binary files /dev/null and b/pic/4p.png differ diff --git a/pic/4s.png b/pic/4s.png new file mode 100644 index 0000000..9120d05 Binary files /dev/null and b/pic/4s.png differ diff --git a/pic/4z.png b/pic/4z.png new file mode 100644 index 0000000..fed7248 Binary files /dev/null and b/pic/4z.png differ diff --git a/pic/5m.png b/pic/5m.png new file mode 100644 index 0000000..105c983 Binary files /dev/null and b/pic/5m.png differ diff --git a/pic/5p.png b/pic/5p.png new file mode 100644 index 0000000..a9f69e8 Binary files /dev/null and b/pic/5p.png differ diff --git a/pic/5s.png b/pic/5s.png new file mode 100644 index 0000000..675874c Binary files /dev/null and b/pic/5s.png differ diff --git a/pic/5z.png b/pic/5z.png new file mode 100644 index 0000000..6289f58 Binary files /dev/null and b/pic/5z.png differ diff --git a/pic/6m.png b/pic/6m.png new file mode 100644 index 0000000..a4026dc Binary files /dev/null and b/pic/6m.png differ diff --git a/pic/6p.png b/pic/6p.png new file mode 100644 index 0000000..cbdcad9 Binary files /dev/null and b/pic/6p.png differ diff --git a/pic/6s.png b/pic/6s.png new file mode 100644 index 0000000..2818568 Binary files /dev/null and b/pic/6s.png differ diff --git a/pic/6z.png b/pic/6z.png new file mode 100644 index 0000000..29ab10c Binary files /dev/null and b/pic/6z.png differ diff --git a/pic/7m.png b/pic/7m.png new file mode 100644 index 0000000..53af2e1 Binary files /dev/null and b/pic/7m.png differ diff --git a/pic/7p.png b/pic/7p.png new file mode 100644 index 0000000..606ccb2 Binary files /dev/null and b/pic/7p.png differ diff --git a/pic/7s.png b/pic/7s.png new file mode 100644 index 0000000..d44adb8 Binary files /dev/null and b/pic/7s.png differ diff --git a/pic/7z.png b/pic/7z.png new file mode 100644 index 0000000..a5735d1 Binary files /dev/null and b/pic/7z.png differ diff --git a/pic/8m.png b/pic/8m.png new file mode 100644 index 0000000..ffdd241 Binary files /dev/null and b/pic/8m.png differ diff --git a/pic/8p.png b/pic/8p.png new file mode 100644 index 0000000..a53bc0d Binary files /dev/null and b/pic/8p.png differ diff --git a/pic/8s.png b/pic/8s.png new file mode 100644 index 0000000..8623448 Binary files /dev/null and b/pic/8s.png differ diff --git a/pic/9m.png b/pic/9m.png new file mode 100644 index 0000000..fb346c9 Binary files /dev/null and b/pic/9m.png differ diff --git a/pic/9p.png b/pic/9p.png new file mode 100644 index 0000000..e555ee9 Binary files /dev/null and b/pic/9p.png differ diff --git a/pic/9s.png b/pic/9s.png new file mode 100644 index 0000000..758b6d5 Binary files /dev/null and b/pic/9s.png differ diff --git a/pic/hu.png b/pic/hu.png new file mode 100644 index 0000000..788e6c1 Binary files /dev/null and b/pic/hu.png differ diff --git a/pic/lizhi.png b/pic/lizhi.png new file mode 100644 index 0000000..b30c6eb Binary files /dev/null and b/pic/lizhi.png differ diff --git a/pic/over.png b/pic/over.png new file mode 100644 index 0000000..68e564e Binary files /dev/null and b/pic/over.png differ diff --git a/pic/s5m.png b/pic/s5m.png new file mode 100644 index 0000000..847f2dd Binary files /dev/null and b/pic/s5m.png differ diff --git a/pic/s5p.png b/pic/s5p.png new file mode 100644 index 0000000..5f5b1f5 Binary files /dev/null and b/pic/s5p.png differ diff --git a/pic/s5s.png b/pic/s5s.png new file mode 100644 index 0000000..77d1982 Binary files /dev/null and b/pic/s5s.png differ diff --git a/pom.xml b/pom.xml index 94002c9..29df706 100644 --- a/pom.xml +++ b/pom.xml @@ -33,6 +33,7 @@ opencv opencv + 3.4.1 system ${basedir}/lib/opencv-341.jar diff --git a/src/main/java/com/mk/Image.java b/src/main/java/com/mk/Image.java deleted file mode 100644 index 6f83a28..0000000 --- a/src/main/java/com/mk/Image.java +++ /dev/null @@ -1,45 +0,0 @@ -package com.mk; - -import lombok.Data; - -@Data -public class Image { - private boolean[][] RGB; - private int width; - private int height; - private int total; - private int effective; - private float rate; - private String name; - - public Image(boolean[][] RGB,String name) { - this.name = name; - this.RGB = RGB; - width = RGB[0].length; - height = RGB.length; - total = width * height; - effective = 0; - for (boolean[] booleans : RGB) { - for (boolean b : booleans) { - if (b) - effective++; - } - } - rate = (float) effective / total; - } - - public boolean get(int height, int width) { - return RGB[height][width]; - } - - @Override - public String toString() { - return "Image{" + - "width=" + width + - ", height=" + height + - ", total=" + total + - ", effective=" + effective + - ", rate=" + rate + - '}'; - } -} diff --git a/src/main/java/com/mk/Main.java b/src/main/java/com/mk/Main.java index db0bca6..900c04a 100644 --- a/src/main/java/com/mk/Main.java +++ b/src/main/java/com/mk/Main.java @@ -1,14 +1,10 @@ package com.mk; -import com.mk.util.Shoupai; -import java.util.ArrayList; +import com.mk.util.CaptureScreen; public class Main { public static void main(String [] args){ - long now =System.currentTimeMillis(); - Shoupai shoupai = new Shoupai("1468m24667889p1s1z"); - System.out.println(shoupai.getRes()); - System.out.println(System.currentTimeMillis() - now); + CaptureScreen.main(args); } } diff --git a/src/main/java/com/mk/Pai.java b/src/main/java/com/mk/Pai.java deleted file mode 100644 index b03ecdf..0000000 --- a/src/main/java/com/mk/Pai.java +++ /dev/null @@ -1,76 +0,0 @@ -package com.mk; - -import lombok.Data; - -import java.util.HashMap; -import java.util.Map; - -@Data -public class Pai implements Comparable{ - private int index; - private int xts; - private int jzs; - private Map jz= new HashMap<>(); - - public Pai(int index) { - this.index = index; - } - - @Override - public String toString() { - return map(index) + - " " + xts + - " " + jzs + - " " + jz; - } - - - @Override - public int compareTo(Object o) { - Pai t = (Pai) o; - - if (jzs != t.jzs) - return t.jzs - jzs ; - boolean z = index>30; - boolean tz = t.index>30; - if (z && !tz) - return -1; - else if (!z && tz) - return 1; - - int i = index % 10; - int ti = t.index % 10; - return Math.abs(ti - 5) - Math.abs(i - 5) ; - } - - public static String map(int index){ - int type = index / 10; - int num = index % 10; - switch (type){ - case 0: - return num + "万"; - case 1: - return num + "筒"; - case 2: - return num + "索"; - case 3: - switch (num){ - case 1: - return " 东"; - case 2: - return " 南"; - case 3: - return " 西"; - case 4: - return " 北"; - case 5: - return " 白"; - case 6: - return " 发"; - case 7: - return " 中"; - } - } - return "error"; - } -} diff --git a/src/main/java/com/mk/util/CaptureScreen.java b/src/main/java/com/mk/util/CaptureScreen.java index 40115a5..d26b1eb 100644 --- a/src/main/java/com/mk/util/CaptureScreen.java +++ b/src/main/java/com/mk/util/CaptureScreen.java @@ -1,202 +1,33 @@ package com.mk.util; -import com.mk.Image; -import com.mk.Pai; import org.opencv.core.Mat; import java.awt.*; import java.awt.event.*; import java.awt.image.BufferedImage; -import java.io.File; import java.io.IOException; -import java.text.SimpleDateFormat; import java.util.*; import java.util.List; -import javax.imageio.ImageIO; import javax.swing.*; public class CaptureScreen { - public static final String path = "D:\\mj\\"; - public static List total = new ArrayList<>(); - public static Image cap; - - public static Map imageMap = new HashMap<>(); - public static Robot robot = null; - + private static Robot robot = null; static { - load(9, "m"); - load(9, "p"); - load(9, "s"); - load(7, "z"); - - - BufferedImage bufferedImage; - bufferedImage = getBufferedImageFromFile(path + "s5m.png"); - imageMap.put("s5m", getImageGRB(bufferedImage, "s5m")); - bufferedImage = getBufferedImageFromFile(path + "s5p.png"); - imageMap.put("s5p", getImageGRB(bufferedImage, "s5p")); - bufferedImage = getBufferedImageFromFile(path + "s5s.png"); - imageMap.put("s5s", getImageGRB(bufferedImage, "s5s")); - } - - public static void load(int num, String type) { - for (int i = 1; i <= num; i++) { - String name = i + type; - BufferedImage bufferedImage = getBufferedImageFromFile(path + i + type + ".png"); - imageMap.put(name, getImageGRB(bufferedImage, name)); - } - } - - public static void captureScreen(String folder, String fileName) throws Exception { - - BufferedImage image = getBufferedImage(); - // 截图保存的路径 - File screenFilePath = new File(folder); - // 如果路径不存在,则创建 - if (!screenFilePath.getParentFile().exists()) { - boolean mkdirResult = screenFilePath.getParentFile().mkdirs(); - assert mkdirResult; - } - //判断文件是否存在,不存在就创建文件 - if (!screenFilePath.exists() && !screenFilePath.isDirectory()) { - boolean mkdirResult = screenFilePath.mkdir(); - assert mkdirResult; - } - - File f = new File(screenFilePath, fileName); - ImageIO.write(image, "png", f); - //自动打开 - /*if (Desktop.isDesktopSupported() - && Desktop.getDesktop().isSupported(Desktop.Action.OPEN)) - Desktop.getDesktop().open(f);*/ - } - - /** - * 根据BufferedImage获取图片RGB数组 - * - * @param bfImage 图像 - * @return RGB - */ - public static Image getImageGRB(BufferedImage bfImage, String name) { - - - int width = bfImage.getWidth(); - int height = bfImage.getHeight(); - - - boolean[][] result = new boolean[height][width]; - for (int h = 0; h < height; h++) { - for (int w = 0; w < width; w++) { - //使用getRGB(w, h)获取该点的颜色值是ARGB,而在实际应用中使用的是RGB,所以需要将ARGB转化成RGB,即bufImg.getRGB(w, h) & 0xFFFFFF。 - int RGB = bfImage.getRGB(w, h) & 0xFFFFFF; - boolean b = getGrayFromRGB(RGB); - result[h][w] = b; - } - } - return new Image(result, name); - } - - private static boolean getGrayFromRGB(int rgb) { - int gray = (rgb / 65536 * 19595 + rgb / 256 % 256 * 38469 + rgb % 256 * 7472) >> 16; - return gray < 90; - } - - public static java.util.List searchImage(Image source, Image target) { - java.util.List points = new ArrayList(); - int targetWidth = target.getWidth() - 1; - int targetHeight = target.getHeight() - 1; - int sourceWidth = source.getWidth() - 1; - int sourceHeight = source.getHeight() - 1; - - boolean[][] sourceRGB = source.getRGB(); - - - boolean isDuplicate; -// -// for (int h = 0; h < sourceHeight - targetHeight; h++) { -// for (int w = 0; w < sourceWidth - targetWidth; w++) { - for (int h = 850; h < 950; h++) { - for (int w = 300; w < sourceWidth - targetWidth; w++) { - isDuplicate = false; - for (Point point : total) { - if (Math.abs(point.getY() - w) < 70) { - isDuplicate = true; - break; - } - } - if (isDuplicate) { - continue; - } - if (sourceRGB[h][w] - || sourceRGB[h + targetHeight][w] - || sourceRGB[h][w + targetWidth] - || sourceRGB[h + targetHeight][w + targetWidth] - ) { - } else { - if (isSame(source, target, h, w)) { - Point point = new Point(h, w); - points.add(point); - total.add(point); - w += 50; - } - } - } - } - return points; - } - - private static boolean isSame(Image source, Image target, int x, int y) { - int targetHeight = target.getHeight() - 1; - int targetWidth = target.getWidth() - 1; - - int total = targetHeight * targetWidth; - total *= target.getRate() / 2.8; - - if (target.getName().equals("9p")) - total += 100; - - int count = 0; - - for (int h = x; h < x + targetHeight; h++) { - for (int w = y; w < y + targetWidth; w++) { - if (source.get(h, w) != target.get(h - x, w - y)) { - count++; - if (count > total) { - - return false; - } - } - } - } - // System.out.println(target.getName() + ":" + count + "/" + total + " ef " + target.getEffective() + " " + target.getTotal()); - return true; - } - - private static BufferedImage getBufferedImage() { - Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); - Rectangle screenRectangle = new Rectangle(screenSize); - Robot robot = null; try { robot = new Robot(); } catch (AWTException e) { e.printStackTrace(); } - return robot.createScreenCapture(screenRectangle); } - public static BufferedImage getBufferedImageFromFile(String fileName) { - - File f = new File(fileName); - try { - return ImageIO.read(f); - } catch (IOException e) { - e.printStackTrace(); - } - return null; + private static BufferedImage getBufferedImage() { + Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); + Rectangle screenRectangle = new Rectangle(screenSize); + return robot.createScreenCapture(screenRectangle); } - public static void click(int x, int y) { + private static void click(int x, int y) { //鼠标移动到某一点 PointerInfo pinfo = MouseInfo.getPointerInfo(); @@ -220,14 +51,8 @@ public static void click(int x, int y) { public static void main(String[] args) { - try { - robot = new Robot(); - } catch (AWTException e) { - e.printStackTrace(); - } - - JFrame f = new JFrame("mj"); + JFrame f = new JFrame("雀魂牌效练习"); JRadioButton radioButton = new JRadioButton(); f.setSize(350, 250); @@ -268,10 +93,10 @@ public static void main(String[] args) { Optional max = paiList.stream().sorted().findFirst(); res.setText("[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n" + - Arrays.toString(Dask.M) + "\n" - + Arrays.toString(Dask.P) + "\n" - + Arrays.toString(Dask.S) + "\n" - + Arrays.toString(Dask.Z) + "\n" + Arrays.toString(Desk.M) + "\n" + + Arrays.toString(Desk.P) + "\n" + + Arrays.toString(Desk.S) + "\n" + + Arrays.toString(Desk.Z) + "\n" + list.toString() ); @@ -294,8 +119,6 @@ public static void main(String[] args) { } //System.out.println("推荐花费" + (System.currentTimeMillis() - timeMillis)); } - - total.clear(); } } @@ -314,41 +137,6 @@ private static String getText(List pais) { return sb.toString(); } - - public static void test() { - imageMap.forEach((s, image) -> { - System.out.println("test " + s); - - imageMap.forEach((s1, image1) -> { - - boolean[][] result = new boolean[image1.getHeight() + 100][image1.getWidth() + 100]; - for (int h = 0; h < image1.getHeight() + 100; h++) { - for (int w = 0; w < image1.getWidth() + 100; w++) { - if (h > 50 && h < image1.getHeight() + 50 && w > 50 && w < image1.getWidth() + 50) { - result[h][w] = image1.get(h - 50, w - 50); - } else { - result[h][w] = false; - } - - } - } - - Image image2 = new Image(result, ""); - - - List points = searchImage(image2, image); - total.clear(); - if (points.size() != 0) { - System.out.println(s + " = " + s1); - } - - }); - - System.out.println(" "); - }); - } - - private static List getAll(JRadioButton radioButton) { BufferedImage source = getBufferedImage(); Mat mat = null; @@ -367,10 +155,10 @@ private static List getAll(JRadioButton radioButton) { int size = pais.size(); if (size % 3 == 2) { - Arrays.fill(Dask.M, (byte) 0); - Arrays.fill(Dask.P, (byte) 0); - Arrays.fill(Dask.S, (byte) 0); - Arrays.fill(Dask.Z, (byte) 0); + Arrays.fill(Desk.M, (byte) 0); + Arrays.fill(Desk.P, (byte) 0); + Arrays.fill(Desk.S, (byte) 0); + Arrays.fill(Desk.Z, (byte) 0); SearchPic.set(mat); } return pais; @@ -407,53 +195,4 @@ private static void isHu(Mat mat) { } } - private static void search(StringBuilder sb, Image imageGRB, int num, String type) { - boolean has = false; - BufferedImage target; - for (int i = 1; i <= num; i++) { - - if (!type.equals("z") && i == 5) { - String name = "s" + i + type; - List points = searchImage(imageGRB, imageMap.get(name)); - for (Point point : points) { - //System.out.println(point); - sb.append(i); - has = true; - } - } - String name = i + type; - List points = searchImage(imageGRB, imageMap.get(name)); - for (Point point : points) { - //System.out.println(point); - sb.append(i); - has = true; - } - } - //if (has) - sb.append(type); - } - - - private static void search() { - BufferedImage source = getBufferedImageFromFile("D:\\image\\source.png"); - BufferedImage target = getBufferedImageFromFile("D:\\image\\2s.png"); - java.util.List points = searchImage(getImageGRB(source, ""), getImageGRB(target, "")); - points.forEach(System.out::println); - } - - public static void cap() { - - Date dt = new Date(); - SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); - SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMddHHmmss"); - String data = sdf.format(dt); - String rd = sdf1.format(dt); - try { - captureScreen("D:\\image\\" + data, rd + ".png"); - } catch (Exception e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } - } \ No newline at end of file diff --git a/src/main/java/com/mk/util/Dask.java b/src/main/java/com/mk/util/Dask.java deleted file mode 100644 index efd22a3..0000000 --- a/src/main/java/com/mk/util/Dask.java +++ /dev/null @@ -1,12 +0,0 @@ -package com.mk.util; - -public class Dask { - public static byte[] M = new byte[10]; - public static byte[] P = new byte[10]; - public static byte[] S = new byte[10]; - public static byte[] Z = new byte[8]; - public static byte[] PM = new byte[10]; - public static byte[] PP = new byte[10]; - public static byte[] PS = new byte[10]; - public static byte[] PZ = new byte[8]; -} diff --git a/src/main/java/com/mk/util/Desk.java b/src/main/java/com/mk/util/Desk.java index ec04464..4eaf1f8 100644 --- a/src/main/java/com/mk/util/Desk.java +++ b/src/main/java/com/mk/util/Desk.java @@ -6,11 +6,16 @@ @Data @AllArgsConstructor public class Desk { - public Desk(int[] shoupai) { - this.shoupai = shoupai; + Desk(int[] shouPai) { + this.shouPai = shouPai; } - private int[] shoupai; - private int[] baopai; - private int[] paihe; + public static byte[] M = new byte[10]; + public static byte[] P = new byte[10]; + public static byte[] S = new byte[10]; + public static byte[] Z = new byte[8]; + + private int[] shouPai; + private int[] baoPai; + private int[] paiHe; } diff --git a/src/main/java/com/mk/util/Pai.java b/src/main/java/com/mk/util/Pai.java index 2d83f7c..ac3fba0 100644 --- a/src/main/java/com/mk/util/Pai.java +++ b/src/main/java/com/mk/util/Pai.java @@ -2,42 +2,75 @@ import lombok.Data; +import java.util.HashMap; import java.util.Map; @Data -public class Pai implements Comparable { - private String name; - private int jinZhangShu; - private double value; - private Map jinZhang; - - public Pai(int number, String type) { - name = number + type; +public class Pai implements Comparable{ + private int index; + private int xts; + private int jzs; + private Map jz= new HashMap<>(); + + Pai(int index) { + this.index = index; } @Override public String toString() { - String res = name + " " + jinZhangShu; - if (jinZhang != null) - res += " " + jinZhang; - return res; + return map(index) + + " " + xts + + " " + jzs + + " " + jz; } + @Override public int compareTo(Object o) { Pai t = (Pai) o; - if (jinZhangShu != t.jinZhangShu) - return t.jinZhangShu - jinZhangShu ; - boolean z = name.contains("Z"); - boolean tz = t.name.contains("Z"); + if (jzs != t.jzs) + return t.jzs - jzs ; + boolean z = index>30; + boolean tz = t.index>30; if (z && !tz) return -1; else if (!z && tz) return 1; - int i = name.charAt(0) - 48; - int ti = t.name.charAt(0) - 48; + int i = index % 10; + int ti = t.index % 10; return Math.abs(ti - 5) - Math.abs(i - 5) ; } + + private static String map(int index){ + int type = index / 10; + int num = index % 10; + switch (type){ + case 0: + return num + "万"; + case 1: + return num + "筒"; + case 2: + return num + "索"; + case 3: + switch (num){ + case 1: + return " 东"; + case 2: + return " 南"; + case 3: + return " 西"; + case 4: + return " 北"; + case 5: + return " 白"; + case 6: + return " 发"; + case 7: + return " 中"; + } + } + return "error"; + } } diff --git a/src/main/java/com/mk/util/SearchPic.java b/src/main/java/com/mk/util/SearchPic.java index 3c38ce0..6c7d9d3 100644 --- a/src/main/java/com/mk/util/SearchPic.java +++ b/src/main/java/com/mk/util/SearchPic.java @@ -9,7 +9,6 @@ import java.awt.*; import java.awt.image.BufferedImage; import java.awt.image.DataBuffer; -import java.awt.image.DataBufferByte; import java.awt.image.DataBufferInt; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; @@ -18,25 +17,24 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; -import java.util.Random; import static org.opencv.imgproc.Imgproc.*; public class SearchPic { - public static Mat[] pa = new Mat[37]; - public static Mat[] spa = new Mat[37]; - public static Mat lizhi; - public static Point lizhidian; - public static Mat hu; - public static Mat over; + private static Mat[] pa = new Mat[37]; + private static Mat[] spa = new Mat[37]; + private static Mat lizhi; + static Point lizhidian; + private static Mat hu; + private static Mat over; static { System.loadLibrary(Core.NATIVE_LIBRARY_NAME); - final String s = "D:\\mj2\\"; + final String s = "pic\\"; final Size dsize = new Size(31, 42); for (int i = 0; i < 9; i++) { Mat tmp = Imgcodecs.imread(s + (i + 1) + "m.png"); @@ -86,10 +84,7 @@ public class SearchPic { over = Imgcodecs.imread(s + "over.png"); } - private static List colorPaiChi; - private static List colorFL; - - public static Mat xiuzheng(Mat src) { + private static Mat xiuzheng(Mat src) { MatOfPoint2f srcTri = new MatOfPoint2f(( new Point(0, 0)), @@ -109,11 +104,7 @@ public static Mat xiuzheng(Mat src) { return dst; } - public static Mat getShouPai(Mat source) { - return source.submat(750, 880, 650, 1350); - } - - public static boolean isHu(Mat source) { + static boolean isHu(Mat source) { Mat huqu = source.submat(570, 633, 133, 188); @@ -124,15 +115,12 @@ public static boolean isHu(Mat source) { double[] doubles = g_result.get((int) maxLoc.y, (int) maxLoc.x); double v = doubles[0]; - if (v > 0.99) { - return true; - } - return false; + return v > 0.99; } - public static boolean isOver(Mat source) { + static boolean isOver(Mat source) { Mat overqu = source.submat(927, 1018, 1600, 1750); @@ -143,10 +131,7 @@ public static boolean isOver(Mat source) { double[] doubles = g_result.get((int) maxLoc.y, (int) maxLoc.x); double v = doubles[0]; - if (v > 0.98) { - return true; - } - return false; + return v > 0.98; } @@ -189,7 +174,7 @@ static List getsPai(Mat source) { return res; } - public static List getPaiChi(Mat source) { + private static List getPaiChi(Mat source) { Mat mat1 = source.submat(397, 547, 825, 1074); Mat mat2 = source.submat(548, 816, 685, 835); Mat mat3 = source.submat(527, 778, 1082, 1232); @@ -205,7 +190,7 @@ public static List getPaiChi(Mat source) { source.submat(788, 938, 842, 1091)); } - public static List getFL(Mat source) { + private static List getFL(Mat source) { // BufferedImage bufferedImage = Mat2BufImg(source); Mat mat1 = source.submat(239, 322, 520, 1010); // bufferedImage = Mat2BufImg(mat1); @@ -271,23 +256,10 @@ private static List getFLPaiBySample(Mat source, Mat colorSource) { return res; } - private static int getCount(Mat source) { - - int count = 0; - for (int i = 5; i < source.cols(); i++) { - if (Core.countNonZero(source.col(i)) < 9) { - count++; - i += 5; - } - } - return count; - } - private static List getSinglePai(Mat source, Mat colorSource) { List res = new ArrayList<>(); int next = 0; - int count = 0; for (int i = 0; i < source.cols(); i++) { if (Core.countNonZero(source.col(i)) < 17) { if (i < 5) { @@ -295,7 +267,6 @@ private static List getSinglePai(Mat source, Mat colorSource) { continue; } Mat ge; - count++; if ((i - next) < 52 && (i - next) > 30) { if ((i - next) > 44) { @@ -331,7 +302,6 @@ private static List getSinglesPai(Mat source, Mat colorSource) { List res = new ArrayList<>(); int next = 0; - int count = 0; for (int i = 0; i < source.cols(); i++) { if (Core.countNonZero(source.col(i)) < 65) { if (i < 5) { @@ -339,7 +309,6 @@ private static List getSinglesPai(Mat source, Mat colorSource) { continue; } Mat ge; - count++; if ((i - next) < 120) { if ((i - next) > 75) { ge = colorSource.submat(0, source.rows(), next + 1, i); @@ -363,77 +332,6 @@ private static boolean isLiZhi(Mat source) { return false; } - public static List getPai(Mat source) { - - BufferedImage image; - - source.convertTo(source, CvType.CV_32SC1); - - List pointList = new ArrayList<>(); - Mat hierarchy = new Mat(); - - // image = Mat2BufImg(source); - Imgproc.findContours(source, pointList, hierarchy, Imgproc.RETR_CCOMP, Imgproc.CHAIN_APPROX_SIMPLE); - - for (int i = 0; i < pointList.size(); i++) { - - double[] ds = hierarchy.get(0, i); - if (ds[2] != -1) { - continue; - } - if (ds[0] == -1) { - continue; - } - MatOfPoint matOfPoint = pointList.get(i); - List points = matOfPoint.toList(); - // 36-150 - if (points.get(0).y > source.rows() / 2 && points.size() > 100 || points.size() > 150 || points.size() < 10) - continue; - int maxX = -1; - int minX = 2000; - int maxY = -1; - int minY = 2000; - - Point maxYp = points.get(0); - for (Point point : points) { - int px = (int) point.x; - int py = (int) point.y; - if (px > maxX) - maxX = px; - if (px < minX) - minX = px; - if (py > maxY) { - maxYp = point; - maxY = py; - } - if (py < minY) - minY = py; - } - - double x = maxX - minX; - double y = maxY - minY; - if (x < 80 && y < 80 && Math.min(x, y) > 32 && Math.max(x, y) > 35) { - //if (x < 150 && y < 150 && Math.min(x, y) > 75 && Math.max(x, y) > 35) { -// if((maxYp.x - minX)<20){ -// int finalMaxX = maxX; -// Point point1 = points.stream().filter(point -> (finalMaxX - point.x) < 20).max((o1, o2) -> (int) (o1.y - o2.y)).get(); -// maxY = (int)point1.y; -// }else { -// int finalMinX = minX; -// Point point1 = points.stream().filter(point -> (point.x - finalMinX) < 20).max((o1, o2) -> (int) (o1.y - o2.y)).get(); -// maxY = (int)point1.y; -// } - - //Mat tmp = source.submat(minY + 25, maxY - 5, minX + 3, maxX - 2); - Mat tmp = source.submat(minY + 1, maxY, minX + 1, maxX); - //Imgcodecs.imwrite("D:/image/tmp/" + i + "-" + points.size() + ".png", tmp); - //Imgproc.drawContours(source,pointList,i,new Scalar(r.nextInt()%255 ,r.nextInt()%255 ,r.nextInt()%255 ),-1); - //image = Mat2BufImg(source); - } - } - //image = Mat2BufImg(source); - return null; - } public static Mat BufImg2Mat(BufferedImage original, int imgType, int matType) { if (original == null) { @@ -463,7 +361,7 @@ public static Mat BufImg2Mat(BufferedImage original, int imgType, int matType) { return mat; } - public static Mat BufferedImage2Mat(BufferedImage image) throws IOException { + static Mat BufferedImage2Mat(BufferedImage image) throws IOException { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); ImageIO.write(image, "jpg", byteArrayOutputStream); byteArrayOutputStream.flush(); @@ -471,7 +369,7 @@ public static Mat BufferedImage2Mat(BufferedImage image) throws IOException { } - public static void set(Mat image) { + static void set(Mat image) { long now = System.currentTimeMillis(); //Mat source = Imgcodecs.imread("D:/image/test4.png"); @@ -492,9 +390,9 @@ public static void set(Mat image) { // Imgcodecs.imwrite("D:/image/tmp/res555.png", dst); final List paiChis = getPaiChi(dst); - colorPaiChi = getPaiChi(image); + List colorPaiChi = getPaiChi(image); final List fl = getFL(dst); - colorFL = getFL(image); + List colorFL = getFL(image); for (int i = 0; i < paiChis.size(); i++) { @@ -523,7 +421,7 @@ public static void set(Mat image) { //System.out.println("搜索牌池:" + (System.currentTimeMillis() - now)); } - private static String search(Mat source) { + private static void search(Mat source) { // BufferedImage bufferedImage = Mat2BufImg(source); Mat g_result = new Mat(); @@ -558,19 +456,18 @@ private static String search(Mat source) { } switch (i) { case 0: - Dask.M[num]++; - return num + "万: " + max; + Desk.M[num]++; + return; case 1: - Dask.P[num]++; - return num + "筒: " + max; + Desk.P[num]++; + return; case 2: - Dask.S[num]++; - return num + "索: " + max; + Desk.S[num]++; + return; case 3: - Dask.Z[num]++; - return num + "z: " + max; + Desk.Z[num]++; + return; default: - return ""; } } diff --git a/src/main/java/com/mk/util/Shoupai.java b/src/main/java/com/mk/util/Shoupai.java deleted file mode 100644 index 2e9657e..0000000 --- a/src/main/java/com/mk/util/Shoupai.java +++ /dev/null @@ -1,950 +0,0 @@ -package com.mk.util; - -import com.sun.istack.internal.NotNull; - -import java.util.*; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -public class Shoupai { - private byte[] M = new byte[10]; - private byte[] P = new byte[10]; - private byte[] S = new byte[10]; - private byte[] Z = new byte[8]; - - int[] pos = new int[14]; - private int level = 0; - private List ps = Collections.singletonList(new Pai(0, "t")); - - private String str; - //private static Long count = 0L; - - - public Shoupai(String s) { - str = s; - Pattern p = Pattern.compile("([0-9]*?)m([0-9]*)p([0-9]*)s([0-9]*)z"); - - Matcher m = p.matcher(s); - - if (m.find()) { - char[] ms = m.group(1).toCharArray(); - char[] ps = m.group(2).toCharArray(); - char[] ss = m.group(3).toCharArray(); - char[] zs = m.group(4).toCharArray(); - for (char c : ms) { - M[(int) c - 48]++; - } - for (char c : ps) { - P[(int) c - 48]++; - } - for (char c : ss) { - S[(int) c - 48]++; - } - for (char c : zs) { - Z[(int) c - 48]++; - } - } -// fuzhang(M); -// fuzhang(P); -// fuzhang(S); - } - - public boolean isWin() { - //count++; -// int key = AgariIndex.calc_key(M, P, S, Z); -// int[] ret = AgariIndex.agari(key); -// return ret != null; - return search(M, 10) || search(P, 10) || search(S, 10) || search(Z, 8); - } - - public boolean isTingPai() { - List pais = tingPai(); - if (pais.size() != 0) - return true; - return false; - } - - public boolean isT1() { - - int i = XiangTingNotList.getXT(M, P, S, Z); - return (i == 1); - } - - public boolean isT2() { - - int i = XiangTingNotList.getXT(M, P, S, Z); - return (i == 2); - } - - public boolean isT3() { - - int i = XiangTingNotList.getXT(M, P, S, Z); - return (i == 3); - } - - public List tingPai() { - //返回打出当前每张牌后,该牌再来N张时的情况 - List res = new ArrayList<>(); - - search(M, "M", 10, res); - if (res.size() > 0 && level > 1) - return res; - search(P, "P", 10, res); - if (res.size() > 0 && level > 1) - return res; - search(S, "S", 10, res); - if (res.size() > 0 && level > 1) - return res; - search(Z, "Z", 8, res); - return res; - } - - public List t1() { - List res = new ArrayList<>(); - - List m = search1t(M, "M", 10); - if (m.size() > 0 && level > 2) - return m; - res.addAll(m); - List p = search1t(P, "P", 10); - if (p.size() > 0 && level > 2) - return p; - res.addAll(p); - List s = search1t(S, "S", 10); - if (s.size() > 0 && level > 2) - return s; - res.addAll(s); - List z = search1t(Z, "Z", 8); - if (z.size() > 0 && level > 2) - return z; - res.addAll(z); - - return res; - } - - public List t2() { - List res = new ArrayList<>(); - - List m = search2t(M, "M", 10); - if (m.size() > 0 && level > 3) - return m; - res.addAll(m); - List p = search2t(P, "P", 10); - if (p.size() > 0 && level > 3) - return p; - res.addAll(p); - List s = search2t(S, "S", 10); - if (s.size() > 0 && level > 3) - return s; - res.addAll(s); - List z = search2t(Z, "Z", 8); - if (z.size() > 0 && level > 3) - return z; - res.addAll(z); - return res; - } - - public List t3() { - List res = new ArrayList<>(); - - res.addAll(search3t(M, "M", 10)); - res.addAll(search3t(P, "P", 10)); - res.addAll(search3t(S, "S", 10)); - res.addAll(search3t(Z, "Z", 8)); - - return res; - } - - public List t4() { - List res = new ArrayList<>(); - - res.addAll(search4t(M, "M", 10)); - res.addAll(search4t(P, "P", 10)); - res.addAll(search4t(S, "S", 10)); - res.addAll(search4t(Z, "Z", 8)); - - return res; - } - - private void search(byte[] Q, String name, int num, List res) { - for (int i = 1; i < num; i++) { - if (Q[i] > 0) { - - Q[i]--; - Map m = addToWin(M, "m", 10); - if (level > 1 && m.size() > 0) { - Q[i]++; - return; - } - Map jinZhang = new HashMap<>(m); - Map p = addToWin(P, "p", 10); - if (level > 1 && p.size() > 0) { - Q[i]++; - return; - } - jinZhang.putAll(p); - Map s = addToWin(S, "s", 10); - if (level > 1 && s.size() > 0) { - Q[i]++; - return; - } - jinZhang.putAll(s); - Map z = addToWin(Z, "z", 8); - if (level > 1 && z.size() > 0) { - Q[i]++; - return; - } - jinZhang.putAll(z); - Q[i]++; - Pai pai = new Pai(i, name); - pai.setJinZhang(jinZhang); - if (jinZhang.size() > 0) { - int jzs = 0; - for (Integer value : jinZhang.values()) { - jzs += value; - } - pai.setJinZhangShu(jzs); - res.add(pai); - } - } - } - } - - private List search1t(byte[] Q, String name, int num) { - List res = new ArrayList<>(); - for (int i = 1; i < num; i++) { - if (Q[i] > 0) { - - Q[i]--; - Map jinZhang = new HashMap<>(); - Map m = addTo1t(M, "m", 10); - if (level > 2 && m.size() > 0) { - Q[i]++; - return ps; - } - jinZhang.putAll(m); - Map p = addTo1t(P, "p", 10); - if (level > 2 && p.size() > 0) { - Q[i]++; - return ps; - } - jinZhang.putAll(p); - Map s = addTo1t(S, "s", 10); - if (level > 2 && s.size() > 0) { - Q[i]++; - return ps; - } - jinZhang.putAll(s); - Map z = addTo1t(Z, "z", 8); - if (level > 2 && z.size() > 0) { - Q[i]++; - return ps; - } - jinZhang.putAll(z); - Q[i]++; - Pai pai = new Pai(i, name); - pai.setJinZhang(jinZhang); - if (jinZhang.size() > 0) { - int jzs = 0; - for (Integer value : jinZhang.values()) { - jzs += value; - } - pai.setJinZhangShu(jzs); - res.add(pai); - } - } - } - return res; - } - - private List search2t(byte[] Q, String name, int num) { - List res = new ArrayList<>(); - for (int i = 1; i < num; i++) { - if (Q[i] > 0) { - - Q[i]--; - Map jinZhang = new HashMap<>(); - Map m = addTo2t(M, "m", 10); - if (level > 3 && m.size() > 0) { - Q[i]++; - return ps; - } - jinZhang.putAll(m); - Map p = addTo2t(P, "p", 10); - if (level > 3 && p.size() > 0) { - Q[i]++; - return ps; - } - jinZhang.putAll(p); - Map s = addTo2t(S, "s", 10); - if (level > 3 && s.size() > 0) { - Q[i]++; - return ps; - } - jinZhang.putAll(s); - Map z = addTo2t(Z, "z", 8); - if (level > 3 && z.size() > 0) { - Q[i]++; - return ps; - } - jinZhang.putAll(z); - Q[i]++; - Pai pai = new Pai(i, name); - pai.setJinZhang(jinZhang); - if (jinZhang.size() > 0) { - int jzs = 0; - for (Integer value : jinZhang.values()) { - jzs += value; - } - pai.setJinZhangShu(jzs); - res.add(pai); - } - } - } - return res; - } - - - private List search2taaa(byte[] Q, String name, int num) { - List res = new ArrayList<>(); - for (int i = 1; i < num; i++) { - if (Q[i] > 0) { - - Q[i]--; - Map jinZhang = new HashMap<>(); - Map m = addTo2t(M, "m", 10); - if (level > 3 && m.size() > 0) { - Q[i]++; - return ps; - } - jinZhang.putAll(m); - Map p = addTo2t(P, "p", 10); - if (level > 3 && p.size() > 0) { - Q[i]++; - return ps; - } - jinZhang.putAll(p); - Map s = addTo2t(S, "s", 10); - if (level > 3 && s.size() > 0) { - Q[i]++; - return ps; - } - jinZhang.putAll(s); - Map z = addTo2t(Z, "z", 8); - if (level > 3 && z.size() > 0) { - Q[i]++; - return ps; - } - jinZhang.putAll(z); - Q[i]++; - Pai pai = new Pai(i, name); - pai.setJinZhang(jinZhang); - if (jinZhang.size() > 0) { - int jzs = 0; - for (Integer value : jinZhang.values()) { - jzs += value; - } - pai.setJinZhangShu(jzs); - res.add(pai); - } - } - } - return res; - } - - private List search3t(byte[] Q, String name, int num) { - List res = new ArrayList<>(); - for (int i = 1; i < num; i++) { - if (Q[i] > 0) { - //便利依次切出手牌 - - Q[i]--; - Map jinZhang = new HashMap<>(); - jinZhang.putAll(addTo3t(M, "m", 10)); - jinZhang.putAll(addTo3t(P, "p", 10)); - jinZhang.putAll(addTo3t(S, "s", 10)); - jinZhang.putAll(addTo3t(Z, "z", 8)); - Q[i]++; - Pai pai = new Pai(i, name); - pai.setJinZhang(jinZhang); - if (jinZhang.size() > 0) { - int jzs = 0; - for (Integer value : jinZhang.values()) { - jzs += value; - } - pai.setJinZhangShu(jzs); - res.add(pai); - } - } - } - return res; - } - - private List search4t(byte[] Q, String name, int num) { - List res = new ArrayList<>(); - for (int i = 1; i < num; i++) { - if (Q[i] > 0) { - //便利依次切出手牌 - - Q[i]--; - Map jinZhang = new HashMap<>(); - jinZhang.putAll(addTo4t(M, "m", 10)); - jinZhang.putAll(addTo4t(P, "p", 10)); - jinZhang.putAll(addTo4t(S, "s", 10)); - jinZhang.putAll(addTo4t(Z, "z", 8)); - Q[i]++; - Pai pai = new Pai(i, name); - pai.setJinZhang(jinZhang); - if (jinZhang.size() > 0) { - int jzs = 0; - for (Integer value : jinZhang.values()) { - jzs += value; - } - pai.setJinZhangShu(jzs); - res.add(pai); - } - } - } - return res; - } - - private Map addToWin(byte[] Q, String name, int num) { - - Map jinZhang = new HashMap<>(); - boolean z = name.equals("z"); - for (int j = 1; j < num; j++) { - if (!z) - switch (j) { - case 1: - if (Q[1] == 0 && Q[2] == 0) - continue; - break; - case 9: - if (Q[9] == 0 && Q[8] == 0) - continue; - break; - default: - if (Q[j - 1] == 0 && Q[j] == 0 && Q[j + 1] == 0) - continue; - break; - } - else if (Q[j] != 2) - continue; - - int q = getQFromName(name, j); - - if (Q[j] < (4 - q)) { - Q[j]++; - if (isWin()) { - jinZhang.put(j + name, 5 - q - Q[j]); - if (level > 1) { - Q[j]--; - return jinZhang; - } - } - Q[j]--; - } - } - return jinZhang; - } - - @NotNull - private int getQFromName(String name, int j) { - switch (name) { - case "m": - case "M": - return Dask.M[j] + Dask.PM[j]; - case "p": - case "P": - return Dask.P[j] + Dask.PP[j]; - case "s": - case "S": - return Dask.S[j] + Dask.PS[j]; - case "z": - case "Z": - default: - return Dask.Z[j] + Dask.PZ[j]; - } - } - - private Map addTo1t(byte[] Q, String name, int num) { - - Map jinZhang = new HashMap<>(); - for (int j = 1; j < num; j++) { - int q = getQFromName(name, j); - - if (Q[j] < (4 - q)) { - Q[j]++; - if (isTingPai()) { - jinZhang.put(j + name, 5 - q - Q[j]); - if (level > 2) { - Q[j]--; - return jinZhang; - } - } - Q[j]--; - } - } - return jinZhang; - } - - private Map addTo2t(byte[] Q, String name, int num) { - - Map jinZhang = new HashMap<>(); - for (int j = 1; j < num; j++) { - int q = getQFromName(name, j); - - if (Q[j] < (4 - q)) { - Q[j]++; - if (isT1()) { - jinZhang.put(j + name, 5 - q - Q[j]); - if (level > 3) { - Q[j]--; - return jinZhang; - } - } - Q[j]--; - } - } - return jinZhang; - } - - private Map addTo3t(byte[] Q, String name, int num) { - - Map jinZhang = new HashMap<>(); - for (int j = 1; j < num; j++) { - int q = getQFromName(name, j); - - if (Q[j] < (4 - q)) { - Q[j]++; - if (isT2()) { - jinZhang.put(j + name, 5 - q - Q[j]); - if (level > 4) { - Q[j]--; - return jinZhang; - } - } - Q[j]--; - } - } - return jinZhang; - } - - private Map addTo4t(byte[] Q, String name, int num) { - - Map jinZhang = new HashMap<>(); - for (int j = 1; j < num; j++) { - int q = getQFromName(name, j); - - if (Q[j] < (4 - q)) { - Q[j]++; - if (isT3()) { - jinZhang.put(j + name, 5 - q - Q[j]); - if (level > 4) { - Q[j]--; - return jinZhang; - } - } - Q[j]--; - } - } - return jinZhang; - } - - private boolean search(byte[] m, int num) { - for (int i = 1; i < num; i++) { - if (m[i] >= 2) { - m[i] -= 2; - if (isToWin()) { - m[i] += 2; - return true; - } else { - m[i] += 2; - } - } - } - return false; - } - - private boolean isToWin() { - return searchMPSZ(M) && searchMPSZ(P) && searchMPSZ(S) && searchZ(Z); - } - - private boolean searchZ(byte[] z) { - for (int i = 1; i < 8; i++) { - switch (z[i]) { - case 0: - break; - case 1: - case 2: - return false; - case 3: - case 4: - z[i] -= 3; - if (isToWin()) { - z[i] += 3; - return true; - } else { - z[i] += 3; - return false; - } - default: - return false; - } - } - return true; - } - - private boolean searchMPSZ(byte[] Q) { - //检查该组 - int size = 0; - for (int i = 1; i < 10; i++) { - if (Q[i] == 0) { - if (size % 3 != 0) - return false; - } else { - size += Q[i]; - } - } - if (size % 3 != 0) - return false; - - for (int i = 1; i < 10; i++) { - switch (Q[i]) { - case -1: - case 0: - break; - case 1: - case 2: - return isKezi(Q, i); - case 3: - Q[i] -= 3; - if (isToWin()) { - Q[i] += 3; - return true; - } else { - Q[i] += 3; - return isKezi(Q, i); - } - case 4: - Q[i] -= 3; - if (isToWin()) { - Q[i] += 3; - return true; - } else { - Q[i] += 3; - return false; - } - } - } - return true; - } - - private boolean isKezi(byte[] Q, int i) { - switch (i) { - case 1: - if (Q[2] > 0 && Q[3] > 0) { - sub(Q, 1); - if (isToWin()) { - add(Q, 1); - return true; - } else { - add(Q, 1); - return false; - } - } else { - return false; - } - case 2: - if (Q[1] > 0 && Q[3] > 0) { - sub(Q, 1); - if (isToWin()) { - add(Q, 1); - return true; - } else { - add(Q, 1); - if (Q[3] > 0 && Q[4] > 0) { - sub(Q, 2); - if (isToWin()) { - add(Q, 2); - return true; - } else { - add(Q, 2); - return false; - } - } else { - return false; - } - } - } else if (Q[3] > 0 && Q[4] > 0) { - sub(Q, 2); - if (isToWin()) { - add(Q, 2); - return true; - } else { - add(Q, 2); - return false; - } - } else { - return false; - } - case 3: - case 4: - case 5: - case 6: - case 7: - if (Q[i - 2] > 0 && Q[i - 1] > 0) { - sub(Q, i - 2); - if (isToWin()) { - add(Q, i - 2); - return true; - } else { - add(Q, i - 2); - if (Q[i - 1] > 0 && Q[i + 1] > 0) { - sub(Q, i - 1); - if (isToWin()) { - add(Q, i - 1); - return true; - } else { - add(Q, i - 1); - if (Q[i + 1] > 0 && Q[i + 2] > 0) { - sub(Q, i); - if (isToWin()) { - add(Q, i); - return true; - } else { - add(Q, i); - return false; - } - } else { - return false; - } - } - } else if (Q[i + 1] > 0 && Q[i + 2] > 0) { - sub(Q, i); - if (isToWin()) { - add(Q, i); - return true; - } else { - add(Q, i); - return false; - } - } else { - return false; - } - } - } else if (Q[i - 1] > 0 && Q[i + 1] > 0) { - sub(Q, i - 1); - if (isToWin()) { - add(Q, i - 1); - return true; - } else { - add(Q, i - 1); - if (Q[i + 1] > 0 && Q[i + 2] > 0) { - sub(Q, i); - if (isToWin()) { - add(Q, i); - return true; - } else { - add(Q, i); - return false; - } - } else { - return false; - } - } - } else if (Q[i + 1] > 0 && Q[i + 2] > 0) { - sub(Q, i); - if (isToWin()) { - add(Q, i); - return true; - } else { - add(Q, i); - return false; - } - } else { - return false; - } - case 8: - if (Q[9] > 0 && Q[7] > 0) { - sub(Q, 7); - if (isToWin()) { - add(Q, 7); - return true; - } else { - add(Q, 7); - if (Q[7] > 0 && Q[6] > 0) { - sub(Q, 6); - if (isToWin()) { - add(Q, 6); - return true; - } else { - add(Q, 6); - return false; - } - } else { - return false; - } - } - } else if (Q[7] > 0 && Q[6] > 0) { - sub(Q, 6); - if (isToWin()) { - add(Q, 6); - return true; - } else { - add(Q, 6); - return false; - } - } else { - return false; - } - case 9: - if (Q[8] > 0 && Q[7] > 0) { - sub(Q, 7); - if (isToWin()) { - add(Q, 7); - return true; - } else { - add(Q, 7); - return false; - } - } else { - return false; - } - - } - System.out.println("ERROR 01"); - return false; - } - - public void add(byte[] a, int i) { - a[i]++; - a[i + 1]++; - a[i + 2]++; - } - - public void sub(byte[] a, int i) { - a[i]--; - a[i + 1]--; - a[i + 2]--; - } - - public void fuzhang(byte[] Q) { - for (int i = 1; i < 10; i++) { - switch (i) { - case 1: - if (Q[1] <= 0 && Q[2] <= 0 && Q[3] <= 0) - Q[1] = -1; - break; - case 9: - if (Q[7] <= 0 && Q[8] <= 0 && Q[9] <= 0) - Q[9] = -1; - break; - case 2: - if (Q[1] <= 0 && Q[2] <= 0 && Q[3] <= 0 && Q[4] <= 0) - Q[2] = -1; - break; - case 8: - if (Q[6] <= 0 && Q[7] <= 0 && Q[8] <= 0 && Q[9] <= 0) - Q[8] = -1; - break; - default: - if (Q[i - 2] <= 0 && Q[i - 1] <= 0 && Q[i] <= 0 && Q[i + 1] <= 0 && Q[i + 2] <= 0) - Q[i] = -1; - } - } - } - - - public Pai getRes() { - - level = 0; - boolean win = isWin(); - if (win) { - return null; - } - System.out.println(); - - List tingPai = tingPai(); - Optional pai0 = tingPai.stream().sorted().findFirst(); - if (pai0.isPresent()) { - return pai0.get(); - } - - List t1 = t1(); - //t1.forEach(System.out::println); - Optional pai1 = t1.stream().sorted().findFirst(); - if (pai1.isPresent()) { - return pai1.get(); - } - - List t2 = t2(); - //t2.forEach(System.out::println); - Optional pai2 = t2.stream().sorted().findFirst(); - if (pai2.isPresent()) { - return pai2.get(); - } - - List t3 = t3(); - //t3.forEach(System.out::println); - Optional pai3 = t3.stream().sorted().findFirst(); - if (pai3.isPresent()) { - return pai3.get(); - } - - List t4 = t4(); - //t4.forEach(System.out::println); - Optional pai4 = t4.stream().sorted().findFirst(); - return pai4.orElse(null); - - } - - public static void main(String[] args) throws InterruptedException { - Shoupai shoupai = new Shoupai("77m45889p8s444z"); - int i = XiangTingShu.getXT(shoupai.M, shoupai.P, shoupai.S, shoupai.Z); - System.out.println(i); - - - while (true) { - long start = System.currentTimeMillis(); - boolean win = shoupai.isWin(); - if (win) { - shoupai.level = 0; - System.out.println("Win"); - System.out.println("time:" + (System.currentTimeMillis() - start)); - return; - } - if (shoupai.isTingPai()) { - System.out.println("听牌 "); - for (Pai p : shoupai.tingPai()) { - System.out.println(p); - } - System.out.println("time:" + (System.currentTimeMillis() - start)); - return; - } - if (shoupai.isT1()) { - System.out.println("一向听"); - for (Pai p : shoupai.t1()) { - System.out.println(p); - } - System.out.println("time:" + (System.currentTimeMillis() - start)); - return; - } - if (shoupai.isT2()) { - System.out.println("两向听"); - for (Pai p : shoupai.t2()) { - System.out.println(p); - } - System.out.println("time:" + (System.currentTimeMillis() - start)); - return; - } - start = System.currentTimeMillis(); - System.out.println("三向听"); - for (Pai p : shoupai.t3()) { - System.out.println(p); - } - System.out.println("time:" + (System.currentTimeMillis() - start)); - return; - } - } -} diff --git a/src/main/java/com/mk/util/XiangTing.java b/src/main/java/com/mk/util/XiangTing.java index 991626e..886e0f4 100644 --- a/src/main/java/com/mk/util/XiangTing.java +++ b/src/main/java/com/mk/util/XiangTing.java @@ -6,7 +6,7 @@ public class XiangTing { private static int S; - private static int count; + public static void main(String[] args) { int[] shouPai = {1, 4, 7, 8,9, 14, 15, 18, 22, 25, 28, 29, 32, 32}; long l = System.currentTimeMillis(); @@ -14,7 +14,7 @@ public static void main(String[] args) { System.out.println(System.currentTimeMillis() - l); } - public static int getXiangTing(int [] shouPai){ + private static int getXiangTing(int[] shouPai){ S = 9; takeMianZi(shouPai, new ArrayList<>(), 0, 4 - (shouPai.length - 2) / 3); return S; @@ -37,7 +37,6 @@ private static void takeMianZi(int[] shouPai, List mianzi, int i, int m) private static void takeDaZi(int[] shouPai, List mianzi, List dazi, int[] duzi, int i, int m, int p, int d) { if (i >= shouPai.length - 1 || d + m == 4) { - count++; d = Math.min(d, 4 - m); int mys = 9 - 2 * m - d - p; S = Math.min(S, mys); diff --git a/src/main/java/com/mk/util/XiangTingNotList.java b/src/main/java/com/mk/util/XiangTingNotList.java index e2a0922..95276d3 100644 --- a/src/main/java/com/mk/util/XiangTingNotList.java +++ b/src/main/java/com/mk/util/XiangTingNotList.java @@ -2,10 +2,6 @@ import java.util.*; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -import com.mk.Pai; public class XiangTingNotList { private static int S; @@ -17,35 +13,11 @@ public static void main(String[] args) { long l = System.currentTimeMillis(); List pais = get(desk); System.out.println(System.currentTimeMillis() - l); + assert pais != null; pais.stream().sorted(Comparator.comparing(Pai::getJzs).reversed()).forEach(System.out::println); } - public static int getXT(byte[]... a) { - List list = new ArrayList<>(); - for (int i = 0; i < 3; i++) { - byte[] bytes = a[i]; - for (int j = 1; j <= 9; j++) { - if (bytes[j] > 0) { - for (int k = 0; k < bytes[j]; k++) { - list.add(j + i * 10); - } - } - } - } - for (int i = 1; i <= 7; i++) { - if (a[3][i] > 0) { - for (int k = 0; k < a[3][i]; k++) { - list.add(i + 30); - } - } - - } - int[] shoupai = list.stream().mapToInt(i -> i).toArray(); - return getXiangTing(shoupai) - 1; - } - - private static int[] transform(int[] shoupai) { int[] res = new int[38]; for (int i : shoupai) { @@ -77,43 +49,35 @@ private static int getXiangTing(int[] shouPaib) { } - public static List get(Desk desk) { - int[] shoupai = desk.getShoupai(); - int[] paihe = desk.getPaihe(); - int[] allShouPai = transform(shoupai); + private static List get(Desk desk) { + int[] shouPai = desk.getShouPai(); + int[] paiHe = desk.getPaiHe(); + int[] allShouPai = transform(shouPai); List pais = new ArrayList<>(); - int XT = getXiangTing(shoupai); + int XT = getXiangTing(shouPai); if (XT == 0) return null; - for (int i = 0; i < shoupai.length; i++) { - final int index = shoupai[i]; + for (int i = 0; i < shouPai.length; i++) { + final int index = shouPai[i]; if (pais.stream().anyMatch(pai -> pai.getIndex() == index)) { continue; } Pai pai = new Pai(index); pai.setXts(XT - 1); - int tmp = shoupai[i]; + int tmp = shouPai[i]; int jzNum = 0; for (int j = 1; j <= 37; j++) { if (j % 10 == 0) continue; - shoupai[i] = j; - int nextXT = getXiangTing(shoupai); + shouPai[i] = j; + int nextXT = getXiangTing(shouPai); if (nextXT < XT) { - int jz = 4 - paihe[j] - allShouPai[j]; + int jz = 4 - paiHe[j] - allShouPai[j]; jzNum += jz; pai.getJz().put(j, jz); } } - shoupai[i] = tmp; - - - if (jzNum > 20 && index >= 30) { - jzNum += 2; - } - if (jzNum > 20 && (index%10 == 1 || index%10==9) ) { - jzNum += 1; - } + shouPai[i] = tmp; pai.setJzs(jzNum); if (jzNum > 0) pais.add(pai); @@ -225,50 +189,21 @@ else if (j >= i + num) { return reserved; } - public static List get(List paiList) { + static List get(List paiList) { int[] shoupai = paiList.stream().mapToInt(Integer::valueOf).map(i -> i % 10 == 0 ? i + 5 : i).toArray(); Desk desk = new Desk(shoupai); int[] paihe = new int[38]; for (int i = 1; i < 38; i++) { if (i < 10) - paihe[i] = Dask.M[i]; + paihe[i] = Desk.M[i]; else if (i > 10 && i < 20) - paihe[i] = Dask.P[i - 10]; + paihe[i] = Desk.P[i - 10]; else if (i > 20 && i < 30) - paihe[i] = Dask.S[i - 20]; + paihe[i] = Desk.S[i - 20]; else if (i > 30) - paihe[i] = Dask.Z[i - 30]; - } - desk.setPaihe(paihe); - List pais = get(desk); - return pais; - } - - private static int[] change(String text) { - int[] shouPai = new int[text.length() - 4]; - Pattern p = Pattern.compile("([0-9]*?)m([0-9]*)p([0-9]*)s([0-9]*)z"); - - Matcher m = p.matcher(text); - - if (m.find()) { - char[] ms = m.group(1).toCharArray(); - char[] ps = m.group(2).toCharArray(); - char[] ss = m.group(3).toCharArray(); - char[] zs = m.group(4).toCharArray(); - int index = 0; - for (char c : ms) { - shouPai[index++] = (int) c - 48; - } - for (char c : ps) { - shouPai[index++] = (int) c - 48 + 10; - } - for (char c : ss) { - shouPai[index++] = (int) c - 48 + 20; - } - for (char c : zs) { - shouPai[index++] = (int) c - 48 + 30; - } + paihe[i] = Desk.Z[i - 30]; } - return shouPai; + desk.setPaiHe(paihe); + return get(desk); } } diff --git a/src/main/java/com/mk/util/XiangTingShu.java b/src/main/java/com/mk/util/XiangTingShu.java deleted file mode 100644 index 90590e0..0000000 --- a/src/main/java/com/mk/util/XiangTingShu.java +++ /dev/null @@ -1,306 +0,0 @@ -package com.mk.util; - -import org.opencv.core.Mat; - -import static java.lang.Math.min; - -public class XiangTingShu { - public static int getXTS(byte[]... b) { - byte[][] a = clone(b); - Integer S = 8, C_max = 0; - getQT(a, size(a), S, C_max, (size(a) - 2) / 3); - return S; - } - - public static int size(byte[]... a) { - int count = 0; - for (byte[] bytes : a) { - for (byte aByte : bytes) { - count += aByte; - } - } - return count; - } - - public static void getQT(byte[][] a, int C_rem, Integer S, Integer C_max, int K) { - for (byte[] m : a) { - for (int i = 1; i < m.length; i++) { - if (m[i] >= 2) { - m[i] -= 2; - byte[][] clone = a.clone(); - m[i] += 2; - getKS(clone, 0, 0,C_rem - 2, S, C_max, K, 1, 0); - } - } - } - getKS(a, 0, 0, C_rem, S, C_max, K, 0, 0); - } - - - public static void getKS(byte[][] b, int i, int j, int C_rem, Integer S, Integer C_max, int K, int P, int G) { - byte[][] a = b.clone(); - if (i == 3 && j == 7) - getKSS(a, C_rem, S, C_max, K, P, G, 0); - - boolean get = putKS(a, i, j); -// -// C_rem -= 3 * count; -// G += count; - - getKSS(a, C_rem, S, C_max, K, P, G, 0); - } - - public static int putKS(byte[]... a) { - int count = 0; - for (int i = 0; i < 3; i++) { - for (int j = 1; j < 10; j++) { - if (j < 8) { - if (a[i][j] >= 3) { - a[i][j] -= 3; - count++; - } - - if (a[i][j] > 0 && a[i][j + 1] > 0 && a[i][j + 2] > 0) { - a[i][j]--; - a[i][j + 1]--; - a[i][j + 2]--; - count++; - } - - if (a[i][j] > 0 && a[i][j + 1] > 0 && a[i][j + 2] > 0) { - a[i][j]--; - a[i][j + 1]--; - a[i][j + 2]--; - count++; - } - - } else { - if (a[i][j] >= 3) { - a[i][j] -= 3; - count++; - } - } - } - } - for (int j = 1; j < 8; j++) { - if (a[3][j] >= 3) { - a[3][j] -= 3; - count++; - } - } - return count; - } - - public static boolean putKS(byte[][] a, int ii, int jj) { - for (int i = ii; i < 3; i++) { - for (int j = jj; j < 10; j++) { - if (j < 8) { - if (a[i][j] >= 3) { - a[i][j] -= 3; - return true; - } - - if (a[i][j] > 0 && a[i][j + 1] > 0 && a[i][j + 2] > 0) { - a[i][j]--; - a[i][j + 1]--; - a[i][j + 2]--; - return true; - } - - } else { - if (a[i][j] >= 3) { - a[i][j] -= 3; - return true; - } - } - } - } - if (ii < 3) - for (int j = jj; j < 8; j++) { - if (a[3][j] >= 3) { - a[3][j] -= 3; - return true; - } - } - return false; - } - - public static int getXT(byte[]... a) { - int size = 4 - (size(a) - 2) / 3; - - int min = 10; - byte[][] b = clone(a); - int mianzi = putKS(b) + size; - - for (byte[] m : b) { - for (int i = 1; i < m.length; i++) { - if (m[i] >= 2) { - m[i] -= 2; - int dazi = getKSS(b); - if (dazi + mianzi > 4) - dazi = 4 - mianzi; - min = min(min, 7 - mianzi * 2 - dazi); - m[i] += 2; - } - } - } - int dazi = getKSS(b); - if (dazi + mianzi > 4) - dazi = 4 - mianzi; - min = min(min, 8 - mianzi * 2 - dazi); - return min; - } - - private static byte[][] clone(byte[][] a) { - byte[][] b = new byte[a.length][]; - for (int i = 0; i < a.length; i++) { - b[i] = a[i].clone(); - } - return b; - } - - public static int getKSS(byte[][] b) { - byte[][] a = clone(b); - int count = 0; - for (int i = 0; i < 3; i++) { - for (int j = 1; j < 10; j++) { - if (j < 8) { - if (a[i][j] >= 2) { - a[i][j] -= 2; - count++; - } - - if (a[i][j] > 0 && a[i][j + 1] > 0) { - a[i][j]--; - a[i][j + 1]--; - count++; - } - - if (a[i][j] > 0 && a[i][j + 2] > 0) { - a[i][j]--; - a[i][j + 2]--; - count++; - } - - } else if (j < 9) { - - if (a[i][j] >= 2) { - a[i][j] -= 2; - count++; - } - - if (a[i][j] > 0 && a[i][j + 1] > 0) { - a[i][j]--; - a[i][j + 1]--; - count++; - } - } else { - if (a[i][j] >= 2) { - a[i][j] -= 2; - count++; - } - } - } - } - for (int j = 1; j < 8; j++) { - if (a[3][j] >= 2) { - a[3][j] -= 2; - count++; - } - } - return count; - } - - - public static void main(String[] args) { - - } - - public static void getKSS(byte[][] b, int C_rem, Integer S, Integer C_max, int K, int P, int G, int Gs) { - byte[][] a = b.clone(); - if (S == -1) { - return; - } - if ((G + Gs) > K) { - return; - } - int C = 3 * G + 2 * Gs + 2 * P; - if (C_rem < C_max - C) { - return; - } - if (C_rem == 0) { - S = min(S, 2 * (K - G) - Gs - P); - C_max = Math.max(C_max, C); - return; - - } - int count = 0; - for (int i = 0; i < 3; i++) { - for (int j = 1; j < 10; j++) { - if (j < 8) { - if (a[i][j] >= 2) { - a[i][j] -= 2; - count++; - } - - if (a[i][j] > 0 && a[i][j + 1] > 0) { - a[i][j]--; - a[i][j + 1]--; - count++; - } - - if (a[i][j] > 0 && a[i][j + 2] > 0) { - a[i][j]--; - a[i][j + 2]--; - count++; - } - - } else if (j < 9) { - - if (a[i][j] >= 2) { - a[i][j] -= 2; - count++; - } - - if (a[i][j] > 0 && a[i][j + 1] > 0) { - a[i][j]--; - a[i][j + 1]--; - count++; - } - } else { - if (a[i][j] >= 2) { - a[i][j] -= 2; - count++; - } - } - } - } - for (int j = 1; j < 8; j++) { - if (a[3][j] >= 2) { - a[3][j] -= 2; - count++; - } - } - col(C_rem - 2 * count, S, C_max, K, P, G, Gs + count); - } - - public static void col(int C_rem, Integer S, Integer C_max, int K, int P, int G, int Gs) { - if (S == -1) { - return; - } - if ((G + Gs) > K) { - return; - } - int C = 3 * G + 2 * Gs + 2 * P; - if (C_rem < C_max - C) { - return; - } - if (C_rem == 0) { - S = min(S, 2 * (K - G) - Gs - P); - C_max = Math.max(C_max, C); - return; - } - col(C_rem - 1, S, C_max, K, P, G, Gs); - } -} - diff --git a/src/main/java/com/mk/util/XiuZheng.java b/src/main/java/com/mk/util/XiuZheng.java deleted file mode 100644 index e3d7fb2..0000000 --- a/src/main/java/com/mk/util/XiuZheng.java +++ /dev/null @@ -1,60 +0,0 @@ -package com.mk.util; - -import org.opencv.core.*; -import org.opencv.core.Point; -import org.opencv.imgcodecs.Imgcodecs; -import org.opencv.imgproc.Imgproc; -import org.opencv.utils.Converters; - -import javax.imageio.ImageIO; -import java.awt.*; -import java.awt.image.BufferedImage; -import java.io.ByteArrayInputStream; -import java.io.InputStream; -import java.util.ArrayList; - -import static org.opencv.imgproc.Imgproc.ADAPTIVE_THRESH_MEAN_C; - -public class XiuZheng { - public static void main(String[] args) { - - System.loadLibrary(Core.NATIVE_LIBRARY_NAME); - - Mat src = Imgcodecs.imread("D:/image/18.png"); - - MatOfPoint2f srcTri = new MatOfPoint2f(( - new Point(0, 0)), - new Point(src.cols() - 1, 0), - new Point(0, src.rows() - 1), - new Point(src.cols() - 1, src.rows() - 1)); - MatOfPoint2f dstTri = new MatOfPoint2f(( - new Point(0, 0)), - new Point(src.cols() - 1, 0), - new Point(src.cols() * 0.12, src.rows() - 1), - new Point(src.cols() * 0.88, src.rows() - 1)); - - final Mat perspectiveTransform = Imgproc.getPerspectiveTransform(srcTri, dstTri); - - Mat dst = new Mat(); - Imgproc.warpPerspective(src, dst, perspectiveTransform, src.size()); - - } - - public static BufferedImage Mat2BufImg(Mat matrix) { - String fileExtension = ".png"; - // convert the matrix into a matrix of bytes appropriate for - // this file extension - MatOfByte mob = new MatOfByte(); - Imgcodecs.imencode(fileExtension, matrix, mob); - // convert the "matrix of bytes" into a byte array - byte[] byteArray = mob.toArray(); - BufferedImage bufImage = null; - try { - InputStream in = new ByteArrayInputStream(byteArray); - bufImage = ImageIO.read(in); - } catch (Exception e) { - e.printStackTrace(); - } - return bufImage; - } -}