-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy path2c-chessNp.py
More file actions
67 lines (55 loc) · 1.95 KB
/
2c-chessNp.py
File metadata and controls
67 lines (55 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
""" Demonstrate fundamental programming elements
by creating a checker board, 8 by 8 fields,
field size is configurable, same width and height
Again, the basic algorith is like before, but this time
we use a more advanced data structure provided by a external library
The library "numerical python" (numpy) is imported with the statement
#import numpy as np
and it's functions are called as "np.function_name()"
We use the "array" data type to realize a 2D matrix, representing
the checker board. The 2D matrix is similar to the list of lists but
offers additional operations e.g. on the entire dataset which we use
in the future
"""
# numpy is a helper library
import numpy as np
# function to make one linear segment of a pattern
def segment(width, color):
""" put <width> items of color in a row"""
# initialize a new list as a numpy array
seg = np.empty(width) #
# add items with a FOR LOOP
for i in range(width):
seg[i] = color
# return the list
return seg
# the main function
def main():
"""create a checker board pattern using a function"""
# constants
fields = 4
fieldWidth = 4
# variables
row = 0
column = 0
# set color to 0
color = 0
# create the board matrix
board = np.empty((fields*fieldWidth,fields*fieldWidth))
while row < fields*fieldWidth and column < fields*fieldWidth:
# append the segment
board[row,column:column+fieldWidth] = segment(fieldWidth,color)
# invert color
color = 1 if color == 0 else 0
# increment column
column = column + fieldWidth if column < fieldWidth*(fields-1) else 0
# increment row if column = 0
row = row + 1 if column == 0 else row
# invert color again if new row and end of field
if column == 0 and row % fieldWidth == 0 :
color = 1 if color == 0 else 0
print("Checker board")
print(board)
# call the main function
if __name__ == "__main__":
main()