Ruby Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesRuby Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now!
  #1  
Old May 12th, 2008, 04:27 PM
dg_83 dg_83 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2008
Posts: 22 dg_83 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 3 m 57 sec
Reputation 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!

Reply With Quote
  #2  
Old May 12th, 2008, 05:19 PM
dg_83 dg_83 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2008
Posts: 22 dg_83 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 3 m 57 sec
Reputation 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

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesRuby Programming > Any1 help (simple problem)!


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 

IBM developerWorks




© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway