#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2008
    Posts
    29
    Rep Power
    0

    Any1 help (simple problem)!


    Hi,

    I'm trying to make a program that is a puzzle game. It's pretty simple, but I'm new to Ruby so am caught up here.

    The program has a string that gets modified as the game progresses. The rule I'm stuck on should iterate through the string, and each time three "I"'s appear in order, it should output them with *'s around them (to draw the player's attention).

    For example, assume the string was 'MIIIIUIIUIII'
    The output should be:
    M*III*IUIIUIII
    MI*III*UIIUIII
    MIIIIUIIU*III*I

    The logic I'm thinking to use would be (in english) starting from the first to the last character, if there are any occurences of "III" output the first one surrounded by "*", then move to the character immediately after the first group of III's and repeat until the end of the string is reached, or until a position is reached from which that position to the end of the string contain no more instances of "III".

    Here's my work in progress on the part of the game relating to that question (again, I'm very new):

    $my_string = 'MIIIIUIIUIII'
    count = 0
    ending = $my_string.length
    while $my_string[ count...ending ].include? 'III'
    count = 0
    display = $my_string
    display['III'] = '*III*'
    puts display
    count += 1
    end

    Which is outputing (not what I want):
    M*III*IUIIUIII
    M**III**IUIIUIII
    M***III***IUIIUIII
    M****III****IUIIUIII
    M*****III*****IUIIUIII
    M******III******IUIIUIII
    M*******III*******IUIIUIII
    M********III********IUIIUIII
    M*********III*********IUIIUIII



    THANKS!
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2008
    Posts
    29
    Rep Power
    0
    I got it!!

    length = $my_string.length
    count = 0
    index = 0
    while index < length
    if $my_string[index..(index + 2)] == 'III'
    display = $my_string.dup
    display[ index..(index + 2) ] = ' *III* '
    puts display
    count += 1
    end
    index += 1
    end

IMN logo majestic logo threadwatch logo seochat tools logo