Advertisement
Guest User

Untitled

a guest
Jun 7th, 2014
3,121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 14.91 KB | None | 0 0
  1. import java.awt.Color;
  2. import java.awt.GraphicsDevice;
  3. import java.awt.GraphicsEnvironment;
  4. import java.awt.MouseInfo;
  5. import java.awt.Point;
  6. import java.awt.Rectangle;
  7. import java.awt.Robot;
  8. import java.awt.Toolkit;
  9. import java.awt.event.InputEvent;
  10. import java.awt.geom.Point2D;
  11. import java.awt.image.BufferedImage;
  12. import java.io.File;
  13. import java.io.FileOutputStream;
  14. import java.io.IOException;
  15. import java.net.MalformedURLException;
  16. import java.net.URL;
  17. import java.nio.channels.Channels;
  18. import java.nio.channels.ReadableByteChannel;
  19. import java.nio.file.Files;
  20. import java.nio.file.Paths;
  21. import java.util.ArrayList;
  22.  
  23.  
  24. import javax.imageio.ImageIO;
  25.  
  26. class Card{
  27.    
  28.     int id;
  29.     String shape;
  30.     String color;
  31.     String infill;
  32.     int shapeCount;
  33.     byte[] fileBytes;
  34.     Point position;
  35.    
  36.     Card(int id, String shape, String color, String infill, int shapeCount) throws Exception{
  37.         this.id = id;
  38.         this.shape = shape;
  39.         this.color = color;
  40.         this.infill = infill;
  41.         this.shapeCount = shapeCount;
  42.         this.fileBytes = Files.readAllBytes(Paths.get(id+".png"));
  43.     }
  44. }
  45.  
  46. public class SetBeater {
  47.  
  48.     public static void main(String[] args) throws Exception {
  49.        
  50.         Card[] allCards = new Card[81];
  51.         allCards[0] = new Card(1, "squiggle", "red", "solid", 1);
  52.         allCards[1] = new Card(2, "squiggle", "red", "solid", 2);
  53.         allCards[2] = new Card(3, "squiggle", "red", "solid", 3);
  54.         allCards[3] = new Card(4, "squiggle", "purple", "solid", 1);
  55.         allCards[4] = new Card(5, "squiggle", "purple", "solid", 2);
  56.         allCards[5] = new Card(6, "squiggle", "purple", "solid", 3);
  57.         allCards[6] = new Card(7, "squiggle", "green", "solid", 1);
  58.         allCards[7] = new Card(8, "squiggle", "green", "solid", 2);
  59.         allCards[8] = new Card(9, "squiggle", "green", "solid", 3);
  60.        
  61.         allCards[9] = new Card(10, "diamond", "red", "solid", 1);
  62.         allCards[10] = new Card(11, "diamond", "red", "solid", 2);
  63.         allCards[11] = new Card(12, "diamond", "red", "solid", 3);
  64.         allCards[12] = new Card(13, "diamond", "purple", "solid", 1);
  65.         allCards[13] = new Card(14, "diamond", "purple", "solid", 2);
  66.         allCards[14] = new Card(15, "diamond", "purple", "solid", 3);
  67.         allCards[15] = new Card(16, "diamond", "green", "solid", 1);
  68.         allCards[16] = new Card(17, "diamond", "green", "solid", 2);
  69.         allCards[17] = new Card(18, "diamond", "green", "solid", 3);
  70.        
  71.         allCards[18] = new Card(19, "oval", "red", "solid", 1);
  72.         allCards[19] = new Card(20, "oval", "red", "solid", 2);
  73.         allCards[20] = new Card(21, "oval", "red", "solid", 3);
  74.         allCards[21] = new Card(22, "oval", "purple", "solid", 1);
  75.         allCards[22] = new Card(23, "oval", "purple", "solid", 2);
  76.         allCards[23] = new Card(24, "oval", "purple", "solid", 3);
  77.         allCards[24] = new Card(25, "oval", "green", "solid", 1);
  78.         allCards[25] = new Card(26, "oval", "green", "solid", 2);
  79.         allCards[26] = new Card(27, "oval", "green", "solid", 3);
  80.        
  81.         allCards[27] = new Card(28, "squiggle", "red", "stripe", 1);
  82.         allCards[28] = new Card(29, "squiggle", "red", "stripe", 2);
  83.         allCards[29] = new Card(30, "squiggle", "red", "stripe", 3);
  84.         allCards[30] = new Card(31, "squiggle", "purple", "stripe", 1);
  85.         allCards[31] = new Card(32, "squiggle", "purple", "stripe", 2);
  86.         allCards[32] = new Card(33, "squiggle", "purple", "stripe", 3);
  87.         allCards[33] = new Card(34, "squiggle", "green", "stripe", 1);
  88.         allCards[34] = new Card(35, "squiggle", "green", "stripe", 2);
  89.         allCards[35] = new Card(36, "squiggle", "green", "stripe", 3);
  90.        
  91.         allCards[36] = new Card(37, "diamond", "red", "stripe", 1);
  92.         allCards[37] = new Card(38, "diamond", "red", "stripe", 2);
  93.         allCards[38] = new Card(39, "diamond", "red", "stripe", 3);
  94.         allCards[39] = new Card(40, "diamond", "purple", "stripe", 1);
  95.         allCards[40] = new Card(41, "diamond", "purple", "stripe", 2);
  96.         allCards[41] = new Card(42, "diamond", "purple", "stripe", 3);
  97.         allCards[42] = new Card(43, "diamond", "green", "stripe", 1);
  98.         allCards[43] = new Card(44, "diamond", "green", "stripe", 2);
  99.         allCards[44] = new Card(45, "diamond", "green", "stripe", 3);
  100.        
  101.         allCards[45] = new Card(46, "oval", "red", "stripe", 1);
  102.         allCards[46] = new Card(47, "oval", "red", "stripe", 2);
  103.         allCards[47] = new Card(48, "oval", "red", "stripe", 3);
  104.         allCards[48] = new Card(49, "oval", "purple", "stripe", 1);
  105.         allCards[49] = new Card(50, "oval", "purple", "stripe", 2);
  106.         allCards[50] = new Card(51, "oval", "purple", "stripe", 3);
  107.         allCards[51] = new Card(52, "oval", "green", "stripe", 1);
  108.         allCards[52] = new Card(53, "oval", "green", "stripe", 2);
  109.         allCards[53] = new Card(54, "oval", "green", "stripe", 3);
  110.        
  111.         allCards[54] = new Card(55, "squiggle", "red", "empty", 1);
  112.         allCards[55] = new Card(56, "squiggle", "red", "empty", 2);
  113.         allCards[56] = new Card(57, "squiggle", "red", "empty", 3);
  114.         allCards[57] = new Card(58, "squiggle", "purple", "empty", 1);
  115.         allCards[58] = new Card(59, "squiggle", "purple", "empty", 2);
  116.         allCards[59] = new Card(60, "squiggle", "purple", "empty", 3);
  117.         allCards[60] = new Card(61, "squiggle", "green", "empty", 1);
  118.         allCards[61] = new Card(62, "squiggle", "green", "empty", 2);
  119.         allCards[62] = new Card(63, "squiggle", "green", "empty", 3);
  120.        
  121.         allCards[63] = new Card(64, "diamond", "red", "empty", 1);
  122.         allCards[64] = new Card(65, "diamond", "red", "empty", 2);
  123.         allCards[65] = new Card(66, "diamond", "red", "empty", 3);
  124.         allCards[66] = new Card(67, "diamond", "purple", "empty", 1);
  125.         allCards[67] = new Card(68, "diamond", "purple", "empty", 2);
  126.         allCards[68] = new Card(69, "diamond", "purple", "empty", 3);
  127.         allCards[69] = new Card(70, "diamond", "green", "empty", 1);
  128.         allCards[70] = new Card(71, "diamond", "green", "empty", 2);
  129.         allCards[71] = new Card(72, "diamond", "green", "empty", 3);
  130.        
  131.         allCards[72] = new Card(73, "oval", "red", "empty", 1);
  132.         allCards[73] = new Card(74, "oval", "red", "empty", 2);
  133.         allCards[74] = new Card(75, "oval", "red", "empty", 3);
  134.         allCards[75] = new Card(76, "oval", "purple", "empty", 1);
  135.         allCards[76] = new Card(77, "oval", "purple", "empty", 2);
  136.         allCards[77] = new Card(78, "oval", "purple", "empty", 3);
  137.         allCards[78] = new Card(79, "oval", "green", "empty", 1);
  138.         allCards[79] = new Card(80, "oval", "green", "empty", 2);
  139.         allCards[80] = new Card(81, "oval", "green", "empty", 3);
  140.        
  141.         GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();  
  142.         GraphicsDevice[] screens = ge.getScreenDevices();  
  143.        
  144.         Rectangle screenBounds = screens[1].getDefaultConfiguration().getBounds();
  145.         BufferedImage image = new Robot().createScreenCapture(screenBounds);
  146.        
  147.         int rows = 3, cols = 4;
  148.         BufferedImage[][] cards = new BufferedImage[rows][cols];
  149.         cards[0][0] = image.getSubimage(245, 499, 95, 62);
  150.         cards[0][1] = image.getSubimage(394, 499, 95, 62);
  151.         cards[0][2] = image.getSubimage(543, 499, 95, 62);
  152.         cards[0][3] = image.getSubimage(693, 499, 95, 62);
  153.        
  154.         cards[1][0] = image.getSubimage(245, 588, 95, 62);
  155.         cards[1][1] = image.getSubimage(394, 588, 95, 62);
  156.         cards[1][2] = image.getSubimage(543, 588, 95, 62);
  157.         cards[1][3] = image.getSubimage(693, 588, 95, 62);
  158.        
  159.         cards[2][0] = image.getSubimage(245, 677, 95, 62);
  160.         cards[2][1] = image.getSubimage(394, 677, 95, 62);
  161.         cards[2][2] = image.getSubimage(543, 677, 95, 62);
  162.         cards[2][3] = image.getSubimage(693, 677, 95, 62);
  163.        
  164.         ArrayList<Byte> [][] cardBytes = new ArrayList[rows][cols];
  165.         for(int i=0; i<rows; i++)
  166.             for(int j=0; j<cols; j++){
  167.                 cardBytes[i][j] = new ArrayList<Byte>();
  168.                 ImageIO.write(cards[i][j], "png", new File("card-"+i+","+j+".png"));
  169.                 byte[] bytes = Files.readAllBytes(Paths.get("card-"+i+","+j+".png"));
  170.                 for(int k=0; k<bytes.length; k++)
  171.                     cardBytes[i][j].add(bytes[k]);
  172.             }
  173.        
  174.         int checkBoxSides = 12;
  175.         Point checkBoxes[][] = new Point[rows][cols];
  176.         for(int i=0; i<rows; i++)
  177.             for(int j=0; j<cols; j++)
  178.                 checkBoxes[i][j] = new Point();
  179.        
  180.         int xOffset = -1600;
  181.         checkBoxes[0][0].setLocation(xOffset+292,575);
  182.         checkBoxes[0][1].setLocation(xOffset+441,575);
  183.         checkBoxes[0][2].setLocation(xOffset+591,575);
  184.         checkBoxes[0][3].setLocation(xOffset+741,575);
  185.        
  186.         checkBoxes[1][0].setLocation(xOffset+292,663);
  187.         checkBoxes[1][1].setLocation(xOffset+441,663);
  188.         checkBoxes[1][2].setLocation(xOffset+591,663);
  189.         checkBoxes[1][3].setLocation(xOffset+741,663);
  190.        
  191.         checkBoxes[2][0].setLocation(xOffset+292,752);
  192.         checkBoxes[2][1].setLocation(xOffset+441,752);
  193.         checkBoxes[2][2].setLocation(xOffset+591,752);
  194.         checkBoxes[2][3].setLocation(xOffset+741,752);
  195.        
  196.         ArrayList<Card> dealtCards = new ArrayList<Card>();
  197.         for(int i=0; i<rows; i++)
  198.             for(int j=0; j<cols; j++)
  199.                 for(int k=0; k<allCards.length; k++)
  200.                     if (cardBytes[i][j].size() == allCards[k].fileBytes.length){
  201.                         //System.out.println("Same length detected");
  202.                         boolean matched = true;
  203.                         for(int l=0; l<allCards[k].fileBytes.length; l++)
  204.                             if (cardBytes[i][j].get(l) != allCards[k].fileBytes[l]){
  205.                                 matched = false;
  206.                                 break;
  207.                             }
  208.  
  209.                         if (matched){
  210.                             allCards[k].position = new Point(i,j);
  211.                             dealtCards.add(allCards[k]);
  212.                         }
  213.                     }
  214.        
  215.         ArrayList<ArrayList<Card>> setsFound;
  216.         setsFound = new ArrayList<ArrayList<Card>>();
  217.        
  218.         for(int i=0; i<dealtCards.size(); i++)
  219.             for(int j=i+1; j<dealtCards.size(); j++)
  220.                 for(int k=j+1; k<dealtCards.size(); k++){
  221.                     boolean sameColor = (dealtCards.get(i).color == dealtCards.get(j).color && dealtCards.get(i).color == dealtCards.get(k).color && dealtCards.get(j).color == dealtCards.get(k).color);
  222.                     boolean sameInfill = (dealtCards.get(i).infill == dealtCards.get(j).infill && dealtCards.get(i).infill == dealtCards.get(k).infill && dealtCards.get(j).infill == dealtCards.get(k).infill);
  223.                     boolean sameShape = (dealtCards.get(i).shape == dealtCards.get(j).shape && dealtCards.get(i).shape == dealtCards.get(k).shape && dealtCards.get(j).shape == dealtCards.get(k).shape);
  224.                     boolean sameShapeCount = (dealtCards.get(i).shapeCount == dealtCards.get(j).shapeCount && dealtCards.get(i).shapeCount == dealtCards.get(k).shapeCount && dealtCards.get(j).shapeCount == dealtCards.get(k).shapeCount);
  225.                    
  226.                     int sameTotal = 0;
  227.                     if (sameColor)
  228.                         sameTotal++;
  229.                     if (sameInfill)
  230.                         sameTotal++;
  231.                     if (sameShape)
  232.                         sameTotal++;
  233.                     if (sameShapeCount)
  234.                         sameTotal++;
  235.                    
  236.                     boolean allDifferentColor = (dealtCards.get(i).color != dealtCards.get(j).color && dealtCards.get(i).color != dealtCards.get(k).color && dealtCards.get(j).color != dealtCards.get(k).color);
  237.                     boolean allDifferentInfill = (dealtCards.get(i).infill != dealtCards.get(j).infill && dealtCards.get(i).infill != dealtCards.get(k).infill && dealtCards.get(j).infill != dealtCards.get(k).infill);
  238.                     boolean allDifferentShape = (dealtCards.get(i).shape != dealtCards.get(j).shape && dealtCards.get(i).shape != dealtCards.get(k).shape && dealtCards.get(j).shape != dealtCards.get(k).shape);
  239.                     boolean allDifferentShapeCount = (dealtCards.get(i).shapeCount != dealtCards.get(j).shapeCount && dealtCards.get(i).shapeCount != dealtCards.get(k).shapeCount && dealtCards.get(j).shapeCount != dealtCards.get(k).shapeCount);
  240.                    
  241.                     int allDifferentTotal = 0;
  242.                     if (allDifferentColor)
  243.                         allDifferentTotal++;
  244.                     if (allDifferentInfill)
  245.                         allDifferentTotal++;
  246.                     if (allDifferentShape)
  247.                         allDifferentTotal++;
  248.                     if (allDifferentShapeCount)
  249.                         allDifferentTotal++;
  250.                    
  251.                     boolean setFound = false;
  252.                    
  253.                     if (allDifferentTotal == 4)
  254.                         setFound = true;
  255.                     else if (allDifferentTotal == 3 && sameTotal == 1)
  256.                         setFound = true;
  257.                     else if (allDifferentTotal == 2 && sameTotal == 2)
  258.                         setFound = true;
  259.                     /*
  260.                     if (sameShape && sameColor && allDifferentInfill && allDifferentShapeCount)
  261.                         setFound = true;
  262.                     else if (sameShape && allDifferentColor && sameInfill && allDifferentShapeCount)
  263.                         setFound = true;
  264.                     else if (sameShape && allDifferentColor && allDifferentInfill && sameShapeCount)
  265.                         setFound = true;
  266.                     else if (sameShape && allDifferentColor && allDifferentInfill && allDifferentShapeCount)
  267.                         setFound = true;
  268.                     else if (allDifferentShape && sameColor && allDifferentInfill && allDifferentShapeCount)
  269.                         setFound = true;
  270.                     else if (allDifferentShape && allDifferentColor && sameInfill && allDifferentShapeCount)
  271.                         setFound = true;
  272.                     else if (allDifferentShape && allDifferentColor && allDifferentInfill && sameShapeCount)
  273.                         setFound = true;
  274.                     else if (allDifferentShape && allDifferentColor && allDifferentInfill && allDifferentShapeCount)
  275.                         setFound = true;
  276.                     */
  277.                     if (setFound == true){
  278.                         ArrayList<Card> set = new ArrayList<Card>();
  279.                         set.add(dealtCards.get(i));
  280.                         set.add(dealtCards.get(j));
  281.                         set.add(dealtCards.get(k));
  282.                    
  283.                         setsFound.add(set);
  284.                     }
  285.                 }
  286.         System.out.println(setsFound.size());
  287.        
  288.         Point okButtonShort = new Point(xOffset+926,182);
  289.         Point okButtonLong = new Point(xOffset+926,213);
  290.         boolean clickedOkButtonSimple = false;
  291.        
  292.         Robot robot = new Robot();
  293.         int checked = 0;
  294.        
  295.         int mouseActionDelay = 500;
  296.        
  297.         for(int i=0; i<setsFound.size(); i++){
  298.             ArrayList<Card> cardSet = setsFound.get(i);
  299.            
  300.             for(int j=0; j<cardSet.size(); j++){
  301.                 robot.mouseMove((int) checkBoxes[(int) cardSet.get(j).position.getX()][(int) cardSet.get(j).position.getY()].getX(), (int) checkBoxes[(int) cardSet.get(j).position.getX()][(int) cardSet.get(j).position.getY()].getY());
  302.                 robot.mousePress(InputEvent.BUTTON1_MASK);
  303.                 robot.mouseRelease(InputEvent.BUTTON1_MASK);
  304.                 checked++;
  305.                 Thread.sleep(mouseActionDelay);
  306.                
  307.                 if (checked == 3){
  308.                     if (clickedOkButtonSimple == false || mouseActionDelay >= 250){
  309.                         robot.mouseMove((int) okButtonShort.getX(), (int) okButtonShort.getY());
  310.                         clickedOkButtonSimple = true;
  311.                     }
  312.                     else
  313.                         robot.mouseMove((int) okButtonLong.getX(), (int) okButtonLong.getY());
  314.                
  315.                     robot.mousePress(InputEvent.BUTTON1_MASK);
  316.                     robot.mouseRelease(InputEvent.BUTTON1_MASK);
  317.                     Thread.sleep(mouseActionDelay);
  318.                     checked = 0;
  319.                 }
  320.             }
  321.         }
  322.     }
  323.    
  324.     static void downloadAllSetCards() throws Exception{
  325.         for(int i=1; i<=3*3*3*3; i++){
  326.             String filename = "";
  327.             if (i < 10)
  328.                 filename = "0"+i;
  329.             else
  330.                 filename = i+"";
  331.            
  332.             filename += ".gif";
  333.             URL website = new URL("http://www.puzzles.setgame.com/images/setcards/small/"+filename);
  334.             ReadableByteChannel rbc = Channels.newChannel(website.openStream());
  335.             FileOutputStream fos = new FileOutputStream(i+".gif");
  336.             fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
  337.         }
  338.     }
  339.    
  340.     void convertCards() throws Exception{
  341.         for(int i=1; i<82; i++){
  342.             BufferedImage bufferedImage = ImageIO.read(new File(i+".gif"));
  343.             BufferedImage newBufferedImage = new BufferedImage(bufferedImage.getWidth(), bufferedImage.getHeight(), BufferedImage.TYPE_INT_RGB);
  344.             newBufferedImage.createGraphics().drawImage(bufferedImage, 0, 0, Color.WHITE, null);
  345.             ImageIO.write(newBufferedImage, "png", new File(i+".png"));
  346.         }
  347.     }
  348.    
  349. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement