This bit of pseudo-code is my result from trying my hand at this Viking Code School assignment, also featured in the Grilled Cheese and Grocery Shopping posts. This code was actually pretty easy to write. The real difficulty was in the edge cases, particularly when the number got to 77 (for obvious reasons). My code evolved a bit throughout the process to deal with edge cases and had to be rearranged several times. I have a sneaking suspicion that this pseudo-code is starting to look like real code, though at this point, I cannot be positive. It uses hardly any plain English and I think I like it that way. I am sure it is a bit more difficult for people to read but I had fun writing it.

The main trouble I ran into was how all the counters changed as it ran through the numbers. The order had to be exact or else numbers that were multiple of seven would flip direction at the wrong time, wrong players would get skipped, etc. The first block of code is my final code, the next block is my first, oldest iteration of this code that had to be rearranged and added to in order to account for the edge cases, and finally is a second . Oh, and I had to do it by hand, but, I believe the answer is 1. Maybe someday I will remember to code this in real life and see what the result is, or maybe I will forget.*

PROGRAM CounterGame
	number = 1
	player = 1
	direction = 1
	skip = false
	WHILE number < 100 DO
		number = number + 1
		IF number / 11 has no remainder THEN
			player = player + 1 x direction
			skip = true
		ELSE IF skip = true THEN
			player = player + 2 x direction
			skip = false
		ELSE player = player + 1 x direction
		IF number / 7 has no remainder THEN
			IF direction = 1 THEN
				direction = -1
			ELSE direction = 1
		ELSE continue
		IF player = 11 THEN
 			player = 1
		ELSE IF player = 12 THEN
			player = 2
		ELSE IF player = -1 THEN
			player = 10
		ELSE IF player = -2 THEN
			player = 9
		ELSE continue
	ELSE
	yell player's number as loud as possible
END

I’m sure there is a cleaner way of writing the player 10 counter but I was trying to stay pretty mathematical in the code for this section. I look forward to being able to write a much cleaner version of this.

This is the first version of the above code that I wrote:

PROGRAM CounterGame
	number = 1
	player = 1
	direction = 1
	WHILE < 100 DO
		IF player = 11 THEN
 			player = 1
		ELSE IF player = -1
			player = 10
		ELSE continue
		number = number + 1
		IF number / 7 has no remainder THEN
			IF direction = 1 THEN
				direction = -1
			ELSE direction = 1
		ELSE continue
		IF number / 11 has no remainder THEN
			player = player + 2 x direction
		ELSE player = player + 1 x direction
	ELSE
END

This is another version of code that I wrote after starting over. I actually think it is cleaner, but I still struggled with the way to calculate player’s number on either end of the number line.

PROGRAM CounterGame
	number = 1
	player = 1
	direction = 1
	skip = false
	IF player = 11 THEN
		player = 1
	ELSE IF player = 12 THEN
		player = 2
	ELSE IF player = -1 THEN
		player = 10
	ELSE IF player = -2 THEN
		player = 9
	ELSE continue
	WHILE number < 100 DO
		number = number + 1
		IF skip = false THEN
			player = player + 1 x direction
		ELSE player = player + 2 x direction
		IF number / 7 has no remainder THEN
			direction = direction x -1
		ELSE continue
		IF number / 11 has no remainder THEN
			skip = true
		ELSE continue
	yell player's number as loud as possible
	DO PROGRAM GrilledCheese in celebration
END

*I had already written this when I decided to stop with the blog posts and prep work at the same time, I am so glad that the course came full circle and I got to write this as a real Ruby program.

2 thoughts on “Duck, Duck, Number 1!

Leave a comment