Program of Calc. Golf h-cap update
1.プログラム更新
ゴルフのハンディキャップを算出するプログラムを改訂しました。
ベストスコアを表示するようにしました。
過去のコーススコアのベスト3を表示します。
2.ソースコード
以前のプログラムに対して、追加部をイタリック体にしてあります。
以前のプログラムは下記を参照ください。
ただし、デバッグが不十分ですので、不具合が残っている可能性があります。
特に、新しいスコア入力時のベストスコアチェックが正しく機能しているかはまだ未確認です。
しばらくは、これ以上のデバッグは予定していませんのでご了承ください。
- import ui
- import csv
- R_MAX = 200
- hc_lst = [0] * R_MAX #list of handicap
- r_num = (-1) #num. of round
- #Table of adj. and how to calc.
- #adjust = -2,-1,0,1,2
- #how to calc. =
- # 0 =scrach
- # 1 =18th/2
- # 2 =18th
- # 3 =18th+17th/2
- # 4 =18th+17th
- # 5 =18th+17th+16th/2
- # 6 =18th+17th+16t
- # 7 =18th+17th+16th+15th/2
- # 8 =18th+17th+16th+15th
- # 9 =18th+17th+16th+15th+14th/2
- # 10 =18th+17th+16th+15th+14th
- s_tbl = [ [0,0],[1,0],[2,0],[-2,1],[-1,1],[0,1], #70-75
- [-2,2],[-1,2],[0,2],[1,2],[2,2], #76-80
- [-2,3],[-1,3],[0,3],[1,3],[2,3], #81-85
- [-2,4],[-1,4],[0,4],[1,4],[2,4], #86-90
- [-2,5],[-1,5],[0,5],[1,5],[2,5], #91-95
- [-2,6],[-1,6],[0,6],[1,6],[2,6], #96-100
- [-2,7],[-1,7],[0,7],[1,7],[2,7], #101-105
- [-2,8],[-1,8],[0,8],[1,8],[2,8], #106-110
- [-2,9],[-1,9],[0,9],[1,9],[2,9], #111-115
- [-2,10],[-1,10],[0,10],[1,10],[2,10]] #116-120
- s_lst = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
- bs_lst = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
- sorted = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
- #Two-dimensional arrays should be inclusive notation
- #”sc_lst = [[0]*20] * R_MAX” is NG
- sc_lst = [[0]*20 for _ in range(R_MAX)]
- s0_lst = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
- newdata = [(‘ ‘) * 80] #4char.x 20
- #for test data
- cal69 = [ ‘3’,’4′,’5′,’3′,’4′,’5′,’2′,’3′,’4′,
- ‘5’,’6′,’2′,’3′,’4′,’6′,’2′,’3′,’5′]
- cal70 = [ ‘3’,’4′,’5′,’3′,’4′,’5′,’2′,’3′,’4′,
- ‘5’,’6′,’2′,’3′,’4′,’7′,’2′,’3′,’5′]
- cal75 = [ ‘5’,’4′,’5′,’3′,’4′,’5′,’2′,’3′,’4′,
- ‘5’,’6′,’2′,’3′,’4′,’8′,’2′,’3′,’7′]
- cal76 = [ ‘5’,’4′,’5′,’3′,’4′,’5′,’2′,’3′,’4′,
- ‘5’,’6′,’2′,’3′,’4′,’9′,’2′,’3′,’7′]
- cal85 = [ ’10’,’4′,’5′,’3′,’4′,’5′,’3′,’3′,’9′,
- ‘5’,’6′,’2′,’3′,’4′,’8′,’2′,’2′,’7′]
- cal89 = [ ’10’,’4′,’5′,’3′,’4′,’5′,’3′,’3′,’9′,
- ‘5’,’6′,’2′,’3′,’4′,’8′,’2′,’2′,’11’]
- #ManaGolf
- cal101 = [‘6′,’5′,’2′,’5′,’6′,’5′,’5′,’6′,’6’,
- ‘6’,’3′,’5′,’7′,’8′,’5′,’8′,’8′,’5′]
- bst1 = 200 #max score is 200.
- bst2 = 200
- bst3 = 200
- rnd_lst = [200] * R_MAX #round score list
- def checkBestScore():
- global rnd_lst
- global bst1,bst2,bst3
- for i in range(0,R_MAX):
- if rnd_lst[i] < bst1: #best1?
- bst1 = rnd_lst[i]
- else:
- if rnd_lst[i] < bst2: #best2?
- bst2 = rnd_lst[i]
- else:
- if rnd_lst[i] < bst3: #best3?
- bst3 = rnd_lst[i]
- print(“best1,2,3=”,bst1,bst2,bst3)
- b1tf.text = str(bst1)
- b2tf.text = str(bst2)
- b3tf.text = str(bst3)
- def set_testdata():
- tf_lst = [h1tf,h2tf,h3tf,h4tf,h5tf,h6tf,h7tf,h8tf,h9tf,
- h10tf,h11tf,h12tf,h13tf,h14tf,h15tf,h16tf,h17tf,h18tf]
- for i in range(0,18):
- tf_lst[i].text = cal101[i]
- def calcHcap(lst):
- global sorted
- print(“before sort=”,lst)
- tscore = 0
- for i in range(0,18):
- tscore += lst[i]
- if tscore < 70: #if less than 70
- adj = 0
- cal = 0
- else:
- if tscore > 120:
- adj = 2
- cal = 10
- else:
- ts = tscore – 70
- adj = s_tbl[ts][0]
- cal = s_tbl[ts][1]
- print(“tscore=”,tscore)
- print(“adj_cal=”,adj,cal)
- lst.sort(reverse=True)
- print(“after sort=”,lst)
- if cal == 0:
- hcap = 0 + adj
- if cal == 1:
- hcap = s_lst[0] / 2 + adj
- if cal == 2:
- hcap = s_lst[0] + adj
- if cal == 3:
- hcap = round(s_lst[0] + s_lst[1] / 2 + adj)
- if cal == 4:
- hcap = s_lst[0] + s_lst[1] + adj
- if cal == 5:
- hcap = round(s_lst[0] + s_lst[1] + s_lst[2] / 2 + adj)
- if cal == 6:
- hcap = s_lst[0] + s_lst[1] + s_lst[2] + adj
- if cal == 7:
- hcap = round(s_lst[0] + s_lst[1] + s_lst[2] + s_lst[3] / 2 + adj)
- if cal == 8:
- hcap = s_lst[0] + s_lst[1] + s_lst[2] + s_lst[3] + adj
- if cal == 9:
- hcap = round(s_lst[0] + s_lst[1] + s_lst[2] + s_lst[3] + s_lst[4] / 2 + adj)
- if cal == 10:
- hcap = s_lst[0] + s_lst[1] + s_lst[2] + s_lst[3] + s_lst[4] + adj
- print(“hcap=”, hcap)
- return hcap
- def makeAddData(hc):
- global r_num
- r_num += 1
- hc_lst[r_num-1] = hc #set handicap-list
- sc_lst[r_num-1][0] = r_num #set round num.
- for i in range(0,18): #set hole data
- sc_lst[r_num-1][i+1] = bs_lst[i]
- sc_lst[r_num-1][19] = hc
- def exeCalcHcap(sender):
- global r_num
- wdata = [”] * 20
- rnd_sc = 0 #round score
- bs_lst[0] = s_lst[0] = int(h1tf.text) #set hole1 score
- bs_lst[1] = s_lst[1] = int(h2tf.text) #set hole2 score
- bs_lst[2] = s_lst[2] = int(h3tf.text) #set hole3 score
- bs_lst[3] = s_lst[3] = int(h4tf.text) #set hole4 score
- bs_lst[4] = s_lst[4] = int(h5tf.text) #set hole5 score
- bs_lst[5] = s_lst[5] = int(h6tf.text) #set hole6 score
- bs_lst[6] = s_lst[6] = int(h7tf.text) #set hole7 score5
- bs_lst[7] = s_lst[7] = int(h8tf.text) #set hole8 score
- bs_lst[8] = s_lst[8] = int(h9tf.text) #set hole9 score
- bs_lst[9] = s_lst[9] = int(h10tf.text) #set hole10 score
- bs_lst[10] = s_lst[10] = int(h11tf.text) #set hole11 score
- bs_lst[11] = s_lst[11] = int(h12tf.text) #set hole12 score
- bs_lst[12] = s_lst[12] = int(h13tf.text) #set hole13 score
- bs_lst[13] = s_lst[13] = int(h14tf.text) #set hole14 score
- bs_lst[14] = s_lst[14] = int(h15tf.text) #set hole15 score
- bs_lst[15] = s_lst[15] = int(h16tf.text) #set hole16 score
- bs_lst[16] = s_lst[16] = int(h17tf.text) #set hole17 score
- bs_lst[17] = s_lst[17] = int(h18tf.text) #set hole18 score
- hc = calcHcap(s_lst)
- makeAddData(hc) #make new data to newdata
- lstdata = ui.ListDataSource(“”) #clear list
- lstdata.items = sc_lst #set items of score list
- hc_tv.data_source = lstdata #set source
- hc_tv.reload_data() #display data
- wdata[0] = str(r_num)
- for i in range(0,18):
- wdata[i+1] = sc_lst[r_num-1][i+1]
- wdata[19] = str(hc)
- print(“write data=”,wdata)
- writeData(wdata)
- for i in range(0,18):
- rnd_sc += s_lst[i]
- rnd_lst[r_num] = rnd_sc
- def writeData(wd):
- global r_num
- fw = open(‘g_hcap.csv’,’a’)
- wp = csv.writer(fw,lineterminator=’\n’)
- wp.writerow(wd)
- fw.close()
- def readCSV():
- global r_num
- fr = open(“g_hcap.csv”) #open file
- rp = csv.reader(fr) #for read csv
- for rd in rp:
- print(“csv=”,rd)
- r_num += 1 #increment round num.
- if r_num > R_MAX: #check Max num.(for mem.)
- print(“Over num. of round!”)
- r_num = R_MAX-1
- break
- rnd_sc = 0
- s0_lst[0] = int(rd[0]) #set round num.
- s_lst[0] = s0_lst[1] = int(rd[1]) #set hole1 score
- s_lst[1] = s0_lst[2] = int(rd[2]) #set hole2 score
- s_lst[2] = s0_lst[3] = int(rd[3]) #set hole3 score
- s_lst[3] = s0_lst[4] = int(rd[4]) #set hole4 score
- s_lst[4] = s0_lst[5] = int(rd[5]) #set hole5 score
- s_lst[5] = s0_lst[6] = int(rd[6]) #set hole6 score
- s_lst[6] = s0_lst[7] = int(rd[7]) #set hole7 score
- s_lst[7] = s0_lst[8] = int(rd[8]) #set hole8 score
- s_lst[8] = s0_lst[9] = int(rd[9]) #set hole9 score
- s_lst[9] = s0_lst[10] = int(rd[10]) #set hole10 score
- s_lst[10] = s0_lst[11] = int(rd[11]) #set hole11 score
- s_lst[11] = s0_lst[12] = int(rd[12]) #set hole12 score
- s_lst[12] = s0_lst[13] = int(rd[13]) #set hole13 score
- s_lst[13] = s0_lst[14] = int(rd[14]) #set hole14 score
- s_lst[14] = s0_lst[15] = int(rd[15]) #set hole15 score
- s_lst[15] = s0_lst[16] = int(rd[16]) #set hole16 score
- s_lst[16] = s0_lst[17] = int(rd[17]) #set hole17 score
- s_lst[17] = s0_lst[18] = int(rd[18]) #set hole18 score
- hc_lst[r_num] = s0_lst[19] = int(rd[19]) #set handicap
- for i in range(0,20):
- sc_lst[r_num][i] = s0_lst[i]
- for i in range(0,18):
- rnd_sc += s_lst[i]
- rnd_lst[r_num] = rnd_sc
- checkBestScore()
- r_num += 1 #set round num. (is not zero)
- fr.close() #close csv
- v = ui.load_view()
- v.present(‘sheet’)
- h1tf = v[‘textfield1’] #text field1 for hole1 score
- h2tf = v[‘textfield2’] #text field1 for hole2 score
- h3tf = v[‘textfield3’] #text field1 for hole3 score
- h4tf = v[‘textfield4’] #text field1 for hole4 score
- h5tf = v[‘textfield5’] #text field1 for hole5 score
- h6tf = v[‘textfield6’] #text field1 for hole6 score
- h7tf = v[‘textfield7’] #text field1 for hole7 score
- h8tf = v[‘textfield8’] #text field1 for hole8 score
- h9tf = v[‘textfield9’] #text field1 for hole9 score
- h10tf = v[‘textfield10’] #text field1 for hole10 score
- h11tf = v[‘textfield11’] #text field1 for hole11 score
- h12tf = v[‘textfield12’] #text field1 for hole12 score
- h13tf = v[‘textfield13’] #text field1 for hole13 score
- h14tf = v[‘textfield14’] #text field1 for hole14 score
- h15tf = v[‘textfield15’] #text field1 for hole15 score
- h16tf = v[‘textfield16’] #text field1 for hole16 score
- h17tf = v[‘textfield17’] #text field1 for hole17 score
- h18tf = v[‘textfield18’] #text field1 for hole18 score
- hc_tv = v[‘tableview1’] #table_view1 for display game_num
- b1tf = v[‘textfield19’] #text field1 for best1 score
- b2tf = v[‘textfield20’] #text field1 for best2 score
- b3tf = v[‘textfield21’] #text field1 for best3 score
- r_num = (-1)
- set_testdata()
- readCSV()
3.画面(UI)
Textfield19,20,21を追加し、そこにベスト1、2、3を表示するようにしました。
4.実行結果
低スコアで公開するのが恥ずかしいですが、この際、しょうがないです。生のデータをそのまま公開します。