# Evil ScienceA whole load of stuff

30May/142

## Harry invokes RASHOMON while talking about MALEFICENT!

Harry shows off his impressive ability to skim Wikipedia pages about a movie and then badly compare it to another movie.?http://www.aintitcool.com/node/67505

A noteworthy snippet from his review:

Burton?s ALICE IN WONDERLAND is a bizarre strange marvel ? and I call it a marvel cuz while watching it?? I marvel at how bizarre it was.

6May/140

## Checking the validity of an NHS Number using 1 line of C#

This article is an implementation of the method for checking NHS Number validity, as described in the article:

This is an experiment to reduce the checking method to one line of code, and makes no?attempt at being efficient.?This single line, below, is arranged into 8 lines to make it easier to read.

```
pNHSNumber.ToCharArray().Where (i=> i>= 48 && i <=57).Count() != 10 ? false :
new List() { pNHSNumber.ToCharArray()
.Where((value, index) => index < 9)
.Select((value, index) => (10 - index) * (value - 48))
.Sum() }
.Select(i=> i % 11)
.Select(i=> (11 - i) == 11 ? 0 : (11-i))
.First() == (pNHSNumber[pNHSNumber.Length - 1] - 48);

```

It is constructed as follows:

• Line 1: Checking the string length is 10 and consists only of digits
• Lines 2 - 5:? Multiply the first nine digits by a weighting factor and sum, storing the?result, a single value, in a generic list.
• Line 3 : select the first 9 digits of pNHSNumber.ToCharArray()
• Line 4: Multiply each of the digits by it's weighting - first digit by 10 e.g. (10 - index of 0), second digit by 9 (e.g. 10 minus index of 1) etc
• Lin 5: sum the values produced by line 4
• Line 6: Get the remainder of the sum when divided by 11
• Line 7: Subtract the remainder from 11, and if the resultant value is 11 change to 0
• Line 8: Test if the check digit is equal to last digit

Usage

Wrap up the above in a function:

```
bool CheckNNHSNumber (pNHSNumber string)
{
pNHSNumber.ToCharArray().Where (i=> i>= 48 && i <=57).Count() != 10 ? false :
new List() { pNHSNumber.ToCharArray()
.Where((value, index) => index < 9)
.Select((value, index) => (10 - index) * (value - 48))
.Sum() }
.Select(i=> i % 11)
.Select(i=> (11 - i) == 11 ? 0 : (11-i))
.First() == (pNHSNumber[pNHSNumber.Length - 1] - 48);
}
```

and call it, thusly:

```
//valid NHS number
Console.WriteLine(CheckNHSNumber("4800963435"));

//invalid NHS Number
Console.WriteLine(CheckNHSNumber("4800963439"));

```
Filed under: C#, Main No Comments
11Mar/140

## Doctor Who missing episode totals by year

The above says it all - the number of episodes of Doctor Who missing by year, the reason for the drop is due to pure look and the dedication of Doctor Who fans in searching for missing episodes.

For more information, go to The Destruction of Time: What Is Missing.

Tagged as: No Comments
1Mar/140

## iPlayer Automator – download BBC iPlayer programs

Currently, Get iPlayer Automator allows you to download and watch BBC & ITV shows on your Mac. Series-Link/PVR functionality ensures you will never miss your favourite shows. Programmes are fully tagged and added to iTunes automatically upon completion. It is simple and easy to use, and runs on any machine running Mac OS X 10.6 or later.

https://www.macupdate.com/app/mac/39019/get-iplayer-automator

Filed under: Mac No Comments
25Feb/140

## C# Brute Force Sudoku Algorithm

I've created in C# an algorithm that solves sudoku using the brute force method. This algorithm is contained in the class csSudokuBruteForce.cs that can be found on GitHub?here.

## A little bit about it

The?csSudokuBruteForce class contains a class called csCell which represents a cell within a sudoku grid with properties representing the row, column, box that cell belongs to, the cell's value and whether it is solved; 81 of these cells are stored within a generic list called grid which represent sudoku.

The public method BruteForce is called to begin the brute algorithm, which returns a integer array representing the solved sudoku. Testing on Quad core PC shows that solutions can be found in under a second for any tried.

This algorithm doesn't test the validity of the provided sudoku before it starts the brute force algorithm, and may therefore crash out if what is given to it is incomplete, malformed, containing incorrect characters. It just assumes it'll get a string 81 characters in length that consists of only the numbers 1 to 9 and 0.

## Using the code

This class is easy to use, and requires only a string of 81 characters?representing?the sudoku to be solved. ?Here's an example of it's use:

```
csSudokuBruteForce b = new csSudokuBruteForce();

<span style="line-height: 1.5em;">string puzzle = "003020600900305001001806400008102900700000008006708200002609500800203009005010300";</span>

//solve the sudoku and return the result as an integer array

int [] solution = ?b.BruteForce(puzzle);

```

If you're using this code in a C# Console Application and want to output the array containing the solution, here's how to do it using Linq:

