Really hard to explain...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • doubleac
    New Member
    • Sep 2008
    • 3

    Really hard to explain...

    Hi

    This is the first time for me asking for help in forums on the net. I hope ill make myself understood. And sorry for my bad english.

    I have 4 classes: Spelare, Händelser, SpelPlan and Start.

    In the class "Händelser" a random message apperas, if the players land on a couple of specific squares. A message is supposed to change the players status, such as position (move to GO), money (paying or reciving).

    The class "Spelare" have all the necessary methods for moving, adding money, and stuff like that.

    The SpelPlan is just the GameBoard, with methods for changing the owner and so on.

    In the class Start (where the method "main" is) i create a new player and a new SpelPlan like this:
    Code:
    private spelPlan planen=new spelPlan();
    private Spelare[] spelare=new Spelare[2];
    and if i for instance want to change the position of the player:

    Code:
    player[0].sättNuvarandePosition(20);
    I can change the players position, adding money and all that from the Start class. If the player land on a free space, i take information from the SpelPlan class about that free space, and the player can choose to buy it.

    When the player arrives on specific spaces for messages, i create a new Händelser:
    Code:
    if (pos==4 || pos==8)
    {
            new händelser();	
    				
    }
    And a random message apperas. BUT i want that random message, what ever that is, to be able TO CHANGE the status of the player i created in the Start class. For instanse: "Move 3 steps forward" would be able to call the method "movePlayer " in the Player class and change his position.

    Can it be done, and if yes: how?

    I really hope you guys understood what i ask, 'cos im confused myself.
    Im glad for any help i get.

    I dont know if the code is neccesary but here is a simplified versions:
    The SpelPlan i just one array and one matrix, and i have already wrote the important codes in Start.

    Code:
     
    import java.util.*;
    public class Spelare {
    	private String namn;
    	private int pengar, position, id;
    	spelPlan planen=new spelPlan();
    	
    	Spelare(){}
    	
    	Spelare(String namn){
    		this.namn=namn;
    		
    	}
    	Spelare(int id,String namn){
    		this.id=id;
    		this.namn=namn;
    		pengar=20000;
    	}
    	
    	//this methods move the player on a 40 square board (like monopoly)
    
    	public int flyttaSpelare(int steg){
    		if((position + steg) > 40){
    			position = (position + steg - 40);
    		}
    		else
    			position =position+steg;
    		return geNuvarandePosition();
    	}
    //this return the current position of the player
    	public int geNuvarandePosition(){
    		return position;
    	}
    //this changes the position of the player
    	public int sättNuvarandePosition(int position){
    		this.position=position;
    		return position;
    	}
    	
    		
    	
    }
    
    
    import java.util.Random;
    
    public class händelser {
    	private Random chans = new Random();
    	private spelPlan rutor= new spelPlan();
    	
    	int slump;
    	
    	händelser()
    	{
    		slumpChans();
    	}
    	//this method creates a random message from the String array
    	public String slumpChans()
    	{	
    		String[] moveMessage = {"Move 3 steps forward","Collect 300$",
    "and so on...."};
    		slump=chans.nextInt(moveMessage.length);
    		System.out.println("du har kommit till chans");
    		System.out.println(moveMessage[slump]);
    		return moveMessage[slump];
    		
    		
    	}	
    
    }
  • MarkoKlacar
    Recognized Expert Contributor
    • Aug 2007
    • 296

    #2
    Hi,

    Visst går det att göra! Sure it can be done!

    I would strongly recommend you to not use 'å', 'ä', 'ö' in you classnames, variables och method names.....

    You need a String in the Spelare class that keeps track of the status, and you could create the methods getStatus() and setStatus(Strin g newStatus) or similar...

    The setStatus method could also contain the random generator.

    Lycka till! Good luck!

    /MK

    Comment

    • doubleac
      New Member
      • Sep 2008
      • 3

      #3
      Hi and thanks for the reply (tack!)

      Okey, ill have it in mind, thanks for the tip.

      But i dont think i understood your answer. Well maybe i didnt explain good enough.

      I already have a similar methods in the class Spelare for the status (the one you suggested), like "sättNuvarandeP osition", "flyttaSpel are" and so on.
      What i need is the Händelser class to CHANGE (calling (anropa) any of the methods in the class Spelare) for instanse the position of the Player class the one i created in Start!

      Im going to write it in swedish, i hope its ok.

      I klassen Start har jag skapat ett objekt (säger man så?) av Spelare.
      Code:
      Spelare new sp();
      Därifrån kan jag genom att anropa t.ex.

      Code:
      sp.flyttaSpelare(5); //det här kommer att förflytta spelaren 5 steg.
      ..flytta spelare 5 steg. Nu hamnar spelare på en ruta som ska generera ett medelande, t.ex "Gå fram 3 steg". Det här går smärtfritt, eftersom objektet av Spelare som jag har skapat ligger i klassen Start.

      Men jag vill inte ha dessa medelande i classen Start, för det skulle bli för mycket kod på samma ställe. Plus att projektet kräver att man har många klasser.Därför vill jag, varje gång någon spelare hamnar på en likadan ruta, skapa ett objekt av klassen Händelser som skall sköta om sånt.
      Alltså:Hur skall jag kunna styra objektet "sp" (t.ex. ändra position), som är skapad i klassen Start, från klassen Händelser?

      Du kanske redan har svarat på frågan, men isf förstod jag inte riktigt.

      Tack!

      Thx :)

      Comment

      • chaarmann
        Recognized Expert Contributor
        • Nov 2007
        • 785

        #4
        Originally posted by MarkoKlacar
        Hi,
        I would strongly recommend you to not use 'å', 'ä', 'ö' in you classnames, variables och method names.....
        /MK
        Why is Suns's Java API written all in English, and there are no local languages available? I mean the same API functions with German function names, Chinese or Arabic ones? Wouldn't it be easier for a Chinese or Arabic person to write source code in his own language with his own character set and keyboard?

        I would strongly recomment not to use any language except English.
        All of us understand english, but only a few speak your language.
        Most times you can understand quickly the meaning of a function or variable by its english name. Just ask yourself: How likely is it that you would help somebody where you can understand what his code is doing within one minute, and on the other side how likely is it that you would help somebody where you need hours to understand because it's all in chinese, kisuaheli or other strange language you don't know?

        With this in mind, you would use string constants instead of putting the strings directly.
        Referring to your source code, you would change:
        Code:
        System.out.println("du har kommit till chans");
        to:
        Code:
        private static final String YOU_HAVE_TO_COMMIT_UNTIL_CHANGED ="du har kommit till chans" // or whatever that means in english 
        System.out.println(YOU_HAVE_TO_COMMIT_UNTIL_CHANGED);
        And if you want to become very professional, read these strings from localized text files using java class java.util.Prope rties.

        Comment

        • doubleac
          New Member
          • Sep 2008
          • 3

          #5
          Hi

          Yeah i just started with swedish namnes (for class namnes and methods), didnt think about back then. From now on i wont.
          Originally posted by doubleac
          Okey, ill have it in mind, thanks for the tip.
          I refered to that in my last reply.

          And i know that ill get a lot more help if i wrote in english, and i did, but couldnt explain good enough so i wrote another more detailed version in swedish because MarkoKlacar knows swedish. But of course you are right.

          thx

          Comment

          • myusernotyours
            New Member
            • Nov 2007
            • 188

            #6
            Thought I saw somewhere that as a policy on this site, Posts should only be in english... here

            Kwani nini mbaya bwana?

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              Originally posted by myusernotyours
              Thought I saw somewhere that as a policy on this site, Posts should only be in english... here

              Kwani nini mbaya bwana?
              You're right; I wanted to send the OP a personal message about it but you beat me
              so I'll write it here: please use the English language only here; it is the lingua Franca
              overhere (and in a lot of other forums for that matter). If you're having problems
              you can always PM a moderator or administrator and s/he will help you out.

              kind regards,

              Jos (moderator)

              ps. and Swahili is not done either ;-)

              Comment

              • Kevinyy
                New Member
                • Jul 2008
                • 77

                #8
                Originally posted by chaarmann
                Why is Suns's Java API written all in English, and there are no local languages available? I mean the same API functions with German function names, Chinese or Arabic ones? Wouldn't it be easier for a Chinese or Arabic person to write source code in his own language with his own character set and keyboard?

                I would strongly recomment not to use any language except English.
                All of us understand english, but only a few speak your language.
                Most times you can understand quickly the meaning of a function or variable by its english name. Just ask yourself: How likely is it that you would help somebody where you can understand what his code is doing within one minute, and on the other side how likely is it that you would help somebody where you need hours to understand because it's all in chinese, kisuaheli or other strange language you don't know?

                With this in mind, you would use string constants instead of putting the strings directly.
                Referring to your source code, you would change:
                Code:
                System.out.println("du har kommit till chans");
                to:
                Code:
                private static final String YOU_HAVE_TO_COMMIT_UNTIL_CHANGED ="du har kommit till chans" // or whatever that means in english 
                System.out.println(YOU_HAVE_TO_COMMIT_UNTIL_CHANGED);
                And if you want to become very professional, read these strings from localized text files using java class java.util.Prope rties.
                English is the universal language. Chinese are required to learn english in schools. so it would not be any more difficult to program in chinese than english

                Comment

                • myusernotyours
                  New Member
                  • Nov 2007
                  • 188

                  #9
                  Originally posted by JosAH

                  ps. and Swahili is not done either ;-)
                  Would you by any chance know what that meant? Am willing to translate it for you. Effectively turning this in to a language forum. Isn't it anyway...

                  Comment

                  • JosAH
                    Recognized Expert MVP
                    • Mar 2007
                    • 11453

                    #10
                    Originally posted by myusernotyours
                    Would you by any chance know what that meant? Am willing to translate it for you. Effectively turning this in to a language forum. Isn't it anyway...
                    I don't really know what it means; I just recognized it as being Swahili. It has
                    a "sir" or "mister" in it and something "bad" but that's as far as I understood it ;-)

                    kind regards,

                    Jos

                    Comment

                    • myusernotyours
                      New Member
                      • Nov 2007
                      • 188

                      #11
                      Originally posted by JosAH
                      I don't really know what it means; I just recognized it as being Swahili. It has
                      a "sir" or "mister" in it and something "bad" but that's as far as I understood it ;-)

                      kind regards,

                      Jos
                      Yah Almost there. Thought you only knew computers and trout fishing ;-)
                      Loose translation "Why, what's wrong man?"

                      Regards

                      Alex.

                      Comment

                      Working...