Calc. Handicap of Golf-3

ゴルフのスコアを入力することでハンディキャップを算出し、リスト化するようにしました。

前回同様、「キャロウェイ方式」で算出する様にしています。

リストはCSVファイルとして保存し、データを追加できるようにしています。

表示の体裁はあまり良くないですが、ハンディキャップの経緯を見るには使えるかなと思います。

※前回のプログラム同様、csvファイルのデータの整合性や、数字、文字のチェックはしていません。また、入力するスコアの数字、文字のチェックもしていません。そのため、ファイルのデータ、入力ミスによって正常に動作しないことがあります。機能実現重視で、きちんとデバッグできていませんがご了承ください。

【開発環境】

 Pythonista + iPad

【画面構成】

.pyuiでの画面構成

【初期データ】

予め、2つのスコアを入力済みのcsvファイルを用意しました。(ファイル名:g_hcap.csv)

左端がラウンドNO、次が1から18ホールのスコア、最後がハンディキャップ、という構成です。

恥ずかしながら、ゴルフを2年前に再開した時の最初の2ラウンド分のスコアをそのまま使用しています。

【実行画面:起動時】

デバッグ用に、初期データを自動表示させています。

(3ラウンド目のスコアをそのまま使用しました)

【実行画面:データ追加実行時】

「Exe. Calc」ボタンを押すとデータ追加が実行され、結果が表示され、同時にCSVファイルにも追記します。

 これを見て気付きましたが、トータルスコアも表示、記録した方が良かったです。

後日、改良したいと思います。

【ソースコード】

 ソースコードの貼り付け方がわかりました。HTML形式のテキストをコピペすれば良さそうです。

