JSweeper

Adding to my collection of clones of popular, well-known games, I created back in November of 2016 a Java-implementation of the all-time Windows classic game, Minesweeper.

Minesweeper was pre-installed on every installation of Windows up to and including Windows 7 and has been ported to a variety of different systems. Because of this, nearly everyone has at least once in their life played Minesweeper or at least heard of it.
In Minesweeper you are presented with a square grid of covered tiles containing either numbers or mines. Your task is it to uncover all tiles which are not mines in the least amount of time. When you uncover a mine, it explodes and the game is lost. To aid in figuring out which tiles are mines and which are not, every tile that is not a mine tells you how many mines are in the neighbouring eight tiles. Tiles which have no neighbouring mines are drawn gray and uncover neighbouring non-mine tiles once uncovered.
More on Minesweeper can be found in this Wikipedia article — I am linking to the German version, as the current English version has major flaws and lacks crucial information. If you are so inclined, feel free to fix the English Minesweeper Wikipedia article.

In my clone, there are three pre-defined difficulty levels, directly ported from the original Minesweeper game, and an option to freely adjust the board’s width and height as well as the number of bombs which will be placed. Gameplay is nearly identical to the original, as my clone also uses a square grid and the tile’s numbers correspond to the number of bombs in the eight tiles surrounding that tile.
The game has a purposefully chosen pixel-look using a self-made font to go along with the pixel-style.

Controls

  • Arrow keys and enter to navigate the main menu
  • Arrow keys or mouse movement to select tiles
  • Space, enter or left-click to expose a tile
  • ‘f’ or right-click to flag a tile
  • ‘r’ to restart game when game is either won or lost
  • Escape to return to the main menu when game is either won or lost
  • F11 toggles fullscreen

To play the game, you can either download the .jar file or compile the source code for yourself. The source code is listed below and can be downloaded as a .java file.

Level select screen Successfully played an easy game A failed attempt at solving a hard game


// Java 1.6 / 1.8 code
// Jonathan Frech  5th of November, 2016
//         edited  7th of November, 2016
//         edited 11th of November, 2016
//         edited 13th of November, 2016
//         edited 14th of November, 2016
//         edited 15th of November, 2016
//         edited 17th of November, 2016
//         edited 19th of November, 2016
//         edited 19th of May     , 2017
//         edited 22nd of May     , 2017
//          * fixed max mine cap when
//            using custom settings

Continue reading

A278328

The On-Line Encyclopedia of Integer Sequences (also known by its acronym, OEIS) is a database hosting hundreds of thousands of — as the name implies — integer sequences. Yet, despite the massive number of entries, I contributed a new integer sequence, A278328.

A278328 describes numbers whose absolute difference to their decimal reverse are square. An example would be 12 or 21 (both are the decimal reverse to each other), since \left|12-21\right|=9 and 9=3^2.

Not a whole lot is known about the sequence, partly due to its definition only resulting in the sequence when using the decimal system, though it is known that there are infinitely many numbers with said property. Since there are infinitely many palindromes (numbers whose reverse is the number itself), \left|n-n\right|=0 and 0=0^2.

Due to there — to my knowledge — not being a direct formula for those numbers, I wrote a Python script to generate them. On the sequence’s page, I posted a program which endlessly spews them out, though I later wrote a Python two-liner, which only calculates those members of the sequence in the range from 0 to 98 (shown below entered in a Python shell).


>>> import math
>>> filter(lambda n:math.sqrt(abs(n-int(str(n)[::-1])))%1 == 0, range(99))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 15, 21, 22, 23, 26, 32, 33, 34, 37, 40, 43, 44, 45, 48, 51, 54, 55, 56, 59, 62, 65, 66, 67, 73, 76, 77, 78, 84, 87, 88, 89, 90, 95, 98]

4096

4096 is a Java-based clone of the well-known web and mobile game 2048, which itself clones 1024 and is similiar to THREES. The naming trend is quite obvious, though note that 2^{12} is a power of two where the exponent is divisible by three, futher connecting to the aforementioned game.

In the game, you are faced with a 4×4 matrix, containing powers of two. By swiping in the four cardinal directions (e.g. pressing the arrow keys), you shove all the non-empty cells to that side. When two equal powers of two collide, they fuse together, adding. Once you shoved, an empty tile pseudo-randomly transforms to either a two-tile (90%) or a four-tile (10%).
Your objective at first is to reach the tile 4096, though the real goal is to achieve the highest score. Your score is the sum of all the collisions you managed to cause.

To play 4096, you can either download the .jar file or review and compile the game for yourself, using the source code listed below.

Controls

  • Up, down, left or right arrow key shoves the tiles
  • Escape restarts the game upon a loss
  • F11 toggles fullscreen

A game after a few moves A finished game with a score of 1700


// Java 1.8 Code
// Jonathan Frech,  5th of December 2016
//          edited  6th of December 2016
//          edited  7th of December 2016
//          edited  8th of December 2016
//          edited  9th of December 2016
//          edited 19th of February 2017
//          edited 24th of February 2017
//          edited 28th of February 2017
//          * gave the 4096 tile a color
//          edited 22nd of April    2017
//          * fixed window positioning by changing
//            frame.setLocationRelativeTo(null); to
//            frame.setLocationByPlatform(true);

