CSE 111, Fall 2000

HW #6 ANSWER

NEW Corrected Version NEW

Here are 2 versions of the program: one with no extraneous comments, which is shorter and easier to see at a glance; and one with comments, which is longer, but might be easier to understand:


Short, uncommented version:
program Nim5;

{Bill Rapaport, 11 Oct 00                            }
{CSE 111, Fall 2000                                  }
{Answer to virtual HW #6                             }
{http://www.cse.buffalo.edu/~rapaport/111F00/hw6.html}

var your1stmove,
    your2ndmove  : varying [20] of char;

begin {Nim5}

  writeln('Your move!');
  readln(your1stmove);
  if your1stmove = 'X'
     then begin {if your1stmove = 'X'}

            writeln('I play O;');
            writeln('your move');
            readln(your2ndmove);
            if your2ndmove = 'X'
               then begin {if your2ndmove = 'X'}

                      writeln('I play OO;');
                      writeln('  I win!')

                    end   {if your2ndmove = 'X'}

               else begin {if your2ndmove <> 'X'}

                      writeln('I play O;');
                      writeln('  I win!')

                    end   {if your2ndmove <> 'X'}

          end   {if your1stmove = 'X'}

     else begin {if your1stmove <> 'X'}

            writeln('I play OO;');
            writeln('your move');
            readln(your2ndmove);
            if your2ndmove = 'X'
               then writeln('You win!')
               else writeln('That''s illegal!')

          end   {if your1stmove <> 'X'}

end.  {Nim5}


Long, commented version:
program Nim5; {Can't be called "5-Nim", because program names are   }
              {identifiers, and identifiers must begin with letters,}
              {not numerals, and cannot have "-" in them.           }

{Bill Rapaport, 11 Oct 00                            }
{CSE 111, Fall 2000                                  }
{Answer to virtual HW #6                             }
{http://www.cse.buffalo.edu/~rapaport/111F00/hw6.html}

var your1stmove,
    your2ndmove  : varying [20] of char;

begin {Nim5}

  {this is the code for decision tree "A"}

  writeln('Your move!');
  readln(your1stmove);
  if your1stmove = 'X'
     then begin {if your1stmove = 'X'}

            {this is the code for decision tree "B"}

            writeln('I play O;');
            writeln('your move');
            readln(your2ndmove);
            if your2ndmove = 'X'
               then begin {if your2ndmove = 'X'}

                      {this is the code for decision tree "D"}

                      writeln('I play OO;');
                      writeln('  I win!')

                    end   {if your2ndmove = 'X'}

               else begin {if your2ndmove <> 'X'}

                      {this is the code for decision tree "E"}

                      writeln('I play O;');
                      writeln('  I win!')

                    end   {if your2ndmove <> 'X'}

          end   {if your1stmove = 'X'}

     else begin {if your1stmove <> 'X'}

            {this is the code for decision tree "C"}

            writeln('I play OO;');
            writeln('your move');
            readln(your2ndmove);
            if your2ndmove = 'X'
               then begin {if your2ndmove = 'X'}

                      {this is the code for decision tree "F"}

                      writeln('You win!')

                    end   {if your2ndmove = 'X'}
                    {Note:  Strictly speaking, the begin/end pair }
                    {       surrounding this single "writeln"     }
                    {instruction is not needed.                   }

               else begin {if your2ndmove <> 'X'}

                      {this is the code for decision tree "G"}

                      writeln('That''s illegal!')

                    end   {if your2ndmove <> 'X'}
                    {Note:  Strictly speaking, the begin/end pair }
                    {       surrounding this single "writeln"     }
                    {instruction is not needed.                   }

                    {Further note:  Look carefully at how Pascal  }
                    {       requires you to print an apostrophe!  }

          end   {if your1stmove <> 'X'}

end.  {Nim5}



Copyright © 2000 by William J. Rapaport (rapaport@cse.buffalo.edu)
file: 111F00/file.date.html