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.
Chapter 8.2: Robots
Introduction
Much of Week 8 revolves around a programming assignment in which you construct a robot simulator. In this simulator, a “robot world” is essentially a grid where “robots” of very little brain conduct their virtual existence.
This project has nine parts. The first two are in this chapter; the rest are in Chapters 8.3 and 9.1.
Before we get started in earnest, let’s form an overview of the program that you’ll be working on.
The Robots Module
The Robots module contains two packages. The classes in o1.robots
constitute the
robot simulator’s internal model; o1.robots.gui
provides a user interface. The user
interface is ready for use as given, and we won’t discuss it further here.
We’ll build the simulator on the same Grid
type that you used in Chapter 8.1.
The following table briefly describes each of the robot simulator’s main components. Below the table, you’ll find a diagram of the components’ relationships.
Package |
Component |
Description |
Status |
---|---|---|---|
|
class |
Represents pairs of coordinates. (Familiar from multiple earlier programs.) |
ready |
|
trait |
Represents grids in general. (Familiar from Chapter 8.1.) |
ready |
|
enumerated type
|
Represents the main compass directions. There are exactly four instances of this class, which stand for north, east, south, and west. (Familiar from Chapter 6.3.) |
ready |
|
class |
Represents the robots’ physical form. Each instance
of |
partially implemented |
|
trait
|
Represents the general properties of the robots’ “artificial intelligence”. |
partially implemented |
|
|
Subtypes of |
|
|
trait |
Represents a single square of a robot world in general terms. |
ready |
|
class |
Inherits |
partially implemented |
|
singleton object |
Inherits |
partially implemented |
|
class |
A subclass of |
partially implemented |
Part 0 of 9: Answer Some Questions
Before you go on
Try to get a general sense of the Robots program by reading the description above and the Scaladocs. Browse the source code, too.
Part 1 of 9: Fundamental Repairs to RobotWorld
You’ve been given a whole bunch of code, much of which works in principle, but the program is not ready to run. IntelliJ lights up with errors.
RobotWorld
has been only partially implemented. Fix it:
The error messages indicate that
RobotWorld
doesn’t have theheight
variable (orwidth
) as expected. It should inherit these from theGrid
trait. Add anextends
clause that makesRobotWorld
a subtype ofGrid
.You may wish to look at
GameBoard
in the Viinaharava game for inspiration.Read the documentation carefully. Make sure you pass in the right numbers as constructor parameters to the superclass.
Once you’re done with this step, try launching the program via
o1.robots.gui.RobotApp
. You should see a robot world that’s completely dark.Be sure to give
Grid
the type parameter it needs. (If you receive error messages that featureNothing
as the type of the grid’s squares, you probably overlooked this.)
Repair the
RobotWorld
methodinitialSquare
, which now fills the entire world with walls. The method is private and not detailed in the Scaladocs, but there’s a comment in the given code that explains what it should do: put walls on the edges and floors in the middle.That is, you’ll need to check the coordinates to determine whether you should create a
Floor
or use a reference to theWall
singleton.Note that
Floor
is a class, not a singleton. The class takes no parameters, so to get a new instance of it, writeFloor()
— the empty brackets need to be there.
Relaunch the application. You should be able to create empty robot worlds, but clicking the floor squares won’t let you add robots or walls.
Part 2 of 9: Adding Content
Implement the
RobotWorld
methodaddWall
.Hint: use
update
inGrid
.
Run the program again and try right-clicking floors to add walls.
Implement
addRobot
in the same class.The method needs to handle several interrelated subtasks. Make sure you attend to each of the things mentioned in the Scaladoc.
Reveal additional hints below if you feel you want them.
Try adding
Spinbot
s in the GUI. They should appear in the robot world, but they don’t do anything yet. You can break them and repair them, though.
Hint: subtasks in addRobot
The method should: 1) create a robot; 2) add it to the end of the robot list; 3) add it in the appropriate square within the robot world; and 4) return a reference to the added robot.
Tools for each subtask:
1) create a new RobotBody
object;
2) update the list in this.robots
;
3) pick the right square and call that square’s addRobot
method; and
4) return a reference to the RobotBody
object that you created.
Hint: picking the appropriate square in addRobot
A RobotWorld
is a Grid
.
Grids have an elementAt
method for accessing a single element
(square) of the grid.
Use that method to pick the appropriate square. Then call the square’s robot-adding method.
Submit your solution to Parts 1 and 2. The assignment continues in upcoming chapters.
A+ presents the exercise submission form here.
Feedback
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.
Credits
Thousands of students have given feedback and so contributed to this ebook’s design. Thank you!
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, Kaisa Ek, Joonatan Honkamaa, Antti Immonen, Jaakko Kantojärvi, Onni Komulainen, Niklas Kröger, Kalle Laitinen, Teemu Lehtinen, Mikael Lenander, Ilona Ma, Jaakko Nakaza, Strasdosky Otewa, Timi Seppälä, Teemu Sirkiä, Joel Toppinen, Anna Valldeoriola Cardó, 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, Juha Sorva, and Jaakko Nakaza. Several of its key components are built upon Aleksi’s SMCL library.
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 services; 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 has been designed and implemented by various students in collaboration with O1’s teachers.
For O1’s current teaching staff, please see Chapter 1.1.
Additional credits appear at the ends of some chapters.