Simple question

Hello all,
first time posting, or emailing as it may be.
Im a newbie to Ruby and I am struggling with something that seems very
very simple to me but no matter what I try it I cant get my desired out
put. (therefore I guess it isnt easy.)

I want to iterate through my_array to see if the last element is == - if
it is replace that element with X

Then I want to repeat this operation.
I want to iterate through my_array to see if the last element is == to -
NO
move to the next to last element, is this element == to - YES replace
with X"

I know how to make them ALL change at once but that isnt my goal, it
needs to be one at time. Example would be receiving user input so I
would want to change the elements one at a time. Kind of like a match
data.

starting array
my_array = [3, -, -, -, -]

Hope this makes sense and thanks in advance for any help!

Troy

The question is a bit vague. Are you just trying to iterate through the
array in reverse? Can you give an example input and output?

I was originally going to just say something like

my_array.reverse.map{|n| n == ‘-‘ ? ‘X’ : n}.reverse

It’s not the most efficient way, it would be better to just iterate from
behind once, but does that do what you want or are you looking for
different output?


Sent from Mailbox

sorry to be vague…
I tried what you suggested. It didnt work for me.

starting array
my_array = [3, -, -, -, -]

second array

my_array = [3, -, -, -, X]

third array
my_array = [3, -, -, X, X]

fourth array
my_array = [3, -, X, X, X]

what im trying to do is basically make a game. I want the user to pick
row 3 to put a game piece in so I need to see if the game piece already
exist or not. if it doesnt insert a game piece if it does check the next
empty spot to see if the game piece exist there if it doesnt insert a
game piece if it does move onto the next and so on make sense…

I really want to try and write the code myself but I have been
struggling with this one piece for two days make sense?

sorry add

num = my_array.length - 1

to the beginning of that script


Sent from Mailbox

Hi Troy,

Could you add how you’d expect the array to look like and how you would
not? Perhaps throw in some different scenarios if that applies?

Regards,

Jorge C.
Senior Software Architect/Owner
2UP Media

Zend Certified Engineer
www.bit.ly/jorgecolonzce

Sent from my mobile device

On Dec 19, 2014, at 10:27 PM, Troy L. [email protected] wrote:

sorry to be vague…
I tried what you suggested. It didn’t work for me.

starting array
my_array = [“3”, “-“, “-“, “-“, “-“]

second array

my_array = [“3”, “-“, “-“, “-“, “X“]

third array
my_array = [“3”, “-“, “-“, “X“, “X“]

fourth array
my_array = [“3”, “-“, “X“, “X“, “X“]

what i’m trying to do is basically make a game. I want the user to pick
‘row’ 3 to put a game piece in so I need to see if the game piece
already
exist or not. if it doesn’t insert a game piece if it does check the
next
empty spot to see if the game piece exist there if it doesn’t insert a
game
piece if it does move onto the next and so on… make sense…

I really want to try and write the code myself but I have been
struggling
with this one piece for two days… make sense?

On Dec 19, 2014, at 9:17 PM, Raj S. [email protected] wrote:

The question is a bit vague. Are you just trying to iterate through the
array in reverse? Can you give an example input and output?

I was originally going to just say something like

my_array.reverse.map{|n| n == ‘-‘ ? ‘X’ : n}.reverse

It’s not the most efficient way, it would be better to just iterate from
behind once, but does that do what you want or are you looking for
different output?


Sent from Mailbox https://www.dropbox.com/mailbox

Troy,

Do you just want a loop like this?

while num >= 0

if my_array[num] == ‘-’

my_array[num] = 'X'

break

end

num -= 1

end

And you would just call this every time a player makes a move?


Sent from Mailbox

Ok…if I under stand here is snippets of the full array (sorry I havent
loaded this up to git or I could show all my code but its a bit of a
mess right now)

this is my array

@board = Array.new(6, Array.new(7, "-))
@header = Array(“1”…"7)

board.unshift(header)

=> produces
1 2 3 4 5 6 7







=> select a column to drop a game piece
=> user input == 3

after the user input the new board would look like this

1 2 3 4 5 6 7






    • x - - - -

is this better?

Thank you to everyone that reached out and helped. the While loop in
fact did work now I just have to work it into my code.

John, I looked at rindex and didnt think it would work. I will research
this further tomorrow, obviously I am not clear on how it works.

I want to thank everyone!! I will post my game after it is complete in
hopes I will receive feed back and ways to improve my code!

thanks
troy

Array#rindex is your friend here. The example here

should be all you need to work out the answer.

John

On Sat, Dec 20, 2014 at 1:02 PM, Troy L. [email protected]
wrote:

… the While loop in fact did work now I just have to work it into my code.

or you could use a stack stru w a stack ptr. advancing the stack
pointer for every push. if you go this way, you dont need a loop.

kind regards --botp

It looks like you’re doing a connect four program. I would probably
create
a class Column that I can drop things in like a bucket with 7 buckets
(instances) and then have a Row class for the lines… Your Column class
would have to inherit from Array and you’d be using #unshift and #pop.
There are several ways to do it, because it doesn’t really need to be a
reverse escalator, but that’s what I’d do.

Hi Troy,

The code you showed in the previous e-mail is different.

fourth array
my_array = [“3”, “-“, “X“, “X“, “X“]

With this initialization you’re getting cols and lines swapped
compared to the previous example.

@board = Array.new(6, Array.new(7, "-“))
@header = Array(“1”…"7”)

Keep in mind that with this initialization above you will transverse
the array like this.

@board[line][column]

For something like this:
1 2 3 4 5 6 7






    • x - - - -

@board[0] # for example

=> [“1”, “2”, “3”, “4”, “5”, “6”, “7”]

@board[6]

=> [ “-”, “-”, “x”, “-”, “-”, “-”, “-” ]

@board[6][2] # line 6, col 2

=> “x”

@board[6][1] # line 6, col 1

=> “-”

Not like the ‘my_array’ example

my_array = [“3”, “-“, “X“, “X“, “X“]

Where the Array has one column.

So, you’re probably willing to loop in reverse from the last line
(6) to the second one (1) (because the first line (0) is the header).
While looping, testing the desired column (that column chosen by the
user), for example column 2 (the third one) to see if it has an “x”.
If not, you should change it to an “x” and break the loop.
It yes, it has an “x”, you should try to loop again, testing the line
before the current line (line-1) and go until line 1 (the second line;
the line after the header).

Is it something like “Conect Four” ?
(Connect Four - Wikipedia)

It’s not hard and I think you’re getting closer.
If you get stuck and need more help just drop another e-mail on this
thread.

Abinoam Jr.