Program of Calc. Golf h-cap update


1.プログラム更新

ゴルフのハンディキャップを算出するプログラムを改訂しました。

ベストスコアを表示するようにしました。

過去のコーススコアのベスト3を表示します。

2.ソースコード

以前のプログラムに対して、追加部をイタリック体にしてあります。

以前のプログラムは下記を参照ください。

ただし、デバッグが不十分ですので、不具合が残っている可能性があります。

特に、新しいスコア入力時のベストスコアチェックが正しく機能しているかはまだ未確認です。

しばらくは、これ以上のデバッグは予定していませんのでご了承ください。

ハンディキャップ算出
  1. import ui
  2. import csv
  3. R_MAX = 200
  4. hc_lst = [0] * R_MAX                        #list of handicap
  5. r_num = (-1)                                        #num. of round
  6. #Table of adj. and how to calc.
  7. #adjust = -2,-1,0,1,2
  8. #how to calc. =
  9. #    0        =scrach
  10. #    1        =18th/2
  11. #    2        =18th
  12. #    3        =18th+17th/2
  13. #    4        =18th+17th
  14. #    5        =18th+17th+16th/2
  15. #    6        =18th+17th+16t
  16. #    7        =18th+17th+16th+15th/2
  17. #    8        =18th+17th+16th+15th
  18. #    9        =18th+17th+16th+15th+14th/2
  19. #    10    =18th+17th+16th+15th+14th
  20. s_tbl = [    [0,0],[1,0],[2,0],[-2,1],[-1,1],[0,1],                #70-75
  21.                     [-2,2],[-1,2],[0,2],[1,2],[2,2],                            #76-80
  22.                     [-2,3],[-1,3],[0,3],[1,3],[2,3],                            #81-85
  23.                     [-2,4],[-1,4],[0,4],[1,4],[2,4],                            #86-90
  24.                     [-2,5],[-1,5],[0,5],[1,5],[2,5],                            #91-95
  25.                     [-2,6],[-1,6],[0,6],[1,6],[2,6],                            #96-100
  26.                     [-2,7],[-1,7],[0,7],[1,7],[2,7],                            #101-105
  27.                     [-2,8],[-1,8],[0,8],[1,8],[2,8],                            #106-110
  28.                     [-2,9],[-1,9],[0,9],[1,9],[2,9],                            #111-115
  29.                     [-2,10],[-1,10],[0,10],[1,10],[2,10]]                    #116-120
  30. s_lst = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
  31. bs_lst = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
  32. sorted = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
  33. #Two-dimensional arrays should be inclusive notation
  34. #”sc_lst = [[0]*20] * R_MAX” is NG
  35. sc_lst = [[0]*20 for _ in range(R_MAX)]
  36. s0_lst = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
  37. newdata = [(‘ ‘) * 80]                #4char.x 20
  38. #for test data
  39. cal69 = [    ‘3’,’4′,’5′,’3′,’4′,’5′,’2′,’3′,’4′,
  40.                     ‘5’,’6′,’2′,’3′,’4′,’6′,’2′,’3′,’5′]
  41. cal70 = [    ‘3’,’4′,’5′,’3′,’4′,’5′,’2′,’3′,’4′,
  42.                     ‘5’,’6′,’2′,’3′,’4′,’7′,’2′,’3′,’5′]
  43. cal75 = [    ‘5’,’4′,’5′,’3′,’4′,’5′,’2′,’3′,’4′,
  44.                     ‘5’,’6′,’2′,’3′,’4′,’8′,’2′,’3′,’7′]
  45. cal76 = [    ‘5’,’4′,’5′,’3′,’4′,’5′,’2′,’3′,’4′,
  46.                     ‘5’,’6′,’2′,’3′,’4′,’9′,’2′,’3′,’7′]
  47. cal85 = [    ’10’,’4′,’5′,’3′,’4′,’5′,’3′,’3′,’9′,
  48.                     ‘5’,’6′,’2′,’3′,’4′,’8′,’2′,’2′,’7′]
  49. cal89 = [    ’10’,’4′,’5′,’3′,’4′,’5′,’3′,’3′,’9′,
  50.                     ‘5’,’6′,’2′,’3′,’4′,’8′,’2′,’2′,’11’]
  51. #ManaGolf
  52. cal101 = [‘6′,’5′,’2′,’5′,’6′,’5′,’5′,’6′,’6’,
  53.                     ‘6’,’3′,’5′,’7′,’8′,’5′,’8′,’8′,’5′]
  54. bst1 = 200        #max score is 200.
  55. bst2 = 200
  56. bst3 = 200
  57. rnd_lst = [200] * R_MAX        #round score list
  58. def checkBestScore():
  59.     global rnd_lst
  60.     global bst1,bst2,bst3
  61.     for i in range(0,R_MAX):
  62.         if rnd_lst[i] < bst1:                        #best1?
  63.             bst1 = rnd_lst[i]
  64.         else:
  65.             if rnd_lst[i] < bst2:                    #best2?
  66.                 bst2 = rnd_lst[i]
  67.             else:
  68.                 if rnd_lst[i] < bst3:                #best3?
  69.                     bst3 = rnd_lst[i]
  70.     print(“best1,2,3=”,bst1,bst2,bst3)
  71.     b1tf.text = str(bst1)
  72.     b2tf.text = str(bst2)
  73.     b3tf.text = str(bst3)
  74. def set_testdata():
  75.     tf_lst = [h1tf,h2tf,h3tf,h4tf,h5tf,h6tf,h7tf,h8tf,h9tf,
  76.                         h10tf,h11tf,h12tf,h13tf,h14tf,h15tf,h16tf,h17tf,h18tf]
  77.     for i in range(0,18):
  78.         tf_lst[i].text = cal101[i]
  79. def calcHcap(lst):
  80.     global sorted
  81.     print(“before sort=”,lst)
  82.     tscore = 0
  83.     for i in range(0,18):
  84.         tscore += lst[i]
  85.     if tscore < 70:                #if less than 70
  86.         adj = 0
  87.         cal = 0
  88.     else:
  89.         if tscore > 120:
  90.             adj = 2
  91.             cal = 10
  92.         else:
  93.             ts = tscore – 70
  94.             adj = s_tbl[ts][0]
  95.             cal = s_tbl[ts][1]
  96.     print(“tscore=”,tscore)
  97.     print(“adj_cal=”,adj,cal)    
  98.     lst.sort(reverse=True)
  99.     print(“after sort=”,lst)
  100.     if cal == 0:
  101.         hcap = 0 + adj
  102.     if cal == 1:
  103.         hcap = s_lst[0] / 2 + adj
  104.     if cal == 2:
  105.         hcap = s_lst[0] + adj
  106.     if cal == 3:
  107.         hcap = round(s_lst[0] + s_lst[1] / 2 + adj)
  108.     if cal == 4:
  109.         hcap = s_lst[0] + s_lst[1] + adj
  110.     if cal == 5:
  111.         hcap = round(s_lst[0] + s_lst[1] + s_lst[2] / 2 + adj)
  112.     if cal == 6:
  113.         hcap = s_lst[0] + s_lst[1] + s_lst[2] + adj
  114.     if cal == 7:
  115.         hcap = round(s_lst[0] + s_lst[1] + s_lst[2] + s_lst[3] / 2 + adj)
  116.     if cal == 8:
  117.         hcap = s_lst[0] + s_lst[1] + s_lst[2] + s_lst[3] + adj
  118.     if cal == 9:
  119.         hcap = round(s_lst[0] + s_lst[1] + s_lst[2] + s_lst[3] + s_lst[4] / 2 + adj)
  120.     if cal == 10:
  121.         hcap = s_lst[0] + s_lst[1] + s_lst[2] + s_lst[3] + s_lst[4] + adj
  122.     print(“hcap=”, hcap)
  123.     return hcap
  124. def makeAddData(hc):
  125.     global r_num
  126.     r_num += 1
  127.     hc_lst[r_num-1] = hc                             #set handicap-list
  128.     sc_lst[r_num-1][0] = r_num                #set round num.
  129.     for i in range(0,18):                        #set hole data
  130.         sc_lst[r_num-1][i+1] = bs_lst[i]
  131.     sc_lst[r_num-1][19] = hc
  132. def exeCalcHcap(sender):
  133.     global r_num
  134.     wdata = [”] * 20
  135.     rnd_sc = 0                    #round score
  136.     bs_lst[0] = s_lst[0] = int(h1tf.text)            #set hole1 score
  137.     bs_lst[1] = s_lst[1] = int(h2tf.text)            #set hole2 score
  138.     bs_lst[2] = s_lst[2] = int(h3tf.text)            #set hole3 score
  139.     bs_lst[3] = s_lst[3] = int(h4tf.text)            #set hole4 score
  140.     bs_lst[4] = s_lst[4] = int(h5tf.text)            #set hole5 score
  141.     bs_lst[5] = s_lst[5] = int(h6tf.text)            #set hole6 score
  142.     bs_lst[6] = s_lst[6] = int(h7tf.text)            #set hole7 score5
  143.     bs_lst[7] = s_lst[7] = int(h8tf.text)            #set hole8 score
  144.     bs_lst[8] = s_lst[8] = int(h9tf.text)            #set hole9 score
  145.     bs_lst[9] = s_lst[9] = int(h10tf.text)        #set hole10 score
  146.     bs_lst[10] = s_lst[10] = int(h11tf.text)        #set hole11 score
  147.     bs_lst[11] = s_lst[11] = int(h12tf.text)        #set hole12 score
  148.     bs_lst[12] = s_lst[12] = int(h13tf.text)        #set hole13 score
  149.     bs_lst[13] = s_lst[13] = int(h14tf.text)        #set hole14 score
  150.     bs_lst[14] = s_lst[14] = int(h15tf.text)        #set hole15 score
  151.     bs_lst[15] = s_lst[15] = int(h16tf.text)        #set hole16 score
  152.     bs_lst[16] = s_lst[16] = int(h17tf.text)        #set hole17 score
  153.     bs_lst[17] = s_lst[17] = int(h18tf.text)        #set hole18 score
  154.     hc = calcHcap(s_lst)
  155.     makeAddData(hc)                                        #make new data to newdata
  156.     lstdata = ui.ListDataSource(“”)        #clear list
  157.     lstdata.items = sc_lst                        #set items of score list
  158.     hc_tv.data_source = lstdata                #set source
  159.     hc_tv.reload_data()                                #display data
  160.     wdata[0] = str(r_num)
  161.     for i in range(0,18):
  162.         wdata[i+1] = sc_lst[r_num-1][i+1]
  163.     wdata[19] = str(hc)
  164.     print(“write data=”,wdata)
  165.     writeData(wdata)
  166.     for i in range(0,18):
  167.         rnd_sc += s_lst[i]
  168.     rnd_lst[r_num] = rnd_sc
  169. def writeData(wd):
  170.     global r_num
  171.     fw = open(‘g_hcap.csv’,’a’)
  172.     wp = csv.writer(fw,lineterminator=’\n’)
  173.     wp.writerow(wd)
  174.     fw.close()
  175. def readCSV():
  176.     global r_num
  177.     fr = open(“g_hcap.csv”)                #open file
  178.     rp = csv.reader(fr)                            #for read csv
  179.     for rd in rp:
  180.         print(“csv=”,rd)            
  181.         r_num += 1                                        #increment round num.    
  182.         if r_num > R_MAX:                        #check Max num.(for mem.)
  183.             print(“Over num. of round!”)
  184.             r_num = R_MAX-1
  185.             break
  186.         rnd_sc = 0
  187.         s0_lst[0] = int(rd[0])            #set round num.
  188.         s_lst[0] = s0_lst[1] = int(rd[1])                        #set hole1 score
  189.         s_lst[1] = s0_lst[2] = int(rd[2])                        #set hole2 score
  190.         s_lst[2] = s0_lst[3] = int(rd[3])                        #set hole3 score
  191.         s_lst[3] = s0_lst[4] = int(rd[4])                        #set hole4 score
  192.         s_lst[4] = s0_lst[5] = int(rd[5])                        #set hole5 score
  193.         s_lst[5] = s0_lst[6] = int(rd[6])                        #set hole6 score        
  194.         s_lst[6] = s0_lst[7] = int(rd[7])                        #set hole7 score
  195.         s_lst[7] = s0_lst[8] = int(rd[8])                        #set hole8 score
  196.         s_lst[8] = s0_lst[9] = int(rd[9])                        #set hole9 score
  197.         s_lst[9] = s0_lst[10] = int(rd[10])                        #set hole10 score        
  198.         s_lst[10] = s0_lst[11] = int(rd[11])                        #set hole11 score
  199.         s_lst[11] = s0_lst[12] = int(rd[12])                        #set hole12 score
  200.         s_lst[12] = s0_lst[13] = int(rd[13])                        #set hole13 score
  201.         s_lst[13] = s0_lst[14] = int(rd[14])                        #set hole14 score
  202.         s_lst[14] = s0_lst[15] = int(rd[15])                        #set hole15 score
  203.         s_lst[15] = s0_lst[16] = int(rd[16])                        #set hole16 score
  204.         s_lst[16] = s0_lst[17] = int(rd[17])                        #set hole17 score
  205.         s_lst[17] = s0_lst[18] = int(rd[18])                        #set hole18 score
  206.         hc_lst[r_num] = s0_lst[19] = int(rd[19])                #set handicap
  207.         for i in range(0,20):
  208.             sc_lst[r_num][i] = s0_lst[i]
  209.         for i in range(0,18):
  210.             rnd_sc += s_lst[i]
  211.         rnd_lst[r_num] = rnd_sc
  212.     checkBestScore()
  213.     r_num += 1        #set round num. (is not zero)
  214.     fr.close()        #close csv
  215. v = ui.load_view()
  216. v.present(‘sheet’)
  217. h1tf = v[‘textfield1’]            #text field1 for hole1 score
  218. h2tf = v[‘textfield2’]            #text field1 for hole2 score
  219. h3tf = v[‘textfield3’]            #text field1 for hole3 score
  220. h4tf = v[‘textfield4’]            #text field1 for hole4 score
  221. h5tf = v[‘textfield5’]            #text field1 for hole5 score
  222. h6tf = v[‘textfield6’]            #text field1 for hole6 score
  223. h7tf = v[‘textfield7’]            #text field1 for hole7 score
  224. h8tf = v[‘textfield8’]            #text field1 for hole8 score
  225. h9tf = v[‘textfield9’]            #text field1 for hole9 score
  226. h10tf = v[‘textfield10’]        #text field1 for hole10 score
  227. h11tf = v[‘textfield11’]        #text field1 for hole11 score
  228. h12tf = v[‘textfield12’]        #text field1 for hole12 score
  229. h13tf = v[‘textfield13’]        #text field1 for hole13 score
  230. h14tf = v[‘textfield14’]        #text field1 for hole14 score
  231. h15tf = v[‘textfield15’]        #text field1 for hole15 score
  232. h16tf = v[‘textfield16’]        #text field1 for hole16 score
  233. h17tf = v[‘textfield17’]        #text field1 for hole17 score
  234. h18tf = v[‘textfield18’]        #text field1 for hole18 score
  235. hc_tv = v[‘tableview1’]            #table_view1 for display game_num
  236. b1tf = v[‘textfield19’]        #text field1 for best1 score
  237. b2tf = v[‘textfield20’]        #text field1 for best2 score
  238. b3tf = v[‘textfield21’]        #text field1 for best3 score
  239. r_num = (-1)
  240. set_testdata()
  241. readCSV()

3.画面(UI)

Textfield19,20,21を追加し、そこにベスト1、2、3を表示するようにしました。

4.実行結果

低スコアで公開するのが恥ずかしいですが、この際、しょうがないです。生のデータをそのまま公開します。


コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です