Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 446 447 [448] 449 450 ... 796

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

RulerOfNothing

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #6705 on: November 22, 2014, 10:08:18 pm »

I'm getting the feeling that I am abutting against the limits of my knowledge of Java and NetBeans here, but I'll try to continue helping. Firstly, what happens if you try to put dynamic/mutable objects in your ArrayList (i.e. is it a compiler error or a runtime error or a logic error)? Secondly, would you mind putting up the code in question so I can have a look at it?
Logged

Dansmithers

  • Bay Watcher
  • [ETHIC:TUNNEL_SNAKES:RULE]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #6706 on: November 22, 2014, 10:36:25 pm »

I'm getting the feeling that I am abutting against the limits of my knowledge of Java and NetBeans here, but I'll try to continue helping. Firstly, what happens if you try to put dynamic/mutable objects in your ArrayList (i.e. is it a compiler error or a runtime error or a logic error)? Secondly, would you mind putting up the code in question so I can have a look at it?
NetBeans adds an error annotation(I assume it would also throw an error, I trust NetBeans) on the line which I have marked with a #, there are no # in the program proper:
Code: [Select]
removedThe error is shown as:  non static variable(the variable in question) cannot be used in a static context.
EDIT: Derp, I was using it in main, which is static.
« Last Edit: November 22, 2014, 10:39:00 pm by Dansmithers »
Logged
Siggy Siggy Hole!

Well, let's say you're going away from Earth on huge spaceship and suddenly shit goes wrong and you have Super Mutants. Social Experiments prepared them for this.

Reelya

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #6707 on: November 23, 2014, 06:03:22 am »

So why can't you do something like
Code: [Select]
parts[x]=PartList[type[x]] where type contains the type of the part at index x? I am assuming that you are trying to populate an array with varying PartRef subclasses (I am also going to assume that the different PartRef subclasses hold code pertaining to the behaviour of the given type of part) so the solution might not be a good fit if I am mistaken in what exactly you want done here.

First up, I'd recommend using ints to refer to the types of parts. References are ok, but it's not possible to save a reference to disk (for e.g. saving the game), because they lack persistence. If you have a central array for parts, then an int to refer to which type of part is a better idea, they can be saved to a file, transmitted over a network/webpage. Refs cannot do that.

Second, avoid inheritance wherever possible. Just vary data to achieve variation whenever you can. I know they focus on inheritance as a design principle when teach Object Oriented Design, but frankly it's the most retarded way possible to get things done. The best methodology is always to minimize the number of different classes you have, vary them with data and other small objects inside them.

If you need to vary actual code (object behavior) either have the object do different things based on if/else checking with the data you've given them, or make a polymorphic object inside the class, using the stategy pattern:
http://en.wikipedia.org/wiki/Strategy_pattern#Java
It's basically the same thing as inheritance, but you hide the inherited object inside your main class so that client code never has to know that there are subclasses. An advantage of this is that all the polymorphic objects present to the client as being the same class, not different classes. That means you can cram all subclass handling code inside the "parts" class. Another cool consequence of using the Strategy Pattern for object design is that you can change the behavior/subclass of the main objects AFTER they've already been created.
« Last Edit: November 23, 2014, 06:56:47 am by Reelya »
Logged

LoSboccacc

  • Bay Watcher
  • Σὺν Ἀθηνᾷ καὶ χεῖρα κίνει
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #6708 on: November 24, 2014, 05:39:45 pm »

well, look at this programming game:

https://www.indiegogo.com/projects/screeps
Logged

Reelya

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #6709 on: November 24, 2014, 06:47:54 pm »

well, look at this programming game:

https://www.indiegogo.com/projects/screeps

I was ready to go "meh, another code game", but that actually looks like a really cool idea. An MMO sandbox RTS where you play by coding the AI for your base and troops, and it's persistent when you're offline. That sounds like something it would be worthwhile to have a bay12 team working together, sharing and improving code, designing our AI's with alliance code so that our troops automatically know to work together if they meet up ...

Sadly, it looks like it's going to be Pay To Play. I prefer my timewasters free, rather than being charged for wasting time. I have enough bought games already to keep me going for years ...
« Last Edit: November 24, 2014, 06:50:53 pm by Reelya »
Logged

EnigmaticHat

  • Bay Watcher
  • I vibrate, I die, I vibrate again
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #6710 on: November 24, 2014, 07:14:28 pm »

First up, I'd recommend using ints to refer to the types of parts.

