from random import randint class game(object): tiles = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] def __init__(self): self.newtile() self.turn() def drawboard(self): string = "" x = 0 y = 0 while y < 4: while x < 4: string += str(self.tiles[y*4 + x]) x = x+1 x = 0 y = y+1 string += "\n" print string def newtile(self): empty = 0 for n in self.tiles: if n == 0: empty = empty+1 if empty == 0: print "GAME OVER!" else: pos = randint(1, empty) i = 0 n = 0 while n < 16: if self.tiles[n] == 0: i = i+1 if i == pos: if randint(1, 10) == 1: self.tiles[n] = 2 else: self.tiles[n] = 1 def turn(self): self.newtile() self.drawboard() game()
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question