-
Notifications
You must be signed in to change notification settings - Fork 0
/
zock.py
402 lines (315 loc) · 11.4 KB
/
zock.py
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
#!/usr/bin/env python3
# zock.py version 2024-08-15
# - with scores of a, b, c for 10,11,12 and "?" as stvg to look up in variable
# - 0 means 'no result', 'strich'
# - hcp with decimal "." will be converted from hcpi to pkayinghcp, otherwise number playinghcp w/o conversion
##### INPUT #####
vorgaben = {"Peter":"17.3","Hans":"19.8","Cengiz":"26.5","Thomas":"10.5","Willy":"14.5","Rainer":"16.6","Horst":"28","Helmut":"18.9", "Rudi":"17"}
secondnine = False
secondninespecialpoint = True # odd stvg on second nine gets next even stvg (both halves equal)
venue = "24-10-23 GCMV 1-9"
inputByHole = 'Willy Thomas,? ?,55 78 54 86 53 47 75 86 65'
# inputByPlayer = '' 059560747 Julia 55,545755536 ThomasS 11,767573745 Lukas 22' #564754005 Rainer ?,665687677 Cengiz ?,685550400 Willy ?' #564643604 Helmut 23,575655077 Horst 28'#675755576 Horst 28,864856865 Peter ?,565443486 Rainer 20,584765696 Cengiz ?,764945575 Willy ?'
inputByPlayer = '' #784765687 Horst ?,664553785 Peter ?,475664696 Willy ?' #'587856607 Julia 55,576053384 ThomasS 11,560564404 Lukas 22' #564754005 Rainer ?,665687677 Cengiz ?,685550400 Willy ?' #564643604 Helmut 23,575655077 Horst 28'#675755576 Horst 28,864856865 Peter ?,565443486 Rainer 20,584765696 Cengiz ?,764945575 Willy ?'
#=' Hans 19.1,665745665 Willy 14.5,904566690 Horst 29'
##### COURSE ######
## 1: PAR distribution 2. difficulty ranking 3. cr/slope/Par
# coursepar18 = [4,4,5,3,4,3,5,3,4,3,4,4,5,4,3,3,5,4] # Dackenheim
coursepar18 = [4,5,3,4,4,3,4,5,4,4,4,4,3,5,4,3,5,4] # GCMV
# [4,5,4,4,3,5,3,4,4,4,4,3,5,4,5,3,4,4] # GC Pfalz
# [4,5,3,4,4,3,4,5,4,4,4,4,3,5,4,3,5,4] # GCMV
#coursepar =[4,5,3,4,4,3,4,5,4] #GCMV 1-9
#coursepar = [4,4,4,3,5,4,3,5,4] #GCMV 10-18
#coursepar = [5,3,4,4,4,4,5,4,3] # Heddesheim 1-9
# coursepar = [5,3,4,4,3,4,4,5,4] # Heddesheim 10-18
# first 9: +1 /2 second 9: /2. if odd on first nine
# coursehcp18 = [9,11,13,3,1,17,5,7,15,18,8,4,6,14,12,10,2,16] #Dackenheim
coursehcp18 = [13,3,1,9,7,15,17,11,5,8,12,14,18,10,2,16,4,6] #GCMV
# [13,17,11,5,15,1,9,7,3,6,12,16,10,4,14,18,2,8] # GC Pfalz
# [13,3,1,9,7,15,17,11,5,8,12,14,18,10,2,16,4,6] #GCMV
#coursehcp = [7,2,1,5,4,8,9,6,3] # GCMV 1-9
#coursehcp = [4,6,7,9,5,1,8,2,3] # GCMV 10-18
#coursehcp18 = [11,13,3,17,7,5,1,9,15,14,16,2,18,10,4,8,6,12] # Heddesheim
#coursehcp = [6,7,2,9,4,3,1,5,8] # Heddesheim 1-9
#coursehcp = [7,8,1,9,5,2,4,3,6] # Heddesheim 1-18
cr, slope, par = 72.5, 130, 72 ### GCMV before nov/1/2024
# cr, slope, par = 72.4, 133, 72 # GCMV ab nov/1/2024
#####################
def stvg2spvg (stvg, par, cr, slope):
## CAREFUL: this formula goes wrong if result if result is exactly x,5 - round does go down!!
return (round(stvg*slope/113 + cr - par))
# OFFICIAL FORMULA for 9 holes: if 9-Loch: stvg/2*slope9/113 + cr9 -par9
def nettopts(strokes, spvg):
p = []
offset = 9 if secondnine else 0 # use the correct PAR numbers
# spvg > 18 or 36 -- a MOD operation would be nicer like ((spvg - 1) % 18) + 1)
if (spvg <= 18):
v0 = 0
else:
v0 = 1 # one bonus at least
spvg -= 18
if spvg > 18:
v0 += 1
spvg -= 18
# SPECIAL RULE for 'secondnine': odd spvg should not get one stroke less than on
# IMPLICATES that secondnine always contains the even hole ranks
if secondnine and ((spvg % 2) == 1) and secondninespecialpoint:
spvg+=1
for i in range(9):
# bonus v0
# print(i," = Rang ", coursehcp18[i+offset], " <=? ", spvg)
if spvg >= coursehcp18[i+offset]:
v1 = v0 +1 # vorgabe am loch? = extra punkt
else: v1 = v0
if strokes[i] > 0: # falls nicht 'strich'
p0 = 2 + coursepar[i] - strokes[i] # 1. brutto par = 2 usw
p0 += v1
if p0 < 0: p0 = 0 # 4. nur positive punkte addieren, negative = 0
else:
p0 = 0
p.append(p0)
p.append(v1) ## little strange: result contains both, the netto points and the bonus at the hole
return p
def adv_matchplay (spvg0, spvg1):
# strokes: diff *3/4 - halved - +0,5 if odd
# positive advantage = player0 receives strokes
# must be under 18 effektive stromes for 18
advantage = round(abs(spvg0 - spvg1) * 3 / 4 + 0.1) #18 holes
times18 = 0
if advantage > 18:
# return([0,0,0,0,0,0,0,0,0]) # should be an error, but 0 should make it visible also
times18 = 1
advantage -= 18
if advantage % 2 == 1:
halfstroke = True
advantage += 1 # although on the last hole it will only be .5
else:
halfstroke = False
#halfstroke = (advantage % 2 == 1) # True/False
advantage = advantage // 2 # plus halfstroke eventually
if (spvg1 > spvg0): advantage *= -1
a_s = []
for hole in range(9):
if abs(advantage) >= coursehcp[hole]:
if (abs(advantage) == coursehcp[hole]) and halfstroke:
a = 0.5
else:
a = 1
a_s.append(a + times18)
if (advantage < 0): a_s[hole] *= -1
else: a_s.append(times18)
## todo: this print statement should not be here but in the main loop where all output is generated
if (spvg1 > spvg0): print(' gebe: ',prettyadvantages(a_s))
else: print(' bekomme: ',prettyadvantages(a_s))
return (a_s)
def _adv_matchplay (spvg0, spvg1):
# strokes: diff *3/4 - halved - +0,5 if odd
# positive advantage = player0 receives strokes
# this methods requires that all odd difficulties are on 1-9 and even on 10-18, better treat them seperately
advantage = round(abs(spvg0 - spvg1) * 3 / 4 + 0.1)
if (spvg1 > spvg0): advantage *= -1
a_s = []
for hole in range(9):
if abs(advantage) >= coursehcp[hole]:
a_s.append(1)
if (abs(advantage) == coursehcp[hole]) and (advantage % 2 == 1): a_s[hole] = 0.5
if (advantage < 0): a_s[hole] *= -1
else: a_s.append(0)
#print('Vorgabevektor: ',a_s)
return (a_s)
def matchplay (strokes0, spvg0, strokes1, spvg1):
advantages = adv_matchplay(spvg0,spvg1)
up = []
for hole in range(len(strokes0)):
if strokes0[hole] == 0 and strokes1[hole] == 0:
up.append(0)
elif strokes0[hole] == 0:
up.append(-1)
elif strokes1[hole] == 0:
up.append(1)
else:
p0adv = strokes1[hole] - strokes0[hole] + advantages[hole]
if p0adv > 0:
up.append(1)
elif p0adv < 0:
up.append(-1)
else:
up.append(0)
return up
def prettymatch(holes):
prettystring=''
resulttxt=''
i=0
match=0
for hole in holes:
match += hole
if hole<0:
prettystring += '▼'
elif hole>0:
prettystring += '▲'
else:
prettystring += '◦'
if i in (2,5): prettystring += ' '
i+=1
if resulttxt=='' and abs(match) > (9-i):
if i==9:
resulttxt = str(abs(match)) + 'auf'
else:
resulttxt = str(abs(match)) + '+' + str(9-i)
if match > 0: prettystring = '✔︎ gewonnen [+'+str(abs(match))+'/'+resulttxt+'] ('+prettystring+')'
elif match < 0: prettystring = '✘ verloren [-'+str(abs(match))+'/'+resulttxt+'] ('+prettystring+')'
else: prettystring = '⦁ geteilt ('+prettystring+')'
return prettystring
def prettyround(holes):
prettystring=''
i=0
for hole in holes:
if hole==0:
prettystring += '-'
else:
if hole>9: prettystring += '('
prettystring += str(hole)
if hole>9: prettystring += ')'
if i in (2,5): prettystring += ' '
i+=1
return prettystring
def prettyadvantages (holes):
prettystring=''
i=0
for hole in holes:
if hole==0:
prettystring += '◦'
elif abs(hole) == 1:
prettystring += '●'
else:
prettystring += '◐'
if i in (2,5): prettystring += ' '
i+=1
return prettystring
def prettypoints (points):
prettystring = ''
total = 0
#i = 0
for hole in range(len(points) // 2):
total += points[hole * 2]
prettystring += str(points[hole * 2])
if points[hole * 2 + 1] == 0:
prettystring += '⁰ ' #'◦ '
elif points[hole * 2 + 1] == 1:
prettystring += '¹ '
else:
prettystring += '² '
if hole in (2,5): prettystring += '| '
prettystring = str(total)+'P << ' + prettystring
return prettystring
def holeranks9 (holeranks18):
#input: list of difficulties, lower is more difficult
#output-list: idx = holenumber, value tells rank starting with 1 for the most difficult
l = []
for idx, value in enumerate(holeranks18):
#print (value,"is at",idx)
l.append ([value,idx]) # check how it works with tuple instad field
#print(l)
l.sort (reverse=False) # sortieren nach den Values, niedrigster Wert zuerst, idx ist die urspgl. Position (das Loch)
#print(l)
# jetzt mit map neuen Vektor y erzeugen ; mal mit map versuchen!?
y=[0,0,0,0,0,0,0,0,0]
for i in range(9):
#y[i] = l[i][1]+1 # idx0 tells number of most difficult hole
y[l[i][1]] = i+1 #
#print("Scorekarte:",v,"Vorgabenverteilung 9:",y)
return(y)
## BuildBoard = populate the internal data structure from various input options
def playinghcp (hcpstr):
if "." in hcpstr:
ph = stvg2spvg (float(hcpstr),par,cr,slope)
else:
ph = int(hcpstr)
return ph
def buildBoardByPlayer (board, input):
# populate board (comma between players, blank between scores/name/spvg)
#board = []
for player in input.split(','):
playerf = []
result = player.split(' ') # score digits / name / spvg
# score digits, 0 = 0 (means none)
for c in result[0]:
if (c >= '0' and c <= '9'):
playerf.append(ord(c)-48)
elif (c >= 'a' and c <= 'f'):
playerf.append(ord(c)-97+10)
if len(playerf) > 9: print('Error: more than 9 scores')
elif len(playerf) < 9:
for i in range(9 - len(playerf)): playerf.append(0)
# name
playerf.append(result[1])
# spvg
#spvg=0
#for c in result[2]:
# if (c >= '0' and c <= '9'):
# spvg = spvg * 10 + (ord(c)-48)
if result[2] == '?':
spvg = playinghcp(vorgaben[result[1]])
else:
spvg = playinghcp (result[2])
playerf.append(spvg)
board.append(playerf)
return
def buildBoardByHole (board, input):
#board = []
sections = input.split(',') # 0:names 1:spvg 2:holebyhole
names = sections[0].split(' ')
spvgs = sections[1].split(' ')
holes = sections[2].split(' ')
for player in range(len(names)):
playerf=[]
# holes
for holestring in holes:
c = holestring[player]
if (c >= '0' and c <= '9'):
playerf.append(ord(c)-48)
elif (c >= 'a' and c <= 'f'):
playerf.append(ord(c)-97+10)
for i in range(9 - len(holes)): playerf.append(0)
# name
playerf.append(names[player])
# spvg
#spvg=0
#for c in spvgs[player]:
# if (c >= '0' and c <= '9'):
# spvg = spvg * 10 + (ord(c)-48)
if spvgs[player] == '?':
spvg = playinghcp(vorgaben[names[player]])
else:
spvg = playinghcp(spvgs[player])
playerf.append(spvg)
board.append(playerf)
return
#### main ###
#board = buildBoardByPlayer(inputByPlayer)
board = []
if len(inputByHole) > 0: buildBoardByHole(board, inputByHole)
if len(inputByPlayer) > 0: buildBoardByPlayer(board, inputByPlayer)
if secondnine:
coursehcp = holeranks9(coursehcp18[9:]) # first 9 = [:9] or last 9 = [9:]:
coursepar = coursepar18[9:]
else:
coursehcp = holeranks9(coursehcp18[:9]) # first 9 = [:9] or last 9 = [9:]:
coursepar = coursepar18[:9]
print ("Coursehcp: ",coursehcp)
# print(board) ## [[1,2,..9,'name',22],[9..,,2,1,'name',11]]
for player in board:
print (player[9],' (SpVg ',player[10],"*" if (secondnine and secondninespecialpoint and (player[10] % 2 == 1)) else "",') : ', prettyround(player[:9]), sep='')
print (' ',prettypoints(nettopts(player[:9],player[10])))
balance = 0
for player2 in board:
if player2[9] != player[9]:
print (' gegen ',player2[9], end='') #( SpVg ',player2[10],')', end='')
match = matchplay(player[:9],player[10],player2[:9],player2[10])
print (' ',prettymatch(match))
sumofmatch = sum(match)
if sumofmatch > 0:
balance += 1
elif sumofmatch <0:
balance -= 1
print (' Gewinn/Verlust: ', balance*5, '€')