{"id":1437,"date":"2019-04-16T13:14:08","date_gmt":"2019-04-16T07:44:08","guid":{"rendered":"http:\/\/blog.openwebsolutions.in\/?p=1437"},"modified":"2019-04-16T13:32:50","modified_gmt":"2019-04-16T08:02:50","slug":"create-simple-puzzle-game-java","status":"publish","type":"post","link":"https:\/\/openwebsolutions.in\/blog\/create-simple-puzzle-game-java\/","title":{"rendered":"How to create a simple puzzle game in java?"},"content":{"rendered":"<p>One quite useful way to view mathematical puzzles is as games where only one person (the person playing the game) makes moves.<\/p>\n<p>We can borrow all the theoretical tools to learn the theory of games to research puzzles.<\/p>\n<p>For instance, we can use the same backtracking algorithm that proves that there is a winning strategy in finite multi-player games with finite choices to show that in finite-state puzzles where all states are achievable, there is also a winning strategy. But a winning strategy in a one-player game isn&#8217;t what we typically think of as a strategy at all &#8211; it is just a path from the initial state to the final state &#8211; exactly a solution to the puzzle.<\/p>\n<p>If a puzzle is just a one-player game, and a one-player game is just a graph where you have to find a sequence of moves from the initial state to the end state, then what may presumably be therefore attention-grabbing a few puzzles?<\/p>\n<p>After all, the correct sequence of moves is just a path from one vertex, labeled as the initial state of the puzzle to the ending state, which is the solved puzzle.<\/p>\n<p>What makes attention-grabbing puzzles attention-grabbing is that the abstract graph of the in-depth variety of the sport isn&#8217;t seen.<\/p>\n<p>The player (or solver) can never see very clearly beyond the first few jumps away from their current position. Instead, the solver must try to move in the general direction of the solution, without seeing precisely where the moves will lead them. In order to do this effectively, the solver must somehow generate ideas for which positions of the puzzle are closer to the solution state than others, and which moves are likely to result in dead-ends.<\/p>\n<p>import java.awt.BorderLayout;<br \/>\nimport java.awt.EventQueue;<\/p>\n<p>import javax.swing.JFrame;<br \/>\nimport javax.swing.JPanel;<br \/>\nimport javax.swing.border.EmptyBorder;<br \/>\nimport java.awt.*;<br \/>\nimport java.awt.event.*;<br \/>\nimport javax.swing.*;<br \/>\npublic class Puzzle extends JFrame implements ActionListener {<br \/>\nJButton b1,b2,b3,b4,b5,b6,b7,b8,b9;<br \/>\nstatic int count=0;<br \/>\nJLabel lb,lb2;<br \/>\nlong before,after;<br \/>\nprivate JPanel contentPane;<\/p>\n<p>\/**<br \/>\n* Launch the application.<br \/>\n*\/<br \/>\npublic static void main(String[] args) {<br \/>\nnew Puzzle();<br \/>\n}<\/p>\n<p>\/**<br \/>\n* Create the frame.<br \/>\n*\/<br \/>\nPuzzle(){<br \/>\nsuper(&#8220;Puzzle Game &#8211; JTP&#8221;);<br \/>\nlb=new JLabel();<br \/>\nlb.setBounds(230,50,180, 20);<br \/>\nlb2=new JLabel();<br \/>\nlb2.setBounds(230,100,180, 20);<br \/>\nb1=new JButton(&#8220;1&#8221;);<br \/>\nb1.setBounds(50,50,50,50);<br \/>\nb2=new JButton(&#8220;2&#8221;);<br \/>\nb2.setBounds(110,50,50,50);<br \/>\nb3=new JButton(&#8220;3&#8221;);<br \/>\nb3.setBounds(170,50,50,50);<\/p>\n<p>b4=new JButton(&#8220;&#8221;);<br \/>\nb4.setBounds(50,110,50,50);<br \/>\nb5=new JButton(&#8220;5&#8221;);<br \/>\nb5.setBounds(110,110,50,50);<br \/>\nb6=new JButton(&#8220;6&#8221;);<br \/>\nb6.setBounds(170,110,50,50);<\/p>\n<p>b7=new JButton(&#8220;7&#8221;);<br \/>\nb7.setBounds(50,170,50,50);<br \/>\nb8=new JButton(&#8220;8&#8221;);<br \/>\nb8.setBounds(110,170,50,50);<br \/>\nb9=new JButton(&#8220;4&#8221;);<br \/>\nb9.setBounds(170,170,50,50);<\/p>\n<p>b1.addActionListener(this);<br \/>\nb2.addActionListener(this);<br \/>\nb3.addActionListener(this);<br \/>\nb4.addActionListener(this);<br \/>\nb5.addActionListener(this);<br \/>\nb6.addActionListener(this);<br \/>\nb7.addActionListener(this);<br \/>\nb8.addActionListener(this);<br \/>\nb9.addActionListener(this);<\/p>\n<p>add(lb);add(lb2);add(b1);add(b2);add(b3);add(b4);add(b5);add(b6);add(b7);add(b8);add(b9);<br \/>\nsetSize(400,400);<br \/>\nsetLayout(null);<br \/>\nsetVisible(true);<br \/>\n}<\/p>\n<p>@Override<br \/>\npublic void actionPerformed(ActionEvent e) {<br \/>\n\/\/ TODO Auto-generated method stub<br \/>\nif(count==0){<br \/>\nbefore=System.currentTimeMillis();<br \/>\n}<br \/>\nlb.setText(&#8220;Total Clicks: &#8220;+(++count));<\/p>\n<p>if(e.getSource()==b1){<br \/>\nString label=b1.getText();<br \/>\nif(b2.getText().equals(&#8220;&#8221;)){<br \/>\nb2.setText(label);<br \/>\nb1.setText(&#8220;&#8221;);<br \/>\n}<br \/>\nif(b4.getText().equals(&#8220;&#8221;)){<br \/>\nb4.setText(label);<br \/>\nb1.setText(&#8220;&#8221;);<br \/>\n}<br \/>\n}\/\/end of b1<br \/>\nif(e.getSource()==b2){<br \/>\nString label=b2.getText();<br \/>\nif(b1.getText().equals(&#8220;&#8221;)){<br \/>\nb1.setText(label);<br \/>\nb2.setText(&#8220;&#8221;);<br \/>\n}<br \/>\nif(b3.getText().equals(&#8220;&#8221;)){<br \/>\nb3.setText(label);<br \/>\nb2.setText(&#8220;&#8221;);<br \/>\n}<br \/>\nif(b5.getText().equals(&#8220;&#8221;)){<br \/>\nb5.setText(label);<br \/>\nb2.setText(&#8220;&#8221;);<br \/>\n}<br \/>\n}\/\/end of b2<br \/>\nif(e.getSource()==b3){<br \/>\nString label=b3.getText();<br \/>\nif(b2.getText().equals(&#8220;&#8221;)){<br \/>\nb2.setText(label);<br \/>\nb3.setText(&#8220;&#8221;);<br \/>\n}<br \/>\nif(b6.getText().equals(&#8220;&#8221;)){<br \/>\nb6.setText(label);<br \/>\nb3.setText(&#8220;&#8221;);<br \/>\n}<br \/>\n}\/\/end of b3<br \/>\nif(e.getSource()==b4){<br \/>\nString label=b4.getText();<br \/>\nif(b1.getText().equals(&#8220;&#8221;)){<br \/>\nb1.setText(label);<br \/>\nb4.setText(&#8220;&#8221;);<br \/>\n}<br \/>\nif(b7.getText().equals(&#8220;&#8221;)){<br \/>\nb7.setText(label);<br \/>\nb4.setText(&#8220;&#8221;);<br \/>\n}<br \/>\nif(b5.getText().equals(&#8220;&#8221;)){<br \/>\nb5.setText(label);<br \/>\nb4.setText(&#8220;&#8221;);<br \/>\n}<br \/>\n}\/\/end of b4<br \/>\nif(e.getSource()==b5){<br \/>\nString label=b5.getText();<br \/>\nif(b2.getText().equals(&#8220;&#8221;)){<br \/>\nb2.setText(label);<br \/>\nb5.setText(&#8220;&#8221;);<br \/>\n}<br \/>\nif(b4.getText().equals(&#8220;&#8221;)){<br \/>\nb4.setText(label);<br \/>\nb5.setText(&#8220;&#8221;);<br \/>\n}<br \/>\nif(b6.getText().equals(&#8220;&#8221;)){<br \/>\nb6.setText(label);<br \/>\nb5.setText(&#8220;&#8221;);<br \/>\n}<br \/>\nif(b8.getText().equals(&#8220;&#8221;)){<br \/>\nb8.setText(label);<br \/>\nb5.setText(&#8220;&#8221;);<br \/>\n}<br \/>\n}\/\/end of b5<br \/>\nif(e.getSource()==b6){<br \/>\nString label=b6.getText();<br \/>\nif(b9.getText().equals(&#8220;&#8221;)){<br \/>\nb9.setText(label);<br \/>\nb6.setText(&#8220;&#8221;);<br \/>\n}<br \/>\nif(b3.getText().equals(&#8220;&#8221;)){<br \/>\nb3.setText(label);<br \/>\nb6.setText(&#8220;&#8221;);<br \/>\n}<br \/>\nif(b5.getText().equals(&#8220;&#8221;)){<br \/>\nb5.setText(label);<br \/>\nb6.setText(&#8220;&#8221;);<br \/>\n}<br \/>\n}\/\/end of b6<br \/>\nif(e.getSource()==b7){<br \/>\nString label=b7.getText();<br \/>\nif(b4.getText().equals(&#8220;&#8221;)){<br \/>\nb4.setText(label);<br \/>\nb7.setText(&#8220;&#8221;);<br \/>\n}<br \/>\nif(b8.getText().equals(&#8220;&#8221;)){<br \/>\nb8.setText(label);<br \/>\nb7.setText(&#8220;&#8221;);<br \/>\n}<br \/>\n}\/\/end of b7<br \/>\nif(e.getSource()==b8){<br \/>\nString label=b8.getText();<br \/>\nif(b9.getText().equals(&#8220;&#8221;)){<br \/>\nb9.setText(label);<br \/>\nb8.setText(&#8220;&#8221;);<br \/>\n}<br \/>\nif(b7.getText().equals(&#8220;&#8221;)){<br \/>\nb7.setText(label);<br \/>\nb8.setText(&#8220;&#8221;);<br \/>\n}<br \/>\nif(b5.getText().equals(&#8220;&#8221;)){<br \/>\nb5.setText(label);<br \/>\nb8.setText(&#8220;&#8221;);<br \/>\n}<br \/>\n}\/\/end of b8<br \/>\nif(e.getSource()==b9){<br \/>\nString label=b9.getText();<br \/>\nif(b6.getText().equals(&#8220;&#8221;)){<br \/>\nb6.setText(label);<br \/>\nb9.setText(&#8220;&#8221;);<br \/>\n}<br \/>\nif(b8.getText().equals(&#8220;&#8221;)){<br \/>\nb8.setText(label);<br \/>\nb9.setText(&#8220;&#8221;);<br \/>\n}<br \/>\n}\/\/end of b9<\/p>\n<p>after=System.currentTimeMillis();<br \/>\nlong seconds=((after-before)\/1000);<br \/>\nlb2.setText(&#8220;Time: &#8220;+seconds+&#8221; Seconds&#8221;);<\/p>\n<p>if(b1.getText().equals(&#8220;1&#8221;)&amp;&amp;b2.getText().equals(&#8220;2&#8221;)&amp;&amp;b3.getText().equals(&#8220;3&#8221;)&amp;&amp;b4.getText().equals(&#8220;4&#8221;)&amp;&amp;b5.getText().equals(&#8220;5&#8221;)&amp;&amp;b6.getText().equals(&#8220;6&#8221;)&amp;&amp;b7.getText().equals(&#8220;7&#8221;)&amp;&amp;b8.getText().equals(&#8220;8&#8221;)&amp;&amp;b9.getText().equals(&#8220;&#8221;)){<br \/>\nJOptionPane.showMessageDialog(this,&#8221;Congratulations, You won! Total Click: &#8220;+count+&#8221;, Total Time: &#8220;+seconds+&#8221; seconds&#8221;);<br \/>\ncount=0;<br \/>\nbefore=0;<br \/>\nafter=0;<br \/>\n}<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p><img loading=\"lazy\" class=\"alignnone size-medium wp-image-1438\" src=\"http:\/\/blog.openwebsolutions.in\/wp-content\/uploads\/2019\/03\/Screenshot-from-2019-03-25-14-19-37-300x300.png\" alt=\"\" width=\"300\" height=\"300\" srcset=\"https:\/\/openwebsolutions.in\/blog\/wp-content\/uploads\/2019\/03\/Screenshot-from-2019-03-25-14-19-37-300x300.png 300w, https:\/\/openwebsolutions.in\/blog\/wp-content\/uploads\/2019\/03\/Screenshot-from-2019-03-25-14-19-37-150x150.png 150w, https:\/\/openwebsolutions.in\/blog\/wp-content\/uploads\/2019\/03\/Screenshot-from-2019-03-25-14-19-37.png 398w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>One quite useful way to view mathematical puzzles is as games where only one person (the person playing the game) makes moves. We can borrow all the theoretical tools to learn the theory of games to research puzzles. For instance, we can use the same backtracking algorithm that proves that there is a winning strategy [&hellip;]<\/p>\n","protected":false},"author":27,"featured_media":1598,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[72],"tags":[119,86,73],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v14.8.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How to create a simple puzzle game in java?<\/title>\n<meta name=\"robots\" content=\"index, follow\" \/>\n<meta name=\"googlebot\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<meta name=\"bingbot\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/openwebsolutions.in\/blog\/create-simple-puzzle-game-java\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to create a simple puzzle game in java?\" \/>\n<meta property=\"og:description\" content=\"One quite useful way to view mathematical puzzles is as games where only one person (the person playing the game) makes moves. We can borrow all the theoretical tools to learn the theory of games to research puzzles. For instance, we can use the same backtracking algorithm that proves that there is a winning strategy [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/openwebsolutions.in\/blog\/create-simple-puzzle-game-java\/\" \/>\n<meta property=\"og:site_name\" content=\"Openweb Solutions Blog\" \/>\n<meta property=\"article:published_time\" content=\"2019-04-16T07:44:08+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-04-16T08:02:50+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/openwebsolutions.in\/blog\/wp-content\/uploads\/2019\/04\/blur-close-up-device-442576-1024x683.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1024\" \/>\n\t<meta property=\"og:image:height\" content=\"683\" \/>\n<meta name=\"twitter:card\" content=\"summary\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/openwebsolutions.in\/blog\/#website\",\"url\":\"https:\/\/openwebsolutions.in\/blog\/\",\"name\":\"Openweb Solutions Blog\",\"description\":\"Transforming ideas into reality\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":\"https:\/\/openwebsolutions.in\/blog\/?s={search_term_string}\",\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/openwebsolutions.in\/blog\/create-simple-puzzle-game-java\/#primaryimage\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/openwebsolutions.in\/blog\/wp-content\/uploads\/2019\/04\/blur-close-up-device-442576.jpg\",\"width\":4800,\"height\":3200,\"caption\":\"game development in java by openweb solutions\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/openwebsolutions.in\/blog\/create-simple-puzzle-game-java\/#webpage\",\"url\":\"https:\/\/openwebsolutions.in\/blog\/create-simple-puzzle-game-java\/\",\"name\":\"How to create a simple puzzle game in java?\",\"isPartOf\":{\"@id\":\"https:\/\/openwebsolutions.in\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/openwebsolutions.in\/blog\/create-simple-puzzle-game-java\/#primaryimage\"},\"datePublished\":\"2019-04-16T07:44:08+00:00\",\"dateModified\":\"2019-04-16T08:02:50+00:00\",\"author\":{\"@id\":\"https:\/\/openwebsolutions.in\/blog\/#\/schema\/person\/85191b5cfa834d711ed43bc1acbe326f\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/openwebsolutions.in\/blog\/create-simple-puzzle-game-java\/\"]}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/openwebsolutions.in\/blog\/#\/schema\/person\/85191b5cfa834d711ed43bc1acbe326f\",\"name\":\"Suchandra Mondal\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/openwebsolutions.in\/blog\/#personlogo\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/b45bdbe7136c8da18a72bc11b2a29ce5?s=96&r=g\",\"caption\":\"Suchandra Mondal\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","_links":{"self":[{"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/posts\/1437"}],"collection":[{"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/users\/27"}],"replies":[{"embeddable":true,"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/comments?post=1437"}],"version-history":[{"count":5,"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/posts\/1437\/revisions"}],"predecessor-version":[{"id":1600,"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/posts\/1437\/revisions\/1600"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/media\/1598"}],"wp:attachment":[{"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/media?parent=1437"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/categories?post=1437"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/tags?post=1437"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}