\n",
"\n",
" \n",
" "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Below is a Python program that takes YOUR move sequence, and applies it to the starting position."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"def play_game(starting_position, move_sequence):\n",
" \n",
" Rlist=[]\n",
" Glist=[]\n",
" Empty=0\n",
" totalmoves=0\n",
" counter=0\n",
" flag=0\n",
"\n",
" for i in range(0,len(starting_position)):\n",
" if starting_position[i]=='R':\n",
" counter+=1\n",
" Rlist.append(counter)\n",
" if starting_position[i]=='G':\n",
" counter+=1\n",
" Glist.append(counter)\n",
" if starting_position[i]=='_':\n",
" counter+=1\n",
" Empty=counter\n",
"\n",
" startRlist=list.copy(Rlist)\n",
" startGlist=list.copy(Glist)\n",
" startEmpty=Empty\n",
"\n",
" for i in range(0,len(move_sequence)):\n",
" if move_sequence[i]=='R':\n",
" totalmoves+=1\n",
" print(\"\")\n",
" if Empty-1 in Rlist:\n",
" Rlist.remove(Empty-1)\n",
" Rlist.append(Empty)\n",
" Empty=Empty-1\n",
" elif Empty-2 in Rlist and Empty-1 in Glist:\n",
" Rlist.remove(Empty-2)\n",
" Rlist.append(Empty)\n",
" Empty=Empty-2\n",
" else:\n",
" print(\"\")\n",
" print(\"ERROR: sorry you made a mistake after\", totalmoves, \"total moves\")\n",
" flag=1\n",
" break\n",
" if move_sequence[i]=='G': \n",
" totalmoves+=1\n",
" print(\"\")\n",
" if Empty+1 in Glist:\n",
" Glist.remove(Empty+1)\n",
" Glist.append(Empty)\n",
" Empty=Empty+1\n",
" elif Empty+2 in Glist and Empty+1 in Rlist:\n",
" Glist.remove(Empty+2)\n",
" Glist.append(Empty)\n",
" Empty=Empty+2\n",
" else:\n",
" print(\"\")\n",
" print(\"ERROR: sorry you made a mistake after\", totalmoves, \"total moves\")\n",
" flag=1\n",
" break\n",
"\n",
" if move_sequence[i]=='R' or move_sequence[i]=='G':\n",
" for j in range(0,counter+1):\n",
" if j in Rlist: print(\"R \", end = \" \") \n",
" elif j in Glist: print(\"G \", end = \" \")\n",
" elif j == Empty: print(\"_ \", end = \" \") \n",
" else: print(\"\", end = \" \")\n",
" print(\"\")\n",
" \n",
" print(\"\")\n",
" \n",
" startRlist.sort()\n",
" startGlist.sort()\n",
" Rlist.sort()\n",
" Glist.sort()\n",
" \n",
" if flag==0:\n",
" if startRlist==Glist and startGlist==Rlist and startEmpty==Empty:\n",
" print(\"Congratulations! You have solved the problem in\", totalmoves, \"total moves\")\n",
" else:\n",
" print(\"You have currently made\", totalmoves, \"total moves. Keep going!\")\n",
" \n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Input your STARTING POSITION below, with team names R and G\n",
"### For example, with four people on each team, write down RRRR _ GGGG"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"starting_position = 'RR_GG'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Now input your MOVE SEQUENCE, to try to solve the game\n",
"### For example, with two people on each team, one solution is G RR GG RR G"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"move_sequence = 'G RR GG RR G'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Click the Run button to check your solution!"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
" R R G _ G \n",
"\n",
" R _ G R G \n",
"\n",
" _ R G R G \n",
"\n",
" G R _ R G \n",
"\n",
" G R G R _ \n",
"\n",
" G R G _ R \n",
"\n",
" G _ G R R \n",
"\n",
" G G _ R R \n",
"\n",
"Congratulations! You have solved the problem in 8 total moves\n"
]
}
],
"source": [
"play_game(starting_position, move_sequence)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"![alt text](https://github.com/callysto/callysto-sample-notebooks/blob/master/notebooks/images/Callysto_Notebook-Banners_Bottom_06.06.18.jpg?raw=true)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.8"
}
},
"nbformat": 4,
"nbformat_minor": 2
}