#title: Interlock Sheet (python with knitout module)
# Knit a sheet with alternating rows of back/front and front/back knits; effectively two [[1x1 rib]] sheets interlocked with each-other.

import knitout
K = knitout.Writer('1 2 3 4 5 6');
K.addHeader('Machine', 'Kniterate');

#Parameters:
min = 1 #needle number of left edge
max = 20 #needle number of right edge
rows = 20 #number of rows to knit
carrier = "3" #carrier name

#Because of its structure, interlock does not require a [[cast-on]].
#Instead, we just bring the yarn in and start knitting:
K.ingripper(carrier)

#On kniterate, we write an extra row here to bring the yarn carrier to the right:
for n in range(min, max+1, 1):
	if n == max:
		#Skip the rightmost stitch in the first row to prevent rightmost column from unravelling:
		continue
	if (max - n) % 2 == 0:
		K.knit("+", 'b' + str(n), carrier)
	else:
		K.knit("+", 'f' + str(n), carrier)

# ---- interlock sheet ----
for r in range(0, rows+1, 1):
	if r % 2 == 0:
		#Even, left-going row:
		for n in range(max, min-1, -1):
			if (max - n) % 2 == 0:
				K.knit("-", 'f' + str(n), carrier)
			else:
				K.knit("-", 'b' + str(n), carrier)
	else:
		#Odd, right-going row:
		for n in range(min, max+1, 1):
			if (max - n) % 2 == 0:
				K.knit("+", 'b' + str(n), carrier)
			else:
				K.knit("+", 'f' + str(n), carrier)

# ---- take carrier out and drop remaining loops ----
#Send carrier back to its parking location:
K.outgripper(carrier)

#drop loops:
for n in range(min, max+1, 1):
	K.drop('f' + str(n))
	K.drop('b' + str(n))

K.write('interlock-sheet.kniterate.k')
