Mine værktøjer
Du er her: Forside diverse filer til download mm. mastermind.py
Handlinger tilknyttet webside

mastermind.py

af adminSidst opdateret 07/06 2007 11:06

opgave fra www.experten.dk/spm/781354

Klik her for at hente fil

Størrelse: 3.7 kB - Filtype text/python-source

Indhold af fil-indholdsobjekt

#!/usr/bin/env python2.4
# -*- coding: UTF-8 -*-

import cherrypy

class mastermind:

    def solution( self ):
        import random

        z = [ str( x ) for x in range( 1, 10 ) ]
        Solution  = ""

        for y in range( 4 ):
            x =  random.choice( z )
            z.remove( x )
            Solution += x

        self.losning = Solution
        print Solution

    def get_history( self ):
        ret = []
        history = [ (x[ 0 ], x[ 1 ]) for x in zip( self.answers, self.status ) ]
        try:
            history.reverse()
        except:
            pass
        for (a,s) in history:
            if s.count( "F" ) > 0 :
                status = "kun 4 forskellige tal er tilladt."
            else:
                status = '<img src="static/bk2.gif">' * s.count( "A" ) + '<img src="static/bk1.gif">' * s.count( "B" )
            strg="<tr><td>%(a)s %(status)s</td></tr>" % vars()
            ret.append( strg )
        return "\n".join(ret)

    def check(self, answer ):
        checked = ""
        try:
            int(answer)
        except:
            checked += "F" # fejl der må kun være en forekomst af et tal.
        for i in range( 4 ):
            if answer[ i ] == self.losning[ i ]:
                checked += "A"
            elif answer[ i ] in self.losning:
                checked += "B"
            else:
                checked += "X"
            if answer.count( answer[ i ] )> 1:
                checked += "F" # fejl der må kun være en forekomst af et tal.
        self.answers.append( answer )
        self.status.append( checked )
        print "answer, self.losning, checked = ", answer, self.losning, checked

    def index(self):
        # make new mastermind
        self.answers = []
        self.status  = []
        return self.initiel_mastermind()
    index.exposed = True

    def play( self, answer="" ):

        comment = ""
        action="play"
        try:
            self.check( answer )
            answer  = self.answers[ -1 ]
            status  = self.status[ -1 ]
        except:
            answer  = ""
            status  = ""
        history = self.get_history()
        if status == "A" * 4:
            comment = """<b>Godt klaret</b>"""
            self.answers = []
            self.status  = []
            self.solution()

        ret = """
            <html>
             <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
             <head>
            <b>
            <p><a target="_blank" href="http://www.databassen.dk:8090/bauerdata/python-program-eksempler/mastermind.py">Se kildeteksten<a></p>
            <p>Dette er en version af mastermind. Test dine logiske evner !<br />

Du skal gætte et 4 cifret tal. Alle tal er forskellige. (0 til 9)<br />
Gæt dit tal i så få forsøg som muligt.<br />
Du kan kun indtaste 4 tal som alle er forskellige.<br />
            </p>
            <p><img src="static/bk2.gif">viser at tallet er placeret rigtigt</p>
            <p><img src="static/bk1.gif">viser at tallet er rigtigt men placeret forkert</p>
            </b>
             <style>
                input { display: inline; }
             </style>
             </head>
             <table><form action=%(action)s method="POST">
              <tr>
              <td>
               <input type="text" name="answer" size=4 maxlength=4>
               <input type=submit value="Check">
               %(comment)s
              </td>
              </tr>
              %(history)s
             </table>
            </html>
            """ % vars()
        return ret

    play.exposed = True

    def initiel_mastermind( self ):
        self.solution()
        return self.play()

cherrypy.root = mastermind()

if __name__ == '__main__':
    cherrypy.config.update(file = 'mastermind.conf')
    cherrypy.server.start()
« august 2018 »
søn man tir ons tor fre lør
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Chat Rooms
Dyrk motion

Trim ballerup

 

Powered by Plone, the Open Source Content Management System