• Post Reply Bookmark Topic Watch Topic
  • New Topic

Finding a path from one side to other in 2D Array  RSS feed

 
Ziva Beckett
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a 2D array of characters. I'm trying to detect if a player connected both sides of the table (in the example below, the 0 player connected both ends). How would I do this?



* - invalid
0 - player 1
X - player 2
. - empty
'-' - horizontal connection
'|' - vertical connection
 
Anayonkar Shivalkar
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to CodeRanch!

I would reduce this problem to trees.

Store all the connection of a player in form of tree, and then try to find a path from head node to leaf node - such that head node belongs to one end, and leaf node belongs to other node.

I hope this helps.
 
Ziva Beckett
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you please explain a bit more?
I don't know much about trees.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
step 1) Turn off your computer.
step 2) get lots of paper, some pencils, and at least one large eraser
step 3) work out how to do it by hand.
step 4) work out how to explain to a ten year old child how to do it. LITERALLY write down the steps.

Once you have done the above, and ONLY once you have done the above, should you even consider turning your computer back on.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ziva Beckett wrote:Can you please explain a bit more?

Funny, because that's exactly what I was going to ask of you.

I'm a bit confused by your "array". Are those [.,-,|] squares positions or connectors? I only ask because you've flagged '.' as meaning "empty", which suggests that you could put a letter in there. Also:
  • your 'O's seem to be out of alignment with your 'X's.
  • you seem to be missing some connectors.
  • but I could well be wrong, because I don't understand the "rules" of your matrix.

    About the only thing I can add to Fred's excellent advice is: separate your data from how it's displayed completely.

    From the look of things, your data is a 2D array of X's and O's; and all those "connector-thingys" just visual candy.
    What you need is a way to translate your data for display.

    HIH

    Winston

    [Edit] Actually, I think I've worked it out now. The 'X's and 'O's are static and the idea is to connect them up from one side to the opposite one without crossing an opponent's connection. Am I right? If so, forget the last part of my post and stick to Fred's advice. If not, please explain the rules to us.
    However, if I am right, then I'd say that the connectors are your data, and the 'X's and 'O's are the eye-candy.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!