Python (civ4 scripting language) tutorial thread

it sounds like your opening Python Command Line which is not what your suppose to use. i guess its alright since you got lesson 2 complete. lesson 1 is just checking if the program is their nothing really big to miss.
 
MSTK - to get to status 'COMPLETED', you need to simply tell me that you have done, or know, the lesson.
Taé Shala - Good to hear that you got lesson 2 done. It just shows how leaning in a group like this can really help.
 
Sorry for my hopeless commitment here - schoolwork can get a bit heavy at this time of year. Expect the next lesson in 24 hours, and start nagging if it isn't ready in 48hrs. I might have an hour or two to write the next lesson.

In the meantime, How about we discuss the logic of Artificial Intelligence? What makes a computer opponent, what does it need to contribute to a game, and how does it compute what a human does, and react accordingly?
 
Let´s take a fight for example. Everyone counts the possibility of winning or loosing a fight by comparing attack vs. defense value. The AI does this too. You compare the terrain and defense status of the defender (fortified, fortress, etc.) and the possibility of surviving for both units. If it is likely that you will win you attack. If it is unlikely you won´t.

Things change when there will be a third unit for support.
If you have artillerie you will weaken up the hp of the defending unit first, trying to get a better possibility of surviving for the attacker. You even attack with both units if there is a chance that the opponent will be destroyed, if it is for any means necessary.

So attacking or not is the choice made using the comparison of possibilities of surviving for both units. If you think you will be lucky enough you attack. If it is unlikely you won´t.
Comparing the possibilities is what the AI does.

Hope you got the point. It is a little bit hard to explain for a non native speaker. :)
 
Hi everyone, I'm gonna have a go at reviving my beer soaked brain & learning this stuff. Learnt some Basic & machine language back in the Z80 processor days, nothing more recent. But what you've covered so far is familiar & I've completed both lessons.

Cheers, Dirk.
 
This looks very interesting.. is there time for me to start too?
 
LESSON 3:
Programs in a file, and variables.

Well, we can make one-liner programs. So What? You want to send programs to other people, so that they can use them, without knowing how to write them.

Writing programs in python to a file is VERY easy. You just need notepad (or a hotted-up version of it that will automatically colour-code python code it recognises). So, go and open notepad. Type the following:

Code:
#A simple program.
print "Mary had a little lamb,"
print "it's fleece was white as snow;"
print "and everywhere that Mary went",
print "her lamb was sure to go."
Keep this exactly the same, down to where the commas are placed. Save the file as 'mary.py' - and make sure notepad doesn't add .txt to the end of the filename. You will have to tell it to save as any file, so it doesn't add that. Turn off 'Hide known file extensions' in windows explorer, if it makes it easier.

Now, open up the Python IDLE program (should be in your start menu). Click 'File > Open' and find mary.py and open it. if you cant find mary.py, set the open dialogue to 'Files of type: All Files (*)'. A new window will open, showing the program you just wrote. To run your program, click 'Run>Run Module' (or just press F5). Your program will now appear in the main Python screen (Titled *Python Shell*) and will look like this:
Code:
[color=blue]Mary had a little lamb,
it's fleece was white as snow;
and everywhere that Mary went her lamb was sure to go.[/color]
We will be writing all of our programs now in the python IDLE program - the notepad thing is just a demonstration to tell you that a .py file is just a simple text file.

There are a couple of things to notice here. First of all, the comment wasn't shown. That is good, because remember - comments aren't compiled. (try compiling it without the comment - it comes out messy)
The second thing is that the 3rd and 4th line got joined. This is because there is a comma just outside the inverted commas that surround the text. In the 'print' command, this stops the program from starting a new line when writing text.

You can also run the program from your command line program (e.g. MSDOS).
Open the prompt up, type 'cd path\to\your\file'. then type 'python mary.py'. Your program will now execute in the command line.

A more complex program:

Now lets start introducing variables. Open up IDLE, click 'File>New Window' - a new window now appears, and it is easy to type in programs. Type the following (or just copy and paste - just read very carefully, and compare the code to the output that the program will make):
Code:
#variables demonstrated
print "This program is a demo of variables"
v = 1
print "The value of v is now", v
v = v + 1
print "v now equals itself plus one, making it worth", v
v = 51
print "v can store any numerical value, to be used in other places later."
print "for example, in a sentence. v is now worth", v, "which is a prime number."
print "v times 5 equals", v*5
print "but v still only remains", v
print "to make v five times bigger, you would have to type v = v * 5"
v = v * 5
print "there you go, now v equals", v, "and not", v / 5

As you can see, variables store values, for use at a later time. You can put in more than numbers, though. Try this program:
Code:
#giving variables text, and adding text.
word1 = "Good"
word2 = "Morning"
word3 = "to you too!"
print word1, word2
sentence = word1 + " " + word2 + " " +word3
print sentence
The output will be:
Code:
[color=blue]
Good Morning
Good Morning to you too!
[/color]
So yes, variable names can be longer than one letter. They can also hold more than numbers! Here, they hold text.
As you can see, words can be added together to make longer words or sentences. However, it doesn't add spaces in between the words - hence me putting in the " " things (there is one space between those).

Well done! We now understand longer programs, and know the use of variables. Next lesson, we look at functions, what they are, and how to use them.

Thanks to all, Gingerbread Man.
 
Finished to.
Also the link from the main message to lesson 2 seems to point to the wrong post.
Edit: It should be post 43.
 
I am done.
 
Wow, good work guys!

I wont be able to update anything tonight, but a new lesson will be out in 30-48hrs. I've got some assignments that I should have started a week ago.
 
Ok, I'm jumping on the band wagon. I've also completed lessons one and two, and working on 3. Thanks for your efforts GBM.

CS
 
Top Bottom