Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 261 262 [263] 264 265 ... 796

Author Topic: if self.isCoder(): post() #Programming Thread  (Read 907901 times)

Dutchling

  • Bay Watcher
  • Ridin' with Biden
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #3930 on: February 17, 2013, 09:59:01 am »

Python trouble again D:
(please forgive my non-existent knowledge about how to call everything :P)

I have 'game' which is an (also the only) instance of the Game class, and contains all default variables and other thins like the current year.
I also have a Person class, and a bunch of instances of that (people). All of these instances also contain a reference to 'game'.

Now when I do this (in an instance of a Person class):
Code: [Select]
self.age = self.game.personAge # personAge is the default age of a newly created personself.age just refers to the value in the game class. It is not 'independent', and thus when anyone ages, they all age Dx, including the default starting agwe itself. Any idea how to fix this?
Logged

Twiggie

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #3931 on: February 17, 2013, 10:47:24 am »

sounds like python is passing a reference to age, not the value. you want to look up how to create another age object or pass by value

also doing that is really bad programming, you should just pass the age into the constructor instead
Logged

Mephisto

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #3932 on: February 17, 2013, 11:25:02 am »

Why are you setting age first in your game class rather than in your person class?
Logged

Twiggie

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #3933 on: February 17, 2013, 11:38:21 am »

yeah it'd be better to have a constant stored in your person class
Logged

MagmaMcFry

  • Bay Watcher
  • [EXISTS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #3934 on: February 17, 2013, 11:43:23 am »

yeah it'd be better to have a constant stored in your person class
Constants aren't really the best idea for a value that is supposed to change throughout the life of an object (in every sense).
Logged

Twiggie

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #3935 on: February 17, 2013, 11:56:19 am »

i mean a constant for the person to be initialised with in case the age is not specified in the constructor
Logged

MorleyDev

  • Bay Watcher
  • "It is not enough for it to just work."
    • View Profile
    • MorleyDev
Re: if self.isCoder(): post() #Programming Thread
« Reply #3936 on: February 17, 2013, 11:57:20 am »

A much better name for it would probably be "defaultPersonAge". Names of variables and classes in programming are very important, the first thing I always do with a variable or class is try to come up with a name to describe it's purpose. If a class's name is too generic, the class is too generic and so runs the risk of rapidly becoming a god class. If a good name for a class or variable can't be thought of, it's a warning you may not have figured out what the class's purpose is.

But yeah, a constant value stored in the Person class (or a constant loaded from configuation) to store the default, with 'copies' of it in the Person that can be mutated, may be the best approach.
Logged

Andrew425

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #3937 on: February 17, 2013, 02:21:05 pm »

Hi, i'm taking a Java class in school right now and i'm stuck on a question

What I want my program to look like is this. It is using the console.

      1
     21
    321
   4321
 54321
654321

Using nested for loops they want me to print that out.

so far my only idea of how to solve it is using a switch command to make spaces but I believe it's a simple matter of using the printf except that I don't know the correct syntax and the Oracle website didn't help me much.
Spoiler (click to show/hide)

I know it won't output anything correctly as of now, I'm just looking to see if I'm on the right track or if their is a much easier way.

Thanks for your time
Logged
May the mass times acceleration be with you

Twiggie

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #3938 on: February 17, 2013, 02:26:51 pm »

protip: if you need to use switch statements, generally you're doing something wrong/could make that look a lot nicer

im assuming you know how to calculate the number of spaces to the left of the number you need. im also assuming you know how to add one space to a string. i can see you already know how to use for loops

start with the empty string, add the appropriate number of spaces, then add the numbers.

then print it, and do it again for the next row.
Logged

MagmaMcFry

  • Bay Watcher
  • [EXISTS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #3939 on: February 17, 2013, 02:27:20 pm »

Spoiler: Very compact solution (click to show/hide)
« Last Edit: February 17, 2013, 02:42:18 pm by MagmaMcFry »
Logged

olemars

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #3940 on: February 17, 2013, 02:36:35 pm »

The "right" solution is to use string formatting options. A formatting of "%xd" will print a right aligned integer of x digits. Any missing digits will be replaced by whitespace.

Code: [Select]
System.out.print(String.format("%10d", number));
Logged

Mephisto

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #3941 on: February 17, 2013, 02:38:04 pm »

The "right" solution is to use string formatting options. A formatting of "%xd" will print a right aligned integer of x digits. Any missing digits will be replaced by whitespace.

Code: [Select]
System.out.print(String.format("%10d", number));

In this case, might as well use printf rather than format. You're not unnecessarily creating a new String, printing it, and throwing it away. It uses the exact same formatting options.
Logged

MagmaMcFry

  • Bay Watcher
  • [EXISTS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #3942 on: February 17, 2013, 02:41:42 pm »

The "right" solution is to use string formatting options. A formatting of "%xd" will print a right aligned integer of x digits. Any missing digits will be replaced by whitespace.

Code: [Select]
System.out.print(String.format("%10d", number));

In this case, might as well use printf rather than format. You're not unnecessarily creating a new String, printing it, and throwing it away. It uses the exact same formatting options.
It also leaves the problem of writing a loop to get that number to place in your format, which is decidedly more complicated than simply printing lots of single characters.
Logged

Twiggie

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #3943 on: February 17, 2013, 03:38:34 pm »

So for my university project, I have a little game. It's written in java and i have a swing gui and an android app for it. what i'd like to know is if i can hook the backend of the game up to a windows phone app in C# (as simply as possible, since time is pretty heavily constrained right now)
Logged

Mego

  • Bay Watcher
  • [PREFSTRING:MADNESS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #3944 on: February 17, 2013, 04:01:49 pm »

The "right" solution is to use string formatting options. A formatting of "%xd" will print a right aligned integer of x digits. Any missing digits will be replaced by whitespace.

Code: [Select]
System.out.print(String.format("%10d", number));

In this case, might as well use printf rather than format. You're not unnecessarily creating a new String, printing it, and throwing it away. It uses the exact same formatting options.

Code: [Select]
System.out.printf("%10d", number);
Yay for pretty code!
Pages: 1 ... 261 262 [263] 264 265 ... 796