display_boardshould print out the specified Tic Tac Toe board.
learnlocally until you pass.
A Tic Tac Toe program is going to need to be able to display the current state of the board before every turn so that the player can see the board before deciding what move to make. You could imagine a program working as follows, from a player's perspective.
Welcome to Tic Tac Toe | | ----------- | | ----------- | | What move would X like to make (1-9)? > 5 | | ----------- | X | ----------- | | What move would O like to make (1-9)? > 1 0 | | ----------- | X | ----------- | | # etc...
Because our program is going to have to do this repeatedly, we should encapsulate the procedure of printing a board within a method so that we can simply call it whenever we need to print the board.
We're going to start with a simplified version of the
display_board method. Instead of actually displaying the state of a Tic Tac Toe board, we're going to only print out an empty, hard-coded, board. Sure, this method wouldn't work in a real game, but if we can't print a predefined, hard-coded, simplest board, we're probably not going to be able to print a real board.
Building a simplified first version of a method is a common practice in programming (these methods are sometimes referred to as 'stubs'). By not trying to solve the entire problem at once, we can first focus on correctly structuring our code, laying out some basics for us to build upon. It allows us to get something working as quickly as possible and then improve the code. This process is called iterating on your code and it's just like editing an essay. It's the natural process of small incremental improvements.
display_board method should print out a board that looks exactly like:
| | ----------- | | ----------- | |
In Ruby, it would be written as:
puts " | | " puts "-----------" puts " | | " puts "-----------" puts " | | "
As you define display_board in
lib/display_board.rb, you're going to want to test your method and see what it does. Just running the test suite with
learn will give you feedback on its behavior, but there's a quicker way to see how your method behaves and if it matches the desired outcome.
Open IRB from your terminal by typing
You'll know you're in your IRB session because your prompt will change and you'll be able to execute arbitrary ruby (type
1+1into your IRB session. If you see
2, you're good, if you see any kind of error, especially mentioning
bash, you're still in your BASH terminal session, type in
Copy the contents of
lib/display_board.rb to your IRB session.
// ♥ irb 001:0 > def display_board 002:1 > puts "A Tic Tac Toe Board" 003:1 > end
Don't forget to hit enter after you paste. You should see
Now you can invoke your method by calling it:
// ♥ irb 001:0 > def display_board 002:1 > puts "A Tic Tac Toe Board" 003:1 > end => :display_board 004:0 > display_board A Tic Tac Toe Board => nil
And you can quickly see what it does!
View Printing a Tic Tac Toe Board on Learn.co and start learning to code for free.
View Display Tic Tac Board Example on Learn.co and start learning to code for free.