Python Forum
Man Don't Get Angry - Game
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Man Don't Get Angry - Game
#1
Hello.
I have a little problem with my code. I tried to make a game Man Don't Get Angry , but when I tried to do "houses" i had a problem how to allow an entrance for a figure. And how to fix a figure in a house. I tried to add condition
if y < size // 2 and doska[x - 1][y] == 'D':
            return x - 1, y
but it didn't work

def create_sachovnica(n):
    global doska
    doska = [[' ' for i in range(n)] for j in range(n)]
    npol = n // 2
    for i in range(n):
        doska[npol - 1][i] ='*'
        doska[npol + 1][i] ='*'
        doska[i][npol - 1] ='*'
        doska[i][npol + 1] ='*'
    doska[npol][0] ='*'
    doska[npol][n - 1] ='*'                                                 
    doska[0][npol] ='*'
    doska[n - 1][npol] ='*'
    for i in range(1, npol):
        doska[npol][i] ='D'
        doska[npol][n - i - 1] ='D'
        doska[i][npol] ='D'
        doska[n - i - 1][npol] ='D'
    doska[npol][npol] ='X'


def print_sachovnica(n):
    for n in doska:                                                               
        print(' '.join(n))
    print()

def nextpos(doska, x, y):
    size = len(doska)
    if x >= size // 2:
        if y < size // 2 and doska[x - 1][y] == '*':
            return x - 1, y
    else:
        if y <= size // 2 and doska[x][y + 1] == '*':
            return x, y + 1
        elif y <= size // 2 and doska[x-1][y] == '*':
            return x-1,y
    if y >= size // 2:
        if x <= size // 2 and doska[x + 1][y] == '*':               
            return x + 1, y
        elif x < size // 2 and doska[x][y+1] == '*':
            return x,y+1
        elif x > size // 2 and doska[x][y-1] == '*':
            return x,y-1
        elif x > size // 2 and doska[x+1][y] == '*':
            return x+1,y
        elif x >= size // 2 and doska[x+1][y] == 'D':
            return x+1,y
    else:
        if x > size // 2 and doska[x][y - 1] == '*':
            return x, y - 1
        elif x > size // 2 and doska[x][y - 1] == '*':
            return x, y - 1
    return x, y
Thanks for all answers
Reply
#2
What does " but it didn't work " mean?  Do you get errors?  What are they?

If you don't get errors, what DO you get, and what are you expecting to get?
Reply
#3
Nope i dont get errors but a figure goes across whole gaming grid
Reply
#4
You appear to have shared only a few defs, so it is hard to figure out what is supposed to happen.
I am trying to help you, really, even if it doesn't always seem that way
Reply
#5
Here is my whole code
import time
import random
doska = []

n=int(input('Zadaj rozmer hracej plochy (Najcastejsie rozmery plchoy 11*11, 9*9):'))
if n<5:
    print('Pole vami zadanym rozmerom nebolo mozne zostrojit')
