Quick actions

cmd+k|ctrl+k

Navigation

Languages

Four in a row

Snippet info

Language

Python

Visibility

public

Author

linus.aspelin

Created

2018-10-18T12:04:30Z

Updated

2018-10-18T16:56:42Z


# usage:  

# width and height      # defines the width and height of the board (should only be set when not playing, before init is called)
# player                # 1 or 2 showing whos turn it is (player id), player 1 starts
# board                 # current board state. used as board[x][y], containing 0 if empty, or player id
# winner                # 0 if no winner, else player id
# state                 # "NotInitialized" before first init, "Playing" while playing and "Tie", "Player1Won" or "Player2Won" if the game has ended
# init()                # starts a new game
# get_available_cols()  # returns a list of all possible values of 'col', used in drop_disc(col)
# drop_disc(col)        # this drops a disc for current player in column 'col' (index 0)


# functions used "behind the scenes"

# set_winner()          # checks if the grid contains a winner and, if that is the case, sets the "winner" variable to that player
# resolve_state()       # sets state to a winning state if state is "Playing" and if the game should be considered over

width = 7
height = 6
player = 1
board = []
winner = 0
state = "NotInitialized"

def init():
    global state
    global player
    global winner
    global board
    player = 1
    winner = 0
    state = "Playing"
    board = [[0] * height for _ in range(width)]
    
def get_available_cols():
    if state == "Playing":
        return [x for x in range(len(board)) if sum(1 for v in board[x] if v > 0) < height]
    else:
        return []
    
def drop_disc(col):
    global player
    if state != "Playing":
        return False
    col_height = sum([1 for x in board[col] if x > 0])
    if col >= 0 and col < width and col_height < height:
        board[col][col_height] = player
        player ^= 3
    
    resolve_state()
    return True

# behind the scenes

def set_winner():
    global winner
    deltas = [1,2,3]
    for x in range(width):
        right = x < width - 3
        for y in range(height):
            upper = y < height - 3
            lower = y > 2
            if board[x][y] > 0 and \
                ((upper and 3 == sum(1 for d in deltas if board[x][y] == board[x][y + d])) or \
                (right and 3 == sum(1 for d in deltas if board[x][y] == board[x + d][y])) or \
                (right and upper and 3 == sum(1 for d in deltas if board[x][y] == board[x + d][y + d])) or \
                (right and lower and 3 == sum(1 for d in deltas if board[x][y] == board[x + d][y - d]))):
                    winner = board[x][y]

def resolve_state():
    global state
    if state == "Playing":
        set_winner()
        if winner > 0:
            state = "Player" + str(winner) + "Won"
        elif width == sum(1 for x in board if sum(1 for y in x if y > 0) == height):
            state = "Tie"
           
# end of file

INFO