What we will do:
In this part we draw game boards and combine terminal battleship code (game engine) with iOS code.
- Although this is an iOS tutorial you may want to read Basics of 2D graphics macOS tutorial where
@IBDesignable
concept is described. - Watch video and repeat in your project all the steps (download video). For this part you need a final code from terminal version of battleship game (download terminal version of battle ship game)
- Make few changes in terminal version of battle ship game files:
- In
EngineGameBattleship.swift
change from
1func shipsAutoSetup(shipsSize: [Int], maxTriesPerShip: Int, who: Who) -> Bool {
to
1func shipsAutoSetup(maxTriesPerShip: Int, who: Who) -> Bool { - In
EngineGameBattleship.swift
change from
123456class EngineGameBattleship {private var shipsSize: [Int]private var rows, cols: Intprivate var boardPlayer: Boardprivate var boardOpponent: Board
to
123456class EngineGameBattleship {private var shipsSize: [Int]private var rows, cols: Intvar boardPlayer: Boardvar boardOpponent: Board - In
Board.swift
change from
1private var board: [[CellType]]
to
1var board: [[CellType]]
- In
- Download source code for this chapter (download source code).
- Look into all source code files. Think about all changes you can find. Migrate all changes into your files.
- Watch video showing that game board resizing works - this part is only for watching (download video).
- Watch the final result (download video). Notice that
cellColor
takes the valueGameColors.grey
only to make test - normally it should have theGameColors.blueLight
value.