Bonus exercise: Make your Tic-Tac-Toe object-oriented
A possible solution is at the end of this page.
Try it yourself
Can you refactor your Tic Tac Toe to be Object-Oriented? How many classes would you introduce?
Possible solution
The original course had no solution for this exercise, so this one was written for this book. It refactors the Tic-Tac-Toe solution TicTacToe.bbj into four files. Board holds the nine cells and the rules, so the game logic has no window code. Player holds a name and a mark, so a later version can add a computer player without touching the board. GameWindow owns the controls and callbacks and asks Board for every decision. PlayTicTacToe.bbj only starts the game. Keep the four files in one folder and run PlayTicTacToe.bbj. You find them in the folder exercises/oo-tic-tac-toe/ of the exercises.zip link.
Player.bbj
rem Player.bbj
rem Part of the possible solution for the bonus exercise Make your Tic-Tac-Toe object-oriented, written for the Introduction to BBj Development book
rem Player keeps the name and the mark of one player, so the window can ask who is up without knowing about X and O.
class public Player
field public BBjString Name$
field public BBjString Mark$
method public Player(BBjString playerName$, BBjString playerMark$)
#Name$ = playerName$
#Mark$ = playerMark$
methodend
classend
PlayTicTacToe.bbj
rem PlayTicTacToe.bbj
rem Part of the possible solution for the bonus exercise Make your Tic-Tac-Toe object-oriented, written for the Introduction to BBj Development book
rem The launcher only starts the game: it creates a GameWindow, shows it and waits for events. Run this file.
use ::GameWindow.bbj::GameWindow
declare auto GameWindow game!
game! = new GameWindow()
game!.show()
process_events