else:
    def create_sachovnica(n):
        global doska
        doska = [[' ' for i in range(n)] for j in range(n)]
        npul = n // 2
        for i in range(n):
            doska[npul - 1][i] = '*'
            doska[npul + 1][i] = '*'
            doska[i][npul - 1] = '*'
            doska[i][npul + 1] = '*'
        doska[npul][0] = '*'
        doska[npul][n - 1] = '*'                            
        doska[0][npul] = '*'
        doska[n - 1][npul] = '*'
        for i in range(1, npul):
            doska[npul][i] = "D"
            doska[npul][n - i - 1] = "D"
            doska[i][npul] = "D"
            doska[n - i - 1][npul] = "D"
        doska[npul][npul] = "X"


    def print_sachovnica(n):
        for n in doska:
            print(' '.join(n))                              
        print()

    def gensachovnicu(n):
        create_sachovnica(n)                            
        print_sachovnica(n)

    def nextpos(doska, x, y):
        size = len(doska)
        if x >= size // 2:
            if y < size // 2 and doska[x - 1][y] == '*':
                return x - 1, y
            elif y < size // 2 and doska[x - 1][y] == 'B':
                return x - 1, y
            elif y < size //2 and doska[x][y-1]=='*':
                return x,y-1
            elif y < size // 2 and doska[x][y-1] == 'B':
                return x, y-1
        else:
            if y <= size // 2 and doska[x][y+1] == '*':
                return x,y+1
            elif y <= size // 2 and doska[x-1][y] == '*':
                return x-1,y
            elif y <= size // 2 and doska[x][y + 1] == 'B':
                return x,y+1
            elif y <= size // 2 and doska[x-1][y] == 'B':
                return x-1,y
        if y > size // 2:
            if x < size // 2 and doska[x+1][y] == '*':   
                return x+1, y
            elif x < size // 2 and doska[x][y+1] == '*':
                return x,y+1
            elif x > size // 2 and doska[x][y-1] == '*':
                return x,y-1
            elif x >= size // 2 and doska[x+1][y] == '*':
                return x+1,y
            elif x < size // 2 and doska[x+1][y] == 'B':
                return x+1, y
            elif x < size // 2 and doska[x][y+1] == 'B':
                return x,y+1
            elif x > size // 2 and doska[x][y-1] == 'B':
                return x,y-1
            elif x >= size // 2 and doska[x+1][y] == 'B':
                return x+1,y
        else:
            if x > size // 2 and doska[x][y - 1] == '*':
                return x, y - 1
            elif x > size // 2 and doska[x+1][y] == '*':
                return x+1,y
            elif x > size // 2 and doska[x][y - 1] == 'B':
                return x, y - 1
            elif x > size // 2 and doska[x+1][y] == 'B':
                return x+1, y

        return x, y

    def nextpos2(doska, s, r):
        velkost = len(doska)
        if s >= velkost // 2:
            if r < velkost // 2 and doska[s-1][r] == '*':
                return s - 1, r
            if r < velkost // 2 and doska[s-1][r] == 'A':
                return s - 1, r
            if r < velkost // 2 and doska[s][r-1] == '*':
                return s, r-1
            if r < velkost // 2 and doska[s][r-1] == 'A':
                return s, r-1
        else:
            if r <= velkost // 2 and doska[s][r + 1] == '*':
                return s, r + 1
            elif r <= velkost // 2 and doska[s-1][r] == '*':
                return s-1,r
            elif r <= velkost // 2 and doska[s][r+1] == 'A':
                return s, r + 1
            elif r <= velkost // 2 and doska[s-1][r] == 'A':
                return s-1,r
        if r > velkost // 2:
            if s < velkost // 2 and doska[s+1][r] == '*':                  
                return s+1, r
            elif s < velkost // 2 and doska[s][r+1] == '*':
                return s,r+1
            elif s > velkost // 2 and doska[s][r-1] == '*':
                return s,r-1
            elif s >= velkost // 2 and doska[s+1][r] == '*':
                return s+1,r
            elif s < velkost // 2 and doska[s+1][r] == 'A':
                return s+1, r
            elif s < velkost // 2 and doska[s][r+1] == 'A':
                return s,r+1
            elif s > velkost // 2 and doska[s][r-1] == 'A':
                return s,r-1
            elif s >= velkost // 2 and doska[s+1][r] == 'A':
                return s+1, r
        else:
            if s > velkost // 2 and doska[s][r-1] == '*':
                return s, r-1
            elif s > velkost // 2 and doska[s+1][r]=='*':
                return s+1,r
            elif s > velkost // 2 and doska[s][r-1] == 'A':
                return s, r-1
            elif s > velkost // 2 and doska[s+1][r] == 'A':
                return s+1,r
        return s, r


    def hra(n):
        sucetA=0
        sucetB=0
        obvod=(3*n)+(n-5)
        x=(n-1)
        y=((n-3)//2)
        s=0
        r=((n-3)//2)+2
        while True :
            create_sachovnica(n)
            doska[x][y] = 'A'
            doska[s][r] = 'B'
            hod = random.randint(1, 6)
            hod1 = random.randint(1, 6)
            sucetA += hod
            sucetB += hod1
            if sucetA>obvod+1:
                doska[n-2][n//2]='A'
            if sucetA>(2*obvod)+3:
                doska[n-2][n//2]='A'
                doska[n-3][n//2]='A'
            if sucetA>(3*obvod)+4:
                doska[n-2][n//2] = 'A'
                doska[n-3][n//2] = 'A'
                doska[n-4][n//2] = 'A'
            if sucetA>(4*obvod)+5:
                doska[n - 2][n // 2] = 'A'
                doska[n - 3][n // 2] = 'A'
                doska[n - 4][n // 2] = 'A'
                doska[n - 5][n // 2] = 'A'
            if sucetA>(5*obvod)+6:
                doska[n - 2][n // 2] = 'A'
                doska[n - 3][n // 2] = 'A'
                doska[n - 4][n // 2] = 'A'
                doska[n - 5][n // 2] = 'A'
                doska[n - 6][n // 2] = 'A'
            if sucetA>(6*obvod)+7:
                doska[n - 2][n // 2] = 'A'
                doska[n - 3][n // 2] = 'A'
                doska[n - 4][n // 2] = 'A'
                doska[n - 5][n // 2] = 'A'
                doska[n - 6][n // 2] = 'A'
                doska[n - 7][n // 2] = 'A'
            if sucetA>(7*obvod)+8:
                doska[n - 2][n // 2] = 'A'
                doska[n - 3][n // 2] = 'A'
                doska[n - 4][n // 2] = 'A'
                doska[n - 5][n // 2] = 'A'
                doska[n - 6][n // 2] = 'A'
                doska[n - 7][n // 2] = 'A'
                doska[n - 8][n // 2] = 'A'
            if sucetA>(8*obvod)+9:
                doska[n - 2][n // 2] = 'A'
                doska[n - 3][n // 2] = 'A'
                doska[n - 4][n // 2] = 'A'
                doska[n - 5][n // 2] = 'A'
                doska[n - 6][n // 2] = 'A'
                doska[n - 7][n // 2] = 'A'
                doska[n - 8][n // 2] = 'A'
                doska[n - 9][n // 2] = 'A'
            if sucetB>obvod+1:
                doska[1][n//2]='B'
            if sucetB>(2*obvod)+3:
                doska[1][n//2]='B'
                doska[2][n//2]='B'
            if sucetB>(3*obvod)+4:
                doska[1][n//2] = 'B'
                doska[2][n//2] = 'B'
                doska[3][n//2] = 'B'
            if sucetB>(4*obvod)+5:
                doska[1][n // 2] = 'B'
                doska[2][n // 2] = 'B'
                doska[3][n // 2] = 'B'
                doska[4][n // 2] = 'B'
            if sucetB>(5*obvod)+6:
                doska[1][n // 2] = 'B'
                doska[2][n // 2] = 'B'
                doska[3][n // 2] = 'B'
                doska[4][n // 2] = 'B'
                doska[5][n // 2] = 'B'
            if sucetB>(6*obvod)+7:
                doska[1][n // 2] = 'B'
                doska[2][n // 2] = 'B'
                doska[3][n // 2] = 'B'
                doska[4][n // 2] = 'B'
                doska[5][n // 2] = 'B'
                doska[6][n // 2] = 'B'
            if sucetB>(7*obvod)+8:
                doska[1][n // 2] = 'B'
                doska[2][n // 2] = 'B'
                doska[3][n // 2] = 'B'
                doska[4][n // 2] = 'B'
                doska[5][n // 2] = 'B'
                doska[6][n // 2] = 'B'
                doska[7][n // 2] = 'B'
            if sucetB>(8*obvod)+9:
                doska[1][n // 2] = 'B'
                doska[2][n // 2] = 'B'
                doska[3][n // 2] = 'B'
                doska[4][n // 2] = 'B'
                doska[5][n // 2] = 'B'
                doska[6][n // 2] = 'B'
                doska[7][n // 2] = 'B'
                doska[8][n // 2] = 'B'
            print_sachovnica(n)
            if hod ==1 :
                x, y = nextpos(doska, x, y)
            elif hod==2:
                for i in range(hod):
                    x, y = nextpos(doska, x, y)
            elif hod ==3:
                for i in range(hod):
                    x, y = nextpos(doska, x, y)               
            elif hod==4:
                for i in range(hod):
                    x, y = nextpos(doska, x, y)
            elif hod == 5:
                for i in range(hod):
                    x, y = nextpos(doska, x, y)
            elif hod ==6:
                for i in range(hod):
                    x, y = nextpos(doska, x, y)
            if hod1==1:
                s, r = nextpos2(doska, s, r)
            elif hod1==2:
                for i in range(hod1):
                    s, r = nextpos2(doska, s, r)
            elif hod1==3:
                for i in range(hod1):
                    s, r = nextpos2(doska, s, r)
            elif hod1==4:
                for i in range(hod1):
                    s, r = nextpos2(doska, s, r)
            elif hod1==5:
                for i in range(hod1):
                    s, r = nextpos2(doska, s, r)
            elif hod1==6:
                for i in range(hod1):
                    s, r = nextpos2(doska, s, r)
            print('Hod hraca A :',hod)
            print('Hod hraca B :',hod1)
            print('Sachovnica s novymi poziciami figuriek')
            if doska[n//2][n//2]=='A':
                print_sachovnica(n)
                print('Hrac A vyhral hru')     
                break
            if doska[n//2][n//2]=='B':
                print_sachovnica(n)
                print('Hrac B vyhral hru')
                break
    hra(n)
If you know this game, I made it a little bit different. There are 2 figures A and B and they are going around gaming grid. One circle one figure to the house , but every time there is an error with my def nextpos(doska, x, y) and def nextpos2(doska, s, r)but I dont know why ? It always says Index out of range ....

Sometimes is everything OK and I wont get any errors , but sometimes error index out of range
Reply
#6
Wow. I recognise the game now. A quick look through the code didn't have anything leaping out at me as an obvious problem, although always a bit suspicious when I see a lot of integer division.

I'm not going to step through the code. Have you done some debugging? Either writing unit tests or liberally adding print statements to print out the values of your variables as you pass through the programme so you can spot where the out of bounds values are being generated.
I am trying to help you, really, even if it doesn't always seem that way
Reply
#7
And have you tried a code ? And I didnt try to debugg because I dont how to use it in Pycharm Big Grin Big Grin. You know its project to school and the deadline is today. So i think this night will be long. Angry Angry

Im stucked at this problem for a hour and cant recognize what problem can it be..
Reply
#8
I ran it several times and did not see any errors on all at first. Started to get them on an 11x11 matrix.

Usually, I advise beginners to avoid PyCharm as it is like having to learn two things at the same time, Python and PyCharm.

I copied the code to an online environment where it is easy (and free) to write and run such code.

It is called repl.it and you can find your code here.

If PyCharm is a problem, try forking the online version and adding print statements. I did note that repl.it had a lot of warning symbols suggesting there were some major code problems.
I am trying to help you, really, even if it doesn't always seem that way
Reply
#9
Can I ask you where i can find this warnings ? :)
Reply
#10
Apologies, I don't see any warnings now. Don't know why they were coming up.

You need to crack on and add print statements (don't worry about messing up the printout of the matrix) that tell you what is going on. It is a crude and tedious technique, but it works. You will discover that some variable does not have the value you expect it to have - obviously, or you would not be trying to reference outside of the lists you've created.
I am trying to help you, really, even if it doesn't always seem that way
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020