0

Given weapons: ["rock" "scissors" "paper"]

If I did player-choice: ask "(r)ock, (p)aper, (s)cissors or (q)uit? "

how could i look for the character entered by the user in the block with word weapons attached to it

Terrence Brannon
  • 4,760
  • 7
  • 42
  • 61

4 Answers4

2

If you only want one match, and to use only the actual item names in your block, your own solution is fine. But one of the important things about Red is how you can structure your data to make things easier. For example, if you want to select items from a list based only on a known key (e.g. first character), you can make that explicit, rather than implicit.

weapons: ["r" "rock" "s" "scissors" "p" "paper"]
player-choice: ask "(r)ock, (p)aper, (s)cissors or (q)uit? "
print select weapons player-choice
Gregg Irwin
  • 151
  • 3
1
weapons: ["rock" "scissors" "paper"]
matching-weapon: func [abbrev][
    foreach weapon weapons [
        if (first weapon) = first abbrev [
            return weapon
        ]
    ]
]
Terrence Brannon
  • 4,760
  • 7
  • 42
  • 61
1
>> abr: "p"
== "p"
>> parse weapons [some [into [x: abr (print x)] | skip]  ]
paper

or

>> parse weapons [collect some [into [x: abr keep (x)] | skip]  ]
== ["paper"]
sqlab
  • 6,412
  • 1
  • 14
  • 29
-1

If you want the block starting from what is found, remove index?

switch player-choice [
    "r" [index? find weapons "rock"]
    "s" [index? find weapons "scissors"]
    "p" [index? find weapons "paper"]
    "q" ["quit"]
]
Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Time Series Lord
  • 23
  • 1
  • 1
  • 5