```
Console.WriteLine (
solution.Select((val, ind) => val.ToString() + ((ind+1) % 9 == 0 ? "\n" : ""))
.Aggregate((total, current) => total + current)
);

```

## What about Project Euler 96?

No. I'm not going to tell you how to to solve it.

## Go on!

No.

I'll give you a hand, but no more.?Here's how to extract one puzzle at a time from Euler 96's text file of puzzles:

```
//load the entire sudoku.txt file

string puzzle;

for (int ctr = 0; ctr <= 49; ctr++)</span>
{

puzzle = puzzles.Where((val, ind) => ind >= (ctr * 10) +1 &amp;amp;amp; ind < (ctr * 10) +10)
.Aggregate((total, current) => total + current);

}

```

It uses loads the text file into a generic list, and then uses Linq to extract the required data from that file. Use your brain and work out how to use it yourself.

You're welcome.

Filed under: C# No Comments
24Feb/141

## Export map array as Bitmap

I have noticed that people have expressed an interest in getting an image of the generated map to pass into Unity3D. So I have updated the Github link to add a new function to the class?csCaveGenerator.cs, that when called generates a bitmap of the map array, and I have pasted it below for your interest.

```/// <summary>
/// Generate a bitmap from the contents of the map array
/// </summary>
/// <returns></returns>
public Bitmap GetMapImage()
{
//adjust to change the pixel size on the image
Size blocksize = new Size(5, 5);

Bitmap bmp = new Bitmap(MapSize.Width * blocksize.Width, MapSize.Height * blocksize.Height);
using (Graphics g = Graphics.FromImage(bmp))
{
using (SolidBrush sbBlack = new SolidBrush(Color.Black))
{
for (int x = 0; x < MapSize.Width; x++)
for (int y = 0; y < MapSize.Height; y++)
if (Map[x, y] == 1)
g.FillRectangle(sbBlack, new Rectangle(x * blocksize.Width, y * blocksize.Height, blocksize.Width, blocksize.Height));

}
}

```
Filed under: Main 1 Comment
23Feb/146

## A C# algorithm to build interesting cave systems for your Roguelike part 1 UPDATED!!!!

I've been playing roguelikes for the last twenty years or so, and one thing that has consistently annoyed me is that dungeons, on the whole, tend to be rectangular rooms connected with long, straight corridors that turn at right angles. I've never came across a roguelike that has complex, chaotic looking cave systems with twisting corridors ?and misshapen rooms, and that has annoyed me so much I decided to put together a simple algoritm in C# to generate such a system. Here's an example of a complex cave systems generated by my code, and below that I discuss the code used to generate it.

## !!!Update!!!

I have been blown away by the interest from the users of Reddit's Unity3D??and made an update to the code allowing users to generate a Bitmap of the generated map. See here for more details.

## Overview

This algorithm works by using a modified form of Conway's Game of Life?where:

1. Each cell in the map is visited and a randomly probability is used to determine if that cell is closed,
2. Cells are randomly visited and the number of neighbours that cell has is used to determine whether to close it or open it.

By repeating the second step thousands of times, "blobs" or "caves" start to form which.?By adjusting various available properties the characteristics of the caves formed can change considerably, which can lead to some very interesting cave systems being formed.

After caves have been generated, a flood fill based algorithm locates each cave and the data for each cave into a generic list. With this list of caves they can be easily connected together with?a dumb corridor building routine is used to try and connect the caves. This works by randomly selecting a cave and growing a corridor in a random direction and seeing if it hits another cave within a time period determined by a series of properties. If sucessful the two connected caves are placed in a list, and a further attempt is made to connect one of those caves to another, and so on.

## The Application

You can find a simple C# application which demos the algorithm I've written?here, and below is a screenshot of it.

The app consists of three areas:

1. Property grid - the properties which govern the generation of the cave system.
2. Picturebox - displays the generated cave system.
3. Buttons:
1. Build Caves - click to build a cave system.
2. Connect Caves - click to connect the caves.
3. Reset Properties - click the reset the property values to their default state.

Simply put, to build a cave system click the?Build Caves button, and to change the appearance of a cave system fiddle with the properties.

### Properties

This application has a number of properties which can be adjusted to determine the appearance of the generated generated cave system:

1. Cave Cleaning
1. Filling -?Fills in holes within caves: an open cell with this number closed neighbours is closed.
2. Lower Limit - the minimum size of caves allowed. If a cave is smaller than this number, it will be removed.
3. Smoothing -?Removes single cells from cave edges: a cell with this number of empty neighbours is removed
4. Upper Limit -?the maximum size of caves allowed. If a cave is larger than this number, it will be removed.
2. Cave Generation
1. Close Cell Prob - this value determines the chance of closing a visited cell.
2. Cells to visit - the number of cells to visit during the generation process.
3. Map Size - guess what this does.
4. Neighbours - when this value is equalled or exceeded, change the cell state.
3. Corridor?
1. Breakout ?- a counter value, that when exceeded stops attempting to generate corridors. Stops the corridor building process from getting stuck.
2. Corridor Spacing?- the number of empty cells the corridor has to have on either side of it for it to be built. This is dependant upon the direction it is travelling. If travelling north, it must have that number of empty cells to the east and west of it.
3. Minimum Length?- the minimum length of a corridor.
4. Maximum Length?- the maximum length of a corridor.
5. Maximum Turns?- the maximum number of direction changes a corridor can make whilst it is being built.
4. Generated Map
1. Caves - the number of caves in the generated system.

