Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

text movement in java


  • Please log in to reply
9 replies to this topic

#1 sausage

sausage

  • Members
  • 388 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Louisville Colorado
  • Local time:05:48 PM

Posted 03 January 2009 - 01:21 AM

Alright so I ahve built a world in text,

public void coord11(...)
{
	 System.out.println("##############");
	 System.out.println("#P						 #");
	 System.out.println("#		###			#
	 System.out.println("#		###			#");
	 System.out.println("##############");
	 System.out.println("1) Right");
	 System.out.println("2) Down");
	 select = keyboard.nextInt();//I do not like switch structures... nor do I know how to use them
	 if(select==1)
	 coord21(...);
	 if(select==2)
	 coord12(...);
That's pretty much my code, the ... are instances of other objects and are unimportant for the question and the world is much bigger than that I would copy-paste but my code is not on this comp

What I'm trying to figure out is how I can move the P from place to place without having to make a seperate method for each movement, It takes a very long time.

This may not sound important but when your map is 10x32 you need to make 320 methods like that to get it to work, and then you have to debug depending on how concentrated you were when you made it.

I know it's possible in other languanges or maybe I just need an applet or something like that but I definetely need something, I haven't updated my sig in three weeks because making my forest is a freaking pain. By the way, I have not used interfaces or enums or whatever else is there. (I have never been taught how) If I need to you can have me post my whole program later to look at it, my sig does not have this code.
If I'm posting, I probably have something horribly wrong with my computer, there's no obvious explanation for it, that's just the way it is.

BC AdBot (Login to Remove)

 


#2 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:05:48 PM

Posted 03 January 2009 - 09:36 AM

when your map is 10x32 you need to make 320 methods


You only need four methods, one to handle each direction of movement (assuming that diagonal moves are not allowed). Then you need a single method to make sure that the move is allowed, ie, that it can go from one coordinate to the next. All of your map data pretty much has to be stored in a multidimensional array. Since I don't have the rest of your code, perhaps you have already done this. that is the only way I can think of that will allow you to track where walls, empty spaces, and your player are.

You need one method to update the array if the player is able to move. And you need one method that draws the map by retrieving the data from the arrays. Of course you also need listeners to capture key strokes.

I am not sure about your logic behind using an applet. How does running this in a browser have any effect on writing the code to move the character? Applets simply allow for an application to run within the browser. They don't do anything else, not have any magic capabilities. :thumbsup:

I know it's possible in other languanges

This is possible in every language since it relies on basic data structures and event handlers.

#3 sausage

sausage
  • Topic Starter

  • Members
  • 388 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Louisville Colorado
  • Local time:05:48 PM

Posted 03 January 2009 - 02:44 PM

You only need four methods, one to handle each direction of movement (assuming that diagonal moves are not allowed). Then you need a single method to make sure that the move is allowed, ie, that it can go from one coordinate to the next. All of your map data pretty much has to be stored in a multidimensional array. Since I don't have the rest of your code, perhaps you have already done this. that is the only way I can think of that will allow you to track where walls, empty spaces, and your player are.


Huh... can I have an example of a basic method of movement and no I have not set up a multi-dimensional array, I don't fully understand those yet, I only know single dimensions

I haven't gone back to my mom's where my whole code is so I can't give exactly what I have right now but all I'm doing is retyping the world while moving the player from one place to the next and changing the if statements around, its taking forever and its really annoying.

I can try to do what your saying but no gaurantees. I'll edit if I can figure it out.

EDIT: No, haven't got it yet but I forgot to ask one thing, is there an easier way to make every array a seperate character without doing each one seperately? I can do it seperately but it will take a while.

EDIT2: Ok, ignore above edit. Still working on getting it to work

EDIT3: Alright I got the array set up almost perfectly I can seem to get the right side of #'s to show up
import java.util.*;

public class Acadaa
{
	public static Scanner keyboard = new Scanner(System.in);
	public static int select;
	public static String[][] map = new String[10][4];
	public static void main(String[] args)
	{
		for(int i=0;i<=9;i++)
		{
			map[i][0] = "#";
			map[i][3] = "#";
			map[i][1] = " ";
			map[i][2] = " ";
			if(i<=3)
			{
				map[0][i] = "#";
				map[9][i] = "#";
			}
		}
		System.out.println(map[0][0]+map[1][0]+map[2][0]+map[3][0]+map[4][0]+map[5][0]+map[6][0]+map[7][0]+map[8][0]+map[9][0]);
		System.out.println(map[0][1]+map[1][1]+map[2][1]+map[3][1]+map[4][1]+map[5][1]+map[6][1]+map[7][1]+map[8][1]+map[9][1]);
		System.out.println(map[0][2]+map[1][2]+map[2][2]+map[3][2]+map[4][2]+map[5][2]+map[6][2]+map[7][2]+map[8][2]+map[9][2]);
		System.out.println(map[0][3]+map[1][3]+map[2][3]+map[3][3]+map[4][3]+map[5][3]+map[6][3]+map[7][3]+map[8][3]+map[9][3]);
	}
}

EDIT4: ok fixed above problem, now I've got a new one, how exactly do I check for which instance contains where the player is and then how do I change the coordinate... Take your time if you need to, I don't need this immediately.

Edited by sausage, 03 January 2009 - 03:36 PM.

If I'm posting, I probably have something horribly wrong with my computer, there's no obvious explanation for it, that's just the way it is.

#4 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:05:48 PM

Posted 03 January 2009 - 03:39 PM

Sorry, had to teach a class.

How about just trying to get a character to move from left to right, with no walls? That would be the easiest way to start. I only have time to do this in pseudo code, but lets pretend that you just have a single array that contains nothing but the player, and we will simply make it 5 space hallway. So we initialize that array with something like:
String[] world = {" ", " ", "P", " ", " "};

So you have your player sitting smack dab in the middle of the world. We mark his location in the world by keeping an index. Since this is simply a one dimensional world world, we only need to keep one variable, so the following should work just fine:
int index = 2;

Since we do not have walls, all we need to do is make sure that the character remains inside the array, and that he has a place to go. So for instance, the player decides he want to move left, so using your bit of code:
if(select==4){

}

All we need to do is check and make sure that we can indeed move left. We know that the lowest index in the array is zero, and we have no walls, so all we need to do to check and see if we can move left is:
if( (index-1)>-1){
   //replace the current location of the player with a blank space
   world[index] = " ";

   //subtract one from the index
  index --;

   //add the player back in
   world[index] = "p";
}

When you want to actually draw out the world, then you just need to build a string out of the contents of the array:
String s ="";

for(int i=0; i<world.length; i++){
   s = s + world[i];
}

System.out.println(s);

In your instance, you would just have an array of arrays, which is no big deal, and you would have an extra variable to keep track of whic harray that you were in. you would also be checking to see if the square in which you were trying to move had a wall in it.

That help a little?

#5 sausage

sausage
  • Topic Starter

  • Members
  • 388 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Louisville Colorado
  • Local time:05:48 PM

Posted 03 January 2009 - 03:58 PM

not really, it seems like a good idea but when I try it out, I get some wierd output like:

p
p p

I think that the draw code will only work correctly the first time it runs which means I need to find a way to run it correctly using a different code... this is so confusing

EDIT: that or I put it in wrong just as a test, I left out the choosing thing I just told it to move on so if you want to help out here's my code (it's just copied from your snippets with a few changes, nothing major though)

import java.util.*;

public class test
{
	public static void main(String[] args)
	{
		String[] world = {" ", " ", "p", " ", " "};
		int index = 2;
String s ="";

for(int i=0; i<world.length; i++){
   s = s + world[i];
}

System.out.println(s);
		if( (index-1)>-1){
   //replace the current location of the player with a blank space
   world[index] = " ";

   //subtract one from the index
  index --;

   //add the player back in
   world[index] = "p";


}
   for(int i=0; i<world.length; i++){
   s = s + world[i];

}
   System.out.println(s);
}



}

I would've actually tried to improve but I needed to make sure I could get it to work... which I can't seem to do

EDIT2: well, that was an easy fix, now to the 2-d thingy and/or being able to clear the screen... maybe

EDIT3: well, never mind, I got it using my acadaa class without using that extra string, here's the whole code if you want to check it out/try and make it more efficient but keep it not confusing...
import java.util.*;

public class Acadaa
{
	public static Scanner keyboard = new Scanner(System.in);
	public static int select;
	public static String[][] map = new String[10][4];
	public static int check = 0;
	public static int indexA = 1;
	public static int indexB = 1;
	public static void main(String[] args)
	{
		for(int i=0;i<=9;i++)
		{
			map[i][0] = "#";
			map[i][3] = "#";
			map[i][2] = " ";
			map[i][1] = " ";
			if(i<=3)
			{
				map[0][i] = "#";
			}
			
		}
			map[9][1] = "#";
			map[9][2] = "#";
			move();

	}
	public static void move()
	{
		map[indexA][indexB] = "P";
		show();
	}
	public static void show()
	{
		System.out.println(map[0][0]+map[1][0]+map[2][0]+map[3][0]+map[4][0]+map[5][0]+map[6][0]+map[7][0]+map[8][0]+map[9][0]);
		System.out.println(map[0][1]+map[1][1]+map[2][1]+map[3][1]+map[4][1]+map[5][1]+map[6][1]+map[7][1]+map[8][1]+map[9][1]);
		System.out.println(map[0][2]+map[1][2]+map[2][2]+map[3][2]+map[4][2]+map[5][2]+map[6][2]+map[7][2]+map[8][2]+map[9][2]);
		System.out.println(map[0][3]+map[1][3]+map[2][3]+map[3][3]+map[4][3]+map[5][3]+map[6][3]+map[7][3]+map[8][3]+map[9][3]);
		option();
	}
	public static void option()
	{
		System.out.println("1) Right");
		System.out.println("2) Left");
		System.out.println("3) Down");
		System.out.println("4) Up");
		select = keyboard.nextInt();
		if(select == 1)
		right();
		else if(select == 2)
		left();
		else if(select == 3)
		down();
		else if(select == 4)
		up();
	}
	public static void right()
	{
		if((indexA+1)<9)
		{
			map[indexA][indexB] = " ";
			indexA++; 
			move();
		}
		else
		{
		System.out.println("Ouch!");
		show();
	}
	}
	public static void left()
	{
		if((indexA-1)>0)
		{
			map[indexA][indexB] = " ";
			indexA--; 
			move();
		}
		else
		{
		System.out.println("Ouch!");
		show();
	}
	}
	public static void down()
	{
		if((indexB+1)<3)
		{
			map[indexA][indexB] = " ";
			indexB++; 
			move();
		}
		else
		{
		System.out.println("Ouch!");
		show();
	}
	}
	public static void up()
	{
		if((indexB-1)>0)
		{
			map[indexA][indexB] = " ";
			indexB--; 
			move();
		}
		else
		{
		System.out.println("Ouch!");
		show();
	}
	}
}

Thanks for the help!

Edited by sausage, 03 January 2009 - 04:19 PM.

If I'm posting, I probably have something horribly wrong with my computer, there's no obvious explanation for it, that's just the way it is.

#6 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:05:48 PM

Posted 03 January 2009 - 04:16 PM

You need to make the part that draws the map a method so that you don't have to keep redoing it.

#7 sausage

sausage
  • Topic Starter

  • Members
  • 388 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Louisville Colorado
  • Local time:05:48 PM

Posted 03 January 2009 - 04:22 PM

I still refuse to update sig, haven't connected it to code yet... neither do I plan on it til I get to mom's (somepoint tonight maybe)
If I'm posting, I probably have something horribly wrong with my computer, there's no obvious explanation for it, that's just the way it is.

#8 sausage

sausage
  • Topic Starter

  • Members
  • 388 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Louisville Colorado
  • Local time:05:48 PM

Posted 04 January 2009 - 06:49 PM

Alright, through this topic I have officially finished what I needed to finish in order to update sig so, it is now official, my sig has been updated!
If I'm posting, I probably have something horribly wrong with my computer, there's no obvious explanation for it, that's just the way it is.

#9 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:05:48 PM

Posted 04 January 2009 - 09:09 PM

So what are you saying? You are spamming your sig? :thumbsup:

#10 sausage

sausage
  • Topic Starter

  • Members
  • 388 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Louisville Colorado
  • Local time:05:48 PM

Posted 04 January 2009 - 09:14 PM

no, I'm just advertising my game... tell me thats not against the rules... It was only that once
If I'm posting, I probably have something horribly wrong with my computer, there's no obvious explanation for it, that's just the way it is.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users