CSE 111, Fall 00, HW #6

CSE 111, Fall 2000

#### Virtual HW #6: CODING DECISION TREES IN PASCAL

### Corrected Version

Write (and test) a complete Pascal program that codes the following decision tree for 5-Nim:

                                  D--------------
                                   | I play OO; |
                                   |   I win!   |
                                   --------------
                                  /
                               X /
                                /
                 B-------------/
                  | I play O; |
                  | your move |
                  -------------\
                 /              \
              X /             XX \
               /                  E-------------
    A---------/                    | I play O; | <--NOTE CORRECTION
     | Your  |                     |   I win!  |
     | move! |                     -------------
     ---------\
               \                  F-------------
             XX \                 /|  You win! |
                 \             X / -------------
                 C--------------/
                  | I play OO; |
                  | your move  |
                  --------------\
                                 \
                               XX \
                                  G------------
                                   | That's   |
                                   | illegal! |
                                   ------------

Hint: Here's the top-level design (= the code for decision tree "A") to get you started (recall from the online lecture notes that "<>" is Pascal's way of saying "is not equal to"):

    program Nim5; {Question to think about:}
                  {why can't I call this   }
                  {"5-Nim" ?               }

    var yourmove : varying [20] of char;

    begin {Nim5}

      writeln('Your move!');
      readln(yourmove);
      if yourmove = 'X'
         then begin {if yourmove = 'X'}
                {put code for decision tree "B" here}
              end   {if yourmove = 'X'}
         else begin {if yourmove <> 'X'}
                {put code for decision tree "C" here}
              end   {if yourmove <> 'X'}

    end.  {Nim5}

Further hint: The code for decision trees "B" and "C" will look almost exactly like the code for decision tree "A": It will be a sequence (or "compound statement") consisting of a "writeln" instruction, followed by a "readln" instruction, followed by an "if-then-else" instruction.

NOT TO BE HANDED IN; DO THIS TONIGHT! ANSWER WILL BE POSTED TO NEWSGROUP TOMORROW