Compose Operators to Implement Game Logic

John Lindquist
InstructorJohn Lindquist
Share this video with your friends

Social Share Links

Send Tweet
Published 4 years ago
Updated 3 years ago

Composing operators allows us to handle the values that come through in a variety of ways. We'll be composing operators to set up game logic which can determine the output of the word combined with the guess. When we compose operators in this manner, we are creating reusable functions that we can use across our site or even other applications. We can also test them separately from the broadcasters we're using inside of our game.

John Lindquist: [0:00] Now it's time to setup some game logic. This can be a pipe, and this is going to receive the values from our game broadcaster, since we can just wrap this around our combine.

[0:12] The first step is that we have an array where one value is null. Let's filter and this will receive an array. The function can go in here, where this would be the guess and this is the word, since this is coming from here, and this is coming from here, and combine is turning that into an array.

[0:36] We just want to make sure that both of these values are strings. Instead of typing that up by hand, I'm just going to bring in the utilities from Lodash, which are every and isString, where we can simply say, every(isString). Then this will take an array, check all of the values against an isString function.

[1:00] Now, we just have a null, which we can actually start with our game, an empty string, so now that starting with an empty string, but once we start typing, so I'll start typing, you'll see the word comes in and our guess comes in.

[1:16] Then, after the filter, let's bring in a map, where again we have the two values, the guess and the word, and the game logic is essentially converting the word into an array, so arrayFromWord, and then mapping each letter that comes through to whether or not the guess includes the letter.

[1:39] If it does, then return the letter in the map and if it doesn't, return the asterisk. Hit Save here, and once I started typing paginate, you'll see my guesses will start coming through. Then, I'll join this back together. I'll type some letters for the guess, and you can see we're able to guess the word.

[2:01] I removed the JSON.stringify, so we don't have those extra quotes. Hit Save and we can go ahead and guess what the word is.

egghead
egghead
~ 6 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today