![]() |
TigerJython | xx für Gymnasien |
Auf einem 8×8-Brett legen zwei Spieler abwechslungsweise gelbe und rote Spielsteine. Zu Beginn des Spiels sind je zwei gelbe und rote Steine in der Mitte des Spielbretts gelegt. Die Spielsteine können per Mausklick in leere Zellen gelegt werden, bei welchen mindestens eine der benachbarten Zellen oben, unten, links oder rechts bereits belegt ist.
|
|
Wird ein Stein gelegt, so werden alle gegnerischen Steine, die sich in Reihen oder Diagonalen zwischen dem neuen und bereits gelegten Steinen der eigenen Farbe befinden, umgedreht. Das Ziel des Spiels ist es, am Ende möglichst viel eigene Steine auf dem Brett zu haben. Das Spiel ist fertig, wenn alle Zellen belegt sind.
# Reversi.py from gamegrid import * # Checks endOfGame def endOfGame(): countYellow = 0 countRed = 0 all = getOccupiedLocations() for lc in all: if getOneActorAt(lc).getIdVisible() == 0: countYellow += 1 else: countRed += 1 if len(all) == 64: if countRed > countYellow: setTitle("Red wins - " + str(countRed) + ":" + str(countYellow)) elif countRed < countYellow: setTitle("Yellow wins - " + str(countYellow) + ":" + str(countRed)) else: setTitle("The game ended in a tie") #Checks if cell has a neighbour in the north, east, south or west def hasNeighbours(loc): locs = toList(loc.getNeighbourLocations(0.5)) for i in range(4): if getOneActorAt(locs[i]) != None: return True return False # set stone def onMousePressed(e): global imageID location = toLocationInGrid(e.getX(), e.getY()) if getOneActorAt(location) == None and hasNeighbours(location): stone = Actor("sprites/token.png", 2) addActor(stone, location) stone.show(imageID) # Check for stones in all 8 directions and if they can be turned add list for c in range (0, 360, 45): actors = [] loc = location.getNeighbourLocation(c) a = getOneActorAt(loc) hasSameImageID = False while a != None and not hasSameImageID: if a.getIdVisible() != imageID: actors.append(a) loc = loc.getNeighbourLocation(c) a = getOneActorAt(loc) else: if a.getIdVisible() == imageID: hasSameImageID = True # Turn stones if hasSameImageID: for actor in actors: actor.show(imageID) refresh() # Change player if imageID == 0: imageID = 1 setTitle("Red plays") else: imageID = 0 setTitle("Yellow plays") endOfGame() refresh() return True makeGameGrid(8, 8, 60, Color.gray, False, mousePressed = onMousePressed) setTitle("Reversi") yellow = Actor("sprites/token.png", 2) yellow2 = Actor("sprites/token.png", 2) red = Actor("sprites/token.png", 2) red2 = Actor("sprites/token.png", 2) addActor(yellow, Location(3, 3)) addActor(yellow2, Location(4, 4)) addActor(red, Location(4, 3)) addActor(red2, Location(3, 4)) yellow.show(0) yellow2.show(0) red.show(1) red2.show(1) imageID = 0 show() |
Erklärungen zum Programmcode:
![]() |
stone = Actor("sprites/token.png", 2) addActor(stone, location) : An der Position des Mausklicks wird ein Actor stone erzeugt |
![]() |
locs = toList(loc.getNeighbourLocations(0.5)): Liste der 4 direkt benachbarten Zellen |
![]() |
for c in range (0, 360, 45): 8 Richtungen 0, 45, 90, 135,...315 |
![]() |
loc = location.getNeighbourLocation(c): liefert benachbarte Zellen, die in der Richtung c liegen |
![]() |
all = getOccupiedLocations(): Liste aller besetzen Zellen |
![]() |
len(all) Anzahl der Elemente in der Liste all |