forked from feberhardt/Project-Digital
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.py
More file actions
77 lines (68 loc) · 2.19 KB
/
functions.py
File metadata and controls
77 lines (68 loc) · 2.19 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
68
69
70
71
72
73
74
75
76
77
# Author: John, Leo, & Viktoria
# Project: Connect 4
# Date: 12/12/2017
# choose a less generic name for this file
import numpy
import pygame
def createboard(rows, columns):
""" Creates a string given rows and columns desired
that can be converted into a matrix through numpy
>>> createboard(5,4)
'0,0,0,0,0; 0,0,0,0,0; 0,0,0,0,0; 0,0,0,0,0'
>>> createboard(3,7)
'0,0,0; 0,0,0; 0,0,0; 0,0,0; 0,0,0; 0,0,0; 0,0,0'
"""
row_size = ''
for rows in range(rows):
if rows == 0:
row_size = row_size + '0'
else:
row_size = row_size + ',0'
# Or:
# ','.join(['0'] * rows)
fullmatrix = ''
for cols in range(columns):
if cols == 0:
fullmatrix = fullmatrix + row_size
else:
fullmatrix = fullmatrix + '; ' + row_size
# Or:
# row = ','.join(['0'] * rows)
# return ';'*
# However…see if you can construct a numpy array w/out going through
# a string representation first.
return fullmatrix
def look_through_rows(board, column, player):
""" Given a matrix, a column of the matrix, and a key,
This function will look through the column bottom to top,
and find the first empty slot indicated by a 0 and place
a piece there (1 or 2)
>>> look_through_rows(numpy.matrix('0,0,0,0,0; 0,0,0,0,0; 0,0,0,0,0; 0,0,0,0,0'), 2, 1)
matrix([[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 1, 0, 0]])
>>> look_through_rows(numpy.matrix('0,0,0; 0,0,0; 0,0,0; 0,0,0; 0,0,0; 0,0,0; 0,0,0'), 1, 2)
matrix([[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
[0, 2, 0]])
"""
if board.shape[1] > column:
count = board.shape[0] - 1
count2 = 1
while count >= 0 and count2 == 1:
if board[count, column] == 0:
board[count, column] = player
count2 = count2 - 1
else:
count = count - 1
return board
else:
raise Exception('Improper Column Given')
if __name__ == "__main__":
import doctest
doctest.testmod(verbose=False)