ttt-4-display-board-rb
Overview
A tic tac toe board can be represented by an array of 9 string elements containing the state of that cell in the board.
An empty board would be:
board = [" "," "," "," "," "," "," "," "," "]If we called display_board and passed in that board as an argument, we'd expect the following:
board = [" "," "," "," "," "," "," "," "," "]
display_board(board)
# Would Print:
# | |
# -----------
# | |
# -----------
# | | A board with an "X" in the middle:
board = [" ", " ", " ", " ", "X", " ", " ", " ", " "]
display_board(board)
# Would Print:
# | |
# -----------
# | X |
# -----------
# | | In these examples, the data from board is being used within the display_board method to print a board based on the current values in the board array passed to it as an argument. Here are a few more examples.
A board with X winning via filling the top row:
If that board was passed to display_board, we'd expect this to print:
Or think about a random board after 5 turns:
That board as an argument to display_board should print:
Or a board entirely filled with "O":
Would print:
Etc...
Our goal is to build a method display_board that accepts a board array as an argument and uses the data in that array to correctly print out an accurate representation of the Tic Tac Toe board from the player's perspective based on the data in the array.
For example, given a correctly defined display_board:
Should print:
The board follows the following format:
When the board is displayed there are three characters per cell. An empty cell is
" "and a filled cell is" X "Three cells per a row.
The middle cell in a row is bordered by 2
|(pipes):O | X |Three rows on the board.
Rows are separated by:
-----------
Note: If you've built the blank tic tac toe board in a previous lab, you may want to go back and copy it in to this lab. Feel free to also re-create the board from scratch.
Building Dynamic Strings with Interpolation
Don't forget that in Ruby you can interpolate data into a string using the "#{1+1}" interpolation syntax. For example:
With variables, interpolation looks like:
In the context of an array, like you'll be dealing with in board, you'll have to do something like:
Make sure to be interpolating data from the board array within your display_board method. This is essential to solving this lab, to evolve from a permanently empty skeleton board as you did before to an actual board that contains live player movement throughout the game.
Objectives
Define a method that accepts an argument.
Use the argument within the method.
Read data from an array.
Print out a multi-line dynamic string using Interpolation
Instructions
Define your
display_boardmethod inlib/display_board.rbGet the test suite passing by running
learnSubmit your solution.
Hint: The rspec --fail-fast test flag
rspec --fail-fast test flagTo ensure that your display_board method honestly works as expected, we had to write a lot of tests to flex the different situations that might occur. When you run learn, you're going to see lots of failing tests that are all basically failing for the same reason - display_board isn't behaving as desired. It might be easier to deal with one test failure at a time and scrolling through the output of 12 failures isn't helpful.
You can limit your test run to stop at the first failure it encounters so that you only see 1 failure and can easily focus on it. By focusing on a single failure, you can quickly change your method based on that failure and re-run the test suite and see if the change solved that single failure. Although this lengthens the command each time, you can instantly redo your last entered command by tapping the Up arrow on your keyboard so you don't have to type the whole thing each time.
It's helpful, for example:
VS:
rspec --fail-fast
Bonus: Write Your Own Tests!
If you open spec/display_board_spec.rb you'll see the test suite for this lesson. We left the last two tests pending. Read the instructions in the file and see if you can implement those tests.
View Display Tic Tac Toe Board on Learn.co and start learning to code for free.
Last updated