iT邦幫忙

DAY 28
0

哇咧~夠了(Oracle SQL)系列 第 25

廿八、用SQL Plus 設計個小遊戲之8-保存Player回覆(Oracle SQL 2013/10/13)

今天寫這段(員工旅遊Day2,終於回到家)
update_result(v_tmp); --將結果更新到最後一次的猜測,供後續檢測用
腦袋一片空白。
先用pl/sql, 再抽空改成package

declare
   v_tmp  varchar2(30);
   v_gsa  varchar2 := substrb(v_tmp, 1, 1);  --xA
   v_gsb  varchar2 := substrb(v_tmp, 2, 1);  --xB

   v_seq_id  number;
begin
   select max(seq_id)   --取得最後的回合數
     into v_seq_id
     from yafuu_guess_history;
   
   update yafuu_guess_history  --將Player回復結果更新回資料庫
      set gs0a = to_number(v_gsa),
          gs0b = to_number(v_gsb)
    where seq_id = v_seq_id;

end;

完成這部分,就只剩下主程式了,這裡還要用到陣列概念,先POST文,

漏掉一段
ELSE --Not yet finish, keep guess
V_MSG := GET_NEXT_GUESS; --Get Next Guess

這裡的一段SQL會去變更126組的樣本,其中不吻合資格者,update gs_flag = 0 means NG

以這個例子
(1)猜1234,結果是0A2B
(2)猜5678,結果是0A2B
(3)猜2187,結果是1A1B
(4)猜2845,結果是0A1B
(5)猜6417,結果是0A2B。
(6)猜7153,結果是4A0B。
當1234=2B時,去把126組裡面,重新比對過,如果A+B數字不同於2,則屬淘汰樣本。
所以會留下1278,1378,3489這類的樣本數60個。

declare
   cursor c_his is
      select seq_id, gs01, gs02, gs03, gs04
        from yafuu_guess_array
       where gs_flag = 1;
   cursor d_samp is
      select seq_id, gs01, gs02, gs03, gs04, gs0a, gs0b
        from yafuu_guess_history
       order by seq_i;
   v_ga  number;
   v_gb  number;
   v_ab varcahr2(10);  --紀錄篩選用
begin
--這裡的比對可以另一個函數?Translate,但沒空仔細研究了。
   for sam in d_samp loop  --所有有效樣本庫
      for hist in c_his loop  --拿樣本庫比對歷史紀錄,失敗者註銷樣本庫資格
         --先計算A數量
         if hist.gs01 = sam.gs01 then
            v_ga := v_ga + 1;
         end if;
         if hist.gs02 = sam.gs02 then
            v_ga := v_ga + 1;
         end if;
         if hist.gs03 = sam.gs03 then
            v_ga := v_ga + 1;
         end if;
         if hist.gs04 = sam.gs04 then
            v_ga := v_ga + 1;
         end if;
         --計算B數量
         if hist.gs01 = sam.gs02 or hist.gs01 = sam.gs03 or hist.gs01 = sam.gs04 then
            v_gb := v_gb + 1;
         end if;
         if hist.gs02 = sam.gs01 or hist.gs02 = sam.gs03 or hist.gs02 = sam.gs04 then
            v_gb := v_gb + 1;
         end if;
         if hist.gs03 = sam.gs01 or hist.gs03 = sam.gs02 or hist.gs03 = sam.gs03 then
            v_gb := v_gb + 1;
         end if;
         if hist.gs04 = sam.gs01 or hist.gs01 = sam.gs02 or hist.gs01 = sam.gs03 then
            v_gb := v_gb + 1;
         end if;
         if to_number(v_ga) + to_number(v_gb) <> to_number(hist.ga01) + to_number(hist.ga01) then
            v_ab = 'NG';
            exit;
         end if;
      end loop;       
      if v_ab = 'NG' then  --比對不出,已經喪失資格。
         update yafuu_guess_array
            set gs_flag = 0
          where seq_id = sam.seq_id;
      end if;
   end loop;

--所有樣本庫全部有效
update yafuu_guess_array
set gs_flag = 1;


上一篇
廿七、用SQL Plus 設計個小遊戲之7-我猜的演算概念((Oracle SQL 2013/10/12)
下一篇
廿九、用SQL Plus 設計個小遊戲之9-小小驗證(Oracle SQL 2013/10/14)
系列文
哇咧~夠了(Oracle SQL)28
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言