forked from feberhardt/Project-Digital
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwinning.py
More file actions
136 lines (128 loc) · 5.08 KB
/
winning.py
File metadata and controls
136 lines (128 loc) · 5.08 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# Author: Siena and Felix
# Date: 11/7/2017
# Determines if game is won and by who when given a matrix
# Scoring scores matrix, checking for wins and streaks
import numpy
# TODO check about range of functions
def winning(matrix, connect=4):
"""Check if player 1 or 2 wins with the board
return tuple with (winning true/false, player who won)
"""
rows = matrix.shape[0]
columns = matrix.shape[1]
# horizontal
for x in range(rows):
for y in range(columns - connect + 1):
if matrix[x, y] == matrix[x, y + 1] == matrix[x, y + 2] == matrix[x, y + 3] and matrix[x, y] != 0:
# print('horiz')
return True, matrix[x, y]
# vertical
for y in range(columns):
for x in range(rows - connect + 1):
if matrix[x, y] == matrix[x + 1, y] == matrix[x + 2, y] == matrix[x + 3, y] and matrix[x, y] != 0:
# print('vertical')
return True, matrix[x, y]
# diagonal
arrayM = numpy.asarray(matrix)
for x in range(columns):
a = numpy.diagonal(arrayM, x - (rows - connect))
for i in range(len(a) - connect + 1):
if a[i] == a[i + 1] == a[i + 2] == a[i + 3] and a[i] != 0:
return True, a[i]
# reverse diagonal
arrayM = numpy.asarray(numpy.fliplr(matrix))
for x in range(columns):
a = numpy.diagonal(arrayM, x - (rows - connect))
for i in range(len(a) - connect + 1):
if a[i] == a[i + 1] == a[i + 2] == a[i + 3] and a[i] != 0:
# print('diag')
return True, a[i]
# tie
if numpy.count_nonzero(arrayM) == rows * columns:
return True, 0
# if not won
return False, 0
def scoring(matrix, my_turn, connect=4):
"""Scores matrix position"""
# TODO: add my_turn
rows = matrix.shape[0]
columns = matrix.shape[1]
a = winning(matrix, connect)[0]
b = winning(matrix, connect)[1]
score1 = 0
score2 = 0
# check if player 1 or 2 wins
if b == 1:
score1 = -100000
return score1
elif b == 2:
score2 = 100000
return score2
else:
# horizontal
for x in range(rows):
for y in range(columns - connect + 2): # 3 in a row
if matrix[x, y] == matrix[x, y + 1] == matrix[x, y + 2] == 1:
score1 += 2500
# print(score1)
elif matrix[x, y] == matrix[x, y + 1] == matrix[x, y + 2] == 2:
score2 += 2500
# print(score2)
for y in range(columns - connect + 3): # 2 in a row
if matrix[x, y] == matrix[x, y + 1] == 1:
score1 += 50
# print(score1)
elif matrix[x, y] == matrix[x, y + 1] == 2:
score2 += 50
# print(score2)
# vertical -- Doesn't look at the last row
# Oh yeah - this (doesn't look at the last row) matches what we were seeing
# on finals day. Any idea why, and how to fix it?
for y in range(columns):
for x in range(rows - connect + 2):
# add another == for Connect 4
if matrix[x, y] == matrix[x + 1, y] == matrix[x + 2, y] == 1:
score1 += 2500
# print(score1)
# add another == for Connect 4
elif matrix[x, y] == matrix[x + 1, y] == matrix[x + 2, y] == 2:
score2 += 2500
# print(score2)
for x in range(rows - connect + 3):
if matrix[x, y] == matrix[x + 1, y] == 1: # add another == for Connect 4
score1 += 50
# print(score1)
elif matrix[x, y] == matrix[x + 1, y] == 2: # add another == for Connect 4
score2 += 50
# print(score2)
# diagonal
arrayM = numpy.asarray(matrix)
# for x in range(columns-1):
for x in range(columns):
a = numpy.diagonal(arrayM, x - (rows - connect))
for i in range(len(a) - connect + 2):
if a[i] == a[i + 1] == a[i + 2] == 1:
score1 += 2500
elif a[i] == a[i + 1] == a[i + 2] == 2:
score2 += 2500
for i in range(len(a) - connect + 3):
if a[i] == a[i + 1] == 1:
score1 += 50
elif a[i] == a[i + 1] == 2:
score2 += 50
# reverse diagonal
arrayM = numpy.asarray(numpy.fliplr(matrix))
for x in range(columns - 1):
a = numpy.diagonal(arrayM, x - (rows - connect))
for i in range(len(a) - connect + 2):
if a[i] == a[i + 1] == a[i + 2] == 1:
score1 += 2500
elif a[i] == a[i + 1] == a[i + 2] == 2:
score2 += 2500
for i in range(len(a) - connect + 3):
if a[i] == a[i + 1] == 1:
score1 += 50
elif a[i] == a[i + 1] == 2:
score2 += 50
return score2 - score1
# remove dead code