HomeTurtlegraficsGPanelRoboticsGameGrid WebTigerPython
 Python - Online
mastermind
Deutsch   English   

MASTERMIND

 

 

GAME DESCRIPTION

 

At the beginning, the computer sets a four-digit colour code selected from six colours. A colour can be used more than once. The player tries to figure out the code by placing coloured pins in rows. After each move, the player receives information about how many pencils they have placed correctly in terms of colour and position (black pencil) and how many pencils are the correct colour but in the wrong position (white pencil). The aim is to guess the colour code in as few moves as possible. If the player does not manage to guess the colour code in 7 moves, the solution is displayed.

The coloured pencils are placed in the corresponding position with a left mouse click. Clicking again changes the colour. A move is ended by clicking on the question mark. The black or white pencils are then displayed.

 

 

Program:

from gamegrid import *
import random

def showTips(whitePegs, blackPegs):
    for i in range(4):
        ep = Actor(True, "sprites/evalpeg.png", 2)
        if blackPegs > 0:
            ep.show(0)
            addActor(ep, Location(1, currentRow))
            ep.turn(90 * i)
            blackPegs -= 1
        elif whitePegs > 0:
            ep.show(1)
            addActor(ep, Location(1, currentRow))
            ep.turn(90 * i)
            whitePegs -= 1
    
def finishRound():
    removeActor(marker)
    x = 2
    for spriteNr in secretCode:
        peg = Actor("sprites/peg.png", 6)
        peg.show(spriteNr)
        addActor(peg, Location(x, 1))
        x += 1
 
def evaluateGuess(guess):
    global currentRow, placedPegs
    blackPegs = 0
    whitePegs = 0
    for i in range(4):
        if guess[i] == secretCode[i]:
            blackPegs += 1
            
    alreadyProcessed = [] 
    for color in secretCode:
        for j in range(4):
            if color == guess[j] and not j in alreadyProcessed: 
               alreadyProcessed.append(j)
               whitePegs += 1
               break
    whitePegs = whitePegs - blackPegs
    showTips(whitePegs, blackPegs)

    if blackPegs == 4: 
        setStatusText("Correct!")
        finishRound()
    else:
        currentRow -= 1
    if currentRow == 1: 
        finishRound()
    marker.setLocation(Location(1, currentRow))
    placedPegs = 0
    removeActor(evaluateBtn)

def pressCallback(e):
    global placedPegs
    global evaluateBtn          
    loc = toLocationInGrid(e.getX(), e.getY())  
    if placedPegs == 4 and loc.x == 1 and loc.y == currentRow:
         guess = [0] * 4
         for i in range(4):
             guess[i] = getOneActorAt(Location(2 + i, 
                        currentRow)).getIdVisible()
         evaluateGuess(guess)
 
    if loc.y == currentRow and loc.x > 1 and loc.x < 6:
         if getOneActorAt(loc) == None:
              addActor(Actor("sprites/peg.png", 6), loc)
              placedPegs += 1
              if placedPegs == 4:
                   evaluateBtn = Actor("sprites/evalbutton.png")
                   addActor(evaluateBtn, Location(1, currentRow))
         else:   
              getOneActorAt(loc).showNextSprite()
    refresh()
    return True    
     
# ---------------- main ----------------------
makeGameGrid(7, 10, 60, None, "sprites/mastermindbg.png", False,
        mousePressed = pressCallback)
setTitle("Mastermind")
placedPegs = 0
currentRow = 8 
secretCode = [0] * 4
for i in range(4):
   secretCode[i] = random.randint(0, 5)
marker = Actor("sprites/rowmarker.png")
addActor(marker, Location(1, currentRow))
show()
doRun()
► Copy to clipboard

 

 

Explanations of the programme code

 
1. The entire game is controlled by the main programme; no further classes are necessary.

2. def showTips(): defines how the black and white correction pens should be placed.

3.
def finishRound(): is called when a round of the game is finished. Displays the solution.

4. def evaluateGuess(): compares the colour pens that have been set with the secret code.

5. def pressCallback: defines the colour selection and the setting of the colour pens by mouse click.