boardgame a minidraw extension
play

BoardGame: A MiniDraw extension Henrik Brbak Christensen Status: - PDF document

BoardGame: A MiniDraw extension Henrik Brbak Christensen Status: Draft. November 29, 2010 Chapter 1 Boardgame Architecture The MiniDraw framework is described in Chapter 30 in Flexible, Reliable Software . The Boardgame extension is a set of


  1. BoardGame: A MiniDraw extension Henrik Bærbak Christensen Status: Draft. November 29, 2010

  2. Chapter 1 Boardgame Architecture The MiniDraw framework is described in Chapter 30 in Flexible, Reliable Software . The Boardgame extension is a set of classes defined in the minidraw.boardgame package and defines an augmented framework in itself for the more specialized domain of supporting graphical user interfaces for board games. It does so by providing im- plementations of some of MiniDraw’s hotspots (essentially turning these into frozen spots) while providing new hotspots to be defined by developers of board games. Boardgame assumes a well-defined decoupling between the game itself (called the domain) and the graphical user interface (the GUI)—which of course is a classic ar- chitecture. Boardgame provides hotspots to support the two flows of information and control: • From GUI to game domain: That is, when a user manipulate some graphical ob- ject, this action is converted into an action in the domain. The archetypical example is the user dragging a token from one square of the board game board to another which must be translated into a call to a move() sor similar method in the game domain. • From game domain to GUI: That is, when the state changes in board game, then the GUI must be updated to reflect this. For instance if a chess piece hits an opponent piece, then the opponent piece must disappear—or move to a place outside the chess board. Boardgame provides a number of hotspots to support this flow more directly than does MiniDraw. [DISCUSSION PENDING] 1.1 Notes on the Process Note that the focus here is on the GUI and no domain code exists beforehand. 2

  3. Chapter 2 Snakes and Ladders—by TDD The snakes and ladders game is a very old childrens’ game. The game’s logic is extremely simple: roll a die and move forward accoring to the die value, if you hit a square with a ladder you will move (forward) to the square at the end of the ladder and if you hit a snake you will move (backward) to the square at the end of the snake. The first player to reach the end square wins. From the extension package boardgame ’s perspective it is ideal because it requires all the behaviour supported by the framework: • Generic type LOCATION: The squares on which the players’ tokens rest. • BoardFigure: Moveable objects with a graphical appearance (representing the tokens of the players) that are moved by the user using the mouse. • Props: Static objects with an appearance that can be clicked by the user (the die, clicking telling it to roll) • PositioningStrategy: Once a tokens has been put on a square we would like to adjust its position to appear nice. To start the project I devise a short test list ✽ Show the board and a die ✽ Make tokens and die into BoardFigures ✽ Move a token invokes the game’s move method 2.1 Iteration 1: Show Board and Die A Ant build script is written and a standard folder setup is created like that in FRS chapter 6. A simple snakes and ladder board is found on wikipedia and its size is increased a bit to make room for a die. Together with die images it is copied to the resource folder. 3

  4. 4 ❚ Snakes and Ladders—by TDD In package snakesandladders.visual I create a simple test program, just to load the board background and show a single die (and target show ). This is basically just a copy of a similar “show graphics” test program from the HotGammon project from FRS. Note that it only uses the standard MiniDraw API. package snakesladders . visual ; import minidraw . standard . ∗ ; import minidraw . framework . ∗ ; import java . awt . ∗ ; import javax . swing . ∗ ; / ∗ ∗ Show v i s u a l appearance o f game . ∗ <# i f type == " code "> <# i n c l u d e "/ data / author . t x t "> </# i f > ∗ / public class ShowLayout { public s t a t i c void main ( String [ ] args ) { DrawingEditor editor = new MiniDrawApplication ( "Show Layout . . . " , new SnakesAndLaddersFactory ( ) ) ; editor . open ( ) ; Figure die = new ImageFigure ( " die4 " , new Point (690 , 4 0 ) ) ; editor . drawing ( ) . add ( die ) ; editor . setTool ( new SelectionTool ( editor ) ) ; } } class SnakesAndLaddersFactory implements Factory { public DrawingView createDrawingView ( DrawingEditor editor ) { DrawingView view = new StdViewWithBackground ( editor , " snakes − and − ladders − background " ) ; return view ; } public Drawing createDrawing ( DrawingEditor editor ) { return new StandardDrawing ( ) ; } public JTextField createStatusField ( DrawingEditor editor ) { JTextField statusField = new JTextField ( " Hello Snakes . . . " ) ; statusField . setEditable ( false ) ; return statusField ; } } The resulting graphics is shown in Figure 2.1.

  5. Iteration 2: Making BoardFigures ❚ 5 Figure 2.1: Visual test case of iteration 1. 2.2 Iteration 2: Making BoardFigures Next I want to make a One Step Test to start building up the boardgame extension stuff. The first one is getting boardgame to understand the three BoardFigures that must be on the GUI. This involves creating BoardFigures using the FigureFactory. I start by making a new test case program “ShowFigures” as a copy of iteration 1 program. Unfortunately, the next step is really a several step process: 1) I have to create a Fig- ureFactory which 2) require generic type LOCATION and 3) configure MiniDraw with a BoardDrawing instance instead of just a StandardDrawing (see method cre- ateDrawing in the previous listing). Furthermore, step 3’s BoardDrawing requires a PositioningStrategy which is then step 4! To get to this point I have to Fake It a lot. It appears that point 3) is actually the proper first step, so I need to fake both LOCA- TION and FigureFactory. This leads to the following code change (TDD rhythm step 1): public Drawing createDrawing ( DrawingEditor editor ) { return new BoardDrawing<Square >( new SnakeLadderPieceFactory (game) , new SnakeLadderPositioningStrategy ( ) , null / ∗ wait with the d i e prop ∗ / ) ; } which of course does not compile at all! I hurry up to define some fake it implemen- tations as local classes in the same file.

  6. 6 ❚ Snakes and Ladders—by TDD class Square { } class SnakeLadderPositioningStrategy implements PositioningStrategy <Square> { public Point calculateFigureCoordinatesIndexedForLocation ( Square location , int index ) { return new Point ( 8 0 , 3 0 0 ) ; } Point calculateFigureCoordinatesForProps ( String keyOfProp ) { public null ; return } } class SnakeLadderPieceFactory implements FigureFactory <Square> { public Map<Square , List <BoardFigure >> generatePieceMultiMap ( ) { return null ; } public Map<String , BoardFigure > generatePropMap ( ) { return null ; } } Now it at least compiles but I get an exception from boardgame extension at run- time: “ BoardGame contract violation: buildPieceMap assumes a non-null map is return from the FigureFactory.” So to get at Step 3 I add the images of two tokens, one for blue and one for red. I start by adding just one token, as I then do not have to add any implementation of Square . The graphics I found on the net and it is licensed under the Free Art License. class SnakeLadderPieceFactory implements FigureFactory <Square> { public Map<Square , List <BoardFigure >> generatePieceMultiMap ( ) { Map<Square , List <BoardFigure >> m = new HashMap<Square , List <BoardFigure > >(); Square square1 = new Square ( ) ; BoardFigure redtoken = new BoardFigure ( "game − token − red " , true , null ) ; L is t s qu ar e 1l i s t = new ArrayList ( ) ; s qu ar e 1l i s t . add ( redtoken ) ; m. put ( square1 , s qu a r e 1l i s t ) ; return m; } public Map<String , BoardFigure > generatePropMap ( ) { return null ; } } I get to step 4 which looks like in Figure2.2. ☞ Why is the red token positioned there? At this stage I still lack to add the blue token and the die. The die is really a “prop” that is an unmoveable object so I postpone that into a new test on the test list.

  7. Iteration 2: Making BoardFigures ❚ 7 Figure 2.2: Iteration 2. ✽ Show the board and a die ✽ Make tokens into BoardFigures ✽ Make die into a Prop BoardFigure ✽ Move a token invokes the game’s move method So I need to add the blue token as well. As it is also located on square 1, this is easy. class SnakeLadderPieceFactory implements FigureFactory <Square> { public Map<Square , List <BoardFigure >> generatePieceMultiMap ( ) { Map<Square , List <BoardFigure >> m = new HashMap<Square , List <BoardFigure > >(); Square square1 = new Square ( ) ; BoardFigure redtoken = new BoardFigure ( "game − token − red " , true , null ) ; BoardFigure bluetoken = new BoardFigure ( "game − token − blue " , true , null ) ; L is t s qu ar e 1l i s t = new ArrayList ( ) ; s qu ar e 1l i s t . add ( redtoken ) ; s qu ar e 1l i s t . add ( bluetoken ) ; m. put ( square1 , s qu a r e 1l i s t ) ; return m; } public Map<String , BoardFigure > generatePropMap ( ) { return null ; } }

Recommend


More recommend