Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

Stuck in a TextAdventure game

Hello, i'm making a textadventure game, and i'm making an A.I that roams around the different rooms i made. I made a human player (with user input), that roams around aswell, if they encouter eachother the human dies. At the moment i just hardcoded the options in, using a random class, so if it is possible for the A.I to go a certain way it will go that way.

take a look here:

if(getCurrentPosition().getNorth() != null) { movements.add("North"); } if(getCurrentPosition().getSouth() != null) { movements.add("South"); } if(getCurrentPosition().getWest() != null) { movements.add("West"); } if(getCurrentPosition().getEast() != null) { movements.add("East"); }

int move = rnd.nextInt(4) + 1; switch(move) { case 1: if(getCurrentPosition().getNorth() != null) { setCurrentPosition(getCurrentPosition().getNorth()); System.out.println("\n\nMONSTER MOVED NORTH"); movement = true; break; } else { break; } case 2: if(getCurrentPosition().getSouth() != null) { setCurrentPosition(getCurrentPosition().getSouth()); System.out.println("\n\nMONSTER MOVED SOUTH"); movement = true; break; } else { break; } case 3: if(getCurrentPosition().getWest() != null) { setCurrentPosition(getCurrentPosition().getWest()); System.out.println("\n\nMONSTER MOVED WEST"); movement = true; break; } else { break; } case 4: if(getCurrentPosition().getEast() != null) { setCurrentPosition(getCurrentPosition().getEast()); System.out.println("\n\nMONSTER MOVED EAST"); movement = true; break; } else { break; } default: break;

can i make these into an arraylist so the A.I goes through the valid options of rooms avalible instead of it just trying till it reaches a valid room.

(here is the github link https://github.com/MalikSh96/TAG.git)

thank you very much in advance.