Seite 1 von 1

Rekursion

Verfasst: Samstag 25. März 2006, 12:37
von zerodx
hi,
ich hab vor kurzem angefangen ein wenig mit python rumzuspielen, hab jetzt allerdings ein problem.

ich wollte ein skript schreiben das mir die im verlaufe eines tic tac toe spiels möglichen spielstellungen in html dateien schreibt. leider gibt es immer nur einen möglichen spielverlauf aus.

hier der code:

Code: Alles auswählen

def html(ground = [[0, 0, 0], [0, 0, 0], [0, 0, 0]], title='unnamed'):
    content = '<html>\n\t<head>\n\t\t<title>File_ID: %s</title>\n\t\t<link rel="stylesheet" media="screen" href="style.css" />\n\t</head>\n\t<body>\n\t\t<h1>File_ID: %s</h1>\n\t\t<table>' % (title, title)
    for y in range(3):
        content += '\n\t\t\t<tr>'
        for x in range(3):
            if ground[x][y] == 0:
                content += '\n\t\t\t\t<td> </td>'
            elif ground[x][y] == 1:
                content += '\n\t\t\t\t<td class="player1">X</td>'
            else:
                content += '\n\t\t\t\t<td class="player2">O</td>'
        content += '\n\t\t\t</tr>'
    content += '\n\t\t</table>\n\t</body>\n</html>'
    filename = 'html/' + title + '.html'
    logfile = open(filename, 'w')
    logfile.write(content)
    logfile.close()

ground = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]

W = 0

def godeep(field = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]):
    global W
    for y in range(3):
        for x in range(3):
            if field[x][y] == 0:
                if W%2 == 0:
                    field[x][y] = 1
                else:
                    field[x][y] = 2
                html(field, str(W))
                W += 1
                godeep(field)
                print field

godeep(ground)

print 'Fertig!'
es wär schön wenn ihr mir helfen könntet.

~zerodx