Giter Club home page Giter Club logo

ttt-7-valid-move's Introduction

Validating Tic-tac-toe Input

Objectives

  1. Use either if statements or boolean expressions to control the return value of a method.
  2. Use a "helper method" - a method called within another method - to make your code more readable.

Overview

In our previous tic-tac-toe lab, we built a method, #position_taken?, that checks to see if the user's submitted position is free or already filled with a token. This is a type of validation. Our #position_taken? method protects our game from breaking when the user (accidentally or otherwise) submits a position that isn't available.

Our validation is still incomplete however. What if a user submits a position that isn't even on the board? A more complete validation might look something like this:

  1. You must move to a position within the tic-tac-toe board.
  2. The position must be vacant, not currently taken by a player.

In this lab, you'll build a #valid_move? method that accepts a board and an index to check and returns true if the move is valid and false or nil if not. A valid move means that the submitted position is:

  1. Present on the game board.
  2. Not already filled with a token.

Helper Methods

We've already defined a method, #position_taken? that handles the second part of our validation procedure (You'll have to re-define it in this lab or copy over the code you've already written). Consequently, we can call that method inside of our #valid_move? method.

The #position_taken? method can thus be referred to as a helper method––a method that handles a discrete unit of behavior and is used inside of other methods to carry out a larger task.

The #position_taken? method can be used directly in a conditional expression, for example:

def some_new_fabulous_method
	if position_taken?
		execute something
	else
		execute something else
	end
end

Instructions

This lab is test-driven, so run the test suite and use the output to help you solve this one. Here are a few things to keep in mind:

  • Arrays are indexed starting at 0, and a user playing your game is unlikely to know that. When a user types in that they'd like to fill position 1, they really mean that they'd like to fill the board array at the index of 0.
  • That being said, the process of converting user input falls outside the scope of #valid_move?. By the time the user's selection reaches your #valid_move? method, you may assume it has already been converted from the user's point of view (squares 1 through 9) to the array's point of view (squares 0 through 8).
  • If the user's selection does not fall in the range of 0 through 8, their input is invalid. There are a few different ways to check to see if a number is included in a range. Look up the #between? method for starters.
  • There are two conditions that need to be met in order for this method to return true––that the position is on the board and that the position is not taken. Think about how to construct a method that must check two conditions. Can you use two if statements? What about a boolean operator like &&?
  • Think back to our lessons on the concept of truthiness. Both false and nil are considered to be "falsey". So, either a false or nil return value for an invalid move will suffice.

View Validating Tic-tac-toe Input on Learn.co and start learning to code for free.

ttt-7-valid-move's People

Contributors

annjohn avatar aturkewi avatar aviflombaum avatar bhollan avatar deniznida avatar gj avatar loganhasson avatar maxwellbenton avatar mendelb avatar peterbell avatar sammarcus avatar sophiedebenedetto avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

ttt-7-valid-move's Issues

lessons

im just emailing to find out if my lessons are in order. I was prompted to do so by one of your employees as my lessons seem to be ok with me but maybe they were move from the chronologically order

seema said a minute ago
ok

seema said just now
yeah maybe use the raise an issue feature and report this

seema said just now
because they may be in the wrong order now

sam said just now

Bolded "do"

Hi Learn.co curriculum owners,

In this lesson, there is an example of how a method can be used directly in a conditional expression. The bolded "do" in the example threw me off -- Although I have seen "do" in spec/rspec code before, I have never seen it used in methods. So when I tried writing code for my method with "do", it failed several times. I reached out to Learn.co community members through Ask a Question for help with my method (and questions about "do"), and they told me that it was a typo: the "do" in "do some stuff" and "do some other stuff" should not be bolded; "do" is simply part of the sentence.

Simply unbolding "do" would greatly clarify the example! Attached is a screenshot of the example code I am referring to.

Happily Learning :),
Margaret

issue with valid tic tac toe move

Disapearing lab files

Something is wrong with the Learn program, not finding my old lessons. tried to delete and refork/clone again, not showing up. Many completed labs not showing up when I also type learn open afterwards, shows up as a new blank lesson again.

Tic Tac Toe Valid Move lab

Hello all,

Issue: The Readme of this lab mentions that you have to account for a user's input of 1-9 when they select a space on the Tic Tac Toe board and convert that number to an array's index number system of 0-8.

From Readme:
"Arrays are indexed starting at 0 and a user playing your game is unlikely to know that. When a user types in that they'd like to fill position 1, they really mean that they'd like to fill the board array at the index of 0. You'll have to account for this in your method."

There are 3 errors you have to clear to pass this lab and all of the test use an index number to check if your code meets the expectations of the test.

Test expectations:
it 'returns true for a valid position on an empty board' do
board = [" ", " ", " ", " ", " ", " ", " ", " ", " "]
index = 0
expect(valid_move?(board, index)).to be_truthy
end

it 'returns nil or false for an occupied position' do
board = [" ", " ", " ", " ", "X", " ", " ", " ", " "]
index = 4

expect(valid_move?(board, index)).to be_falsey

end

it 'returns nil or false for a position that is not on the board' do
board = [" ", " ", " ", " ", " ", " ", " ", " ", " "]
index = 100

expect(valid_move?(board, index)).to be_falsey

end

end

Recommendation:
I humbly recommend to remove the section pasted above from the Readme.

Possible edits in Lesson Instructions

