HomeTurtlegraficsGPanelRoboticsGameGrid WebTigerPython
 Python - Online
fourInARow
Deutsch   English   

FOUR IN A AROW

 

 

GAME DESCRIPTION

 


Four In A Row is a two-player strategy game. The game board consists of seven columns (vertical) and six rows (horizontal). The players take turns placing their pieces by moving the piece in the upper channel with the mouse over the selected column and dropping it with a mouse click. If the column is not already full, the piece occupies the lowest free space in the column. The winner is the player who is the first to get four of their pieces in a row horizontally, vertically or diagonally. The game ends in a draw if the board is completely filled without either player forming a line of four.

 

 

Program:

# FourInARow.py

from gamegrid import *

#------------- class Token -------------------
class Token(Actor):
    def __init__(self, player):
        Actor.__init__(self, "sprites/token.png", 2)
        self.player = player
        self.show(player)
        self.nb = 0
        self.setActEnabled(False) # do not start act() yet
 
    def act(self):
        global activeToken, isFalling, isFinished
        nextLoc = Location(self.getX(), self.getY() + 1)
        if (getOneActorAt(nextLoc) == None and self.isMoveValid()):
            if self.nb == 6:
                self.nb = 0
                self.setLocationOffset(Point(0, 0))
                self.move()
            else:
                self.setLocationOffset(Point(0, 10 * self.nb))
            self.nb = self.nb + 1         
        else:  # arrived
            self.setActEnabled(False)  # old token will stay where it is
            if check4Win(self.getLocation()):
                if self.player == 0:
                    won = "Yellow"
                else:
                    won = "Red"    
                setTitle("Game Over, player " + won + " won!")                 
                isFinished = True
            else:
                self.player = (self.player + 1) % 2 
                activeToken = Token(self.player)
                addActor(activeToken, Location(6, 0), Location.SOUTH)
                activeToken.setActEnabled(False)
            isFalling = False          

def onMousePressed(e):
    global isFalling
    isFalling = True
    activeToken.setActEnabled(True) # start act()
    
def onMouseMoved(e):
    if isFalling:
        return
    loc = toLocationInGrid(e.getX(), e.getY())
    if not isFinished: 
        activeToken.setX(loc.x)
    refresh() 
    
def getPlayerOfTokenAt(x, y):
    loc = Location(x, y)
    if getOneActorAt(loc) == None:
        return -1
    else:
        return getOneActorAt(loc).getIdVisible()

#for checking nrOfTokens (win situation: nrOfTokens = 4)
def checkDiagonally1(col, row, nrOfTokens):
    for j in range(nrOfTokens):
        adjacentSameTokens = 0
        for i in range(nrOfTokens):
            if (col+i-j) >= 0 and (col+i-j) < 7 and (row+i-j) >= 1 \
               and (row + i - j) < 7 \
               and getPlayerOfTokenAt(col + i - j, row + i - j) == \
                getPlayerOfTokenAt(col, row):
                adjacentSameTokens = adjacentSameTokens + 1
        if adjacentSameTokens >= nrOfTokens:
            return True
    return False

def checkDiagonally2(col, row, nrOfTokens):
    for j in range(nrOfTokens):
        adjacentSameTokens = 0
        for i in range(nrOfTokens):
            if (col - i + j) >= 0 and (col - i + j) < 7 \
               and (row + i - j) >= 1 and (row + i - j) < 7 \
               and getPlayerOfTokenAt(col - i + j, row + i - j) == \
                 getPlayerOfTokenAt(col, row):
                 adjacentSameTokens = adjacentSameTokens + 1
        if adjacentSameTokens >= nrOfTokens:
            return True
    return False

def checkHorizontally(col, row, nrOfTokens):
    adjacentSameTokens = 1
    i = 1
    while col - i >= 0 and getPlayerOfTokenAt(col - i, row) ==\ 
        getPlayerOfTokenAt(col, row):
        adjacentSameTokens = adjacentSameTokens + 1
        i = i + 1
    i = 1
    while col + i < 7 and getPlayerOfTokenAt(col + i, row) ==\ 
        getPlayerOfTokenAt(col, row):
        adjacentSameTokens = adjacentSameTokens + 1
        i = i + 1
    return adjacentSameTokens >= nrOfTokens

def checkVertically(col, row, nrOfTokens):
    adjacentSameTokens = 1
    i = 1
    while row + i < 7 and getPlayerOfTokenAt(col, row + i) ==\ 
       getPlayerOfTokenAt(col, row):
       adjacentSameTokens = adjacentSameTokens + 1
       i = i + 1
    return adjacentSameTokens >= nrOfTokens

#return true, if four are connected through that token
def check4Win(loc):
    col = loc.x
    row = loc.y
    return (checkVertically(col, row, 4) 
      or checkHorizontally(col, row, 4)
      or checkDiagonally1(col, row, 4)
      or checkDiagonally2(col, row, 4))

# ---------------- main ----------------------           
makeGameGrid(7, 7, 70, None, "sprites/connectbg.png", False,
     mousePressed = onMousePressed,
     mouseMoved = onMouseMoved)
setBgColor(makeColor("white"))
isFalling = False
activeToken =  Token(0)
isFinished = False
addActor(activeToken, Location(0, 0), Location.SOUTH)
setSimulationPeriod(30)
addStatusBar(30)
setTitle("Move mouse to a column and click to set the token.")
show()
doRun()
► Copy to clipboard

 

 

Explanations of the programme code

 
1. class Token(): a game piece is modelled by the Token() class. The sprite images token_0.png and token_1.png represent the yellow and red pieces.

2.
check4Win(loc): returns True if one of the checks (horizontal, vertical and both diagonals) is positive

3. onMouseMoved(): the active game piece is moved to the position of the mouse pointer

4. onMousePressed(): the act() method of the Token class is enabled. The game piece moves to the lowest free space in the column
5. \: Long lines can be continued on the next line