今回は、「srctohtml」というツールを使用させて頂きました。

  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. def set_testdata():
  55.     tf_lst = [h1tf,h2tf,h3tf,h4tf,h5tf,h6tf,h7tf,h8tf,h9tf,
  56.                         h10tf,h11tf,h12tf,h13tf,h14tf,h15tf,h16tf,h17tf,h18tf]
  57.     for i in range(0,18):
  58.         tf_lst[i].text = cal101[i]
  59. def calcHcap(lst):
  60.     global sorted
  61.     print(“before sort=”,lst)
  62.     tscore = 0
  63.     for i in range(0,18):
  64.         tscore += lst[i]
  65.     if tscore < 70:                #if less than 70
  66.         adj = 0
  67.         cal = 0
  68.     else:
  69.         if tscore > 120:
  70.             adj = 2
  71.             cal = 10
  72.         else:
  73.             ts = tscore – 70
  74.             adj = s_tbl[ts][0]
  75.             cal = s_tbl[ts][1]
  76.     print(“tscore=”,tscore)
  77.     print(“adj_cal=”,adj,cal)    
  78.     lst.sort(reverse=True)
  79.     print(“after sort=”,lst)
  80.     if cal == 0:
  81.         hcap = 0 + adj
  82.     if cal == 1:
  83.         hcap = s_lst[0] / 2 + adj
  84.     if cal == 2:
  85.         hcap = s_lst[0] + adj
  86.     if cal == 3:
  87.         hcap = round(s_lst[0] + s_lst[1] / 2 + adj)
  88.     if cal == 4:
  89.         hcap = s_lst[0] + s_lst[1] + adj
  90.     if cal == 5:
  91.         hcap = round(s_lst[0] + s_lst[1] + s_lst[2] / 2 + adj)
  92.     if cal == 6:
  93.         hcap = s_lst[0] + s_lst[1] + s_lst[2] + adj
  94.     if cal == 7:
  95.         hcap = round(s_lst[0] + s_lst[1] + s_lst[2] + s_lst[3] / 2 + adj)
  96.     if cal == 8:
  97.         hcap = s_lst[0] + s_lst[1] + s_lst[2] + s_lst[3] + adj
  98.     if cal == 9:
  99.         hcap = round(s_lst[0] + s_lst[1] + s_lst[2] + s_lst[3] + s_lst[4] / 2 + adj)
  100.     if cal == 10:
  101.         hcap = s_lst[0] + s_lst[1] + s_lst[2] + s_lst[3] + s_lst[4] + adj
  102.     print(“hcap=”, hcap)
  103.     return hcap
  104. def makeAddData(hc):
  105.     global r_num
  106.     r_num += 1
  107.     hc_lst[r_num-1] = hc                             #set handicap-list
  108.     sc_lst[r_num-1][0] = r_num                #set round num.
  109.     for i in range(0,18):                        #set hole data
  110.         sc_lst[r_num-1][i+1] = bs_lst[i]
  111.     sc_lst[r_num-1][19] = hc
  112. def exeCalcHcap(sender):
  113.     global r_num
  114.     wdata = [”] * 20
  115.     bs_lst[0] = s_lst[0] = int(h1tf.text)            #set hole1 score
  116.     bs_lst[1] = s_lst[1] = int(h2tf.text)            #set hole2 score
  117.     bs_lst[2] = s_lst[2] = int(h3tf.text)            #set hole3 score
  118.     bs_lst[3] = s_lst[3] = int(h4tf.text)            #set hole4 score
  119.     bs_lst[4] = s_lst[4] = int(h5tf.text)            #set hole5 score
  120.     bs_lst[5] = s_lst[5] = int(h6tf.text)            #set hole6 score
  121.     bs_lst[6] = s_lst[6] = int(h7tf.text)            #set hole7 score5
  122.     bs_lst[7] = s_lst[7] = int(h8tf.text)            #set hole8 score
  123.     bs_lst[8] = s_lst[8] = int(h9tf.text)            #set hole9 score
  124.     bs_lst[9] = s_lst[9] = int(h10tf.text)        #set hole10 score
  125.     bs_lst[10] = s_lst[10] = int(h11tf.text)        #set hole11 score
  126.     bs_lst[11] = s_lst[11] = int(h12tf.text)        #set hole12 score
  127.     bs_lst[12] = s_lst[12] = int(h13tf.text)        #set hole13 score
  128.     bs_lst[13] = s_lst[13] = int(h14tf.text)        #set hole14 score
  129.     bs_lst[14] = s_lst[14] = int(h15tf.text)        #set hole15 score
  130.     bs_lst[15] = s_lst[15] = int(h16tf.text)        #set hole16 score
  131.     bs_lst[16] = s_lst[16] = int(h17tf.text)        #set hole17 score
  132.     bs_lst[17] = s_lst[17] = int(h18tf.text)        #set hole18 score
  133.     hc = calcHcap(s_lst)
  134.     makeAddData(hc)                                        #make new data to newdata
  135.     lstdata = ui.ListDataSource(“”)        #clear list
  136.     lstdata.items = sc_lst                        #set items of score list
  137.     hc_tv.data_source = lstdata                #set source
  138.     hc_tv.reload_data()                                #display data
  139.     wdata[0] = str(r_num)
  140.     for i in range(0,18):
  141.         wdata[i+1] = sc_lst[r_num-1][i+1]
  142.     wdata[19] = str(hc)
  143.     print(“write data=”,wdata)
  144.     writeData(wdata)
  145. def writeData(wd):
  146.     global r_num
  147.     fw = open(‘g_hcap.csv’,’a’)
  148.     wp = csv.writer(fw,lineterminator=’\n’)
  149.     wp.writerow(wd)
  150.     fw.close()
  151. def readCSV():
  152.     global r_num
  153.     fr = open(“g_hcap.csv”)                #open file
  154.     rp = csv.reader(fr)                            #for read csv
  155.     for rd in rp:
  156.         print(“csv=”,rd)            
  157.         r_num += 1                                        #increment round num.    
  158.         if r_num > R_MAX:                        #check Max num.(for mem.)
  159.             print(“Over num. of round!”)
  160.             r_num = R_MAX-1
  161.             break
  162.         s0_lst[0] = int(rd[0])            #set round num.
  163.         s_lst[0] = s0_lst[1] = int(rd[1])                        #set hole1 score
  164.         s_lst[1] = s0_lst[2] = int(rd[2])                        #set hole2 score
  165.         s_lst[2] = s0_lst[3] = int(rd[3])                        #set hole3 score
  166.         s_lst[3] = s0_lst[4] = int(rd[4])                        #set hole4 score
  167.         s_lst[4] = s0_lst[5] = int(rd[5])                        #set hole5 score
  168.         s_lst[5] = s0_lst[6] = int(rd[6])                        #set hole6 score        
  169.         s_lst[6] = s0_lst[7] = int(rd[7])                        #set hole7 score
  170.         s_lst[7] = s0_lst[8] = int(rd[8])                        #set hole8 score
  171.         s_lst[8] = s0_lst[9] = int(rd[9])                        #set hole9 score
  172.         s_lst[9] = s0_lst[10] = int(rd[10])                        #set hole10 score        
  173.         s_lst[10] = s0_lst[11] = int(rd[11])                        #set hole11 score
  174.         s_lst[11] = s0_lst[12] = int(rd[12])                        #set hole12 score
  175.         s_lst[12] = s0_lst[13] = int(rd[13])                        #set hole13 score
  176.         s_lst[13] = s0_lst[14] = int(rd[14])                        #set hole14 score
  177.         s_lst[14] = s0_lst[15] = int(rd[15])                        #set hole15 score
  178.         s_lst[15] = s0_lst[16] = int(rd[16])                        #set hole16 score
  179.         s_lst[16] = s0_lst[17] = int(rd[17])                        #set hole17 score
  180.         s_lst[17] = s0_lst[18] = int(rd[18])                        #set hole18 score
  181.         hc_lst[r_num] = s0_lst[19] = int(rd[19])                #set handicap
  182.         for i in range(0,20):
  183.             sc_lst[r_num][i] = s0_lst[i]
  184.     r_num += 1        #set round num. (is not zero)
  185.     fr.close()        #close csv
  186. v = ui.load_view()
  187. v.present(‘sheet’)
  188. h1tf = v[‘textfield1’]            #text field1 for hole1 score
  189. h2tf = v[‘textfield2’]            #text field1 for hole2 score
  190. h3tf = v[‘textfield3’]            #text field1 for hole3 score
  191. h4tf = v[‘textfield4’]            #text field1 for hole4 score
  192. h5tf = v[‘textfield5’]            #text field1 for hole5 score
  193. h6tf = v[‘textfield6’]            #text field1 for hole6 score
  194. h7tf = v[‘textfield7’]            #text field1 for hole7 score
  195. h8tf = v[‘textfield8’]            #text field1 for hole8 score
  196. h9tf = v[‘textfield9’]            #text field1 for hole9 score
  197. h10tf = v[‘textfield10’]        #text field1 for hole10 score
  198. h11tf = v[‘textfield11’]        #text field1 for hole11 score
  199. h12tf = v[‘textfield12’]        #text field1 for hole12 score
  200. h13tf = v[‘textfield13’]        #text field1 for hole13 score
  201. h14tf = v[‘textfield14’]        #text field1 for hole14 score
  202. h15tf = v[‘textfield15’]        #text field1 for hole15 score
  203. h16tf = v[‘textfield16’]        #text field1 for hole16 score
  204. h17tf = v[‘textfield17’]        #text field1 for hole17 score
  205. h18tf = v[‘textfield18’]        #text field1 for hole18 score
  206. hc_tv = v[‘tableview1’]            #table_view1 for display game_num
  207. r_num = (-1)
  208. set_testdata()
  209. readCSV()


コメントを残す

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

Golf

前の記事

Calc. Handicap of Golf-2
WordPress

次の記事

Google Search Console error