0% found this document useful (0 votes)
11 views8 pages

Message

Download as txt, pdf, or txt
Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1/ 8

{

"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Source code Game Mind Reader"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 1. Penjelasan dan Ide Game\n",
"**Mind Reader** adalah game sejenis dengan <span style='color:blue'>batu-
gunting-kertas</span> melawan <span style='color:red'>komputer</span>. <span
style='color:green'>Anda</span> dapat memilih angka **1 atau 0**. Kemudian <span
style='color:red'>komputer</span> akan menebak pilihan <span
style='color:green'>Anda</span> menggunakan **machine learning**. Jika <span
style='color:red'>komputer</span> benar maka ia mendapat poin, kebalikannya maka
<span style='color:green'>Anda</span> mendapat poin. Yang pertama mencapai **10
poin** menang!"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 2. Import tools\n",
"import tools yang akan kita pakai untuk membuat game ini"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"from ipywidgets import *\n",
"import numpy as np"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 3. Membuat Tombol\n",
"Tombol ini digunakan untuk menentukan pilihan pengguna"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "042538964bbc4ed2958ca107783d44f2",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"HBox(children=(Button(description='0', style=ButtonStyle()),
Button(description='1', style=ButtonStyle())))"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"btn_zero = Button( description='0' )\n",
"btn_one = Button( description='1' )\n",
"btns = HBox( [btn_zero, btn_one] )\n",
"btns"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 4. Scoreboard\n",
"Kita menggunakan dua progress bar untuk menunjukkan scoreboard"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "e74ace9c1b3241a4b9922aca6094d605",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"VBox(children=(IntProgress(value=0, bar_style='success',
description='You:', max=30), IntProgress(value=0, bar…"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"usr_score = IntProgress( value=0, min=0, max=30, description='You:',
bar_style='success')\n",
"bot_score = IntProgress( value=0, min=0, max=30, description='Bot:',
bar_style='danger')\n",
"scoreboard = VBox( [usr_score, bot_score])\n",
"scoreboard"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 5. Pesan Game Over\n",
"Di akhir permainan, bergantung dari anda kalah atau menang kita akan mencetak
*\"You Win\"* atau *\"You Loose!\"*"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "489b92d14fa444f494dfa8f15afcb64e",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"HTML(value=\"<h1 style='color:green'> You Win!</h1>\")"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"final_msg = HTML(\"<h1 style='color:green'> You Win!</h1>\")\n",
"final_msg"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 6. Game Box\n",
"Sekarang kita akan menyatukan semua widgets di dalam sebuah kotak bernama
game_box"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "0fe2713029e54cc2a2b13331ae9b7e84",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"VBox(children=(HBox(children=(VBox(children=(IntProgress(value=0,
bar_style='success', description='You:', max…"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"game_box = VBox([ HBox( [scoreboard, final_msg] ), \n",
" btns \n",
" ])\n",
"game_box"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"final_msg.layout.visibility = 'hidden'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 7. Function : Update Game\n",
"Di sini kita akan membuat sebuah fungsi untuk melakukan update setelah setiap
kali kita memilih angka. Yang akan dilakukan adalah :\n",
"1. Komputer akan menebak angka yang kita pilih dari riwayat pilihan kita
sebelumnya\n",
"2. Menambahkan 1 pada score komputer jika menebak angka anda dengan tepat, dan
menambahkan 1 pada score anda jika tebakannya salah\n",
"3. Stop permainan saat salah satu sudah menang"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"def click_zero(b):\n",
" update_game(0)\n",
"btn_zero.on_click( click_zero )"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"def click_one(b):\n",
" update_game(1)\n",
"btn_one.on_click( click_one )"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"def update_game( usr_choice ):\n",
" prob = sum( usr_history ) / len( usr_history )\n",
" comp_choice = np.random.binomial(1, prob, 1)[0]\n",
" usr_history.append( usr_choice )\n",
" \n",
" if comp_choice == usr_choice:\n",
" bot_score.value += 1\n",
" else:\n",
" usr_score.value += 1\n",
" \n",
" if usr_score.value == 30 or bot_score.value == 30:\n",
" if bot_score.value == 30:\n",
" final_msg.value = \"<h1 style='color:red'> You Loose!</h1>\"\n",
" final_msg.layout.visibility = \"visible\"\n",
" btn_zero.disabled = True\n",
" btn_one.disabled = True\n",
" \n",
" return"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"usr_history = []"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"usr_history.append(1)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"usr_history.append(0)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1, 0]"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"usr_history"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sum( usr_history )"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len( usr_history )"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.5"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"prob = sum( usr_history ) / len( usr_history)\n",
"prob"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"comp_choice = np.random.binomial(1, prob)\n",
"comp_choice"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 8. Play\n",
"Agar game Mind Reader tampil di file lain"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "0fe2713029e54cc2a2b13331ae9b7e84",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"VBox(children=(HBox(children=(VBox(children=(IntProgress(value=0,
bar_style='success', description='You:', max…"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"display( game_box )"
]
},
{
"cell_type": "code",
"execution_count": None,
"metadata": {},
"outputs": [],
"source": []
}
],
"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.9"
}
},
"nbformat": 4,
"nbformat_minor": 4
}

You might also like