2015-05-01 17:47:26

i am currently learning python. so as i'm getting fairly decent with it i thought it was time to start on a bit of a project. i am coding a monopoly game. this isn't really something i plan on releasing but if it  turns out to be something really good, i'll consider it.
i have to major issues at this point. i've been working on a lot of the player methods  currently. so here comes the questions.
my board is a tuple i am  using my player object instance attribute to move my to players along the board. however, once the number gets higher than the number of things in the board, 40, i get a tuple index is out of range error. how might a be able to loop it back and start again like the normal game loops around the board?
secondly, are there any general practices to for alternating between players in a game? i kind of have something working in this regard, but it breaks half the time and thought I'd ask here for any advice from any other more experienced pythonians.

I don’t believe in fighting unnecessarily.  But if something is worth fighting for, then its always a fight worth winning.
check me out on Twitter and on GitHub

2015-05-01 19:23:33 (edited by dhruv 2015-05-01 19:24:33)

Hi,
First, you might be able to catch it with a try: except clause, but there are probably better solutions out there.
Second, could I please get the code?  smile it'll really help me learn python more.

This is not a signature.

2015-05-01 23:27:59

i've seen try statements around and i had to look up specifically what they were and stuff. i'll see what i can do with that.
i'll probably will eventually post my code, but its in no state to be shown to anyone. at this point, its split into several different files according to different parts of the game so i can run tests on new code easier. i'll probably want to add comments and stuff before posting as well.

I don’t believe in fighting unnecessarily.  But if something is worth fighting for, then its always a fight worth winning.
check me out on Twitter and on GitHub

2015-05-02 00:06:24

well update time, it seems i am now able to loop around the board. yeah go me.
a simple if conditional solved it. with a bit of math to manipulate the numbers a bit.
but still don't really have a reliable way to switch players.
i'm using an if block that if its player 1's turn, it changes it to player 2's. but the hang up, or at least part of it, is that the elif block is that if its player 2's turn it changes it to player 1's. and those execute if then elif. and player two doesn't actually get to  take his turn.

I don’t believe in fighting unnecessarily.  But if something is worth fighting for, then its always a fight worth winning.
check me out on Twitter and on GitHub

2015-05-02 01:33:31

Just a trick, instead of using a condition, you can use modulos wink

You can also use players in a list, and ++ the index of this list for each turn, and again with a modulo to loop around it

2015-05-02 01:50:04

hmm, i'm assuming by modulo you mean module?
i understand i think, but could you provide an example?

I don’t believe in fighting unnecessarily.  But if something is worth fighting for, then its always a fight worth winning.
check me out on Twitter and on GitHub

2015-05-02 02:12:01 (edited by Genroa 2015-05-02 02:13:23)

Oh sorry, the french word is 'modulo' and I thought it was the same word smile

Hm I didn't programed in python for more than a year, but when you want to play a turn, you play it, and at the end of the loop, before updating the player index (to know that you are playing the turn of the player 1 or 2), you do : current_player = (current_player+1) % player_nb

player_nb being the number of players, current_player being the current player (the one which is playing). If you want to keep a track of the number of turns, you can keep a variable counting it. This way, the player which is playing is turn % player_nb .

The module trick is assuming that the 0 result is for player 1, the 1 result for player 2, etc. That's why it is so cool when it is used as the index of a list smile

I don't remember if lists index begins at 1 or 0 in python, but if they begin with 1 the calcul is : current_player = ((current_player+1) % player_nb)+1, same thing for the calcul using the turn variable smile

2015-05-06 12:43:56

You could also look at iterators from itertools
They are objects you call next on and it will alternate between 1 and 2.
for example:
import itertools

l = ["player1", "player2", "player3"]

iter = itertools.cycle(l)

for i in range(5):
    print(next(iter))


result:
player1
player2
player3
player1
player2

2015-05-08 04:29:51

@Kyleman123, Modulo is a mathimatical term. It is the equivalent of a modulus, which is the base with respect to which a congruence is computed. It could also be The absolute value of a complex number. Either suits it. However, since we're tlaking about both math, in the programming context it is a An operator placed between two numbers, to get the remainder of the division of those numbers.

"On two occasions I have been asked [by members of Parliament!]: 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out ?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question."    — Charles Babbage.
My Github