\n",
"\n",
" \n",
" "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Below is the code that takes YOUR move sequence, and applies it to the starting position\n",
"### Feel free to ignore this :-)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"def play_game(starting_position, move_sequence):\n",
" \n",
" Alist=[]\n",
" Blist=[]\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]=='A':\n",
" counter+=1\n",
" Alist.append(counter)\n",
" if starting_position[i]=='B':\n",
" counter+=1\n",
" Blist.append(counter)\n",
" if starting_position[i]=='_':\n",
" counter+=1\n",
" Empty=counter\n",
"\n",
" startAlist=list.copy(Alist)\n",
" startBlist=list.copy(Blist)\n",
" startEmpty=Empty\n",
"\n",
" for i in range(0,len(move_sequence)):\n",
" if move_sequence[i]=='A':\n",
" totalmoves+=1\n",
" print(\"\")\n",
" if Empty-1 in Alist:\n",
" Alist.remove(Empty-1)\n",
" Alist.append(Empty)\n",
" Empty=Empty-1\n",
" elif Empty-2 in Alist and Empty-1 in Blist:\n",
" Alist.remove(Empty-2)\n",
" Alist.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]=='B': \n",
" totalmoves+=1\n",
" print(\"\")\n",
" if Empty+1 in Blist:\n",
" Blist.remove(Empty+1)\n",
" Blist.append(Empty)\n",
" Empty=Empty+1\n",
" elif Empty+2 in Blist and Empty+1 in Alist:\n",
" Blist.remove(Empty+2)\n",
" Blist.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]=='A' or move_sequence[i]=='B':\n",
" for j in range(0,counter+1):\n",
" if j in Alist: print(\"A \", end = \" \") \n",
" elif j in Blist: print(\"B \", end = \" \")\n",
" elif j == Empty: print(\"_ \", end = \" \") \n",
" else: print(\"\", end = \" \")\n",
" print(\"\")\n",
" \n",
" print(\"\")\n",
" \n",
" startAlist.sort()\n",
" startBlist.sort()\n",
" Alist.sort()\n",
" Blist.sort()\n",
" \n",
" if flag==0:\n",
" if startAlist==Blist and startBlist==Alist 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 A and B\n",
"### For example, with four people on each team, write down AAAA _ BBBB"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"starting_position = 'AA_BB'"
]
},
{
"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 A BB AA BB A"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"move_sequence = 'A BB AA BB A '"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
" A _ A B B \n",
"\n",
" A B A _ B \n",
"\n",
" A B A B _ \n",
"\n",
" A B _ B A \n",
"\n",
" _ B A B A \n",
"\n",
" B _ A B A \n",
"\n",
" B B A _ A \n",
"\n",
" B B _ A A \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
}