In this project, you will create a command-line Tic-Tac-Toe game. To do this,
you will fill out two classes, cursor.js
and ttt.js
. cursor.js
contains
logic for navigating around the board while ttt.js
contains the game logic.
To render the game, you have been given a Screen
API that handles
command-line rendering. You do not need to understand how the code in Screen
works but you will need to make use of the commands provided. You may be
tempted to spend a large amount of time digging through the API code under
the reasoning that you want to understand all of the project to best be able to add
to it. There are times where it is necessary to do this, but on most projects, there
simply isn't time or need. Think of this as an opportunity to practice your time
management skills. Consider the API as a black box. Review the documentation
thoroughly to make sure that you understand the inputs and outputs, but don't
peek inside unless you have extra time after completing the project.
The API is documented below. Try out the commands to see how they work.
To process keypresses, you will need to load Command
objects into the Screen
API using Screen.addCommand
. This function takes a key
which triggers the
command, a string description
, and an action
callback which is executed
when key
is pressed.
The game should alternate turns between O
and X
until one player wins or
the game ends in a tie.
- Type
npm install
to install all packages - Run
node game.js
to run the game - Run
mocha
to run tests
- Fill out movement logic in
class/cursor.js
untilmocha test/cursor-spec.js
passes all tests - Fill out game logic in
class/ttt.js
untilmocha test/ttt-spec.js
passes all tests - Create commands for cursor movement in
ttt.js
that callcursor.up
,cursor.down
,cursor.left
, andcursor.right
- Use
setBackgroundColor
andresetBackgroundColor
incursor.js
to highlight the cursor's current position on the grid - Create a command in
ttt.js
that places a move at the cursor's position - Fill out the game state in
ttt.js
that checks for wins after each move
Screen
is a static class with the following methods. You do not need to
modify this class at all. The functionality you will need is documented below.
Screen.initialize(numRows, numCols)
will initialize a grid with the given dimensions.
Screen.setGridlines(gridLines)
will insert lines between each grid element isgridLines
is true, or hide them ifgridLines
is false.
Screen.addCommand(key, description, action)
will add a command that calls theaction
callback anytimekey
is typed on the keyboard.description
will be displayed in the help message.Screen.printCommands()
will show a list of all loaded commands and their descriptions.
Screen.setGrid(row, col, char)
sets the character atrow
andcol
to the givenchar
.Screen.setTextColor(row, col, color)
sets the text color atrow
andcol
to the givencolor
.Screen.setBackgroundColor(row, col, color)
sets the background color atrow
andcol
to the givencolor
.
Valid colors are:
- black
- red
- green
- yellow
- blue
- cyan
- white
- magenta
Screen.setQuitMessage(quitMessage)
sets a message to be printed when the user quits.Screen.quit(showMessage=true)
quits the game and prints the message ifshowMessage
is true.
Screen.render()
will update the display. This must be called anytime the grid or messages change.
Screen.setMessage(msg)
takes in a string to be printed below the grid each time it is rendered.