Skip to main content

Python Game By Nishad TK


from random import randint
import re
class game:
    def __init__(self):
        self.no_of_guesses =0
        self.rabbit=0
        self.portuga=0
        self.inp=0
        self.secret_no=self.rand()
    def addRabbit(self):
        self.rabbit+=1
    def addPortuga(self):
        self.portuga+=1
    def addGuess(self):
        self.no_of_guesses+=1
    def rand(self):
        unique=[]
        while not (len(unique)>3):
            ran=[]
            for i in range(0,9):
                no=(randint(0,9))
                ran.append(no)
            for j in range(0,9):
                if ran[j] not in unique:
                    unique.append(ran[j])
            return str(unique[0])+str(unique[1])+str(unique[2])+str(unique[3])
    def checkDupe(self,inc):
        flag=0
        for i in range(0,4):
            x=inc[:i]+inc[i+1:]
            if inc[i] in x:
                flag+=1
        if flag==0:
            return False
        else:
            return True
    def play(self):
        while self.inp!=self.secret_no:
            self.rabbit=0
            self.portuga=0
            tester=re.compile(r'[0-9]+')
            self.inp=str(raw_input("Enter your Guess:"))
            while not tester.match(self.inp) or len(self.inp)!=4 or self.checkDupe(self.inp):
                self.inp=str(raw_input("The Guess Expected To Be Four Digit Number Without Repeated Digits , Please Try Again:"))
            self.addGuess()
            if self.inp==self.secret_no:
                continue
            for i in range(0,4):
                for j in range(0,4):
                    if self.inp[i]==self.secret_no[j]:
                        if i==j:
                            self.addRabbit()
                        else:
                            self.addPortuga()
            print "Rabbit:",self.rabbit
            print "Portuga:",self.portuga
        print "Congratulations , You have successfully found the Secret Number ",self.secret_no," in ",self.no_of_guesses," attempts."
gm=game()
gm.play()

Comments

Post a Comment

Popular posts from this blog

JavaScript:Magic Square

<html> <head> <title> Game:MagicalSquare. </title> </head> <style> table{ border-style: solid; border-color: white; border-width: 2px; width: 30%; height: 25%; text-align: center; padding-top: 20px; margin-top: 5px; padding-bottom: 20px; background-color:#00edff; } input[type=text]{ width:60px; height:60px; border: solid #063; border-radius:20px; font-size:16px; text-align:center; background-color:#55edff; color:white; } body{ border: solid #063; border-radius:20px; width=30%; height:70%; color:white; background-color:gray; } h2{ margin-top: 10px; border-color: white; border-width: 1px; border-style: solid; width: 30%; height: 40px; padding-top: 10px;; } div{ width:30%; height:12%; border-style:solid; border-color:white; border-width:2px; margin-top:5px; } input[type=button]:hover, input[type=button]:active { background-color: red; } input[type=button] { ...