loading

Hello all,

The Chinese Rings Puzzle with Arduino is my version of a centennial Chinese puzzle.

It is very simple to play and it is an example of a combinatorial puzzle and lots of patience and concentration is required to solve it.

The objective is to disentangle the long loop from all nine rings and the solution takes 341 moves (minimum possible).

Once you learn the method to solve the puzzle, you will see that is very easy to play it!

This project is very simple to be assembled, using only an Arduino UNO R3 and a LCD Keypad Shield.

It took me some time, but the code I developed reproduced exactly the same conditions and constraints as the original mechanical puzzle.

Let's do it and have a fun for a long time!!

LAGSILVA

Step 1: The Puzzle Story

Stewart Culin in his book "Games of the Orient" records a story that the Chinese Rings puzzle was invented by the famous Chinese hero Mung Ming (A.D. 181 - 234).

He apparently gave it to his wife when he went to war so she would have something to keep her busy in his absence. The story relates that she forgot her sorrow while trying to solve the puzzle.

This puzzle or the Cardano's Rings was introduced in Europe by Girolamo Cardano in 1550 and it is perhaps the greatest mechanical puzzle of China.

The original puzzle consists of a long loop with a handle on one end that is interlocked with nine rings.

  • A little of Math

The total of movements for a number of rings (n) is determined by the following equations:

1) When n is odd number:

T (n) = (2 ^ (n+1) − 1 ) / 3

2) When n is even number:

T (n) = (2 ^ (n+1) − 2 ) / 3

n	T(n)

1	1
2	2
3	5
4	10
5	21
6	42
7	85
8	170
9	341
10	682
11	1365
12	2730
13	5461
14	10922
15	21845
16	43690
17	87381
18	174762
19	349525
20	699050

Step 2: Material List

  • Arduino UNO R3
  • LCD Keypad Shield

Step 3: The Code

The code is attached to this project.

Just upload it to your Arduino.

For this project you will need the Liquid Crystal library.

// CHINESE RINGS PUZZLE - V1.2
// Copyright by LAGSILVA // lagsilva@gmail.com // 29.Mai.2017 // // 09 Rings / 341 Moves

#include LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

byte anelOff[8] = { B00000, B00000, B00000, B11111, B00000, B00000, B00000, B00000, };

byte anelOn[8] = { B00100, B01010, B10001, B11111, B10001, B01010, B00100, B00000, };

byte ponteiro[8] = { B00100, B01110, B11111, B00100, B00100, B00100, B00100, B00000, };

String aneis; int px, p1, movimentos,k; int botoes = 0; int estadoBotoes = 0;

#define btDir 1 #define btEsq 2 #define btCima 3 #define btBaixo 4 #define btOk 5 #define btNone 6

// Leitura dos Botoes int ler_botoes() { estadoBotoes = analogRead(0); if (estadoBotoes < 50) return btDir; if (estadoBotoes < 250) return btCima; if (estadoBotoes < 450) return btBaixo; if (estadoBotoes < 650) return btEsq; if (estadoBotoes < 850) return btOk; return btNone; // Retorna None qdo todas as opcoes falharem }

void setup() {

lcd.createChar(0, anelOff); lcd.createChar(1, anelOn); lcd.createChar(2, ponteiro);

lcd.begin(16, 2);

lcd.clear(); lcd.setCursor(1, 0); lcd.print("CHINESE RINGS"); lcd.setCursor(2, 1); lcd.print("by LAGSILVA");

delay(3000);

telaInicial();

}

void loop() {

do { botoes = ler_botoes(); delay(100); } while (botoes == btNone);

delay(100);

switch (botoes) {

case btDir: p1 = aneis.indexOf('1', 1); if (px <= p1) { if (aneis.substring(p1, p1 + 1) == "1") { px = px + 1; } } lcd.setCursor(0, 1); lcd.print(" "); lcd.setCursor(px - 1 , 1); lcd.write(byte(2));

break;

case btEsq:

px = 1; lcd.setCursor(0, 1); lcd.print(" "); lcd.setCursor(px - 1 , 1); lcd.write(byte(2));

break;

case btOk:

if (aneis.substring(px - 1, px) == "1") { if (aneis.substring(px, px + 1) == "1") { aneis.setCharAt(px, '0'); lcd.setCursor(px - 1, 0); lcd.write(byte(0)); } else { aneis.setCharAt(px, '1'); lcd.setCursor(px - 1, 0); lcd.write(byte(1)); } } movimentos = movimentos + 1; lcd.setCursor(12, 1); lcd.print(movimentos);

break;

}

if (aneis == "1000000000") { lcd.clear(); botoes = ler_botoes(); lcd.setCursor(0, 0); lcd.print("CONGRATULATIONS!"); lcd.setCursor(0, 1); lcd.print("Your Grade: "); lcd.print(34100 / movimentos);

do { botoes = ler_botoes(); delay(100); } while (botoes == btNone); delay(500); telaInicial(); }

}

void telaInicial() {

// Imprime Aneis e Estado Inicial lcd.clear(); for (k = 0; k <= 8; k++) { lcd.setCursor(k, 0); lcd.write(byte(1)); } lcd.setCursor(12, 0); lcd.print("Move");

aneis = "1111111111"; movimentos = 0;

px = 1; //Posicao do Ponteiro "^" p1 = 1; //Posicao do Primeiro "1"

lcd.setCursor(p1 - 1, 1); lcd.write(byte(2)); lcd.setCursor(12, 1); lcd.print(movimentos);

}

Step 4: Playing

At first line of LCD are shown the 09 rings and in the second line is shown the pointer.

You must use the keys LEFT & RIGHT to move the pointer and the key SELECT to remove or to insert a ring.

The LEFT key moves the pointer direct to the first ring position and the RIGHT key moves the pointer one position step by step.

The LCD also shows the total amount of moves, counting every time you press SELECT key to remove or insert a ring.

The key RESTART is used to start again the game at any moment.

At the end, after you solve the puzzle completely, you will see the "Congratulations" message and your grade.

If you complete the puzzle using the minimum moves (341) your grade will be 100.

Beyond that, your grade will be lower.

(See VIDEO)

Note: You will see there are some restrictions to move the pointer and also to remove a ring, but this is part of the rules to solve the puzzle.

About This Instructable

381views

5favorites

License:

Bio: I am a Mechanical Engineer and an enthusiastic Maker. I like to develop new ideas combining electronics and programming.
More by lagsilva:Chinese Rings Puzzle With Arduino Talking Clock with Arduino Stopwatch with Lap Time Counter 
Add instructable to: