| HomeTortue graphiqueGPanelRobotique WebTigerPython |
| Python - Online |
![]()
DESCRIPTION DU JEU |
Le jeu de Nim est l'un des jeux les plus connus de la théorie des jeux, car il est relativement facile de déterminer une stratégie gagnante. Dans notre exemple, le joueur affronte l'ordinateur, qui applique une stratégie gagnante.
Au début du jeu, 15 allumettes sont disposées sur le plateau. Le joueur retire 1, 2 ou 3 allumettes en cliquant dessus avec la souris. C'est ensuite au tour de l'ordinateur. Programme: # NimGame.py from gamegrid import * import random class Match(Actor): def __init__(self): Actor.__init__(self, True, "sprites/match.gif") def pressCallback(e): global nbMatches global nbTakenMatches loc = toLocationInGrid(e.getX(), e.getY()) if loc.x > 46 and loc.x < 50 and loc.y > 2 and loc.y < 6: if nbTakenMatches == 0: setTitle("You have to remove at least 1 match!") else: nbTakenMatches = 0 if nbMatches > 0: setTitle("Computer play") computerMove() else: actor = None for y in range(2, 8): actor = getOneActorAt(Location(loc.x, y)) if actor != None: break if actor != None: if nbTakenMatches == 3: setTitle("Take a maximum of 3. Click 'OK' to continue!") else: actor.removeSelf() nbMatches -= 1 nbTakenMatches += 1 setTitle(str(nbMatches) + " matches remaining. Click 'OK' to continue.") if nbMatches == 0: setTitle("You lost!") addActor(Actor("sprites/gameover.gif"),Location(15,4)) refresh() return True def nbWin(n): f = ((n - 1) // 4) * 4 + 1 return f def computerMove(): global nbMatches nbRemovedMatches = nbMatches - nbWin(nbMatches) if nbRemovedMatches == 0: nbRemovedMatches = 1 # optimal strategy impossible nbMatches = nbMatches - nbRemovedMatches for x in range(nbRemovedMatches): matches = getActors(Match) if len(matches) > 0: k = int(random.random() * len(matches)) removeActor(matches[k]) setTitle(str(nbMatches)+" matches remaining. Your move now.") if nbMatches == 0: setTitle("You win.") addActor(Actor("sprites/gameover.gif"), Location(15, 4)) refresh() makeGameGrid(52, 9, 12, False, mousePressed = pressCallback) nbMatches = 15 nbTakenMatches = 0 for i in range(15): match = Match() addActor(match, Location(2 + 3 * i , 4)) addActor(Actor("sprites/btn_ok_0.gif"), Location(48 , 4)) setTitle(str(nbMatches) + " matches. Click on 1, 2 or 3 matches to remove, then click 'OK'.") show() doRun() |
Explications du code |
|
![]()