Welcome to the Bitcave! Nerdiness, Jokes, Mods, Ideas, and coding!

Here @bitassassin i’ll try to explain it better.
I’m making a game where you start with four villagers. Each villager has a positive and negative personality trait, age and a job. The game goes by in seasons, and you will be prompted to do things in special events.
Villagers will eventually reproduce, meaning that the new villagers will need new variables for each one. You should be able to have an infinite amount of villagers, so predefining the variable for each villager that is to be born is useless. Example:

villager1 = [personality, personality, age, job]
villager2 = [personality, personality, age, job]
villager3 = [personality, personality, age, job]
villager4 = [personality, personality, age, job]
etc...

So i researched if there could be a solution and a heard that classes and objects could do this. I never delved into OOP because it always results in headaches and me not getting anywhere. Then, once i had finally made learnt how to make classes and from them objects, i encountered the same problem: You need a variable for each villager. Example:

class Person(object):
"""A friendly villager"""

def __init__(self, name):
	print("A villager has been born by the name of " + name)
	self.name = name
    #Other methods go here such as attributes & job calculation
		
print("Please input four names, one at a time")
vil0 = Person(input())
vil1 = Person(input())
vil2 = Person(input())
vil3 = Person(input())

The problem is writing a variable/list for each existing variable and for all villagers to come.
Sorry if this is still vague, I don’t know how else to explain it.

1 Like

Well if the bonus is received every round then pure hunt is sustainable so long as the other person hunts at least one third of the time. That’s low enough that a heavy hunt strategy might be good, the trick is making an algorithm that can adapt based on the amount of bonuses received, how many hunts were made in relation to m, and how to slack as much as possible while still receiving the bonus.

For generators here is some basic examples of various things you could use.

import random

def random_number():
	return random.randint(3,50) # Change to whatever, includes both endpoints

def random_string_from_list():
	list_of_strings = ['Tall', 'Short', 'Fat', 'Skinny', 'Smart', 'Stupid'] # Just change the list
	return random.choice(list_of_strings)

def random_boolean():
	return random.randint(0,1)

def random_string_from_parts(): # More random strings like names, also more complex generator
	parts = ['ah', 'za', 'hi', 'bedd', 'des', 'de', 'ert', 'sin', 'sh', 'un', 'zi']
	length = random.randint(2,4)
	tempString1 = ''
	for i in xrange(length):
		tempString1 += random.choice(parts)
	# Remove triple letters
	tempString2 = ''
	for i in xrange(1,len(tempString1)-1):
		if tempString1[-i] != tempString1[-i-1] or tempString1[-i] != tempString1[-i-2]:
			tempString2 = tempString1[-i] + tempString2
	tempString2 = tempString1[:2] + tempString2
	# Capatilize and return
	return tempString2.capitalize()

def random_weighted_choice():
	choices = [['Happy',6],
	           ['Angry',2],
	           ['Friendly',10],
	           ['Sad',5],
	           ['Burly',1],
	           ['Clumsy',5]]
	totalWeight = 0
	for i in choices:
		totalWeight += i[1]
	choice = random.randint(1,totalWeight)
	for i in choices:
		choice -= i[1]
		if choice <= 0:
			return i[0]

There is plenty of room to expand the various things in there, like making the random parts function off sounds instead of letter groups or combining them with weighting so some parts are more likely than others. The weighted choice can be used for a whole bunch of things but with something like inheritance being a major one as some traits could be more likely to be inherited than others

1 Like

you should probably remove the user form the villager creation process…

have a function, which is triggered by some in-game activity, that calls the class to create the villager (and passes in params that are dynamically pulled form a list of villager details - name, personality, age, etc)

Hello, all! I have recently been trying to work my way into Lua (I have had no programming experience before today) because of it’s uses in SH, and I figured that this would be a good place to be to get started!

PS I’ve spent like 45 minutes trying to get Minecraft Forge up and running on my Win8 PC… it wasn’t fun… And then I scrolled down and you said," It’s on FTB."

I think my head may have popped off after I read that.

BTW, I was referring to ComputerCraft.

Yeah CC is in quite a few modpacks, Tekkit/Technic being another notable one. If you are doing stuff with them though I’d suggest focusing on the turtles more than the consoles, maybe see if you can take a crack at the self replicating turtle challenge that allows for you to place a turtle and have it both fuel itself and replicate itself into another turtle just by collecting all the resources itself. I can’t remember the other thing I was going to say anymore, oh well.

I was wondering if there was any way that I could get a function to repeat several times without doing either:

functionA()
functionA()
functionA()
etc…

or running a for-loop? Is there any way to condense the top one into one line, like:

“functionA(4)” would make functionA run four times? (I know that doesn’t work, but just giving a sort of context example type thing.)

EDIT: And I know about while-do-loops, but in, for example, computercraft, if I want my turtle to dig out a 5x5 area, is there any way I can do that without just doing:

functionA()
functionB()
functionA()
functionB()
etc…

like 5 times in a row? Is there a way to condense that?

EDIT2: Please excuse my idiocy. It was actually pretty simple.

feel free to post your code, as i was about to suggest something… i’m still not 100% sure i understood what you were trying to accomplish, or why you wouldnt have approached it using a loop that simply called the corresponding loop…

x=0
while(x<=5) do:
  functionA()
  x++
  end
end

Or something like that. I know I mixed the syntax of like three languages there, so meh.

1 Like

Ah, yes, that would work. Thankreas pancreas.

Why don’t you want a for loop? Because that’s the exact thing a for loop does.

Well, I was just looking for alternatives.

But @Xavion I made it all simple and stuff. xD God answering stuff at work is like trying to tweet your life story on the freeway.

But you just coded a for loop. Doesn’t matter that it was labeled a while loop it was functionally a for loop for [0,5]. I’m kind of interested in what he did to minimize it myself as it seems drive back and forth seems like the simplest fastest option.

Ideally he would just have an argument that specified the number of iterations. Ta-da. I wonder how that system would handle negative integers… Oh.

So you’re saying he’d have a for loop? Something that loops through x rows sounds like a for loop to me. As for negative ints I’d say he just puts in correct input, sanitising/validating input only matters if if will be used by user.

Heh, @Xavion I’m totally not disagreeing with you. Haven’t been the whole time. I actually think the for loop would be more efficient. When I wrote the while loop above I assumed it’d be easier for @ManOfRet to get right off the bat, and implement easily in his code. I’f you’d like to demo a for() loop that does the exact same thing I’m sure he’d appreciate it. =D

I know how to do a for loop.

for i=1,5 do
turtle.etc

etc.
etc.

end

EDIT: Yaaaay, I got my turtle to excavate a 6x6 square! It still messes up with gravel occasionally, though.

I assume it’s gravel falling into the hole that’s the problem? because if so just use a while loop to mine up until there isn’t a block above it, that way it will mine out all falling blocks directly above it while excavating.

I found this site http://projecteuler.net, it has a lot of math problems to solve using programming, really good exercises for learning programming. I’ve done the first 11 problems in Python, before I got a bit sidetracked, I thought python was a bit slow, so I did what every good nerd would do. I found pyOpenCL, have now spent a couple of days learning OpenCL (luckily I already know some C), to speed up calculations. :smiley: And I really should get back to coding some VHDL.

On a side note, how should I go about learning the C languages? C, then C++, then C# or what? And what’s the real difference?