# Jonnect Jour

After having programmed Jic-Jac-Joe – where you need to connect three pieces –, I now programmed Connect Four (four pieces).
This version – although it does not have a computer player – includes win detection.

#### Controls

• Left clicking puts your piece where the mouse cursor is positioned
• Escape restarts the game

``````# Python 2.7.7 Code
# Pygame 1.9.1 (for Python 2.7.7)
# Jonathan Frech 13th of August, 2015
#         edited 14th of August, 2015``````

``````# importing needed modules
import pygame, sys, time, math, os, random

""" CLASSES """
# dummy class for global variables
class dummy():
pass

""" FUNCTIONS """
# validates color integer
# extra feature: _min and _max implementation
def colorValid(_color, _min = 0, _max = 255):
newColor = math.fabs(_color)
n = _max - _min
if newColor > n:
if int(newColor / n) % 2 == 0:
newColor = newColor % n
else:
newColor = n - (newColor % n)

return int(newColor) + _min

# gets the position on a circle
# circle center                           : '_pos'
# angle from center to point on the circle: '_angle'
return [
]

# returns an integer version of given positon
def intpos(_pos):
return [int(_pos[0]), int(_pos[1])]

# basic vector functions
def vecConvert(p1, p2):
return [p2[0] - p1[0], p2[1] - p1[1]]
def vecLen(vec):
return math.sqrt( (vec[0]**2) + (vec[1]**2) )
def vecMultiply(vec, n):
return [vec[0] * n, vec[1] * n]
def vecGetPoint(vec, point):
return [point[0] + vec[0], point[1] + vec[1]]
return [vec1[0] + vec2[0], vec1[1] + vec2[1]]

# calculates distance between given positions
def posDistance(p1, p2):
return math.sqrt( (p2[0] - p1[0])**2 + (p2[1] - p1[1])**2 )

# quits the program
def quit():
sys.exit()

""" TICK; RENDER """
# tick function
def tick():
# handle events
for event in pygame.event.get():
# quit
if event.type == pygame.QUIT:
quit()

# keyup
if event.type == pygame.KEYUP:
# handle 'main.KEYSDOWN'
if event.key in main.KEYSDOWN:
main.KEYSDOWN.remove(event.key)

# keydown
if event.type == pygame.KEYDOWN:
# handle 'main.KEYSDOWN'
if event.key not in main.KEYSDOWN:
main.KEYSDOWN.append(event.key)
if event.key == pygame.K_ESCAPE:
reset()

if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
if main.WINNER == None:
p = pygame.mouse.get_pos()
p = [p[0]/main.FIELDSCALE, p[1]/main.FIELDSCALE]
for y in range(0, len(main.FIELD)):
if main.FIELD[len(main.FIELD) - y - 1][p[0]] == None:
main.FIELD[len(main.FIELD) - y - 1][p[0]] = main.TURN
main.TURN = not main.TURN
break

# win detection
for y in range(0, len(main.FIELD)):
for x in range(0, len(main.FIELD[y])):
# check up
if y >= 3:
if main.FIELD[y][x] in [0, 1] and main.FIELD[y][x] == main.FIELD[y-1][x] and main.FIELD[y-1][x] == main.FIELD[y-2][x] and main.FIELD[y-2][x] == main.FIELD[y-3][x]:
main.WINPOINTS = [[x, y], [x, y-3]]
main.WINNER = main.FIELD[y][x]
break

# check left
if x >= 3:
if main.FIELD[y][x] in [0, 1] and main.FIELD[y][x] == main.FIELD[y][x-1] and main.FIELD[y][x-1] == main.FIELD[y][x-2] and main.FIELD[y][x-2] == main.FIELD[y][x-3]:
main.WINPOINTS = [[x, y], [x-3, y]]
main.WINNER = main.FIELD[y][x]
break

# check up left
if y >= 3 and x >= 3:
if main.FIELD[y][x] in [0, 1] and main.FIELD[y][x] == main.FIELD[y-1][x-1] and main.FIELD[y-1][x-1] == main.FIELD[y-2][x-2] and main.FIELD[y-2][x-2] == main.FIELD[y-3][x-3]:
main.WINPOINTS = [[x, y], [x-3, y-3]]
main.WINNER = main.FIELD[y][x]
break

# check up right
if y >= 3 and x <= main.FIELDWIDTH - 3-1:
if main.FIELD[y][x] in [0, 1] and main.FIELD[y][x] == main.FIELD[y-1][x+1] and main.FIELD[y-1][x+1] == main.FIELD[y-2][x+2] and main.FIELD[y-2][x+2] == main.FIELD[y-3][x+3]:
main.WINPOINTS = [[x, y], [x+3, y-3]]
main.WINNER = main.FIELD[y][x]
break

main.DRAW = True
for x in range(0, len(main.FIELD[0])):
if main.FIELD[0][x] == None:
main.DRAW = False
break

# render function
def render():
# fill
main.SURF.fill(main.COLOR)