It would be helpful to mention that the last lesson's method position_taken? needs to be copy pasted into this lessons lib/ file.

Information on the formatting of adding methods to position (i.e. position.to_i.between?(a, b)) would also be helpful. The documentation isn't specific enough.

Also the sequence of methods could be emphasized. This could be included in the last section, Instructions. (i.e. position.between?(1,9).to_i is incorrect while position.to_i.between?(1, 9) is correct)

Should remove confusing comment in specs

Student just thought the specs were broken because they said "needed more specs". I'm open to us adding new tests, but that suggestion should live in an issue, not the code.

See #15 for proposed solution

Clarification for the #between? method

The use and syntax of the #between? method in this lab is clear, but it is not clear if it is inclusive or exclusive i.e. its semantics. If we use the variable "number" to query number.between?(1, 3), are the integers 1 and 3 also included in the acceptable numbers? In other words is it the case that the method would apply to x such that 1 <= x >= 3 or if the method would apply to x such that 1 < x < 3 only? I use the terms "inclusive" for the former example and "exclusive" for the latter example though I don't know if that is the proper terminology. (Sorry, philosophy major here.) Upon further searching, I found that it's inclusive, but explicitly stating this in the lab would help greatly with its clarity.

First two position variables are strings should be ints

From valid_move_spec.rb

7. board = [" ", " ", " ", " ", " ", " ", " ", " ", " "]
8. position = "1" #<<<Should be int
9. expect(valid_move?(board, position)).to be_truthy


13. board = [" ", " ", " ", " ", "X", " ", " ", " ", " "]
14. position = "5"#<<<Should be int

16. expect(valid_move?(board, position)).to be_falsey

The last position variable(line 21) is an int. The other two should match.

Wrong code passed tests

Hello!

When I first started this lab (ttt-7-valid-move) I was able to pass it with the following method:

def valid_move? (board, index)
board[index] == " "
end

The lab requires using #position_taken? as a helper method so I figured that was not the proper way to complete it, and was able to pass it the right way with different code. Not sure if it actually matters, but I figured I would just bring it to your attention. Thanks!

-Tim

Telling Learn student to account for user choice to correct index conversion

The instructions tell the Learn student to compensate for a user selecting between 1–9 positions on the Tic Tac Toe board, but really meaning 0-8 indexes of the board array. However, the first test passes in 0 as the index parameter value and expects "truthy"; which is confusing because, if the student has done something like (board_index = index - 1), this test will not pass because -1 isn't a valid index.

The first test should really pass in "1" so the student can convert that to index "0", like the instructions say to do.
screen shot 2017-04-12 at 8 46 25 am
screen shot 2017-04-12 at 8 46 55 am

Index vs. Index-1

The lab specifies that the user input will be 1-9, suggesting that we need to subtract 1 from the index param that's passed to valid_move?. But in the rspec file, the user input is the actual index (first test case has index == 0).

confusing instruction...

Why is the following instruction in this lab?

"Arrays are indexed starting at 0 and a user playing your game is unlikely to know that. When a user types in that they'd like to fill position 1, they really mean that they'd like to fill the board array at the index of 0. You'll have to account for this in your method."

It's confusing...

You're already telling me in the next instruction:

"The valid positions on the board, from the user's point of view, are 1-9, but we'll be checking to see if we have a number between 0-8 because we'll be looking at the array index. If the user inputs a number not included in that range, their input is invalid. There are a few different ways to check to see if a number is included in a range. Look up the #between? method for starters."

Just feedback

I'm having general problems

This is not necessarily for this lab, but for the whole program so far. My environment seems to be setup ok. The only issue being git, which seems to be common.

The "Ask a Question" button has never appeared for me on any of my lessons:

screen shot 2016-02-12 at 8 44 20 pm

Also, while I am trying to test-drive my labs, I am getting some pretty obscure messages back. Here is an example:

screen shot 2016-02-12 at 8 52 16 pm

There have been a few times where I'm certain I have the right answer (and verified by finding the solution on github) and it still does not work. Frustrating to say the least. Any help would be greatly appreciated! Thanks!

postion_taken? does not require "" and nil to pass

The previous lesson requires position_taken? to read "" and nil as false, whereas this lesson does not test for this requirement. The below code has that requirement commented out and passes the test.

def valid_move?(board, pos)
 return true while pos < 9 && position_taken?(board, pos) == false
 return false
end

def position_taken?(board, pos)
  if board[pos] == " " #|| board[pos] == "" || board[pos] == nil
    return false
  end
  return true
end

As this lesson builds on the previous one, it is expected to have the same requirements and should not pass without it. Changing the requirements can cause confusion.

Tests dont jive with the readme

Readme instructs students to convert user input to an index:

Arrays are indexed starting at 0 and a user playing your game is unlikely to know that. When a user types in that they'd like to fill position 1, they really mean that they'd like to fill the board array at the index of 0. You'll have to account for this in your method.

But the tests pass the index directly in:

 it 'returns true for a valid position on an empty board' do
    board = [" ", " ", " ", " ", " ", " ", " ", " ", " "]
    index = 0
    expect(valid_move?(board, index)).to be_truthy
  end

If the student converts a user input of 0 to an index, then index will be -1 and will be a falsey value, not truthy.

Related commit: learn-co-students/ttt-7-valid-move-q-000@bb68b5f

@PeterBell

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.