All Manuals > LispWorks® User Guide and Reference Manual > 17 iOS interface

17.5 The Othello demo for iOS

The Othello demo is a simple iOS app showing the basics of using LispWorks for iOS Runtime. It contains an Xcode project to run the GUI and some Lisp source code to play the game.

To try the demonstration, see the file:

(example-edit-file "ios/README.txt")

17.5.1 Notes about the Xcode project

The Xcode project in examples/ios/OthelloDemo/ has a standard layout, with the class OthelloAppDelegate defined in:

(example-edit-file "ios/OthelloDemo/OthelloDemo/OthelloAppDelegate.m")

implementing the UIApplicationDelegate protocol.

The file:

(example-edit-file "ios/OthelloDemo/OthelloDemo/main.m")

initializes LispWorks by calling LispWorksInitialize and then runs the application main loop using the OthelloAppDelegate.

The application has two storyboards (MainStoryBoard_iPhone and MainStoryBoard_iPad) which display a Tab Bar allowing you to switch between an Othello game and a Lisp evaluation pane.

17.5.2 The Othello game

The Othello game is displayed by the Othello scene and contains an Othello board (the boardView outlet), a few buttons on a toolbar and a label showing the state of the game (the stateView outlet).

The scene is controlled by the class OthelloViewController, defined in:

(example-edit-file "ios/OthelloDemo/OthelloDemo/OthelloViewController.m")

The 64 tiles on the board are represented by UIImageView objects, created dynamically in the viewDidLoad method. The contents of the tiles are the images "empty", "white" and "black" which are loaded from the Images.xcassets asset catalog. The viewDidLoad method also creates an instance of the class OthelloServer, which is implemented in Lisp (see 17.5.4 Notes about the Lisp code).

The tiles in the board are dynamically positioned by the viewDidLayoutSubviews method.

The action methods restartOthello: and undoMove: are connected to the toolbar buttons in the storyboards and call into the Lisp code to update the game.

The action method playUISquare: is triggered when the user touches a square on the board (see viewDidLoad) and calls into the Lisp code to play that square.

The methods changeOthelloSquare:, updateStateString: and signalBadMove are called by the Lisp code to modify the GUI.

17.5.3 The Lisp evaluation pane

The Lisp evaluation pane is displayed by the Lisp Panel scene and contains a text field for entering a Lisp form (the formInputView outlet), a text field to display the evaluation results (the textOutputView outlet) and toolbar.

The scene is controlled by the class LispPanelViewController, defined in:

(example-edit-file "ios/OthelloDemo/OthelloDemo/LispPanelViewController.m")

The action methods evaluate:, clearTextOutput: and showHistory: are connected to the toolbar buttons in the storyboards.

The History button pops up a history of the forms entered so far. This is displayed by the History Table scene controlled by HistoryTableViewController, defined in:

(example-edit-file "ios/OthelloDemo/OthelloDemo/HistoryTableViewController.m")

and communicates back to the LispPanelViewController using the HistoryTableViewControllerDelegate protocol.

The keyboardWasShown: and keyboardWillBeHidden: notification methods resize the textOutputView to avoid the on-screen keyboard.

The method appendTextOutputString: is called by Lisp code to update the textOutputView.

17.5.4 Notes about the Lisp code

The Lisp code triggered by the GUI is in the file:

(example-edit-file "ios/ios-othello-user")

and uses the shared Othello logic in:

(example-edit-file "misc/othello")

The function init-othello-server is the main entry point of the Lisp code and is called when LispWorksInitialize is called from main.m. It initializes the LispWorks Objective-C interface and creates a helper object (lispworks-main-threads-funcalls-object) used by invoke-in-main-thread for making Lisp calls in the main thread of the application.

The Lisp code implements an Objective-C class OthelloServer using the Lisp class othello-server. This class implements the methods initWithViewController for initialization and initOthello, playSquare: and undoMove for the Othello game GUI code to call into Lisp in response to user gestures.

The Lisp code also implements the functions othello-user-change-a-square, othello-user-update-state-string, othello-user-signal-bad-move and othello-user-print-diagnostics-message which the shared Othello logic calls to update the GUI. Most of these functions call methods on the OthelloViewController object, taking care to invoke them in the main (GUI) thread of the application. This thread switching is needed because the Othello logic plays the game in a background thread to avoid hanging the GUI while considering its move (see perform-computer-play in examples/misc/othello.lisp).

Finally, the Lisp code implements an Objective-C class LispPanelServer using the Lisp class lisp-panel-server, with a method evaluate: to evaluate a Lisp form. This evaluate: method is called by the evaluate: action method in LispPanelViewController.

Note that the Othello logic can also be run via a desktop application using:

(example-edit-file "capi/applications/simple-othello")

LispWorks® User Guide and Reference Manual - 01 Dec 2021 19:30:21