for x in range(0, main.FIELDWIDTH):
for y in range(0, main.FIELDHEIGHT):
if main.FIELD[y][x] == None:
pygame.draw.rect(main.SURF, [255, 255, 255], [x * main.FIELDSCALE, y * main.FIELDSCALE, main.FIELDSCALE, main.FIELDSCALE])
elif main.FIELD[y][x] == 0:
pygame.draw.rect(main.SURF, [255, 255, 0], [x * main.FIELDSCALE, y * main.FIELDSCALE, main.FIELDSCALE, main.FIELDSCALE])
elif main.FIELD[y][x] == 1:
pygame.draw.rect(main.SURF, [255, 0, 0], [x * main.FIELDSCALE, y * main.FIELDSCALE, main.FIELDSCALE, main.FIELDSCALE])
elif main.FIELD[y][x] == 2:
pygame.draw.rect(main.SURF, [0, 0, 0], [x * main.FIELDSCALE, y * main.FIELDSCALE, main.FIELDSCALE, main.FIELDSCALE])

for x in range(1, main.FIELDWIDTH):
pygame.draw.line(main.SURF, [50, 50, 50], [x * main.FIELDSCALE, 0], [x * main.FIELDSCALE, main.HEIGHT])
for y in range(1, main.FIELDHEIGHT):
pygame.draw.line(main.SURF, [50, 50, 50], [0, y * main.FIELDSCALE], [main.WIDTH, y * main.FIELDSCALE])

if main.WINPOINTS:
pygame.draw.line(main.SURF, [0, 0, 0], [main.WINPOINTS[0][0] * main.FIELDSCALE + main.FIELDSCALE / 2, main.WINPOINTS[0][1] * main.FIELDSCALE + main.FIELDSCALE / 2], [main.WINPOINTS[1][0] * main.FIELDSCALE + main.FIELDSCALE / 2, main.WINPOINTS[1][1] * main.FIELDSCALE + main.FIELDSCALE / 2], main.FIELDSCALE / 3)
pygame.draw.circle(main.SURF, [0, 0, 0], [main.WINPOINTS[0][0] * main.FIELDSCALE + main.FIELDSCALE / 2, main.WINPOINTS[0][1] * main.FIELDSCALE + main.FIELDSCALE / 2], main.FIELDSCALE / 6)
pygame.draw.circle(main.SURF, [0, 0, 0], [main.WINPOINTS[1][0] * main.FIELDSCALE + main.FIELDSCALE / 2, main.WINPOINTS[1][1] * main.FIELDSCALE + main.FIELDSCALE / 2], main.FIELDSCALE / 6)

p = pygame.mouse.get_pos()
p = [p[0]/main.FIELDSCALE*main.FIELDSCALE, p[1]/main.FIELDSCALE*main.FIELDSCALE]

n = main.FIELDSCALE/3
if main.TURN == 0:
pygame.draw.rect(main.SURF, [255, 255, 0], [p[0]+n, p[1]+n, n, n])
elif main.TURN == 1:
pygame.draw.rect(main.SURF, [255, 0, 0], [p[0]+n, p[1]+n, n, n])

# blit and flip
main.SCREEN.blit(main.SURF, [0, 0])
pygame.display.flip()

if main.WINNER != None:
pygame.display.set_caption(main.CAPTION + " (" + ["yellow", "red"][main.WINNER] + " won)")
if main.DRAW:
pygame.display.set_caption(main.CAPTION + " draw...")

""" RESET; INIT """
# reset game
def reset():
main.FIELD = []
for _ in range(0, main.FIELDHEIGHT):
f = []
for __ in range(0, main.FIELDWIDTH):
f.append(None)
main.FIELD.append(f)

main.TURN = 0
main.WINPOINTS = None
main.WINNER = None
main.DRAW = False

# initialize program
def init():
main.FIELDWIDTH, main.FIELDHEIGHT = 7, 6
main.FIELDSCALE = 100
reset()

main.WIDTH, main.HEIGHT = main.FIELDWIDTH * main.FIELDSCALE, main.FIELDHEIGHT * main.FIELDSCALE
main.SIZE = [main.WIDTH, main.HEIGHT]
main.SCREEN = pygame.display.set_mode(main.SIZE)
main.SURF = pygame.Surface(main.SIZE)

main.CAPTION = "Jonnect Jour"
main.COLOR = [0, 0, 0]
main.TICKS = 0
main.KEYSDOWN = []

# functions
pygame.display.set_caption(main.CAPTION)

""" RUN """
# run function (uses tick() and render())
def run():
ticksPerSecond = 60
lastTime = time.time() * 1000000000
nsPerTick =  1000000000.0 / float(ticksPerSecond)

ticks = 0
frames = 0

lastTimer = time.time() * 1000
delta = 0.0

while True:
now = time.time() * 1000000000
delta += float(now - lastTime) / float(nsPerTick)
lastTime = now
shouldRender = False

while delta >= 1:
ticks += 1
main.TICKS += 1
tick()
delta -= 1
shouldRender = True

if shouldRender:
frames += 1
render()

if time.time() * 1000 - lastTimer >= 1000:
lastTimer += 1000

# debug
# print("Frames: " + str(frames) + ", ticks: " + str(ticks))

frames = 0
ticks = 0

# main variable
main = dummy()
init()

# start program
run()
``````