Friday, 6 August 2010

flashcards.m

use iouse filesuse uiclass Card  front  back  // f is an open file  function load(file)    s = io.readln(file);    i = index(s, "@");                     front = substr(s, 0, i-1);                     back = substr(s, i+1);                   end  // f is an open file  function save(file);                     io.writeln(file, front + "@" + back);                   end  // f is an open file  function init(file)    load(file)  endendclass Cards  cards  setname  function filename()    return setname +".cards";                   end  function get(i)    return cards[i];                   end  function add(c)    append(cards, c);                   end  // f is an open file  function load()    f = io.open(filename());                     while 0 < io.avail(f) do      c:Card = Card(f);                       add(c);                     end  end  function init(setname)    this.setname = setname;                     cards = [];                     load();                   end  // f is an open file  function save(f)    c:Card = null;    for c in cards do      c.save(f);                     end  end    endclass Drawers  drawers  setname  function count()    return len(drawers);  end  function filename()    return setname +".drawers";                   end  function init(setname)    this.setname = setname;                     drawers = [];                     f = io.open(filename());                     do      s = io.readln(f);                       if s # null then        append(drawers, num(s));                       end    until s = null;                   end  function save()    f = io.open(filename());                     for n in drawers do        io.writeln(f, n);                end;    io.close(f);                   end  function move(card, drawer)    drawers[card] = drawer;                   endend    class Flash_Set  setname  cards:Cards  drawers:Drawers  // Load from disk if possible.  function init(name)    setname = name;                     cards = Cards(name);                     drawers = Drawers(name);                   endendclass Show  currentcard  currentdrawer   flashset:Flash_Set    function init(set)    currentcard = -1;                     flashset = set;                     currentdrawer = 1;                   end  function getnextindrawer(drawer)    while currentcard+1 < flashset.drawers.count() do      currentcard += 1;                       d = flashset.drawers[currentcard];                       if d = drawer then        return currentcard;                       end    end;    return -1;                   end  function getnextindrawers(first, last)    for i = first to last do      c = getnextindrawer(i);                       if c#-1 then        currentdrawer = i;                         return c;                       end    end  end  function getnext()        c = getnextindrawer(currentdrawer);    if c#-1 then      return c;                     end;    return getnextindrawers(1, 5);    end  function shownext()    currentcard = getnext();                     c:Card = flashset.cards[currentcard];                     print c.front;                     // wait for user to click a key, don't care what.    k=ui.cmd();     // show the back of the card;                     print c.back;                     // wait for user to respond    k=ui.cmd();    correct = (k = 63497); // if known    if correct then      drawer = flashset.drawers[currentcard];                       if drawer < 5 then        flashset.drawers[currentcard] = drawer + 1;                       end;    else      flashset.drawers[currentcard] = 1;                     end;  end;end// mainui.keys(ui.strokes); // return keystrokesf:Flash_Set = Flash_Set("flashcards");                 s:Show = Show(f);                 while true do  s.shownext();                 end

Posted via email from kwhitefoot's posterous

No comments:

Post a Comment

Followers