Topics Covered: Tic-tac-toe
Push your website to a Github page. Make sure it works :)
We're going to review what we learned in class about classes. Check out this code, and write down the answers to the questions below, in a file called people_quiz.rb
. Have a teacher check them for you.
class Person
def initialize(name)
@name = name # Line 3
end
end
class Engineer < Person
def initialize(name, field)
@name = name
@field = field
end
def go_to_work
puts "Going to work as an engineer in #{field}..."
end
end
elon_musk = Engineer.new('Elon Musk', 'space travel')
Engineer
a subclass or a superclass?Engineer.go_to_work
do? Why?elon_musk
? Choose all that apply:
One of the best uses of a computer is to find things—meaning information or other data. Facebook finds what your friends are doing, Buzzfeed finds things you kind of care about, and even Bing finds things sometimes, too. For this exercise, you're going to write your own code that finds things. Note: if you're wondering how your methods should work in certain cases, check out the example tests in the code section below. Writing examples which make it clear how you want your method to work before implementing it is a great way to organize your thoughts!
Write a method index_of
that takes two parameters, a string and a letter. Return the index of the first time the letter occurs in that string, or -1 if it doesn't occur.
Write a method find_by_name
, which takes an array of hashes, each of which has the property :name
, and a string and finds the first one that has that name.
Write a second, very similar method filter_by_name
, which returns and array with all the items with the given name. Note that that method should always return an array, even if only one item is found.
# finding_things.rb
# Your code here
index_of("abcdefghijklmnop", "m")
# => 12
index_of("abcdefghijklmnop", "z")
# => -1
people = [
{
:id => 1,
:name => "bru"
},
{
:id => 2,
:name => "ski"
},
{
:id => 3,
:name => "brunette"
},
{
:id => 4,
:name => "ski"
}
]
find_by_name(people, "ski")
# => {:id=>2,:name=>"ski"}
find_by_name(people, "kitten!")
# => nil
filter_by_name(people, "ski")
# => [{:id=>2,:name=>"ski"}, {:id=>4,:name=>"ski"}]
filter_by_name(people, "bru")
# => [{:id=>1,:name=>"bru"}] (Note this is still an array)
filter_by_name(people, "puppy!!!")
# => []
This exercise is actually more of a mini-project: we're going to work through building a simple tic-tac-toe application from scratch. Pick a partner who you haven't worked with before, create tictactoe.rb
in d5
and let's get started.
As usual, we're going to plan out our approach before we start coding away. Read the following carefully—your program must do the following:
The player input will be an integer from 0-8 and the squares will be numbered top-left to bottom-right (0 is the top-left square, 2 is the top-right square, 4 is the middle square, and 8 is the bottom-right square).
In tictactoe.rb
, answer the following questions:
# tictactoe.rb
# How will you keep track of whose turn it is?
# What data structure will you use to keep track of the board? You only
# know two (Array, Hash), but remember they can be nested (meaning an
# array can contain another array—it can even contain a hash that contains
# another hash, though unclear if that will be useful :P)
# When a player makes a move, what should happen?
# How do you keep track of which squares have Xs and which have Os?
# How do you detect a winner?
# When does the program stop? Is there a loop? If so, what kind?
Pseudo-code is an informal, high-level description of your program's operation. Pseudo-code is written in english and outlines your program's behavior using step by step instructions. An example for tic-tac-toe:
# Every turn:
# Update whose turn it is
# Get user input
# Update board
# Check for winner
Of course, that's pretty easy to write. However, it may be less obvious how you would translate pseudo-code into good ol' Ruby. What exactly is a turn? How does one update the board? These are questions you've started thinking about in Step 1. For Step 2, expand your thinking, and the above pseudo-code, to make a more detailed outline. That is, your writing may contain words like "Array" and "Hash", but probably doesn't contain any "[" or "{" glyphs. As you write, consider these questions, and be ready to answer them:
It may be starting to become clear that coding is much more about thinking than anything else. While there isn't necessarily a best solution, there are solutions that are much more intuitive and easier to understand than others. Remember that code is meant to be read by humans, and when two pieces of code do the same thing with the same efficiency, the one that's simpler is always better.
Check in with a TA before continuing!
Now that you have your pseudo-code, it should be a simple matter to turn it into real code. Take a crack at it, and see if it works!
$ ruby tictactoe.rb
Player 1:
0
Player 2:
1
Player 1:
kittens
Error: Invalid input
Player 1:
10
Error: Invalid input
Player 1:
3
Player 2:
2
Player 1:
2
Error: Square already taken
Player 1:
6
Congrats, player 1 won!
If you've gotten this far, you actually know quite a bit about coding, and the coding process. This was actually a Google coding interview problem (though keep that fact hush hush!). When you interview at companies—or try to code anything, really—this workflow is a good one to keep in mind: first talk through your solution and think about what data structures you'll use. Then write pseudo-code and try to foresee as many problems as you can. Only when you're confident in your thinking should you start coding.
To be honest, while your program is pretty awesome, it's not very user friendly. Since humans aren't very good at remembering what the board looks like, add a method to print out the board after each step. We've picked a design below, but feel free to make it look like however you want.
$ ruby tictactoe.rb
Player 1:
4
| |
———+———+———
| X |
———+———+———
| |
Player 2:
0
O | |
———+———+———
| X |
———+———+———
| |
Player 1:
2
O | | X
———+———+———
| X |
———+———+———
| |
Player 2:
6
O | | X
———+———+———
| X |
———+———+———
O | |
Player 1:
3
O | | X
———+———+———
X | X |
———+———+———
O | |
Player 2:
5
O | | X
———+———+———
X | X | O
———+———+———
O | |
Player 1:
1
O | X | X
———+———+———
X | X | O
———+———+———
O | |
Player 2:
7
O | X | X
———+———+———
X | X | O
———+———+———
O | O |
Player 1:
8
O | X | X
———+———+———
X | X | O
———+———+———
O | O | X
Cats game!
Find the answers at /year01/tests/tictactoe.rb.