
    
        
        
        
                
        
        
        
                
        
        
        
                
        
        
        
                
        
        
        
                
        
        
        
                
    
        
        
                
    
        
        
            
{"version":"https:\/\/jsonfeed.org\/version\/1","title":"mathspp.com feed","home_page_url":"https:\/\/mathspp.com\/blog\/tags\/game","feed_url":"https:\/\/mathspp.com\/blog\/tags\/game.json","description":"Stay up-to-date with the articles on mathematics and programming that get published to mathspp.com.","author":{"name":"Rodrigo Gir\u00e3o Serr\u00e3o"},"items":[{"title":"JavaScript 2D scrolling game tutorial","date_published":"2025-02-28T12:08:00+01:00","id":"https:\/\/mathspp.com\/blog\/javascript-2d-scrolling-game-tutorial","url":"https:\/\/mathspp.com\/blog\/javascript-2d-scrolling-game-tutorial","content_html":"<p>This tutorial walks you through implementing a 2D scrolling game in JavaScript.<\/p>\n\n<p>This JavaScript tutorial is for people who know programming (for example, in Python) but have no JavaScript knowledge.\nIn this tutorial we will build a 2D scrolling game where the player (a red ball) will jump to avoid obstacles (black rectangles) that come toward the player at increasing speeds.<\/p>\n<p>You can play a demo of the game below.\nClick the gray rectangle to start the game and press <kbd>SPACE<\/kbd> to make the player jump over obstacles.\n(If you click the game area again, it restarts.)<\/p>\n<iframe style=\"border: 0;\" width=\"100%\" height=\"330\" src=\"\/blog\/javascript-2d-scrolling-game-tutorial\/game21.html\"><\/iframe>\n<h2 id=\"javascript-and-the-web\">JavaScript and the web<a href=\"#javascript-and-the-web\" class=\"toc-anchor after\" data-anchor-icon=\"#\" aria-label=\"Anchor\"><\/a><\/h2>\n<p>JavaScript is a language that runs directly in your browser without you having to do anything else.\nIn fact, if you click the address bar (where you usually type the URL addresses of the websites you want to visit) and type something like <code>javascript:alert(\"Hello, world!\");<\/code>, the browser should display a pop-up with the message &ldquo;Hello, world!&rdquo;.<\/p>\n<p>JavaScript is tightly integrated with your browser.\nThis means you can also use JavaScript to manipulate web pages.\nIn JavaScript, <code>document<\/code> is a variable that refers to the web page (document) you're currently on, and it lets you interact with the page programmatically.\nHere's a fun example:<\/p>\n<ol><li>open the Wikipedia page at <wikipedia.org>; and<\/wikipedia.org><\/li>\n<li>type <code>javascript:document.getElementById(\"www-wikipedia-org\").remove()<\/code> in your address bar.<\/li>\n<\/ol><p>If all goes well[^1] and you copy that code correctly, it should look like the page disappeared.\nIn fact, if you look at the end of the code you pasted, there's a <code>remove<\/code> in there.\nWhat we did was ask the web page for an element with the unique ID <code>www-wikipedia-org<\/code>, which for the main Wikipedia page refers to the whole visible page.<\/p>\n<p>Don't worry, you didn't hack Wikipedia and you aren't going to jail.\nIf you refresh the page, you should get a brand new copy of the page you were at, and everything should be back to normal.\nThat's because the JavaScript code you ran when you pasted it in the address bar was running <em>locally<\/em>, on your browser, on your copy of the Wikipedia main page.\nIt ran on the &ldquo;client side&rdquo;.<\/p>\n<h2 id=\"developer-console\">Developer console<a href=\"#developer-console\" class=\"toc-anchor after\" data-anchor-icon=\"#\" aria-label=\"Anchor\"><\/a><\/h2>\n<p>Next, I want to tell you about the developer console.\nThe developer console is like a debugger, but instead of being built into your IDE of choice, it's built into your web browser.\nAll major web browsers have a developer console: Safari, Chrome, Firefox, Opera, Brave, etc.<\/p>\n<p>Each browser has a different shortcut to open the developer console, so you may want to look up &ldquo;Open developer console INSERT_BROWSER_HERE&rdquo;.<\/p>\n<p>In most browsers, a roundabout way of achieving this effect is right-clicking <em>anywhere<\/em> on a page and then looking for the option that says something like &ldquo;Inspect&rdquo; or &ldquo;Inspect element&rdquo;.\nThat should open a window that lets you peek at the source code of the page you're currently on, and it should have a tab that says something like &ldquo;Console&rdquo; or &ldquo;JavaScript console&rdquo;.<\/p>\n<p>If you haven't been able to open the console yet, look...<\/p>","summary":"This tutorial walks you through implementing a 2D scrolling game in JavaScript.","date_modified":"2025-12-19T21:10:22+01:00","tags":["game","javascript","programming"],"image":"\/user\/pages\/02.blog\/javascript-2d-scrolling-game-tutorial\/thumbnail.webp"},{"title":"Solving Wordle with APL","date_published":"2022-03-28T15:00:00+02:00","id":"https:\/\/mathspp.com\/blog\/solving-wordle-with-apl","url":"https:\/\/mathspp.com\/blog\/solving-wordle-with-apl","content_html":"<p>Join me in solving the word game Wordle in (Dyalog) APL.<\/p>\n\n<figure class=\"image-caption\"><img title=\"A screenshot of a session of Wordle I played.\" alt=\"\" src=\"\/images\/2\/f\/1\/8\/4\/2f1843fc9239532c53c2d0d2ca0b46632768f425-thumbnail.webp\"><figcaption class=\"\">A screenshot of a session of Wordle I played.<\/figcaption><\/figure><h2 id=\"introduction\">Introduction<a href=\"#introduction\" class=\"toc-anchor after\" data-anchor-icon=\"#\" aria-label=\"Anchor\"><\/a><\/h2>\n<p><a href=\"https:\/\/www.nytimes.com\/games\/wordle\/index.html\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" class=\"external-link no-image\">Wordle<\/a> is a conceptually simple game that you can play online.\n(For those of you who know it, it's like <a href=\"https:\/\/en.wikipedia.org\/wiki\/Mastermind_(board_game)\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" class=\"external-link no-image\">Mastermind<\/a> (the board game),\nbut with words.)\nThere is a secret word that you have to guess and you are given 6 tries\nto find out what word is the secret word.\nWhen you make a guess, you get some information back:<\/p>\n<ul><li>you are told which letters of the guess match letters of the secret word;<\/li>\n<li>you are told which letters of the guess exist in the secret word,\nbut are in the wrong position in the guess; and<\/li>\n<li>you are told which letters of the guess don't exist in the secret word.<\/li>\n<\/ul><p>For example, assume that the secret word is &ldquo;chess&rdquo;, and you guess &ldquo;caves&rdquo;:<\/p>\n<ul><li>the &ldquo;c&rdquo; and the &ldquo;s&rdquo; are in their correct positions when compared to the secret word;<\/li>\n<li>the letter &ldquo;e&rdquo; in &ldquo;caves&rdquo; exists in the secret word but it's in the wrong position; and<\/li>\n<li>the letters &ldquo;a&rdquo; and &ldquo;v&rdquo; do not exist in the secret word.<\/li>\n<\/ul><p>Here is how the game would represent this information:<\/p>\n<figure class=\"image-caption\"><img title=\"The visual representation of the letter information.\" alt=\"The word &ldquo;caves&rdquo; where the &ldquo;c&rdquo; and the &ldquo;s&rdquo; are highlighted in green, the &ldquo;e&rdquo; is in yellow, and the &ldquo;a&rdquo; and the &ldquo;v&rdquo; are shaded in grey.\" src=\"\/user\/pages\/02.blog\/solving-wordle-with-apl\/_caves.webp\"><figcaption class=\"\">The visual representation of the letter information.<\/figcaption><\/figure><p>Again, recall that:<\/p>\n<ul><li>the green squares around &ldquo;c&rdquo; and &ldquo;s&rdquo; tell you that those letters are in the correct place;<\/li>\n<li>the yellow square around &ldquo;e&rdquo; tells you that &ldquo;e&rdquo; exists in the secret word but in some other position; and<\/li>\n<li>the gray squares around &ldquo;a&rdquo; and &ldquo;v&rdquo; tell you that &ldquo;a&rdquo; and &ldquo;v&rdquo; are not in the secret word.<\/li>\n<\/ul><h2 id=\"objective\">Objective<a href=\"#objective\" class=\"toc-anchor after\" data-anchor-icon=\"#\" aria-label=\"Anchor\"><\/a><\/h2>\n<p>What we want to do is write an <a href=\"https:\/\/apl.wiki\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" class=\"external-link no-image\">APL<\/a> program that helps us play Wordle.\n(I have written <a href=\"\/blog\/solving-wordle-with-python\">a similar article<\/a> in the past, but using Python.)<\/p>\n<p>We will write a couple of functions:<\/p>\n<ul><li>a function <code>Score<\/code> that scores a given guess with respect to a given secret word; and<\/li>\n<li>a function <code>Filter<\/code> that returns the possible secret words with respect to a given guess and its score.<\/li>\n<\/ul><div class=\"notices yellow\">\n<p>All the code is available in <a href=\"https:\/\/github.com\/rodrigogiraoserrao\/WordleAPL\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" class=\"external-link no-image\">this GitHub repository<\/a>.<\/p>\n<\/div>\n<p>As for the letter information, which I'll call the score of a guess, we will encode it in this way:<\/p>\n<ul><li>a <code>0<\/code> means the letter isn't in the secret word;<\/li>\n<li>a <code>1<\/code> means the letter is in the incorrect position; and<\/li>\n<li>a <code>2<\/code> means the letter is in the correct position.<\/li>\n<\/ul><p>Here are a couple of examples:<\/p>\n<table><thead><tr><th style=\"text-align: left;\">Secret word<\/th>\n<th style=\"text-align: left;\">Guess<\/th>\n<th style=\"text-align: left;\">Score<\/th>\n<\/tr><\/thead><tbody><tr><td style=\"text-align: left;\">chess<\/td>\n<td style=\"text-align: left;\">caves<\/td>\n<td style=\"text-align: left;\"><code>2 0 0 1 2<\/code><\/td>\n<\/tr><tr><td style=\"text-align: left;\">chess<\/td>\n<td style=\"text-align: left;\">swiss<\/td>\n<td style=\"text-align: left;\"><code>0 0 0 2 2<\/code><\/td>\n<\/tr><tr><td style=\"text-align: left;\">talon<\/td>\n<td style=\"text-align: left;\">fault<\/td>\n<td style=\"text-align: left;\"><code>0 2 0 1 1<\/code><\/td>\n<\/tr><\/tbody><\/table><p>Take a second to make sure you understand all of the scores above.<\/p>\n<h2 id=\"getting-some-data\">Getting some data<a href=\"#getting-some-data\" class=\"toc-anchor after\" data-anchor-icon=\"#\" aria-label=\"Anchor\"><\/a><\/h2>\n<p>The first thing we will do is <a href=\"https:\/\/raw.githubusercontent.com\/rodrigogiraoserrao\/WordleAPL\/main\/WORD.LST\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" class=\"external-link no-image\">grab a list of words<\/a> to play with.\nIf I'm not mistaken, the list I linked to is a free list of the Scrabble dictionary.<\/p>\n<p>(If you are on a Unix system, you may also get away with just using the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Words_(Unix)\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" class=\"external-link no-image\">\n  <code>words<\/code>\n<\/a> file.)<\/p>\n<p>Now that we have some data,...<\/p>","summary":"Join me in solving the word game Wordle in (Dyalog) APL.","date_modified":"2025-07-23T16:49:02+02:00","tags":["apl","game","logic","programming"],"image":"\/user\/pages\/02.blog\/solving-wordle-with-apl\/thumbnail.webp"},{"title":"Solving Wordle with Python","date_published":"2022-01-03T00:00:00+01:00","id":"https:\/\/mathspp.com\/blog\/solving-wordle-with-python","url":"https:\/\/mathspp.com\/blog\/solving-wordle-with-python","content_html":"<p>In this article we try to crack Wordle, a variation of mastermind played with (English) words.<\/p>\n\n<figure class=\"image-caption\"><img title=\"A screenshot of a session of Wordle I played.\" alt=\"\" src=\"\/images\/6\/d\/3\/c\/2\/6d3c23ef1c8158c00266b2ac30fad133d242db98-thumbnail.webp\"><figcaption class=\"\">A screenshot of a session of Wordle I played.<\/figcaption><\/figure><h2 id=\"introduction\">Introduction<a href=\"#introduction\" class=\"toc-anchor after\" data-anchor-icon=\"#\" aria-label=\"Anchor\"><\/a><\/h2>\n<p><a href=\"https:\/\/www.nytimes.com\/games\/wordle\/index.html\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" class=\"external-link no-image\">Wordle<\/a> is a conceptually simple game that you can play online.\n(For those of you who know it, it's like <a href=\"https:\/\/en.wikipedia.org\/wiki\/Mastermind_(board_game)\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" class=\"external-link no-image\">Mastermind<\/a> (the board game),\nbut with words.)\nThere is a secret word that you have to guess and you are given 6 tries\nto find out what word is the secret word.\nWhen you make a guess, you get some information back:<\/p>\n<ul><li>you are told which letters of the guess match letters of the secret word;<\/li>\n<li>you are told which letters of the guess exist in the secret word,\nbut are in the wrong position in the guess; and<\/li>\n<li>you are told which letters of the guess don't exist in the secret word.<\/li>\n<\/ul><p>For example, assume that the secret word is &ldquo;chess&rdquo;, and you guess &ldquo;caves&rdquo;.<\/p>\n<ul><li>The &ldquo;c&rdquo; and the &ldquo;s&rdquo; are in their correct positions, when compared to the secret word.<\/li>\n<li>The letter &ldquo;e&rdquo; in &ldquo;caves&rdquo; exists in the secret word, just not as the second-to-last letter.<\/li>\n<li>The letters &ldquo;a&rdquo; and &ldquo;v&rdquo; do not exist in the secret word.<\/li>\n<\/ul><p>Here is how the game would represent this information:<\/p>\n<figure class=\"image-caption\"><img title=\"The visual representation of the letter information.\" alt=\"The word &ldquo;caves&rdquo; where the &ldquo;c&rdquo; and the &ldquo;s&rdquo; are highlighted in green, the &ldquo;e&rdquo; is in yellow, and the &ldquo;a&rdquo; and the &ldquo;v&rdquo; are shaded in grey.\" src=\"\/user\/pages\/02.blog\/solving-wordle-with-python\/_caves.webp\"><figcaption class=\"\">The visual representation of the letter information.<\/figcaption><\/figure><p>What we want to do is write a Python program that plays Wordle for us.<\/p>\n<p>By the way, I am only doing this because I was &ldquo;nerd snipped&rdquo; by <a href=\"https:\/\/twitter.com\/willmcgugan\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" class=\"external-link no-image\">Will McGugan<\/a> on Twitter\n(<a href=\"https:\/\/twitter.com\/willmcgugan\/status\/1478043926636417026\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" class=\"external-link no-image\">this<\/a> is the tweet at fault).<\/p>\n<h2 id=\"how-to-play-wordle\">How to play Wordle?<a href=\"#how-to-play-wordle\" class=\"toc-anchor after\" data-anchor-icon=\"#\" aria-label=\"Anchor\"><\/a><\/h2>\n<p>The very first step in writing a program that plays a game &ndash; Wordle, in this instance&nbsp;&ndash;\nis determining the <em>algorithm<\/em> used to play the game.\nFor Wordle, we will follow this strategy:<\/p>\n<ul><li>we start with a (big) list of words that could be the secret word;<\/li>\n<li>whenever we give a guess, we look at our list of words and remove all words\nthat do not respect the information we get back from our guess; and<\/li>\n<li>we keep guessing until our list of possible words only has a single word &ndash; the secret word.<\/li>\n<\/ul><p>For example, our initial list of words could be something like\n&ldquo;balls&rdquo;, &ldquo;caves&rdquo;, &ldquo;chess&rdquo;, &ldquo;doses&rdquo;, &ldquo;piece&rdquo;, &ldquo;swiss&rdquo;, and &ldquo;truss&rdquo;.\nIf I were playing perfectly, after my initial guess of &ldquo;piece&rdquo; I can already exclude\nwords like &ldquo;doses&rdquo; and &ldquo;swiss&rdquo; as the correct word.<\/p>\n<p>Why?\nBecause I already know the secret word doesn't have any &ldquo;e&rdquo;s or &ldquo;i&rdquo;s.\nHowever, because I can't hold a list of all 5-letter English words I know in my head,\nI kept playing familiar words (like &ldquo;doses&rdquo; and &ldquo;swiss&rdquo;) to look for other tid-bits of information.<\/p>\n<p>A computer won't struggle with this, so this is the strategy we will follow.<\/p>\n<h2 id=\"tl-dr\">TL;DR:<a href=\"#tl-dr\" class=\"toc-anchor after\" data-anchor-icon=\"#\" aria-label=\"Anchor\"><\/a><\/h2>\n<p>If all you care about is the code, you can get it from <a href=\"https:\/\/github.com\/RodrigoGiraoSerrao\/projects\/blob\/master\/misc\/wordle.py\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" class=\"external-link no-image\">here<\/a>.<\/p>\n<h2 id=\"getting-the-data\">Getting the data<a href=\"#getting-the-data\" class=\"toc-anchor after\" data-anchor-icon=\"#\" aria-label=\"Anchor\"><\/a><\/h2>\n<p>The first step in writing a program that plays Wordle for us is fetching a (big) list of English words.\nI was lucky enough that <a href=\"https:\/\/twitter.com\/willmcgugan\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" class=\"external-link no-image\">Will<\/a> had...<\/p>","summary":"In this article we try to crack Wordle, a variation of mastermind played with (English) words.","date_modified":"2025-07-23T16:49:02+02:00","tags":["game","logic","programming","python"],"image":"\/user\/pages\/02.blog\/solving-wordle-with-python\/thumbnail.webp"},{"title":"Problem #042 \u2013 mine captcha","date_published":"2021-08-08T00:00:00+02:00","id":"https:\/\/mathspp.com\/blog\/problems\/mine-captcha","url":"https:\/\/mathspp.com\/blog\/problems\/mine-captcha","content_html":"<p>Can you solve this little minesweeper puzzle?<\/p>\n\n<p><img alt=\"4 by 4 minesweeper grid containing, from left to right, top to bottom: 2, gray, 1, gray, gray, gray, 3, gray, 3, gray, gray, gray, gray, 1, gray, 1\" src=\"\/images\/5\/3\/8\/6\/2\/53862db52e0c8e8e389ecae3cca546fa73fa02d0-thumbnail.webp\"><\/p>\n<h2 id=\"problem-statement\">Problem statement<a href=\"#problem-statement\" class=\"toc-anchor after\" data-anchor-icon=\"#\" aria-label=\"Anchor\"><\/a><\/h2>\n<p>The <a href=\"https:\/\/xkcd.com\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" class=\"external-link no-image\">xkcd<\/a> website published its cartoon <a href=\"https:\/\/xkcd.com\/2496\/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" class=\"external-link no-image\">#2496<\/a>, containing a minesweeper grid,\nand that is the problem for this post:\nto locate all of the mines in the minesweeper grid above.<\/p>\n<p>For those of you who don't know the minesweeper game,\nhere are the rules:<\/p>\n<ul>\n<li>some squares contain mines, others don't;<\/li>\n<li>squares that do not contain mines contain numbers instead,\nand those numbers indicate how many mines are neighbours to that numbered square\n(the neighbourhood of a square contains the squares that are directly\nadjacent to it, plus the squares that share a vertex in the diagonal).<\/li>\n<li>gray squares can contain mines or other numbers.<\/li>\n<\/ul>\n<p>You must find all the mines by reasoning about the numbers you see.<\/p>\n<p>You can play minesweeper online or you can play <a href=\"\/blog\/minesweeper-remake\">my minesweeper remake<\/a>.<\/p>\n<div class=\"notices blue\">\n<p>Give it some thought!<\/p>\n<\/div>\n<p>If you need any clarification whatsoever, feel free to ask in the comment section below.<\/p>\n<h2 id=\"solvers\">Solvers<a href=\"#solvers\" class=\"toc-anchor after\" data-anchor-icon=\"#\" aria-label=\"Anchor\"><\/a><\/h2>\n<p>Congratulations to the ones that solved this problem correctly and, in particular, to the ones\nwho sent me their correct solutions:<\/p>\n<ul>\n<li>David H., Taiwan;<\/li>\n<li>Gerard M., Ireland;<\/li>\n<li>Peter K., USA;<\/li>\n<li>Rodrigo H. M., Mexico;<\/li>\n<li>Rita F., Portugal;<\/li>\n<\/ul>\n<p>Join the list of solvers by <a href=\"mailto:rodrigo@mathspp.com?subject=Solution%20to%20Problem%20#042%20%E2%80%93%20mine%20captcha\" class=\"mailto\">emailing me<\/a> your solution!<\/p>\n<h2 id=\"solution\">Solution<a href=\"#solution\" class=\"toc-anchor after\" data-anchor-icon=\"#\" aria-label=\"Anchor\"><\/a><\/h2>\n<p>Let's look for the mines together!<\/p>\n<p>This is the mine field we have to work with:<\/p>\n<p><img alt=\"\" src=\"\/user\/pages\/02.blog\/03.problems\/p042-mine-captcha\/_1.webp\"><\/p>\n<p>Let's look at the lower-left corner, at the <code>3<\/code> and the <code>1<\/code>.<\/p>\n<p>The <code>3<\/code> has four neighbouring cells:<\/p>\n<p><img alt=\"\" src=\"\/user\/pages\/02.blog\/03.problems\/p042-mine-captcha\/_2.webp\"><\/p>\n<p>The two cells that have a double circle are next to the <code>1<\/code>,\nand therefore we can't have mines in both of those cells.\nTherefore, the two top circles <em>must<\/em> contain mines.<\/p>\n<p><img alt=\"\" src=\"\/user\/pages\/02.blog\/03.problems\/p042-mine-captcha\/_3.webp\"><\/p>\n<p>Two mines have been found!<\/p>\n<p>The <code>3<\/code> that we were looking at now has two mines placed\nnext to it, so we only have to find the last one.<\/p>\n<p>Regardless of where that mine is (one of the two double circles below),\nit is always going to be next to the bottom-left <code>1<\/code>.\nThis means that there can be <em>no<\/em> mines between the two <code>1<\/code>s\non the bottom row.<\/p>\n<p><img alt=\"\" src=\"\/user\/pages\/02.blog\/03.problems\/p042-mine-captcha\/_4.webp\"><\/p>\n<p>By crossing out those two locations, we know that the bottom-right\n<code>1<\/code> has a mine right above it.<\/p>\n<p><img alt=\"\" src=\"\/user\/pages\/02.blog\/03.problems\/p042-mine-captcha\/_5.webp\"><\/p>\n<p>For the next step, we can look at the top-right <code>1<\/code>.\nThe top-right <code>1<\/code> already has a neighbouring mine\n(to the left, and down) which means all other neighbouring\ncells must be empty.<\/p>\n<p><img alt=\"\" src=\"\/user\/pages\/02.blog\/03.problems\/p042-mine-captcha\/_6.webp\"><\/p>\n<p>In crossing out those positions,\nwe realise that the <code>3<\/code> on the second row, to the right,\nonly has one position left for its third mine.\nTherefore, we can place a mine there.<\/p>\n<p><img alt=\"\" src=\"\/user\/pages\/02.blog\/03.problems\/p042-mine-captcha\/_7.webp\"><\/p>\n<p>As we place that mine, the puzzle is complete!\nTake a look for yourself:<\/p>\n<p><img alt=\"\" src=\"\/user\/pages\/02.blog\/03.problems\/p042-mine-captcha\/_8.webp\"><\/p>\n<p><a href=\"https:\/\/mathspp.com\/subscribe\">Don't forget to subscribe to the newsletter<\/a> to get bi-weekly\nproblems sent straight to your inbox.<\/p>","summary":"In this problem we tackle a puzzle posted by the infamous xkcd cartoon website.","date_modified":"2025-07-23T16:49:02+02:00","tags":["game","logic","mathematics"],"image":"\/user\/pages\/02.blog\/03.problems\/p042-mine-captcha\/thumbnail.webp"},{"title":"Problem #023 - guess the polynomial","date_published":"2020-11-15T00:00:00+01:00","id":"https:\/\/mathspp.com\/blog\/problems\/guess-the-polynomial","url":"https:\/\/mathspp.com\/blog\/problems\/guess-the-polynomial","content_html":"<p>In this problem you have to devise a strategy to beat the computer in a \"guess the polynomial\" game.<\/p>\n\n<script>\n    var max_degree = 3;\n    var max_coef = 3;\n    var poly_times = 0;\n    var evaluated_at = [];\n\n    \/\/ Generate a random integer between a and b, inclusive.\n    randint = function(a, b) {\n        return Math.floor(Math.random()*(1+b-a)) + a;\n    }\n\n    reset_poly = function() {\n        poly_times = 0;\n        evaluated_at = [];\n        document.getElementById(\"polyHint\").innerHTML = \"\";\n        document.getElementById(\"polyTimes\").innerHTML = 0;\n        document.getElementById(\"polyResult\").innerHTML = \"\";\n        reset_test_coefs();\n        set_disables(guessing = true);\n    }\n\n    \/* set the disabled status of inputs and buttons, depending on whether\n     * the user is currently guessing the poly or not. *\/\n    set_disables = function(guessing) {\n        document.getElementById(\"newPolyBtn\").disabled = guessing;\n        document.getElementById(\"verifyPolyBtn\").disabled = !guessing;\n        document.getElementById(\"giveUpPolyBtn\").disabled = !guessing;\n        set_disabled_test_coefs(disabled = !guessing);\n    }\n\n    reset_test_coefs = function() {\n        for (var i = 0; i <= max_degree; ++i) {\n            document.getElementById(`c${i}`).value = 0;\n        }\n    }\n\n    set_disabled_test_coefs = function(disabled) {\n        for (var i = 0; i <= max_degree; ++i) {\n            document.getElementById(`c${i}`).disabled = disabled;\n        }\n    }\n\n    var poly = new Array(max_degree + 1);\n    generate_poly = function() {\n        for (var i = 0; i <= max_degree; ++i) {\n            poly[i] = randint(0, max_coef);\n        }\n        reset_poly();\n    }\n\n    evaluate_poly = function() {\n        var a = parseInt(document.getElementById(\"polyAt\").value);\n        var value = 0;\n        for (var i = 0; i <= max_degree; ++i) {\n            value += poly[i]*a**i;\n        }\n        document.getElementById(\"polyHint\").innerHTML = `p(${a}) = ${value}`;\n        if (-1 === evaluated_at.indexOf(a)) {\n            evaluated_at.push(a);\n            ++poly_times;\n            document.getElementById(\"polyTimes\").innerHTML = poly_times;\n        }\n    }\n\n    verify_poly = function() {\n        var right = true;\n        for (var i = 0; i <= max_degree; ++i) {\n            right &= document.getElementById(`c${i}`).value === `${poly[i]}`;\n        }\n        if (right) {\n            document.getElementById(\"polyResult\").innerHTML = \"Correct!\";\n            set_disables(guessing = false);\n        } else {\n            document.getElementById(\"polyResult\").innerHTML = \"Wrong!\";\n        }\n    }\n\n    give_up_poly = function() {\n        set_disables(guessing = false);\n        polyResult = `The polynomial was p(n) = ${poly[0]}`\n        for (var i = 1; i<= max_degree; ++i) {\n            polyResult += ` + ${poly[i]}n^${i}`;\n        }\n        document.getElementById(\"polyResult\").innerHTML = polyResult;\n    }\n\n    window.onload = generate_poly;\n<\/script><figure class=\"image-caption\"><img title=\"Photo by Emily Morter on Unsplash\" alt=\"A question mark in a neon light\" src=\"\/images\/6\/e\/e\/9\/7\/6ee97fa1f35461424a54a2d0112230b1a0cc2f18-thumbnail.jpg\"><figcaption class=\"\">Photo by Emily Morter on Unsplash<\/figcaption><\/figure><h2 id=\"problem-statement\">Problem statement<a href=\"#problem-statement\" class=\"toc-anchor after\" data-anchor-icon=\"#\" aria-label=\"Anchor\"><\/a><\/h2>\n<p>I want you to play a little game with the computer.\nThe computer is going to think of a polynomial with non-negative, integer coefficients.\nLet's say <span class=\"mathjax mathjax--inline\">\\(p(n)\\)<\/span> is the secret polynomial the computer is thinking of.<\/p>\n<p>I want you to find out what <span class=\"mathjax mathjax--inline\">\\(p(n)\\)<\/span> is, and the only thing you can do is to ask for hints in the form of values of <span class=\"mathjax mathjax--inline\">\\(p(n)\\)<\/span> for <span class=\"mathjax mathjax--inline\">\\(n \\geq 0\\)<\/span> integer.\nFor example, you can ask what <span class=\"mathjax mathjax--inline\">\\(p(0)\\)<\/span> or <span class=\"mathjax mathjax--inline\">\\(p(49)\\)<\/span> is, but you can't ask for the value of <span class=\"mathjax mathjax--inline\">\\(p(-1)\\)<\/span> or <span class=\"mathjax mathjax--inline\">\\(p(0.5)\\)<\/span>.\nYou have to come up with the best strategy possible, that allows you to determine <span class=\"mathjax mathjax--inline\">\\(p(n)\\)<\/span> with the least number of hints possible.<\/p>\n<p>You can test your strategy with the computer below.\nThe computer will only think of polynomials with degree at most <span class=\"mathjax mathjax--inline\">\\(3\\)<\/span>\nand the coefficients will be at most <span class=\"mathjax mathjax--inline\">\\(3\\)<\/span> as well, but that is just to make testing your strategy easier.\nThe strategy should work for higher degrees and larger coefficients.<\/p>\n<p>With the restrictions for the computer test, we have<\/p>\n<p class=\"mathjax mathjax--block\">\\[\np(n) = c_0 + c_1n + c_2n^2 + c_3n^3, 0 \\leq...<\/p>","summary":"In this problem you have to beat the computer in a guessing game.","date_modified":"2025-07-23T16:49:02+02:00","tags":["mathematics","polynomials","number theory","game"],"image":"\/user\/pages\/02.blog\/03.problems\/p023-guess-the-polynomial\/thumbnail.jpg"},{"title":"HueHue: a colourful game","date_published":"2017-11-24T00:00:00+01:00","id":"https:\/\/mathspp.com\/blog\/huehue","url":"https:\/\/mathspp.com\/blog\/huehue","content_html":"<p>HueHue is a very colourful game I wrote with my colleague <a href=\"https:\/\/github.com\/inesfmarques\/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" class=\"external-link no-image\">@inesfmarques<\/a>.<\/p>\n\n<p><img alt=\"a screenshot of an initial scrambled degradee from the game\" src=\"\/images\/4\/c\/f\/d\/a\/4cfda6d73573a752d17bce5ec37c6b3c848a23f2-astart.webp\"><\/p>\n<p>We programmed a small puzzle game, based solely on colours, that we called HueHue (please mind that the idea for how the game works is not ours). The way the game works is pretty straightforward: you start with a bunch of coloured tiles all mixed up, like in the image above, and then you want to organize them to create a degradee like the one below! The only thing you can take for granted is that the four corners are already correctly placed (and you can't move them, just to prevent accidents).<\/p>\n<p><img alt=\"a screenshot of the corresponding final state with the degradee in place\" src=\"\/user\/pages\/02.blog\/huehue\/end.webp\"><\/p>\n<p>The instructions are also quite simple: you use left and right clicks to swap tiles. Whenever you left-click a tile, you are telling the game that you will want to swap that tile. Whenever you right-click a tile, that tile gets swapped with the last tile you left-clicked! Simple, right?<\/p>\n<p>Or you can just drag the tiles around, that is even simpler.<\/p>\n<p>When you finish the puzzle and rearrange the degradee, the window caption changes and you can't swap any more tiles. To change the number of tiles in the puzzle or their sizes edit the file <code>HueHueConfig.py<\/code>.<\/p>\n<p>When we had the game logic already implemented but were still testing the way we would create the degradee, I created another small script that just populates the screen with a random degradee like the one below. <\/p>\n<p><img alt='a \"continuous\" degradee from the helper script' src=\"\/user\/pages\/02.blog\/huehue\/degradee.webp\"><\/p>\n<p>You can find the code for the game and for the random degradee generator in <a href=\"https:\/\/github.com\/RodrigoGiraoSerrao\/minigames\/tree\/master\/huehue\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" class=\"external-link no-image\">this GitHub folder<\/a>. Enjoy!<\/p>\n<p>How would you improve this game? Let me know in the comments below.<\/p>","summary":"In this post I show you a colourful game I wrote with a friend of mine.","date_modified":"2024-08-13T12:47:46+02:00","tags":["game","programming","pygame","python"],"image":"\/user\/pages\/02.blog\/huehue\/a_start.webp"},{"title":"Pigeon Pooping Simulator","date_published":"2017-10-13T00:00:00+02:00","id":"https:\/\/mathspp.com\/blog\/pigeon-pooping-simulator","url":"https:\/\/mathspp.com\/blog\/pigeon-pooping-simulator","content_html":"<p>This blog post has a single purpose, which is to show you the weird game I made, inspired by Flappy Bird and my crazy English teacher.<\/p>\n\n<p><img alt=\"Gameplay screenshot\" src=\"\/user\/pages\/02.blog\/pigeon-pooping-simulator\/simulator_screenshot.webp\"><\/p>\n<p>The inspiration for this game came after an English class in which my English teacher jokingly talked about a pigeon simulator game. Of course I took it seriously and a couple\nof lessons later I had this game working: a game where you just fly up and down, hitting SPACE to try and poop on the heads of the people you fly past.<\/p>\n<p>The way the game works is pretty simple: use the spacebar to poop on top of the people you fly past. Hitting <strong>Q<\/strong> will exit the game and on the first screen you see, when you\nstart the game, you can use the <strong>+<\/strong> and <strong>-<\/strong> keys to change the difficulty (which relates to the speed at which the pigeon flies).<\/p>\n<p>The goal is to hit as many people as possible, knowing that you can't poop <em>constantly<\/em>. There is a little - intentional - delay. After flying and pooping for 60 seconds\nyou are greeted by a screen with some stats, namely the percentage of people you managed to hit and the percentage of poops that hit \/ missed people.<\/p>\n<p>The code was written Python 3 and pygame and the code + images can be found in <a href=\"https:\/\/github.com\/RodrigoGiraoSerrao\/minigames\/tree\/master\/pigeon-simulator\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" class=\"external-link no-image\">this GitHub repo<\/a>. A windows executable can also be\nfound inside <a href=\"https:\/\/drive.google.com\/open\" id=\"0ByBeLS6ciLYVWElhc2dZdFc1Ykk\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" class=\"external-link no-image\">this Drive folder<\/a>. In order to be able to run the executable, the folder must be left as-is.<\/p>","date_modified":"2024-08-13T12:47:46+02:00","tags":["game","programming","pygame","python"],"image":"\/user\/pages\/02.blog\/pigeon-pooping-simulator\/simulator_screenshot.webp"},{"title":"Minesweeper remake","date_published":"2017-10-13T00:00:00+02:00","id":"https:\/\/mathspp.com\/blog\/minesweeper-remake","url":"https:\/\/mathspp.com\/blog\/minesweeper-remake","content_html":"<p>Minesweeper has to be one of the most well-known minigames of all time, no? I spent my fair share of Sunday mornings playing minesweeper in my Windows XP computer...<\/p>\n\n<p><img alt=\"A screenshot of my implementation of Minesweeper\" src=\"\/images\/a\/2\/b\/c\/d\/a2bcd8115582d64e9195be47937edccd6f6b241b-minesweeper-screenshot.webp\"><\/p>\n<p>I made a remake of Minesweeper when I was trying to sharpen my Python skills and you can see above a screenshot of my version of the game.<\/p>\n<p>You can find the code in <a href=\"https:\/\/github.com\/RodrigoGiraoSerrao\/minigames\/tree\/master\/minesweeper\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" class=\"external-link no-image\">this GitHub repo<\/a> GitHub repo, where you can also find a Windows executable.<\/p>\n<p>The game is fairly easy to play:<\/p>\n<ul>\n<li>The objective is to find all of the mines.<\/li>\n<li>Using the left mouse button reveals what is beneath the cell clicked. Clicking a mine means game over.<\/li>\n<li>If you are sure of the location of a mine, use the right mouse button to flag it.<\/li>\n<li>A numbered cell indicates how many mines are present in its neighbouring cells.<\/li>\n<\/ul>","date_modified":"2024-08-13T12:47:46+02:00","tags":["programming","python","pygame","game"],"image":"\/user\/pages\/02.blog\/minesweeper-remake\/minesweeper-screenshot.webp"}]}
