solitaire
Deutsch   English   

SOLITAIRE GAME

 

 

GAME DESCRIPTION

 

As the name Solitaire suggests, this game is usually played by a single person. The game starts with 32 tiles arranged in a cross shape across 33 fields. The field in the centre is empty. The aim is to eliminate all but one of the tiles by jumping over them with an adjacent tile. The tiles are moved to their new position by holding down the mouse button, whereby they may only be moved vertically or horizontally, but not diagonally.

 
 
 
allowed
 
not allowed
 

The tiles are returned to their starting position if an illegal move is made.

Program:

#Solitaire.py

from gamegrid import *

def checkGameOver():
    global isGameOver
    marbles = getActors() 
    if len(marbles) == 1:
        isGameOver = True
        addActor(Actor("sprites/you_win.gif"), Location(3, 3))
    else:
        if not isMovePossible():
           isGameOver = True
           addActor(Actor("sprites/gameover.gif"), Location(3, 3))

def isMovePossible():
   for a in getActors():  # run over all remaining marbles
        for x in range(7): # run over all holes
            for y in range(7):
                loc = Location(x, y)
                if getOneActorAt(loc) == None and \
                  getRemoveMarble(a.getLocation(),Location(x,y))!= None:
                    return True
   return False
    
def getRemoveMarble(start, dest):
    if getOneActorAt(start) == None:
        return None
    if getOneActorAt(dest) != None:
        return None
    if not isMarbleLocation(dest):
        return None
    if dest.x - start.x == 2 and dest.y == start.y:
        return getOneActorAt(Location(start.x + 1, start.y))
    if start.x - dest.x == 2 and dest.y == start.y:
        return getOneActorAt(Location(start.x - 1, start.y))
    if dest.y - start.y == 2 and dest.x == start.x:
        return getOneActorAt(Location(start.x, start.y + 1))
    if start.y - dest.y == 2 and dest.x == start.x:
        return getOneActorAt(Location(start.x, start.y - 1))
    return None

def isMarbleLocation(loc):
    if loc.x < 0 or loc.x > 6 or loc.y < 0 or loc.y > 6:
        return False
    if (loc.x == 0 or loc.x == 1 or loc.x == 5 or loc.x == 6) and \
       (loc.y == 0 or loc.y == 1 or loc.y == 5 or loc.y == 6):
        return False
    return True

def initBoard():
    for x in range(7):
        for y in range(7):
            loc = Location(x, y)
            if isMarbleLocation(loc):
                marble = Actor("sprites/marble.png")
                addActor(marble, loc)
    removeActorsAt(Location(3, 3)) # Remove marble in center

def pressEvent(e):
    global startLoc, movingMarble
    if isGameOver:
        return
    startLoc = toLocationInGrid(e.getX(), e.getY())
    movingMarble = getOneActorAt(startLoc)  

def dragEvent(e):
    if isGameOver:
        return
    if movingMarble == None:
        return
    startPoint = toPoint(startLoc)
    movingMarble.setLocationOffset(e.getX() - startPoint.x, 
                                   e.getY() - startPoint.y) 

def releaseEvent(e):
    if isGameOver:
        return
    if movingMarble == None:
        return
    destLoc = toLocationInGrid(e.getX(), e.getY())
    movingMarble.setLocationOffset(0, 0)
    removeMarble = getRemoveMarble(startLoc, destLoc)
    if removeMarble != None:
        removeActor(removeMarble)
        movingMarble.setLocation(destLoc)    
        checkGameOver()

startLoc = None
movingMarble = None
isGameOver = False

makeGameGrid(7, 7, 70, None, "sprites/solitaire_board.png", False,
   mousePressed = pressEvent, mouseDragged = dragEvent, 
   mouseReleased = releaseEvent)
setBgColor(Color(255, 166, 0))
setSimulationPeriod(20)
initBoard()
show()
doRun()
► Copy to clipboard

 

 

Explanations of the programme code

 
1. checkGameOver(): The game is over when only one stone remains. In this case, the player has won. However, there are also game situations in which several game pieces remain on the board, but no further moves are possible

2. pressEvent(e) : Selects a marble
3.
dragEvent(e)): The marble can be moved by holding down the mouse button
4. releaseEvent(e): The piece is placed in its current position; it must be checked whether the move is correct
5. getRemoveMarble(start, dest): Returns the piece to be removed for a legal move and None for an illegal move.