Program of Calc. Bowling score update

1.プログラム更新

ボウリングスコア算出プログラムを更新しました。

前回までの機能に加え、ハイスコアトップ3を表示するようにしました。

前回までのプログラムは下記リンクを参照願います。

ゲーム数が多くなってきたので、ハイスコアは覚えていますが、その次とかは忘れてしまうし、探すのも大変なので、プログラムを改訂しました。

2.ソースコード

ソースコードは下記の通りです。

コメントがずれているところがありますがご了承ください。

今回追加したコードはイタリック体にしました。

注意)

ハイスコアベスト3を検出する処理はデバッグ不十分で、新しいデータ入力時、正しく動作するか否かは今後確認します。

もし不具合が見つかった場合は適宜修正する予定です。

  1. #A program that calculates the average score of bowling games
  2. #Input_file name is “b_score2.csv” (csv_file)
  3. #Don’t add new_line to final_data.
  4. import ui
  5. import csv
  6. new_score = 0
  7. t_sc = 0                    #total score
  8. a_sc = 0                    #average score
  9. g_num = 0                    #number of games
  10. G_MAX = 500                #Max. game nums
  11. gn_lst = [0] * G_MAX            #game num. list
  12. sc_lst = [0] * G_MAX            #score list
  13. ts_lst = [0] * G_MAX            #total score 
  14. av_lst = [0] * G_MAX            #average list
  15. sp_lst = [0] * G_MAX            #spare num. list
  16. st_lst = [0] * G_MAX            #strike num. list
  17. spr_lst = [0] * G_MAX            #spare ratio list
  18. str_lst = [0] * G_MAX            #strike ratio list
  19. top1 = 0                    #Top score = Highest
  20. top2 = 0                    #Second score
  21. top3 = 0                    #3rd score
  22. ret = [(”,”,”,”)] * G_MAX
  23. def initData():
  24.     for i in range(0,G_MAX):
  25.         spr_lst[i] = 0
  26.         str_lst[i] = 0
  27. def checkHighScore(sc):
  28.     global top1,top2,top3
  29.     if top3 < sc:
  30.         if top2 < sc:
  31.             if top1 < sc:
  32.                 top1 = sc
  33.             else:
  34.                 top2 = sc
  35.         else:
  36.             top3 = sc    
  37. def displayHighScore():
  38.     top1_tf.text = str(top1)
  39.     top2_tf.text = str(top2)
  40.     top3_tf.text = str(top3)
  41. def readData():
  42.     global g_num
  43.     global ret
  44.     fr = open(“b_score2.csv”)                #open file
  45.     rp = csv.reader(fr)                            #for read csv
  46.     for rd in rp:
  47.         print(rd)    
  48.         if rd == []:
  49.             break    
  50.         else:
  51.             if g_num > G_MAX:                        #check Max num.(for mem.)
  52.                 break
  53.             print(rd[0], rd[1], rd[2],rd[3])
  54.             ret[(g_num)*4] = rd[0]            #for check
  55.             ret[(g_num)*4+1] = rd[1]        #for check
  56.             ret[(g_num)*4+2] = rd[2]
  57.             ret[(g_num)*4+3] = rd[3]
  58.             gn_lst[g_num] = rd[0]                #set game num. list
  59.             sc_lst[g_num] = rd[1]                #set score list
  60.             sp_lst[g_num] = rd[2]                #set spare list
  61.             st_lst[g_num] = rd[3]                #set strike list
  62.             g_num += 1
  63.             checkHighScore(int(rd[1]))
  64.     fr.close()
  65.     return ret
  66. def renewTableView(tv,lst):
  67.     lstdata = ui.ListDataSource(“”)        #clear list
  68.     tmp_lst = [‘0’] * (g_num)                    #malloc table
  69.     for i in range(0,g_num):                    #include title
  70.         tmp_lst[i] = lst[i]                            #data copy
  71.     lstdata.items = tmp_lst                        #set items of game number list
  72.     tv.data_source = lstdata                    #set source
  73.     tv.reload_data()                                    #display data
  74. #Display game num. and score
  75. def displayGameAndScore():
  76.     #Game Number
  77.     renewTableView(gn_tv, gn_lst)
  78.     renewTableView(sc_tv, sc_lst)
  79. #Display total score and average
  80. def displayAverage():
  81.     #calc. total_score and Average
  82.     for i in range(0,g_num):                #to LastData
  83.         if i == 0:                                        #first data
  84.             ts_lst[0] = sc_lst[0]
  85.             av_lst[0] = sc_lst[0]                #same top data
  86.         else:                                                    #2nd.—
  87.             ts_lst[i] = str(int(ts_lst[i-1]) + int(sc_lst[i]))
  88.             av_lst[i] = str(round(int(ts_lst[i]) / (i+1)))
  89.     #Total Score to tableview
  90.     renewTableView(total_tv, ts_lst)
  91.     #Average to tableview
  92.     renewTableView(ave_tv, av_lst)
  93.     print(“game_num=”,g_num)                    #for check
  94. def displaySpareAndStrike():
  95.     renewTableView(sp_tv, sp_lst)
  96.     renewTableView(st_tv, st_lst)
  97. def displaySpareAndStrikeRatio():
  98.     global g_num
  99.     #calc. Ratio
  100.     tsp = 0
  101.     tst = 0
  102.     for i in range(0,g_num):                #to LastData
  103.         if i == 0:                                        #first data
  104.             spr_lst[0] = int(sp_lst[0])*10        #num/10*100=num*10(%)
  105.             str_lst[0] = int(st_lst[0])*10        #num/10*100=num*10(%)
  106.             tsp = int(sp_lst[0])
  107.             tst = int(st_lst[0])
  108.         else:                                                    #2nd.—
  109.             spr_lst[i] = str(round((tsp + int(sp_lst[i]))*10/(i+1)))
  110.             str_lst[i] = str(round((tst + int(st_lst[i]))*10/(i+1)))
  111.             tsp += int(sp_lst[i])
  112.             tst += int(st_lst[i])
  113.     #SpareRatio to tableview
  114.     renewTableView(spr_tv, spr_lst)
  115.     #StrikeRatio to tableview
  116.     renewTableView(str_tv, str_lst)    
  117. def writeData(nd, sp, st):
  118.     global g_num
  119.     print(“g_num before write=”, g_num)
  120.     fw = open(‘b_score2.csv’,’a’)
  121.     wp = csv.writer(fw,lineterminator=’\n’)
  122.     ad_data = [”,”,”,”]                                    #append data
  123.     gn_lst[g_num] = ad_data[0] = str(g_num+1)
  124.     sc_lst[g_num] = ad_data[1] = nd
  125.     sp_lst[g_num] = ad_data[2] = sp
  126.     st_lst[g_num] = ad_data[3] = st
  127.     ts_lst[g_num] = str(int(ts_lst[g_num-1]) + int(sc_lst[g_num]))
  128.     av_lst[g_num] = str(round(int(ts_lst[g_num]) / (g_num+1)))
  129.     g_num += 1                #increment here. because for display 0 to g_num-1
  130.     renewTableView(gn_tv, gn_lst)
  131.     renewTableView(sc_tv, sc_lst)
  132.     renewTableView(total_tv, ts_lst)
  133.     renewTableView(ave_tv, av_lst)
  134.     displaySpareAndStrike()
  135.     displaySpareAndStrikeRatio()
  136.     displayHighScore()
  137.     print(“game num after write=”,g_num)                                        #for check
  138.     print(“add data=”, ad_data[0],ad_data[1],ad_data[2],ad_data[3])        #for check
  139.     wp.writerow(ad_data)
  140.     fw.close()
  141. #Add new data proc.
  142. def addData(sender):
  143.     res = 0                                        #input data is OK
  144.     nd = nd_tf.text
  145.     if nd == ”:                            #check blank
  146.         print(“Input new score!”)
  147.         res = -10
  148.     else:
  149.         if int(nd) == 0:                    #check zero
  150.             print(“Input-score is 0!”)
  151.             res = -20
  152.         else:
  153.             if int(nd) > 300:                    #check over 300
  154.                 print(“Input score is over 300”)
  155.                 res = -30
  156.     checkHighScore(int(nd))    
  157.     sp = nsp_tf.text
  158.     if sp == ”:                            #check blank
  159.         print(“Input new spareNum!”)
  160.         res = -110
  161.     else:
  162.         if int(sp) > 10:                    #check over 10
  163.             print(“Input spareNum is over 10”)
  164.             res = -130
  165.     st = nst_tf.text
  166.     if st == ”:                            #check blank
  167.         print(“Input new strikeNum!”)
  168.         res = -210
  169.     else:
  170.         if int(st) > 12:                    #check over 300
  171.             print(“Input strikeNum is over 12”)
  172.             res = -230
  173.     print(nd, sp, st)
  174.     if res == 0:
  175.         writeData(nd,sp,st)                            #display new data and write to file
  176. v = ui.load_view()
  177. v.present(‘sheet’)
  178. nd_tf = v[‘textfield1’]            #text field1 for new score
  179. nsp_tf = v[‘textfield2’]            #text field1 for new spareNum.
  180. nst_tf = v[‘textfield3’]            #text field1 for new strikeNum.
  181. top1_tf = v[‘textfield4’]            #text field1 for TOP1 score
  182. top2_tf = v[‘textfield5’]            #text field1 for TOP2 score
  183. top3_tf = v[‘textfield6’]            #text field1 for TOP3 score
  184. gn_tv = v[‘tableview1’]            #table_view1 for display game_num
  185. sc_tv = v[‘tableview2’]            #table_view1 for display score
  186. sp_tv = v[‘tableview3’]            #table_view1 for display spare
  187. st_tv = v[‘tableview4’]            #table_view1 for display strike
  188. total_tv = v[‘tableview5’]    #table_view1 for display total score
  189. ave_tv = v[‘tableview6’]        #table_view1 for display average
  190. spr_tv = v[‘tableview7’]            #table_view1 for display spare ratio
  191. str_tv = v[‘tableview8’]            #table_view1 for display strike ratio
  192. initData()
  193. r_data = readData()                    #read csv-file and set data
  194. print(r_data)
  195. displayGameAndScore()                #display game-score to tableview
  196. displayAverage()                        #display total-score and average to tableview
  197. displaySpareAndStrike()            #display num. and % of spare and strike
  198. displaySpareAndStrikeRatio()
  199. displayHighScore()

3.画面(UI)

画面には、ハイスコア表示用のテキストフィールドを追加しました。

4.実行結果

実行結果は以下の通りです。

※自動スクロールはしないので、最新のデータ部を見るには各列を手動でスクロールする必要があります。

5.開発環境

下記だけです。

(1)iPad Air (第3世代)

(2)Pythonista


コメントを残す

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

Bowling

前の記事

Bowling score update 3/1
Golf

次の記事

Golf practice after a long absence