hacker_by_streetlover-d18ueka

Python mastermind project

When I was in highschool in early 2000, i used to survive long hour boring class with playing mastermind with friends. It was simple and easy. We both wrote a 4 unique secret number in some place, and exchange paper with another 4 unique number. Try to guess each other’s code. We give score for each guesses. First score indicates how many number are correct no matter the location, and the second score indicates how much the correct number with correct location. Of course the winner is the one who reach 4-4 score. It’s quite easy to develop some guessing strategy for this game using logical step. And soon, no one find it interesting anymore.

But I still love the game. I doesnt even know that it called mastermind back then. At the same time i’m corrently into macro virus programming. Microsof Word is very famous back then and not everybody have internet, and be able to write malicious code to ultimately change Microsoft word’s behavious is very tempting for a teenager like me. I’ll probably tell the story in different post, but this is about Mastermind. When finally i’m in touch with Visual Basic 6 in University (my major is Accounting), one of the first program i wrote was Mastermind (the other was pet simulation like tamagotchi utilizing downloadble free icon as graphic).

When I’m finally got a serious job in Huawei as Finance staff, I spent first week doing nothing while waiting my access priviledge is ready for me to actually work. Laptop cannot connect to internet or install anything fun, I decide to make a mastermind game with microsoft excel (will write about this too, sometimes.)

Three years later I’m deciding to re-learn again programming language, I choose python over ruby for it’s broader application. After learning basic data types and flow I challage myself to write script to generate Rubik’s cube scramble. Not entirely easy, as my first program using python, I ended up writing over 600 lines full of ‘if’ and ‘elif’ just to senerate 1 line of scramble. I know I have to revisit that project sometimes in the future to make it as eficient as possible. For now, efectiveness is enough. Back to python mastermind project.

The Goal:
The goal is to make python script that can be run in terminal (or any python intepreter) which contain Mastermind game. The script generate 4 unique number (non repetitive) as secret code to be guessed by player. Player then input 4 unique code and the program must be able to detect any error in input such as: player input more or less than 4 number, player input any charracter other than number, player input repetitive number. If the input is mistake free, program will generate score as mentioned above. First score indicate how much the number is correct dismissing the location, and the second score indicates how much number is correct with correct location. Program tell the score and if it’s not 4-4 it will allow user to input another guess up to 10 times. The game is over when the player guess the right code or used 10 times attempt.


import random
import re
import collections

secret = random.sample([1,2,3,4,5,6,7,8,9,0],4)
secret=map(int,secret)

print " _____ __ .__ .___ "
print " / \ _____ _______/ |_ ___________ _____ |__| ____ __| _/ "
print " / \ / \\__ \ / ___/\ __\/ __ \_ __ \/ \| |/ \ / __ | "
print "/ Y \/ __ \_\___ \ | | \ ___/| | \/ Y Y \ | | \/ /_/ | "
print "\____|__ (____ /____ > |__| \___ >__| |__|_| /__|___| /\____ | "
print " \/ \/ \/ \/ \/ \/ \/ "
print " coded by VirKill "
print "========================================================================"
print " Welcome to mastermind. This is simple game written in python script. We"
print " have a secret code you need to guess in 10 tries only. The code is "
print " consist of 4 unique number. We give hints by showing you two scores. "
print " First scores indicates how much your number is correct (max is 4, of "
print " course), and the second score indicates how much of the correct number "
print " have correct position also. Therefore, you will WIN if your score is "
print " 4-4. Means you have 4 number correct and 4 position correct. "
print " Are you ready?? "
print "========================================================================"
print " "

cekInput=True
cekHasil=True
jumlahcoba=0
while cekInput and cekHasil:
while cekInput:
try:
i = raw_input()
if len(i) > 4 or len(i) < 4 or i[0] == i[1] or i[0] == i[2] or i[0] == i[3] or i[1] == i[2] or i[1] == i[3] or i[2] == i[3] or i[2] == i[3]:
raise ValueError()
i = [int(d) for d in str(i)]
cekInput = False
jumlahcoba = jumlahcoba + 1
except ValueError:
print "\n"
print "Wrong input! Make sure it's a 4 digit non duplicate number. Please try Again."
while cekHasil:
nilaiA=0
nilaiB=0
if secret[0]==i[0] or secret[0]==i[1] or secret[0]==i[2] or secret[0]==i[3]:
nilaiA=nilaiA + 1
if secret[1]==i[0] or secret[1]==i[1] or secret[1]==i[2] or secret[1]==i[3]:
nilaiA=nilaiA + 1
if secret[2]==i[0] or secret[2]==i[1] or secret[2]==i[2] or secret[2]==i[3]:
nilaiA=nilaiA + 1
if secret[3]==i[0] or secret[3]==i[1] or secret[3]==i[2] or secret[3]==i[3]:
nilaiA=nilaiA + 1
if secret[0]==i[0]:
nilaiB=nilaiB + 1
if secret[1]==i[1]:
nilaiB=nilaiB + 1
if secret[2]==i[2]:
nilaiB=nilaiB + 1
if secret[3]==i[3]:
nilaiB=nilaiB + 1
if nilaiA == 4 and nilaiB == 4:
cekHasil=False #Game over!!
print "Out of 4 number guess, you have ", nilaiA, "numbers right. Congratsss!"
break
else:
print i," = ", nilaiA, " - ", nilaiB
cekInput=True
if jumlahcoba == 10:
cekHasil=False
cekInput=False
break
break
if jumlahcoba == 10:
print " "
print "======================================================================="
print " YOU FAILED! "
print "The correct answer is: ", secret
print "You have been guessing in ", jumlahcoba, " tries."
print "======================================================================="
print " ___ __ _ _ ____ __ _ _ ____ ____ "
print " / __) / _\ ( \/ )( __) / \ / )( \( __)( _ \ "
print "( (_ \/ \/ \/ \ ) _) ( O )\ \/ / ) _) ) / "
print " \___/\_/\_/\_)(_/(____) \__/ \__/ (____)(__\_) "
else:
print " "
print " "
print "======================================================================="
print "You are CORRECT!"
print "The right answer is: ", secret
print "You succeed in ", jumlahcoba, " tries. "
print "Congratulation!"
print " _ _ __ _ _ _ _ __ __ _ _ "
print "( \/ )/ \ / )( \ / )( \( )( ( \ / \ "
print " ) /( O )) \/ ( \ /\ / )( / / \_/ "
print "(__/ \__/ \____/ (_/\_)(__)\_)__) (_) "
print "======================================================================="

If you’re not familiar with python and want to test the scipt above, copy the entire text and save with name “mastermind.py”. Open terminal (or supershell in windows) and type ‘python mastermind.py’ (make sure you type in the right directory.

Of course if you know python, many of the code above is highly inneficient. Later on I google ‘mastermind python’ and found out this useful discussion in stack overflow.

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s