CS 106 Winter 2018

Assignment 01: Arrays and Strings



Question 1 Toads and Frogs

Toads and Frogs is an abstract game, used by mathematicians to study the properties of games in general. Though it's easy to describe, it leads to some surprisingly complex mathematical ideas. Here we won't get into the math, but we'll develop a Processing sketch that allows two people to play Toads and Frogs interactively.

The game is played in a row of squares, where each square is either empty, or contains one toad or one frog. For example, let's consider a nine-square board, with three toads starting in the first three squares and three frogs starting in the last three. Toads move to the right, and frogs move to the left. The initial board might look like this:



Two players take turns making moves, with toads moving first. There are two legal moves:

For example, the first row in the diagram below shows a small game in progres. From Step 1, one player hops his toad over his opponent's frog to get to the board in Step 2. The other player responds by stepping her frog one square forward, arriving at the board shown in Step 3.



The game ends when one of the two players has no legal moves left, at which point the other player (i.e., the last to move) wins.

Write a Processing sketch called ToadsAndFrogs to play this game. You must support at least the following features:

That's it. Note that you don't need to detect when the game is over. At some point, one of the players will simply have no legal moves left, and the game will stop responding to clicks. To start a new game, quit and restart the sketch.

If you're not sure where to begin, here's a suggested plan of attack for implementing the sketch.

  1. At the top of the sketch, declare an array of integers as a global variable, to hold the board. Declare a second integer global variable to keep track of whose turn it is. In the array, use the convention that +1 represents a toad, -1 represents a frog, and 0 represents an empty square. Similarly, use +1 and -1 for the turn variable to keep track of whether it's toad's turn or frog's turn.
  2. Write a setup() function to initialize the sketch. Set the size of the sketch to be large enough to hold the board. Initialize the global variables as needed (you can also initialize them as part of declaring them, in which case setup() can be very short).
  3. Write a draw() function. The function should use a loop to iterate over the board array, drawing every square. When drawing a square, check if it has a frog or toad in it. If so, draw some kind of marker for that piece. As mentioned above, you can use whatever graphics you want, as long as you can tell toads apart from frogs.
  4. Write a mousePressed() function to respond to clicks. The function must first detect which square was clicked. (Hint: if the sketch window is the exact size of the board, then every click is inside some square. You can detect the array index of the clicked square with a single line of code, by dividing mouseX by the square's side length.) Determine whether that square corresponds to a legal move for the player whose turn it is. If so, execute that move by modifying the array and setting the turn variable to the other player. If the click wasn't a legal move, do nothing.

You are invited to explore enhancements to this basic sketch to create a more fully featured game, as long as the core requirements above are supported. We will award bonus marks for attractive, creative, or interesting enhancements. Just include a comment at the top of the sketch to tell us what to look for. Here are some suggestions:

Place your ToadsAndFrogs sketch inside a folder titled A01.


Question 2 This Is Just To Say



This Is Just To Say was composed in 1934 by the great imagist poet William Carlos Williams. A bit more recently, Mark Sample created the Twitter bot @JustToSayBot, a kind of "Mad Libs" mash-up of the poem with random words substituted in strategic locations. In this question, you will use the RiTa library, introduced in Lab 01, to reproduce the bot.

  1. If you haven't already done so, install the RiTa library by following Lab 01, Question 2, Step 2. Create a new sketch called JustToSay. As you did in the lab, include an import statement at the top, after your name and ID.
  2. Write a setup() function. Set the size of the sketch window to 300×500. After that, include the line textSize( 22 ); to make text drawn in the sketch window larger than the default.
  3. Now write a draw() function. Set the background colour of the sketch and choose a text colour (via fill()). You can choose whatever colours you want, as long as they go well together. Add the line noLoop(); at the end of draw().
  4. In draw(), between the colours and the call to noLoop(), add code to generate a random variation of the poem, and display it in the sketch window. Use the following template, filling in the blanks with the required parts of the speech and including the blank lines between stanzas (click to download a PDF):


    Use the function RiTa.randomWord() to fill in the blanks above. You pass the function a code indicating what part-of-speech you want a word for, and it picks one for you. The codes you need are "nn" for noun, "vbg" for a verb ending in "ing", "vb" for a regular verb, and "jj" for an adjective. To obtain a plural noun, use the function RiTa.pluralize(), passing it a singular noun as an argument. (There's a way to generate plural nouns directly, but it appears to be tragically broken at the moment.)
  5. Finally, add a keyPressed() function that simply calls loop(). The idea is that we draw a single poem to the screen and then stop looping. By pressing a key, you instruct Processing to restart the program loop, which causes it to go through another round of draw(), displaying a new poem and then pausing again. The result is that you can press any key to move to a new poem.

There are a few different ways to generate and draw the poem above. The easiest way is probably to define a variable of type String that holds the complete poem, including "\n" to indicate line breaks and using the + operator to glue in random words when they're needed. If you have one long string with line breaks, you can draw it to the sketch window with a single call to text(). Make sure that every word is chosen separately from the others—your poem should not re-use the same adjective three times!

Put your solution in a sketch titled JustToSay in your A01 folder.

Submission

When you are ready to submit, please follow these steps.

  1. Please ensure that any sketches you submit compile and run. It's better to submit a sketch that runs smoothly but implements fewer required features than one that has broken code for all features. If you get partway into a feature but can't make it work, comment it out so that the sketch works correctly without it.

  2. If necessary, review the Code Style Guide and use Processing's built-in auto format tool. You do not need to use the precise coding style outlined in the guide, but whatever style you use, your code must be clear, concise, consistent, and commented.

  3. If necessary, review the How To Submit document for a reminder on how to submit to LEARN.

  4. Make sure to include a comment at the top of all source files containing your name and student ID number.

  5. Create a zip file called A01.zip containing the entire A01 with its subfolders ToadsAndFrogs and JustToSay.

  6. Upload A01.zip to LEARN. Remember that you can (and should!) submit as many times as you like. That way, if there's a catastrophe, you and the course staff will still have access to a recent version of your code.

  7. If LEARN isn't working, and only if LEARN isn't working, please email your ZIP file to the course account (see the course home page for the address). In this case, you must mail your ZIP file before the deadline. Please use this only for emergencies, not "just in case". Submissions received after the deadline may receive feedback, but their marks will not count.