Skip to content

Commit faabf5f

Browse files
author
Andreas Kugel
committed
update
1 parent 7d0087d commit faabf5f

6 files changed

Lines changed: 169 additions & 0 deletions

File tree

src/2a-chessPrint.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
""" Demonstrate fundamental programming elements
2+
by creating a checker board, 8 by 8 fields,
3+
field size is configurable, same width and height
4+
"""
5+
6+
# function to make one linear segment of a pattern
7+
def segment(width, color):
8+
""" print <width> items of a color """
9+
# add items with a FOR LOOP
10+
for i in range(width):
11+
# print(color,end="") # simple verion
12+
print("{0:2d}".format(color),end="") # this looks better
13+
14+
# the main function
15+
def main():
16+
"""create a checker board pattern using a function"""
17+
18+
# constants
19+
fields = 8
20+
fieldWidth = 4
21+
22+
# variables
23+
row = 0
24+
column = 0
25+
# set color to 0
26+
color = 0
27+
while row < fields*fieldWidth and column < fields*fieldWidth:
28+
# print segment
29+
segment(fieldWidth,color)
30+
# invert color
31+
color = 1 if color == 0 else 0
32+
# increment column
33+
column = column + fieldWidth if column < fieldWidth*(fields-1) else 0
34+
# new line and increment row if column = 0
35+
if column == 0:
36+
print("") # prints a new line
37+
# reset line
38+
row = row + 1
39+
# invert color again if new row and end of field
40+
if column == 0 and row % fieldWidth == 0 :
41+
color = 1 if color == 0 else 0
42+
43+
44+
# call the main function
45+
if __name__ == "__main__":
46+
main()
47+
48+
49+

src/2b-chessList.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
""" Demonstrate fundamental programming elements
2+
by creating a checker board, 8 by 8 fields,
3+
field size is configurable, same width and height
4+
"""
5+
6+
# function to make one linear segment of a pattern
7+
def segment(width, color):
8+
""" put <width> items of color in a row starting at position row,col (= y,x)"""
9+
# initialize a new list as a numpy array
10+
segment = []
11+
# add items with a FOR LOOP
12+
for i in range(width):
13+
segment.append(color)
14+
# return the list
15+
return segment
16+
17+
# the main function
18+
def main():
19+
"""create a checker board pattern using a function"""
20+
21+
# constants
22+
fields = 8
23+
fieldWidth = 2
24+
25+
# variables
26+
row = 0
27+
column = 0
28+
# set color to 0
29+
color = 0
30+
# create the empty board
31+
board = []
32+
# create an empty line to start with
33+
line = []
34+
while row < fields*fieldWidth and column < fields*fieldWidth:
35+
# append the segment to the line
36+
line.append(segment(fieldWidth,color))
37+
# invert color
38+
color = 1 if color == 0 else 0
39+
# increment column
40+
column = column + fieldWidth if column < fieldWidth*(fields-1) else 0
41+
# add line to board and increment row if column = 0
42+
if column == 0:
43+
board.append(line)
44+
# reset line
45+
line = []
46+
row = row + 1
47+
# invert color again if new row and end of field
48+
if column == 0 and row % fieldWidth == 0 :
49+
color = 1 if color == 0 else 0
50+
51+
52+
print("Checker board")
53+
for b in board:
54+
print(b)
55+
56+
57+
# call the main function
58+
if __name__ == "__main__":
59+
main()
60+
61+
62+

src/2c-chessNp.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
""" Demonstrate fundamental programming elements
2+
by creating a checker board, 8 by 8 fields,
3+
field size is configurable, same width and height
4+
"""
5+
6+
# numpy is a helper library
7+
import numpy as np
8+
9+
# function to make one linear segment of a pattern
10+
def segment(width, color):
11+
""" put <width> items of color in a row starting at position row,col (= y,x)"""
12+
# initialize a new list as a numpy array
13+
segment = np.empty(width) #
14+
# add items with a FOR LOOP
15+
for i in range(width):
16+
segment[i] = color
17+
# return the list
18+
return segment
19+
20+
# the main function
21+
def main():
22+
"""create a checker board pattern using a function"""
23+
24+
# constants
25+
fields = 8
26+
fieldWidth = 2
27+
28+
# variables
29+
row = 0
30+
column = 0
31+
# set color to 0
32+
color = 0
33+
# create the board matrix
34+
board = np.empty((fields*fieldWidth,fields*fieldWidth))
35+
while row < fields*fieldWidth and column < fields*fieldWidth:
36+
# append the segment
37+
board[row,column:column+fieldWidth] = segment(fieldWidth,color)
38+
# invert color
39+
color = 1 if color == 0 else 0
40+
# increment column
41+
column = column + fieldWidth if column < fieldWidth*(fields-1) else 0
42+
# increment row if column = 0
43+
row = row + 1 if column == 0 else row
44+
# invert color again if new row and end of field
45+
if column == 0 and row % fieldWidth == 0 :
46+
color = 1 if color == 0 else 0
47+
48+
49+
print("Checker board")
50+
print(board)
51+
52+
53+
# call the main function
54+
if __name__ == "__main__":
55+
main()
56+
57+
58+
File renamed without changes.

0 commit comments

Comments
 (0)