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] {

JavaScript: Interactive Login form Using Cookie

Interactive Login form Using Cookie Good day guys,               Today I am here to have a discussion regarding a small program for Beginners with JavaScript and CSS. Even though it's a small program. I hope it will be pretty useful to everyone. First of all, let's have a look at what we are going to make: A screenshot from the latest version of Firefox. We can see that the page has a simple but beautiful user interface.I have accomplished this using simple HTML and CSS.Now , I would like describe the HTML in a few steps so that it can make this tutorial readable and easy to understand. Step 1 We have to make some containers to wrap the HTML elements(the text inputs , buttons etc.) so that it will be easy to manage.Here I use <div> tag to make the containers here.I have used three <div> elments which the first one contains the other two where the second one contains the username TextInput , Password the Heading and the Image.The third one ho