Luet oppimateriaalin englanninkielistä versiota. Mainitsit kuitenkin taustakyselyssä
osaavasi suomea. Siksi suosittelemme, että käytät suomenkielistä versiota, joka on
testatumpi ja hieman laajempi ja muutenkin mukava.
Suomenkielinen materiaali kyllä esittelee englanninkielisetkin termit.
Kieli vaihtuu A+:n sivujen yläreunan painikkeesta. Tai tästä: Vaihda suomeksi.
About This Page
Questions Answered: How can a program operate on other programs?
How might we make virtual robots act in collaboration?
Topics: Another programming language as part of a computer
program; “virtual machines”. The stack collection type; implementing
a simple call stack. Additional practice on earlier topics.
What Will I Do? Study and extend a given program.
Rough Estimate of Workload:? Four hours? Five? Six? There
actually isn’t all that much code to write, but it will take time to
understand the program.
Points Available: C5 — there’s just a handful of points available
for some multiple-choice questions that introduce the theme of the
actual programming assignment. (For most O1 students, this chapter’s
contents and assignments are completely optional. However, if you
are taking Programming Studio 1 in parallel with O1,
do note that these assignments are an official part of that course.)
Related Modules: RobotTribes (new), which depends on Robots.
Two robot tribes in mid-fight. The “Guardians” have settled in a
compact cluster. The “Tigers” are roaming for prey.
In this chapter, we’ll again pick up the theme of virtual robots and design some “robot
tribes” that do battle with each other.
Each robot tribe has its own program code, which specifies how the tribe’s members
behave. Each tribe’s code is written in a separate text file, which is not in Scala but
in a custom programming language called RoboSpeak. A RoboSpeak program consists of simple
commands that instruct the robots. For instance, the short RoboSpeak program below orders
robots to keep walking clockwise in a square pattern:
move # Moves the robot forward by one square.
spin # Turns the robot clockwise.
goto 1 # Returns to line 1 of this program.
The Scala app in module RobotTribes reads in RoboSpeak programs from text files, interprets
the RoboSpeak instructions therein, stores the instructions in memory as Scala objects, and
directs tribal robots accordingly. You can define new tribes simply by writing new files
of RoboSpeak; you don’t need to touch the Scala code for that.
In addition to following their tribe’s RoboSpeak program, tribal robots are always looking
to “hack” the members of other tribes, causing those competitors to join their tribe. As a
consequence, if you put two tribes in the same robot world, they’ll end up fighting for
survival as each tribe attempts to gain more members by converting the other. Once a tribe
gets the upper hand, the weaker tribe is often eliminated completely, as even its final
members defect to the winning side. You’ll see some examples of such tribal duels later.
This chapter revolves around the RobotTribes module. Once again, we’re going to give you
a program that almost works but is missing some crucial pieces. The chapter comprises a
programming assignment in several parts, in which you’ll add those pieces.
Let’s start with an overview.
About the Robots module
The RobotTribes module depends on the Robots module of Chapters 8.1,
8.2, and 8.3. If you don’t know it from Week 8, go back there and
read up on the module. You’ll need it here.
If you didn’t do the earlier assignments on Robots, do them now
or use the example solutions.
RobotTribes consists of two packages:
The table below lists the main contents of o1.robots.tribal.
Now’s a good time to try out some RoboSpeak programs.
A+ presents the exercise submission form here.
Return to class Tribe’s Scaladocs
of class Tribe and read the sections Using Memory Slots,
Radar Commands, and Hacking and Talking.
Implement determineTribe in class TribalBot. The class’s other
methods need this method in order to tell friend from foe.
Use determineTribe and other ingredients
from the TribalBot class.
Once you’ve implemented this method, the
robots should stop when they see an enemy
but they still don’t do anything to the
You can now try talk, enemiesnear, friendsnear, foddernear, fodderleft, score,
friendsdir, and enemiesdir in your RoboSpeak programs. These commands depend on the
implementations that you just wrote.
The RoboSpeak instructions callsub and return don’t work yet, but you’ll soon fix
that. First, though, you should read the Subprograms section in Tribe’s Scaladocs and answer the questions below.
set hackline 4
set hackline 1
iflt radar -3 22
set mem 4
set mem 8
In order to implement these methods, you’ll need to give each tribal bot its own call
stack. The bot can use the stack to track which subprogram calls are active and where it
should resume the program after a subprogram returns.
The TribalBots module comes with a Frame class, which represents simple call-stack
frames. Take a look.
In principle, you could represent the call stack as, say, a Buffer[Frame]. However,
we’ll instead go for a collection type that is specifically designed for representing
A call stack is a special case of the more generic concept of a stack (pino). A
stack is a collection that follows the LIFO principle: last in, first out. The
main operations of a stack are these two:
Read the example below, which introduces you to Scala’s Stack class. Of course, you
can also experiment with it on your own in the REPL.
import scala.collection.mutable.Stackimport scala.collection.mutable.Stack
val wordStack = new Stack[String]()wordStack: Stack[String] = Stack()
wordStack.push("first")res0: Stack[String] = Stack(first)
wordStack.push("second")res1: Stack[String] = Stack(second, first)
wordStack.push("third")res2: Stack[String] = Stack(third, second, first)
wordStack.pop()res3: String = third
wordStack.pop()res4: String = second
wordStack.push("fourth")res5: Stack[String] = Stack(fourth, first)
wordStack.pop()res6: String = fourth
wordStack.pop()res7: String = first
wordStack.pop()java.util.NoSuchElementException: empty collection
wordStack.isEmptyres8: Boolean = true
Class TribalBot already has a callStack variable with the type Stack[Frame]: a
stack that contains frame objects. The class doesn’t use the variable for anything,
Implement the methods callSubprogram and returnFromSubprogram. Use the callStack
variable for manipulating the stack.
You can then test your solution on the Patrolman tribe or a RoboSpeak program of your
Further reading: domain-specific languages
RoboSpeak can be called a domain-specific language or DSL
(täsmäkieli). Look up the term. What are DSLs used for? Is RoboSpeak
an internal DSL or an external DSL? Could we say that the musical
strings that we pass to o1.play form a DSL?
Further reading: the collect method
shout uses a method named collect. Look up collect in the
Scala API and work out its purpose in shout.
Challenge: extend RoboSpeak
Please note that this section must be completed individually.
Even if you worked on this chapter with a pair, each of you should submit the form separately.
Time spent: (*) Required
Please estimate the total number of minutes you spent on this chapter (reading, assignments,
etc.). You don’t have to be exact, but if you can produce an estimate to within 15 minutes or
half an hour, that would be great.
Written comment or question:
You aren’t required to give written feedback. Nevertheless, please
do ask something, give feedback, or reflect on your learning!
(However, the right place to ask urgent questions about programs
that you’re currently working on isn’t this form but the lab sessions
or Piazza. We can’t guarantee that anyone will even see anything
you type here before the weekly deadline.)
Thousands of students have given feedback that has contributed to this ebook’s design.
The ebook’s chapters, programming assignments, and weekly bulletins have been written in
Finnish and translated into English by Juha Sorva.
The appendices (glossary, Scala reference,
FAQ, etc.) are by Juha Sorva unless otherwise specified on the page.
The automatic assessment of the assignments has been developed by: (in alphabetical order)
Riku Autio, Nikolas Drosdek, Joonatan Honkamaa, Jaakko Kantojärvi, Niklas Kröger, Teemu
Lehtinen, Strasdosky Otewa, Timi Seppälä, Teemu Sirkiä, and Aleksi Vartiainen.
The illustrations at the top of each chapter, and the similar drawings elsewhere in the
ebook, are the work of Christina Lassheikki.
The animations that detail the execution Scala programs have been designed by Juha
Sorva and Teemu Sirkiä. Teemu Sirkiä and Riku Autio did the technical implementation,
relying on Teemu’s Jsvee and Kelmu toolkits.
The other diagrams and interactive presentations in the ebook are by Juha Sorva.
The O1Library software
has been developed by Aleksi Lukkarinen and Juha Sorva. Several of its key components
are built upon Aleksi’s SMCL
The pedagogy of using O1Library for simple graphical programming (such as Pic) is
inspired by the textbooks How to Design Programs by Flatt, Felleisen, Findler, and
Krishnamurthi and Picturing Programs by Stephen Bloch.
The course platform A+ was originally created at Aalto’s LeTech
research group as a student project. The open-source project
is now shepherded by the Computer Science department’s edu-tech team and hosted by the department’s IT
Markku Riekkinen is the current lead developer; dozens of Aalto students and others have also contributed.
The A+ Courses plugin,
which supports A+ and O1 in IntelliJ IDEA, is another open-source project. It was created by Nikolai
Denissov, Olli Kiljunen, Nikolas Drosdek, Styliani Tsovou, Jaakko Närhi, and
Paweł Stróżański with input from Juha Sorva, Otto Seppälä, Arto Hellas, and others.
For O1’s current teaching staff, please see Chapter 1.1.
The notion of programmable “tribes” or “species” that fight each other on a grid comes
from a programming assignment by Nick Parlante.
Viljami Nurminen and Rune Pönni contributed additional commands to the RoboSpeak language,
drawing on student feedback.