I was going to suggest this, and it makes me happy that someone else did it first because that means I kinda know what I'm doing.
Logged
"T-take this non-euclidean geometry, h-humanity-baka. I m-made it, but not because I l-li-l-like you or anything! I just felt s-sorry for you, b-baka."
You misspelled seance.  Are possessing Draignean?  Are you actually a ghost in the shell? You have to tell us if you are, that's the rule

Skyrunner

  • Bay Watcher
  • ?!?!
    • View Profile
    • Portfolio
Re: if self.isCoder(): post() #Programming Thread
« Reply #6711 on: November 24, 2014, 07:26:34 pm »

well, look at this programming game:

https://www.indiegogo.com/projects/screeps
if they manage to scrape together $30000, they say they'll open-source the engine, which shohld theoretically allow someone to host a server.
Logged

bay12 lower boards IRC:irc.darkmyst.org @ #bay12lb
"Oh, they never lie. They dissemble, evade, prevaricate, confoud, confuse, distract, obscure, subtly misrepresent and willfully misunderstand with what often appears to be a positively gleeful relish ... but they never lie" -- Look To Windward

Antsan

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #6712 on: November 25, 2014, 11:14:05 am »

I have taken a liking to screeps. I also programmed in JavaScript maybe... around 8 years ago or so and back then only a little, so I learn the language as I go.

I have the following snippet of code to automatically regulate spawning of new creeps:
Code: [Select]
var _ = require('lodash') ;

module.exports = function (spawn) {
   
    var count = function (collection, string) {
        return _.size ( _.filter ( _.values ( collection ) , function (a) { a.memory.role == string } ) ) ;
    } ;
   
    var harvesters = count ( Game.creeps, "harvester" ) ;
    var builders = count ( Game.creeps , "builder" ) ;
   
    if ( !spawn.spawning && harvesters < 3 ) {
        spawn.createCreep ( [ Game.CARRY, Game.WORK, Game.WORK, Game.MOVE, Game.MOVE ], 'harvester' + Game.time , { role : 'harvester' } );
    }
   
    if ( !spawn.spawning && builders < 3 ) {
        spawn.createCreep ( [ Game.CARRY, Game.CARRY, Game.WORK, Game.WORK, Game.MOVE ], 'builder' + Game.time , { role : 'builder' } );
    }
} ;
Instead of first spawning three harvesters and then spawning 3 builders as intended this just spawns an endless stream of builders (well, until all my builders huddle up around the spawn point and the sole harvester cannot access it anymore to load of its energy).
What am I doing wrong?

[Edit]
Found it. There is a 'return' missing in the local function.Friggin' procedural languages needing 'return', how stupid is that even, why would you ever not want to return a value... *grumble*
Still, builders are spawned before harvesters. No idea why that is.
[/Edit]
« Last Edit: November 25, 2014, 11:20:48 am by Antsan »
Logged
Taste my Paci-Fist

Reelya

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #6713 on: November 25, 2014, 06:17:21 pm »

I know it's probably uncouth to ask for straight up pointers on where I'm screwing up with my homework, but I've spent close to sixteen or eighteen hours on this one assignment and I cannot wrap my brain around where I've gone wrong with implementing a heap-sort. I've looked at examples. I've done trial-and-error. I'm not really even sure how I got one example working, but the code wasn't operating with a user-made object or with insert or delete functions, which is what my professor is looking for.

I need someone to tell me where my code is wrong and broken and why, definitely why. Would anyone be willing to look it over? It's here.

first up, where you have bounds checking in "insert" you should reject a new value if the heap's already full. you're inserting the value, and only then checking if it's full up. Just so new values don't overwrite a full heap. This is a minor thing though and won't affect your program, but might affect your mark if your lecturer sees it as an error. check first, then insert.

your system of determining parent node is wrong. If you fix that you can also set the "if(x>2)" to "if(x>0)" and do away with the entire "else" section of makeheap, it's not needed if you set the parent node correctly.

x/2 only works when "1" is the root node (e.g. for nodes 2 & 3, x/2 = 1). since c++ arrays start at zero you need to compensate by using floor(x/2)-1 or int(x/2)-1 floor((x-1)/2) or int((x-1)/2). It's good form to explicitly do the conversion to indicate to the code reader and the compiler that the value is being rounded on purpose, not by accident. Look at your commented out heapify example code, node 2, has children of 5 & 6, not 4 & 5, which it would have if parent was int(x/2).


