{"id":2451,"date":"2021-06-19T20:21:57","date_gmt":"2021-06-19T14:51:57","guid":{"rendered":"https:\/\/www.rangakrish.com\/?p=2451"},"modified":"2021-06-19T20:21:57","modified_gmt":"2021-06-19T14:51:57","slug":"snake-and-ladder-game-in-prolog","status":"publish","type":"post","link":"https:\/\/www.rangakrish.com\/index.php\/2021\/06\/19\/snake-and-ladder-game-in-prolog\/","title":{"rendered":"Snake and Ladder Game in Prolog"},"content":{"rendered":"<p><a href=\"https:\/\/en.wikipedia.org\/wiki\/Snakes_and_ladders\" target=\"_blank\" rel=\"noopener\"><em><strong>Snake and Ladder<\/strong><\/em><\/a>\u00a0is a popular game that originated in <em><strong>India<\/strong><\/em> and later spread to other parts of the world. Even now it is a hit among kids in <em><strong>South India<\/strong><\/em>, where people use the traditional game board, coins and dice.<span class=\"Apple-converted-space\">\u00a0<\/span><\/p>\n<p>Last week, I had a chance to play this game with my 5-year-old granddaughter, who was visiting us, and it was great fun! A wonderful way to relax in the present stressful Covid situation!<\/p>\n<p>After she left, I decided to implement the game in <em><strong>Prolog<\/strong><\/em>. Today&#8217;s article is about the implementation. I will share the source code at the end of the article.<\/p>\n<p>Here is the game board.<\/p>\n<figure id=\"attachment_2452\" aria-describedby=\"caption-attachment-2452\" style=\"width: 500px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/06\/snake-and-ladder.png?ssl=1\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" data-attachment-id=\"2452\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2021\/06\/19\/snake-and-ladder-game-in-prolog\/snake-and-ladder\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2021\/06\/snake-and-ladder.png\" data-orig-size=\"1024,1016\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Snake-And-Ladder Game Board\" data-image-description=\"&lt;p&gt;Snake-And-Ladder Game Board&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Snake-And-Ladder Game Board&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2021\/06\/snake-and-ladder.png\" class=\"wp-image-2452\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/06\/snake-and-ladder.png?resize=500%2C496&#038;ssl=1\" alt=\"Snake-And-Ladder Game Board\" width=\"500\" height=\"496\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/06\/snake-and-ladder.png?resize=300%2C298&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/06\/snake-and-ladder.png?resize=150%2C150&amp;ssl=1 150w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/06\/snake-and-ladder.png?resize=768%2C762&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/06\/snake-and-ladder.png?w=1024&amp;ssl=1 1024w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/a><figcaption id=\"caption-attachment-2452\" class=\"wp-caption-text\"><strong>Snake-And-Ladder Game Board<\/strong><\/figcaption><\/figure>\n<p>There are 100 <em><strong>&#8220;Cells&#8221;<\/strong><\/em> numbered 1 to 100. There are a few\u00a0<em><strong>Snakes<\/strong><\/em> and <em><strong>Ladders<\/strong><\/em>\u00a0that connect cell pairs. When a player reaches a cell that has the head of a snake, he will have to slide down the snake and move to the cell that has the tip of the tail. On the other hand, if a player reaches the bottom of a ladder, he has to climb up the ladder and reach the cell at the top of the ladder.<\/p>\n<p>Usually 3 or 4 players play the game, but 2 is the minimum. At the start of the game, players agree upon the order in which they will play. The game board we used came with two traditional metal dice. In order to <em><strong>&#8220;open&#8221;<\/strong><\/em> the game, a player must get <em><strong>&#8220;1&#8221;<\/strong><\/em> when rolling the dice. If he gets 5, 6 or 12, then he will roll the dice again. Any other value (2, 3, 4) will transfer control to the next player in the agreed order.<\/p>\n<p>The first player to reach 100, or cross it, is the winner (by the way, there are minor variations of the game, but what I present is what I played).<\/p>\n<p>Here is the code that defines the board:<\/p>\n<figure id=\"attachment_2453\" aria-describedby=\"caption-attachment-2453\" style=\"width: 450px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/06\/Board-config.jpg?ssl=1\"><img data-recalc-dims=\"1\" decoding=\"async\" data-attachment-id=\"2453\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2021\/06\/19\/snake-and-ladder-game-in-prolog\/board-config\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2021\/06\/Board-config.jpg\" data-orig-size=\"685,808\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1624094925&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Configuring the Board\" data-image-description=\"&lt;p&gt;Configuring the Board&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Configuring the Board&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2021\/06\/Board-config.jpg\" class=\"wp-image-2453\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/06\/Board-config.jpg?resize=450%2C531&#038;ssl=1\" alt=\"Configuring the Board\" width=\"450\" height=\"531\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/06\/Board-config.jpg?resize=254%2C300&amp;ssl=1 254w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/06\/Board-config.jpg?w=685&amp;ssl=1 685w\" sizes=\"(max-width: 450px) 100vw, 450px\" \/><\/a><figcaption id=\"caption-attachment-2453\" class=\"wp-caption-text\"><strong>Configuring the Board<\/strong><\/figcaption><\/figure>\n<p>I could have hard-coded the start and end positions as 1 and 100 respectively, but I chose to keep it flexible.<\/p>\n<p>How to model the dice and rolling the dice? Here is the code fragment:<\/p>\n<figure id=\"attachment_2454\" aria-describedby=\"caption-attachment-2454\" style=\"width: 500px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/06\/dice-logic.jpg?ssl=1\"><img data-recalc-dims=\"1\" decoding=\"async\" data-attachment-id=\"2454\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2021\/06\/19\/snake-and-ladder-game-in-prolog\/dice-logic\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2021\/06\/dice-logic.jpg\" data-orig-size=\"876,470\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1624095132&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Modelling the Dice\" data-image-description=\"&lt;p&gt;Modelling the Dice&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Modelling the Dice&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2021\/06\/dice-logic.jpg\" class=\"wp-image-2454\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/06\/dice-logic.jpg?resize=500%2C268&#038;ssl=1\" alt=\"Modelling the Dice\" width=\"500\" height=\"268\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/06\/dice-logic.jpg?resize=300%2C161&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/06\/dice-logic.jpg?resize=768%2C412&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/06\/dice-logic.jpg?w=876&amp;ssl=1 876w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/a><figcaption id=\"caption-attachment-2454\" class=\"wp-caption-text\"><strong>Modelling the Dice<\/strong><\/figcaption><\/figure>\n<p>Deciding the order in which the players will play and choosing the <em><strong>&#8220;next&#8221;<\/strong><\/em> player at any point in time are handled by the following clauses:<\/p>\n<figure id=\"attachment_2455\" aria-describedby=\"caption-attachment-2455\" style=\"width: 500px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/06\/player-section.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"2455\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2021\/06\/19\/snake-and-ladder-game-in-prolog\/player-section\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2021\/06\/player-section.jpg\" data-orig-size=\"984,559\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1624102049&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Selecting Players in Order\" data-image-description=\"&lt;p&gt;Selecting Players in Order&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Selecting Players in Order&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2021\/06\/player-section.jpg\" class=\"wp-image-2455\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/06\/player-section.jpg?resize=500%2C284&#038;ssl=1\" alt=\"Selecting Players in Order\" width=\"500\" height=\"284\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/06\/player-section.jpg?resize=300%2C170&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/06\/player-section.jpg?resize=768%2C436&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/06\/player-section.jpg?w=984&amp;ssl=1 984w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/a><figcaption id=\"caption-attachment-2455\" class=\"wp-caption-text\"><strong>Selecting Players in Order<\/strong><\/figcaption><\/figure>\n<p>Initialization and the main <em><strong>&#8220;play&#8221;<\/strong><\/em> loop are given below:<\/p>\n<figure id=\"attachment_2456\" aria-describedby=\"caption-attachment-2456\" style=\"width: 500px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/06\/main-loop.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"2456\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2021\/06\/19\/snake-and-ladder-game-in-prolog\/main-loop\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2021\/06\/main-loop.jpg\" data-orig-size=\"955,433\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1624102260&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"The Main Loop\" data-image-description=\"&lt;p&gt;The Main Loop&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;The Main Loop&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2021\/06\/main-loop.jpg\" class=\"wp-image-2456\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/06\/main-loop.jpg?resize=500%2C227&#038;ssl=1\" alt=\"The Main Loop\" width=\"500\" height=\"227\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/06\/main-loop.jpg?resize=300%2C136&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/06\/main-loop.jpg?resize=768%2C348&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/06\/main-loop.jpg?w=955&amp;ssl=1 955w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/a><figcaption id=\"caption-attachment-2456\" class=\"wp-caption-text\"><strong>The Main Loop<\/strong><\/figcaption><\/figure>\n<p>The logic to handle specific dice values when a player rolls the dice is implemented in these clauses:<\/p>\n<figure id=\"attachment_2457\" aria-describedby=\"caption-attachment-2457\" style=\"width: 500px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/06\/handle-value.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"2457\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2021\/06\/19\/snake-and-ladder-game-in-prolog\/handle-value\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2021\/06\/handle-value.jpg\" data-orig-size=\"948,714\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1624110656&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Handling Dice Values\" data-image-description=\"&lt;p&gt;Handling Dice Values&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Handling Dice Values&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2021\/06\/handle-value.jpg\" class=\"wp-image-2457\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/06\/handle-value.jpg?resize=500%2C377&#038;ssl=1\" alt=\"Handling Dice Values\" width=\"500\" height=\"377\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/06\/handle-value.jpg?resize=300%2C226&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/06\/handle-value.jpg?resize=768%2C578&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/06\/handle-value.jpg?w=948&amp;ssl=1 948w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/a><figcaption id=\"caption-attachment-2457\" class=\"wp-caption-text\"><strong>Handling Dice Values<\/strong><\/figcaption><\/figure>\n<p>Moving the coin and associated special handling for snakes and ladders is shown below:<\/p>\n<figure id=\"attachment_2458\" aria-describedby=\"caption-attachment-2458\" style=\"width: 500px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/06\/move-coin.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"2458\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2021\/06\/19\/snake-and-ladder-game-in-prolog\/move-coin\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2021\/06\/move-coin.jpg\" data-orig-size=\"1157,716\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1624110748&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Moving the Coin from One Cell to Another\" data-image-description=\"&lt;p&gt;Moving the Coin from One Cell to Another&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Moving the Coin from One Cell to Another&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2021\/06\/move-coin-1024x634.jpg\" class=\"wp-image-2458\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/06\/move-coin.jpg?resize=500%2C309&#038;ssl=1\" alt=\"Moving the Coin from One Cell to Another\" width=\"500\" height=\"309\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/06\/move-coin.jpg?resize=300%2C186&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/06\/move-coin.jpg?resize=1024%2C634&amp;ssl=1 1024w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/06\/move-coin.jpg?resize=768%2C475&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/06\/move-coin.jpg?w=1157&amp;ssl=1 1157w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/a><figcaption id=\"caption-attachment-2458\" class=\"wp-caption-text\"><strong>Moving the Coin from One Cell to Another<\/strong><\/figcaption><\/figure>\n<p>I have covered the core logic. Let us run the program and see how it performs. I have implemented this program in <em><strong>Sicstus prolog 4.6.0<\/strong><\/em> on my <em><strong>Windows 10<\/strong><\/em> machine.<span class=\"Apple-converted-space\">\u00a0 <\/span>Here is the output generted when I run the game in the <em><strong>IDE<\/strong><\/em>:<\/p>\n<figure id=\"attachment_2459\" aria-describedby=\"caption-attachment-2459\" style=\"width: 450px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/06\/session1.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"2459\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2021\/06\/19\/snake-and-ladder-game-in-prolog\/session1-4\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2021\/06\/session1.jpg\" data-orig-size=\"635,737\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1624111730&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Sample Output\" data-image-description=\"&lt;p&gt;Sample Output&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Sample Output&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2021\/06\/session1.jpg\" class=\"wp-image-2459\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/06\/session1.jpg?resize=450%2C522&#038;ssl=1\" alt=\"Sample Output\" width=\"450\" height=\"522\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/06\/session1.jpg?resize=258%2C300&amp;ssl=1 258w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/06\/session1.jpg?w=635&amp;ssl=1 635w\" sizes=\"(max-width: 450px) 100vw, 450px\" \/><\/a><figcaption id=\"caption-attachment-2459\" class=\"wp-caption-text\"><strong>Sample Output<\/strong><\/figcaption><\/figure>\n<p>What is weird in this case is that only <em><strong>&#8220;p2&#8221;<\/strong><\/em> managed to open the game. The other two did not get the mandatory <em><strong>&#8220;1&#8221;<\/strong><\/em> to open the game. Because of randomness in selecting players and rolling the dice, the output is likely to change each time we run the program. Here is another run:<\/p>\n<figure id=\"attachment_2460\" aria-describedby=\"caption-attachment-2460\" style=\"width: 450px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/06\/session2.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"2460\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2021\/06\/19\/snake-and-ladder-game-in-prolog\/session2-3\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2021\/06\/session2.jpg\" data-orig-size=\"628,1092\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1624111646&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Another Run\" data-image-description=\"&lt;p&gt;Another Run&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Another Run&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2021\/06\/session2-589x1024.jpg\" class=\"wp-image-2460\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/06\/session2.jpg?resize=450%2C782&#038;ssl=1\" alt=\"Another Run\" width=\"450\" height=\"782\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/06\/session2.jpg?resize=173%2C300&amp;ssl=1 173w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/06\/session2.jpg?resize=589%2C1024&amp;ssl=1 589w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/06\/session2.jpg?w=628&amp;ssl=1 628w\" sizes=\"(max-width: 450px) 100vw, 450px\" \/><\/a><figcaption id=\"caption-attachment-2460\" class=\"wp-caption-text\"><strong>Another Run<\/strong><\/figcaption><\/figure>\n<p>Fortunately, in this run, all three players managed to open the game, but <em><strong>&#8220;p1&#8221;<\/strong><\/em> won finally.<\/p>\n<p>Well, it was interesting to implement the game in <em><strong>Prolog<\/strong><\/em>! You can download the source <a href=\"http:\/\/www.rangakrish.com\/downloads\/snake-and-ladder.pro\" target=\"_blank\" rel=\"noopener\"><em><strong>here<\/strong><\/em><\/a>.<\/p>\n<p>Have a nice weekend!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Snake and Ladder\u00a0is a popular game that originated in India and later spread to other parts of the world. Even now it is a hit among kids in South India, where people use the traditional game board, coins and dice.\u00a0 Last week, I had a chance to play this game with my 5-year-old granddaughter, who [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[17,147],"tags":[285,148,284],"class_list":["post-2451","post","type-post","status-publish","format-standard","hentry","category-programming","category-prolog","tag-game","tag-prolog","tag-snake-and-ladder"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p9OLnF-Dx","jetpack-related-posts":[{"id":3555,"url":"https:\/\/www.rangakrish.com\/index.php\/2024\/11\/22\/multiobjective-optimization-moo-in-lisp-and-prolog\/","url_meta":{"origin":2451,"position":0},"title":"Multiobjective Optimization (MOO) in Lisp and Prolog","author":"admin","date":"November 22, 2024","format":false,"excerpt":"Recently I came across a nice article\u00a0by Jose Crespo, where the author stresses that the future of programming revolves around the application of math concepts such as Functor, Monads, Folds, etc. In addition, he argues that familiarity with C\/C++ is essential in this modern AI age. The author goes through\u2026","rel":"","context":"In &quot;C++&quot;","block_context":{"text":"C++","link":"https:\/\/www.rangakrish.com\/index.php\/category\/c\/"},"img":{"alt_text":"Lisp Code for Pareto Front","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2024\/11\/lisp-code-249x300.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":2922,"url":"https:\/\/www.rangakrish.com\/index.php\/2022\/10\/06\/why-learn-prolog\/","url_meta":{"origin":2451,"position":1},"title":"Why Learn Prolog?","author":"admin","date":"October 6, 2022","format":false,"excerpt":"There are several programming languages in use today and a simple google search will throw up interesting recommendations of a subset of these languages to learn, usually based on popularity ranking. As is expected, the popularity of a programming language varies over time and hence a language that was in\u2026","rel":"","context":"In &quot;Programming&quot;","block_context":{"text":"Programming","link":"https:\/\/www.rangakrish.com\/index.php\/category\/programming\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":3260,"url":"https:\/\/www.rangakrish.com\/index.php\/2023\/11\/26\/generating-polynomials-in-prolog\/","url_meta":{"origin":2451,"position":2},"title":"Generating Polynomials in Prolog","author":"admin","date":"November 26, 2023","format":false,"excerpt":"Polynomial is an important topic in High School maths curriculum. There are many online courses that explain the topic in great details with lots of examples and sample exercises. Wouldn\u2019t it be interesting if we can generate polynomials of given degree programmatically? It turns out that this is not such\u2026","rel":"","context":"In &quot;Programming&quot;","block_context":{"text":"Programming","link":"https:\/\/www.rangakrish.com\/index.php\/category\/programming\/"},"img":{"alt_text":"Generating Polynomials - Main Clauses","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/11\/Code1-300x163.jpg?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/11\/Code1-300x163.jpg?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/11\/Code1-300x163.jpg?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":2832,"url":"https:\/\/www.rangakrish.com\/index.php\/2022\/06\/12\/definite-clause-grammars-in-lisp-part-4\/","url_meta":{"origin":2451,"position":3},"title":"Definite Clause Grammars in Lisp &#8211; Part 4","author":"admin","date":"June 12, 2022","format":false,"excerpt":"In a series of articles\u00a0written earlier, I had shown how it is possible to model Definite Clause Grammars (DCG) in LispWorks Lisp (Enterprise Edition). We use defgrammar\u00a0in Common Prolog (available as part of KnowledgeWorks package) to define our grammar rules. Here is a toy English grammar represented using defgrammar: This\u2026","rel":"","context":"In &quot;LISP&quot;","block_context":{"text":"LISP","link":"https:\/\/www.rangakrish.com\/index.php\/category\/lisp\/"},"img":{"alt_text":"DCG Using Defgrammar","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/06\/defgrammar-version-300x177.jpg?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/06\/defgrammar-version-300x177.jpg?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/06\/defgrammar-version-300x177.jpg?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":1727,"url":"https:\/\/www.rangakrish.com\/index.php\/2019\/09\/15\/exploring-word-patterns\/","url_meta":{"origin":2451,"position":4},"title":"Exploring Word Patterns","author":"admin","date":"September 15, 2019","format":false,"excerpt":"Working with word patterns can be an exciting (and challenging) creative activity. Such patterns come into picture while playing word games, solving word puzzles or even writing poetry. It is precisely to facilitate such tasks that I am building my \"iLexicon\" system.\u00a0 One popular word game goes like this: The\u2026","rel":"","context":"In &quot;Natural Language Processing&quot;","block_context":{"text":"Natural Language Processing","link":"https:\/\/www.rangakrish.com\/index.php\/category\/natural-language-processing\/"},"img":{"alt_text":"Prolog Predicates for Word Pair Generation","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/09\/Code.jpg?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/09\/Code.jpg?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2019\/09\/Code.jpg?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":3573,"url":"https:\/\/www.rangakrish.com\/index.php\/2024\/12\/17\/using-prolog-to-solve-the-word-transformation-puzzle\/","url_meta":{"origin":2451,"position":5},"title":"Using Prolog to Solve the Word Transformation Puzzle","author":"admin","date":"December 17, 2024","format":false,"excerpt":"In today\u2019s article, I want to share an interesting word puzzle, and then show how to solve it in Prolog. Here is the puzzle: You are given two words of the same length. You have to transform the first word into the second word, by changing only one letter at\u2026","rel":"","context":"In &quot;Programming&quot;","block_context":{"text":"Programming","link":"https:\/\/www.rangakrish.com\/index.php\/category\/programming\/"},"img":{"alt_text":"Our Dictionary","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2024\/12\/prolog1-300x280.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/2451","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/comments?post=2451"}],"version-history":[{"count":0,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/2451\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/media?parent=2451"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/categories?post=2451"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/tags?post=2451"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}