Skip to content

Commit

Permalink
Merge pull request learn-co-students#21 from learn-co-curriculum/mast…
Browse files Browse the repository at this point in the history
…er-fix

Master fix
  • Loading branch information
Avidor Turkewitz committed May 18, 2016
2 parents cb6ba68 + dc55223 commit 7e9633f
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 25 deletions.
33 changes: 18 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ A turn of Tic Tac Toe is composed of the following routine:

1. Asking the user for their move by position 1-9.
2. Receiving the user input.
3. If the move is valid, make the move and display the board to the user.
4. If the move is invalid, ask for a new move until a valid move is received.
3. Convert position to an index.
4. If the move is valid, make the move and display the board to the user.
5. If the move is invalid, ask for a new move until a valid move is received.

All these procedures will be wrapped into our `#turn` method. However, the majority of the logic for these procedures will be defined and encapsulated in individual methods (some of which you may have built previously).

Expand All @@ -25,8 +26,9 @@ You can imagine the pseudocode for the `#turn` method:
```
ask for input
get input
if input is valid
make the move for input
convert input to index
if index is valid
make the move for index
show the board
else
ask for input again until you get a valid input
Expand All @@ -45,13 +47,13 @@ Should accept a board as an argument and print out the current state of the boar

#### `#valid_move?`

Should accept a board and a position from the user (remember that the user will input a number 1-9 but your board is *really* indexed 0-8) and return true if the position is within the correct range of 1-9 and is currently unoccupied by an X or O token.
Should accept a board and an index from the user and return true if the index is within the correct range of 0-8 and is currently unoccupied by an X or O token.

**Hint: While not explicitly required by this lab, you might want to encapsulate the logic to check if a position is occupied in its own method, perhaps `#position_taken?`**

#### `#move`

This method should accept a board, a position from the user (which will be in the 1-9 format), and a token to mark that position with (you can give that argument a default value of 'X'––we're not worrying about whose turn it is yet). The method should set the correct index value of that position within the board equal to the token.
This method should accept a board, an index from the user (which was converted from their raw input with `input_to_index`), and a token to mark that position with (you can give that argument a default value of 'X'––we're not worrying about whose turn it is yet). The method should set the correct index value of that position within the board equal to the token.

### Workflow

Expand Down Expand Up @@ -163,7 +165,8 @@ The hard part of the turn method is figuring out how to handle invalid input. We

```
get input
if input is valid
convert input to index
if index is valid
make the move for input
else
ask for input again until you get a valid input
Expand Down Expand Up @@ -231,15 +234,15 @@ board = [" ", " ", " ", " ", " ", " ", " ", " ", " "]

puts "Welcome to Tic Tac Toe!"

move(board, "5", "X")
move(board, "1", "O")
move(board, 4, "X")
move(board, 0, "O")

display_board(board)

turn(board)
```

Notice that before the game even really starts, we hard code an execution of two moves, X to the middle position ("5"), and O to the top left ("1").
Notice that before the game even really starts, we hard code an execution of two moves, X to the middle position (4), and O to the top left (0).

When we run the CLI, we'd see:

Expand Down Expand Up @@ -267,9 +270,9 @@ board = [" ", " ", " ", " ", " ", " ", " ", " ", " "]

puts "Welcome to Tic Tac Toe!"

move(board, "5", "X")
move(board, "1", "O")
move(board, "2", "X")
move(board, 4, "X")
move(board, 0, "O")
move(board, 1, "X")

display_board(board)

Expand Down Expand Up @@ -394,11 +397,11 @@ Please enter 1-9:
X | X | X
-----------
X | X | X
```

Another issue, besides only marking Xs as described above, is that the game played way too many turns! We need it to know how to quit if someone wins.

Even with these deficiencies, this `#turn` method means you are very close to building a complete Tic Tac Toe. Get excited!

<p data-visibility='hidden'>View <a href='https://learn.co/lessons/ttt-8-turn' title='Building a Tic Tac Toe Turn'>Building a Tic Tac Toe Turn</a> on Learn.co and start learning to code for free.</p>
<p data-visibility='hidden'>View <a href='https://learn.co/lessons/ttt-8-turn' title='Building a Tic Tac Toe Turn'>Building a Tic Tac Toe Turn</a> on Learn.co and start learning to code for free.</p>
53 changes: 43 additions & 10 deletions spec/turn_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,48 @@
end
end

describe '#input_to_index' do

it 'convervets a user_input to an integer' do
user_input = "1"

expect(input_to_index(user_input)).to be_a(Integer)
end

it 'subtracts 1 from the user_input' do
user_input = "6"

expect(input_to_index(user_input)).to be(5)
end

it 'returns -1 for strings without integers' do
user_input = "invalid"

expect(input_to_index(user_input)).to be(-1)
end

end

describe '#valid_move?' do
it 'returns true/false based on position' do
it 'returns true/false based on index' do
board = [" ", " ", " ", " ", "X", " ", " ", " ", " "]

position = "1"
expect(valid_move?(board, position)).to be_truthy
index = 0
expect(valid_move?(board, index)).to be_truthy

position = "5"
expect(valid_move?(board, position)).to be_falsey
index = 4
expect(valid_move?(board, index)).to be_falsey

position = "invalid"
expect(valid_move?(board, position)).to be_falsey
index = -1
expect(valid_move?(board, index)).to be_falsey
end
end

describe '#move' do
it 'allows "X" player in the bottom right and "O" in the top left ' do
board = [" ", " ", " ", " ", " ", " ", " ", " ", " "]
move(board, 1, "O")
move(board, 9, "X")
move(board, 0, "O")
move(board, 8, "X")

expect(board).to eq(["O", " ", " ", " ", " ", " ", " ", " ", "X"])
end
Expand All @@ -72,12 +94,23 @@
turn(board)
end

it 'calls the input_to_index method' do
board = [" ", " ", " ", " ", " ", " ", " ", " ", " "]
allow($stdout).to receive(:puts)

allow(self).to receive(:gets).and_return("1")

expect(self).to receive(:input_to_index)

turn(board)
end

it 'validates the input correctly' do
board = [" ", " ", " ", " ", " ", " ", " ", " ", " "]
allow($stdout).to receive(:puts)

expect(self).to receive(:gets).and_return("1")
expect(self).to receive(:valid_move?).with(board, "1").and_return(true)
expect(self).to receive(:valid_move?).with(board, 0).and_return(true)

turn(board)
end
Expand Down

0 comments on commit 7e9633f

Please sign in to comment.