One thing you should use more of is debug statements, i.e. in each "makeheap" operation, you could have print out the value, the position, and the parent node position / value, as well as a "heap so far" full print (include both values and index with that). That would have helped indicate something was wrong with the wiring.
« Last Edit: November 25, 2014, 07:19:04 pm by Reelya »
Logged

RulerOfNothing

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #6714 on: November 25, 2014, 07:10:02 pm »

Reelya, floor(x/2)-1 doesn't work either (using that, the parent of node 1 is node -1, which is obviously incorrect). I think you want ((x+1)/2)-1 (or you could use floor, but that would require that you cast to a floating-point type as it doesn't work with integer division)
Logged

Reelya

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #6715 on: November 25, 2014, 07:13:31 pm »

oh yeah, sorry, i took that straight from the wiki page for binary heap, should have checked the context better
http://en.wikipedia.org/wiki/Binary_heap

I think what I actually used originally when I did this was floor((x-1)/2). The -1 is before the halving, not after. A nice way to write it would be (x-1) >> 1, using a bitshift rather than division.

((x+1)/2)-1 is a little over complex:

(x+1)/2-1 = (x+1)/2-2/2 = (x+1-2)/2 = (x-1)/2
« Last Edit: November 25, 2014, 07:29:15 pm by Reelya »
Logged

alexandertnt

  • Bay Watcher
  • (map 'list (lambda (post) (+ post awesome)) posts)
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #6716 on: November 26, 2014, 06:06:48 am »

rm is the normal bash file deletion command right? I'm not just having a brain-fart right?

Code: [Select]
Alexanders-iMac:src alexander$ rm lua.o
override rw-r--r--  root/staff for lua.o?

Do what now? I don't know what root/staff is, but it sounds sort of important...


EDIT: lua.o isn't there in Finder, but it comes up with ls...

EDIT2: Yes they are, Finder just decided that they should be the only files not sorted alphabetically and dumped them at the bottom (after print.o)
« Last Edit: November 26, 2014, 06:12:10 am by alexandertnt »
Logged
This is when I imagine the hilarity which may happen if certain things are glichy. Such as targeting your own body parts to eat.

You eat your own head
YOU HAVE BEEN STRUCK DOWN!

Thief^

  • Bay Watcher
  • Official crazy person
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #6717 on: November 26, 2014, 06:33:46 am »

rw-r--r--  root/staff means:
file is read/write to user root
file is read-only to group staff
file is read-only to everyone else

To delete it you need to be the root user. The easiest way to do this (if you have appropriate permission) is to put "sudo" before the command.
Logged
Dwarven blood types are not A, B, AB, O but Ale, Wine, Beer, Rum, Whisky and so forth.
It's not an embark so much as seven dwarves having a simultaneous strange mood and going off to build an artifact fortress that menaces with spikes of awesome and hanging rings of death.

alexandertnt

  • Bay Watcher
  • (map 'list (lambda (post) (+ post awesome)) posts)
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #6718 on: November 26, 2014, 07:02:11 am »

But I don't want to delete root/staff, I want to delete lua.o. I don't know where root/staff is supposed to factor into that.
Logged
This is when I imagine the hilarity which may happen if certain things are glichy. Such as targeting your own body parts to eat.

You eat your own head
YOU HAVE BEEN STRUCK DOWN!

Reelya

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #6719 on: November 26, 2014, 07:18:05 am »

huh? Thief didn't say anything of the kind he was clearly talking about deleting lua.o. What he described was what that line of output actually meant. It's a printout of the user permissions attached to the file you tried to delete, lua.o. Thief explained what all of it meant, and what it signified - you can only "write" to that file if you're using the "root" account, which is like Administrator in Windows. "write" privileges include deleting the file.

root is the sysadmin account, staff is the group the file is attached to. that's what root/staff is saying, and the r's and w's (and x's but not here) stand for read, write and execute permissions for 3 levels of users - root, usergroup (which here is named "staff") and strangers. So it's security settings basically to stop idiots deleting system files.

It sounds like you don't know anything about UNIX file permissions and the chmod command. Without knowing how that works, you are basically stuffed for doing anything with files in UNIX or Linux environments.

Thief suggested you try the delete command again but put "sudo" before it. sudo = "superuser do", i.e. "Run as Administrator" in Windows.
« Last Edit: November 26, 2014, 07:36:36 am by Reelya »
Logged
Pages: 1 ... 446 447 [448] 449 450 ... 796