「こんにちは、兵士さん!」
「こんにちは、リス船長さん!」
「おめでとうございます。今日はお休みです。」
「それで、私たちはやりたいことを何でもできるのですか?」
「はい、アミーゴ。おもちゃで一日中遊べます。たとえば、倉庫番。子供の頃から大好きなゲームです。レベル 435 を超えられません。手伝ってもらえますか?」
「もちろん、私も手伝いますよ、教えてあげるよ、船長」
リス船長は古いゲームボーイをポケットから取り出し、モノクロ版の倉庫番を起動します。しかしその後、失望が起こります。電池が切れてゲームボーイが停止してしまう。
船長は黙ってゲームボーイを見て、次にアミーゴを見て、そしてまたゲームボーイを見た。彼は振り返り、ゆっくりとオフィスに向かって歩きます。
「キャプテン! 私たちだけのバージョンの倉庫番を書きましょう! 追加のレベルを 1000 個考えて、クールなグラフィカル インターフェイスを作ります。」
「アミーゴ、あなたはいつも私を驚かせてくれます。エージェント IntelliJ IDEA に連絡してください。彼が他にあなたに任せる任務がなく、休日にあなたを手伝いたいのであれば、続行してください。」
20
タスク
Java Collections, レベル 10, レッスン 15
Sokoban (part 1)
Today we're going to write our own implementation of the game Sokoban. This is a logical puzzle game.
You can read more about it on Wikipedia. The game will consist of 3
main components (as you may have guessed, you'll need the MVC pattern).
The graphical interface will be implemented using Swing.
10
タスク
Java Collections, レベル 10, レッスン 15
Sokoban (part 2)
The foundation is laid, so now let's start building. You can think of the gameplay
as the interaction of GameObject objects. We'll have several types of them:
Box, StorageLocation (the place where you need to put the box), Wall, and Player.
2.1. Add an abstract GameObject class to the model package
10
タスク
Java Collections, レベル 10, レッスン 15
Sokoban (part 3)
Soon we'll be creating various game objects. It would be handy to be able
to draw them somewhere right away and see how they look. Objects will be
drawn on the game board represented by a Board object.
You've received modified code for the View class that creates a Board object and
sets up the view
20
タスク
Java Collections, レベル 10, レッスン 15
Sokoban (part 4)
We already have a shared GameObject class. It's time to create classes
for specific types of game objects.
4.1. Some game objects can move (player and boxes) and some can't (walls
and storage locations).
4.1.1. Add the Movable interface to the model package.
4.1.2. The Movable interface must have a
20
タスク
Java Collections, レベル 10, レッスン 15
Sokoban (part 5)
Box and Player game objects can not only
collide with other objects, but also move around the board.
5.1. Create a Player class and a Box class in the model package. Make each of them
inherit the most appropriate class.
5.2. The created classes must support the interface responsible for object
move
10
タスク
Java Collections, レベル 10, レッスン 15
Sokoban (part 6)
It's time to create the StorageLocation class. This class will be responsible for the game board cells
that all the boxes must be moved to. Objects of this type must not move
across the board or collide with other game objects.
6.1. Add a StorageLocation class to the model package.
6.2. The class m
10
タスク
Java Collections, レベル 10, レッスン 15
Sokoban (part 7)
Let's make the Wall class. A wall can collide with other objects, but it
can't move.
7.1. Add a Wall class to the model package.
7.2. Make the class inherit the appropriate parent class.
7.3. Implement a constructor with int x and int y parameters.
7.4. Implement the method for drawing.
Hint: you c
10
タスク
Java Collections, レベル 10, レッスン 15
Sokoban (part 8)
You've created the complete collection of game objects. Let's create a class to
store these objects.
8.1. Create a GameObjects class in the model package.
8.2. Add the following:
8.2.1. Set walls, Set boxes, Set storageLocations, and Player player fields.
8.2.2. Getters
10
タスク
Java Collections, レベル 10, レッスン 15
Sokoban (part 9)
Various events will occur during the game. Let's create an
EventListener interface. Every class that wants to handle events must
implement it. And classes that generate events will call
this interface's methods.
9.1. Add the EventListener interface to the controller package.
9.2. Add these void met
20
タスク
Java Collections, レベル 10, レッスン 15
Sokoban (part 10)
The game will have several levels, and all of them will be stored in a text file. Now we'll
write a LevelLoader test stub. Why a test stub? We don't
need the full functionality yet. It's pretty complicated, so we'll leave it for later. For now:
10.1. Create a LevelLoader class in the model package.
20
タスク
Java Collections, レベル 10, レッスン 15
Sokoban (part 11)
Let's begin fleshing out the Model class' functionality. Add the following:
11.1. A GameObjects gameObjects field. It will store our game objects.
11.2. An int currentLevel field that stores the current level. Initialize it
to 1.
11.3. A LevelLoader levelLoader field that stores the level loader.
I
20
タスク
Java Collections, レベル 10, レッスン 15
Sokoban (part 12)
Let's try organizing the interaction between the view and model.
12.1. Add an update() method to the View class. It must call the repaint() method on the
board field. In other words, the update() method will update the view
(redraw the board).
12.2. Add the GameObjects getGameObjects() method to th
20
タスク
Java Collections, レベル 10, レッスン 15
Sokoban (part 13)
Let's fill out the controller's functionality.
13.1. In addition to what the Controller class constructor already has, add event listeners
on the model and view. The controller must itself be a listener.
13.2. Implement the controller's methods:
13.2.1. move(Direction direction) - should call the m
20
タスク
Java Collections, レベル 10, レッスン 15
Sokoban (part 14)
Let's make our game more interactive (using a keyboard
to move the player). We'll start by handling keystrokes.
14.1. Add to the Board class a nested KeyHandler class that
inherits KeyAdapter.
14.2. Overload its keyPressed() method. If the key with code VK_LEFT is pressed,
then send an event with a
40
タスク
Java Collections, レベル 10, レッスン 15
Sokoban (part 15)
The time has come to implement the model's move() method, which is responsible for movement. But first,
let's implement some helper methods. Add the following methods to the Model class:
15.1. boolean checkWallCollision(CollisionObject gameObject, Direction direction). This
method checks for collis
20
タスク
Java Collections, レベル 10, レッスン 15
Sokoban (part 16)
All that remains is to finish implementing the level loader.
16.1. Open the levels.txt file and carefully study the file structure. The character 'X'
represents a wall, ‘*’ represents a box, ‘.’ represents a storage location, ‘&’ represents a box in a storage location, and ‘@’ represents the player
10
タスク
Java Collections, レベル 10, レッスン 15
Sokoban (part 17)
You're a superstar! You've made a great game. Now you can relax a bit and play it.
If you ever get tired of it, which is practically impossible, you could make the following
improvements:
17.1. Use images to represent the objects.
17.2. Make a level editor.
17.3. Add rankings. You could even post t