Continue reading

Slitherlink Solver

Slitherlink is a neat puzzle in which you are presented with a number matrix and the goal is to draw one connected, not interlooping line in between the cells. The number in each cell determines exactly how many line segments must be drawn around each cell (0, 1, 2 or 3). When a cell does not contain any number, the number of line segments adjacent to this cell are unrestricted.

*  *  *  *  *  *          *--*  *  *--*--*
     2  1  3              |  | 2  1| 3   |
*  *  *  *  *  *          *  *--*  *--*  *
        2  2  2           |     | 2  2| 2|
*  *  *  *  *  *          *--*  *--*  *  *
     2              ->       | 2   |  |  |
*  *  *  *  *  *          *  *--*  *--*  *
  1  3  1     1             1  3| 1     1|
*  *  *  *  *  *          *--*--*  *--*  *
  3     2     3           | 3     2|  | 3|
*  *  *  *  *  *          *--*--*--*  *--*

A sample 5x5 Slitherlink with solution.

Slitherlink was invented by the Japanese publisher Nikoli in 1989. It has many other names than ‘Slitherlink’, yet I prefer this descriptive name. Imagining a snake slithering along the board, seeking to link up with itself is a bit charming.

As with most of these puzzles that have simple rules and are fairly easy to work out by hand — on small scales that is –, writing a solver for them can prove to be more difficult than one may expect.

The first solving strategy I tried out was to brute force the problem. Using the Slitherlink from above as an example, there would be 5 \cdot 5 = 25 different cells with 2 \cdot 5 \cdot 5 + 5 + 5 = 60 line segments. With each line segment either being drawn or not, there are 2^{60} = 1.15 \cdot 10^{18} different boards to check. With one board being checked per nanosecond the solver would take \frac{2^{60}}{10^9} = 1.15 \cdot 10^9 seconds or 36.56 years. Brute force is definitely not a viable way to conquer Slitherlink.

After this harsh discovery, I needed a better way to approach solving a given Slitherlink puzzle. Doing some research, I even discovered that Slitherlink is an NP-complete problem (see this paper by Stefan Herting), whereby it — assuming \text{P} \neq \text{NP} — is not even possible to write a solving algorithm which takes polynomial time.
However, solving small Slitherlink puzzles is fortunately possible in a reasonable time frame.

The strategy I used in the solver consists of pre-programmed rules — figured out by humans — which determine parts of the board based on special arrangements and enforcing the puzzle’s rules (such as that there must only be one line). Using those clues, the solver partly solves a given Slitherlink until there are no more known rules to advance. At that point the solver guesses for a given line segment to be either crossed (marking it cannot be drawn) or drawn, building a tree.
Conflicting attempts (where the solver wrongly guessed, then later — through applying the given rules — determines the attempt as flawed) are thrown away, only leaving possible solved scenarios. Because each Slitherlink has one unique solution, this process ultimately results in one surviving attempt, which then is checked for correctness and printed out as the solution.
A list of Slitherlink rules can be found in this Wikipedia article.

Using the above described method, my solver takes roughly 0.05 seconds on an Intel Core i7 (4.00 GHz) to solve the example 5×5 Slitherlink. A 10×10 Slitherlink takes around 1.6 seconds whereas it takes 32 seconds to solve a 15×15 Slitherlink. The non-polynomial time is clearly recognisable.

My solver best runs in a bash shell, as it uses ANSI escape sequences to give the solved line a vivid blue and is entirely written in Python as well as fully text-based. The source code is listed below.

Other people also have written solvers, including puzzle generators, such as kakuro-online or appspot. The latter even supports different polygons as the Slitherlink base.


# Python 2.7.7 Code
# Jonathan Frech, 22nd of December 2016
# edited 23rd, 24th, 25th, 26th, 27th, 29th, 30th, 31st of December 2016
# edited 1st, 2nd, 3rd, 5th, 8th of January 2017
# edited 10th of February 2017

Continue reading

Christmas

Christmas tree gets chopped,
Excitement fills the people.
Forming winter mood.

         *         
        ***        
       *****       
      *******      
     *********     
    ***********    
   *************   
  ***************  
 ***************** 
       *****       

__ = 9;_ = chr;"istmas tree gets chopped"
exec _(95)+_(95)+_(95)+_(95)+_(61)+_(108)+_(97)+_(109)+_(98)+_(100)+_(97)+_(32)+_(120)+_(58)\
	+_(114)+_(97)+_(110)+_(103)+_(101)+_(40)+_(120)+_(41)+_(10)+_(100)+_(101) +_(102)+_(32)\
	+_(95)+_(40)+_(115)+_(41)+_(58)+_(112)+_(114)+_(105)+_(110)+_(116)+_(32)+_(115)
"itement fills the people"
for m in ____(__):"g winter";___ = m;"ood";_(" "*(__-___)+"*"*(2*___+1)+" "*(__-___))
_(" "*(1/2+__-__/4)+"*"*(__/2)+["","*"][1+2*__-__/2-2*(1/2+__-__/4)]+" "*(1/2+__-__/4))