## The Code

The code used to generate a caver system is contained within the class csCaveGenerator.cs, which sits in a standard C# form with a few controls on it which can manipulate that class.

For an overview of the class?csCaveGenerator.cs, click here.

For a more detailed look at using the class csCaveGenerator.cs, click here.

To view the class?csCaveGenerator.cs in GitHub click?here.

## Interesting Patterns

Playing around with the properties can produce cave systems with markedly different appearances, here are a few interesting systems that can be produced.

### Several Large Caves

Setting the properties to:

1. MaxSizeToRemove: 1500
2. MinSizeToRemove: 450
3. EmptyCells > EmptyCellNeighbours: 3

and clicking build a few times produced these three lovely caves.

### Many small chunky caves

Setting the properties to:

1. Iterations: 10,000
2. Smothing>EmptyNeighbours:3
3. EmptyCells>EmptyNeighbours:4

And clicking build, produces lots of little chunky caves with straight edges, packed closer together.

### One Massive Cave

Setting the properties to:

1. Caves > MaxSizeToRemove:?10000
2. Cells > CloseCellProb: 55

And clicking Go a few times will produce a lovely, humongous cave, as shown below.

Github: To view the class?csCaveGenerator.cs,?which is used to generate the cave system, click?here.

To download a C# app which demos the Cave Generator algorithm click??here.

Filed under: C#, Roguelike 6 Comments
23Feb/140

## A C# algorithm to build interesting cave systems for your Roguelike – part 3

This post contains a more detailed look at the class?csCaveGenerator.cs, whose layout is discussed here, and?can be viewed on?GitHub here.?

Within the class?csCaveGenerator.cs, the source code is fully commented and easy to follow, and because I'm lazy I'm not going to copy those comments again :). However I will tell you how to use the class. Refer to the section below called Using the Class.

## Using the class

This is simple enough:

```
csCaveGenerator?cavgen = new csCaveGenerator();

```

To build a cave system:

```
cavgen.Build();

```

Connecting rooms is also simple:

```
cavgen.ConnectCaves();

```

## The Generated Map

The publicly exposed property Map is a 2d array which contains the generated map - a value of 1 indicates a closed cell.

Filed under: C#, Roguelike No Comments
23Feb/140

## A C# algorithm to build interesting cave systems for your Roguelike – part 2 class layout

This article describes the layout of the class csCaveGenerator.cs which is used to generate a cave system, and can be viewed on GitHub here.?Within this class are several regions which are used to group together related methods, properties, data structures etc. These regions are:

1. Properties
2. Misc
3. Map Structures
4. Lookups
5. Cave Related
6. Corridor Related
7. Direction Related
8. Cell Related

## Properties Region

Contains the properties used to control the appearance of the cave system being generated. Discussed in detail in part 1.

## Misc

Contains the class constructor and the method Build() which generates the caves.

## Map Structures Region

This contains the generic lists used to hold cave system data: Caves, Corridors and Map.

## Lookups Region

Contains two lists of points which contain directions:

1. Directions: four points which represent North, South, East and West.
2. Directions1: 8 points which represent?North, South, East, West, North East, North West, South East and South West.

These lists are used to examine the neighbours of a cell for the cave generation, smoothing and filling operations.

## Cave Related Region

Contains the code used to generate caves, and subdivided into three regions:

1. Make Caves
2. Locate Caves
3. Connect Caves

#### Make Caves

This contains the method BuildCaves() which generates the cave system

#### Locate Caves

This contains three methods used to locate discrete caves on the map and place them in the generic list?Caves.?The method GetCaves() is called to do this - which uses a recursive flood fill algorithm.

#### Connect Caves

Contains the method?ConnectCaves(), a brute force method which randomly attempts to connect caves. Dependant upon methods in the Cave Related Region.

## Corridor Related Region

Contains the methods:

1. Corridor_GetEdge()
2. Corridor_Attempt()
3. Corridor_PointTest()

Which are all used by the method ConnectCaves().

## Direction Related Region

This region contains a series of methods using in locating the neighbours of a cell, generating a randomly direction etc

## Cell Related Region

A series of methods used for manipulating map cells.

Filed under: C#, Roguelike No Comments
11Feb/140

## HTML 5: Canvas Element

ML 5 defines?the <canvas> element?as ?a resolution-dependent bitmap canvas which can be used for rendering graphs, game graphics, or other visual images on the fly.? A?canvas?is a rectangle in your page where you can use JavaScript to draw anything you want.

This excellent article examines the canvas element in detail, clearly and concisely.

Filed under: HTML5 No Comments