1

I'm a beginner at programming and I want to build a warship game in python 3.6 for my school project, I have some mistakes but I don't know how to fix it. If you try to run my code, run it until there is a mistake because sometimes there is no problem. I used to have pop out of range in placer_bateau_verticalement function and a list index out of range mistakes. Here is my code :

EDIT 4 : python code with the pseudo-code

"""//----------------library----------------\\"""
import random



"""//----------------variables----------------\\"""

gridPlayerA = [0] * 100 #IA's grid
gridPlayerB = [0] * 100 #Player's grid


gridGamePlayerA = [0] * 100 # just create a copy of the IA boards and only show the empty board



OnGame = True # turn on the game

PrevPlayerShoots = []
PrevIAShoots = []





"""//----------------functions----------------\\"""

def main():

    BoatTouchedByPlayer = 0
    BoatTouchedByIA = 0


    put_boats(gridPlayerA)#put IA's boats randomly
    put_boats(gridPlayerB)#put Player's boats randomly (temporarily)

    while OnGame == True: #while we're in the game


        print(gridGamePlayerA) # print the IA's grid (with no boats)


        PlayerShoot = input("It's your turn, where do you want to shoot ? (give the coords like this : A,1) ")
        PlayerShoot.split(",") #Split the coords
        Lettre = PlayerShoot[0] # take the lettre
        Number = int(PlayerShoot[2]) # take the number, convert into an integer

        Player_Shoot = int(ord(Lettre)) - 65 + 10*(Number - 1) # transform the lettre and the number into a number coord


        if can_shoot(PrevPlayerShoots, Player_Shoot) == True: #if we can shoot



            if gridPlayerA[Player_Shoot] == 1: # if we touch a boat in the IA's board
                shoot_Touched(gridGamePlayerA, Player_Shoot) # call the shoot function that replace the coord by an F on the empty board
                print("touched")
                BoatTouchedByPlayer = BoatTouchedByPlayer + 1# count until 14 (number of boat "parts")
                End_Game("Player") # check if the player won


            else:
                shoot(gridGamePlayerA, Player_Shoot)# show the shot next time to remind the player where he has shoot
                print("missed, IA's turn : ")
                IA_shot = random.randint(0, 99) # make the IA shot randomly 
                if gridPlayerB[IA_shot] == 1:# if IA touch a boat in the player's board
                    print("IA touched one of your boat")
                    shoot_Touched(gridPlayerB, IA_shot) # call the shoot function that replace the coord by an F
                    print(gridPlayerB) # print the player's board
                    BoatTouchedByIA = BoatTouchedByIA + 1 # count until 14 (number of boat "parts")
                    End_Game("IA") # check if the IA won

                else:
                    shoot(gridPlayerB, IA_shot) # replace the shoot by an X
                    print("Lucky, IA missed !")




def shoot_Touched(grid, shot):
    grid.pop(shot)
    grid.insert(shot, 'F')


def shoot(grid, shot):
    grid.pop(shot)
    grid.insert(shot, 'X')



def End_Game(grid):
    if grid == "IA":
        if grid == 14: # if IA has touched all the player's boat
            OnGame = False #turn off the game
            print("Too bad, IA has won !")

    elif grid == "Player":    
        if grid == 14:
            OnGame = False #turn off the game
            print("Wow, you have won !")



def can_shoot(List, shot):
    for x in range(len(List)):#check if the shoot didn't already done
        if List[x] == shot:
            print("you've already shoot here !")
            return False
    List.append(shot)        
    return True


def put_boats(grid):
    for n in range(2,6):
        x, y, direction = random.randint(0,9), random.randint(0,9), random.randint(0,1)
        while cant_put_boat(n, x, y, direction, grid):
            x, y, direction = random.randint(0,9), random.randint(0,9), random.randint(0,1)
        put_boat(n, x, y, direction, grid)



def put_boat(length, x, y, direction, grid):
    if direction == 0: # HORIZ direction
        for i in range(length):
            position = x + 10*y
            grid.pop(position)
            grid.insert(position, 1)
            position = position + 1

    if direction == 1: # VERT direction
        for i in range(length):
            position = x + 10*y
            grid.pop(position)
            grid.insert(position, 1)
            position = position + 10

def cant_put_boat(length, x, y, direction, grid):
    return direction == 0 and x + length >= 10 or direction == 1 and y + length >= 10





"""//----------------main code----------------\\"""


main()#simply run the main function

EDIT 3 : pseudo-code working ?

def main():
    grid = []
    put_boats(grid)
    for _ in range(100):
        play(grid)
    print(grid)

def put_boats(grid):
    for b in range(2,6):
        x, y, direction = 4, 5, 0
        while cant_put_boat(b, x, y, direction, grid):
            x, y, direction = 0, 0, 0
        put_boat(b, x, y, direction, grid)

def cant_put_boat(b, x, y, direction, grid):
    # you don't need to test every position
    return direction == 0 and x + b >= 10 or direction == 1 and y + b >= 10

def put_boat(b, x, y, direction, grid):
    if direction == 0:
        for i in range(b):
            position = x + 10*y
            grid.append(position)
            position = position + 1

    if direction == 1:
        for k in range(b):
            position = x + 10*y
            grid.append(position)
            position = position + 10 

def play(grid):
    x, y = 5, 5
    while can_shoot(x, y) == False: #
        x, y = 1, 5
    shoot(grid, x + y)

def can_shoot(grid, shot):
    prev_shoots = []
    for j in range(len(prev_shoots)):
        if shot == prev_shoots[j]:
            return False
    return True

def shoot(grid, shot):
    grid.pop(shot)
    grid.insert(shot, 'X')



main()

EDIT 2 : pseudo-code :

def main():
    grid = init_grid()
    put_boats(grid)
    for _ in range(100):
        play(grid)

def put_boats(grid):
    for b in range(2,6):
        x, y, dir = random
        while cant_put_boat(b, x, y, dir):
            x, y, dir = random
        put_boat(b, x, y, dir, grid)

def cant_put_boat(b, x, y, dir, grid):
    # you don't need to test every position
    return dir == HORIZ and x + b >= 10
        or dir == VERT and y + b >= 10

def put_boat(b, x, y, dir, grid):
    if dir == HORIZ:
        for i in range(b):
            position = x + 10*y
            grid.append[position]
            position = position + 1

    if dir == VERT:
        for k in range(b):
            position = x + 10*y
            grid.append[position]
            position = position + 10 

def play(grid):
    x, y = random
    while cant_shoot(grid, x, y)
        x, y = random
    shoot(x, y)

def can_shoot(grid, shot):
    prev_shoots = []
    for j in range(len(prev_shoots)):
        if shot == prev_shoots[j]:
            return False
    return True

def shoot(grid, shot):
    grid.pop(shot)
    grid.insert(shot, 'X')

EDIT 1 : English version of my code :

"""//----------------bibliothèques----------------\\"""
import random



"""//----------------initialisation des variables----------------\\"""
grid_playerA_init = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0,       #IA's grid (player A)
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0,]

grid_playerB_init = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0,       #Player's grid (player B)
                        0, 0, 0, 1, 0, 0, 0, 0, 0, 0,
                        0, 1, 0, 1, 0, 0, 0, 1, 1, 1,
                        0, 1, 0, 1, 0, 0, 0, 0, 0, 0,
                        0, 1, 0, 1, 0, 0, 0, 0, 0, 0,
                        0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 
                        0, 1, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
                        0, 0, 0, 1, 1, 1, 1, 0, 0, 1,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0,]


previous_shots = []#empty matrix which will contain all the shots
touch = False #False because we don t already touch a boat
NbBoatTouchPlayer = 0 #count the amount of time the player touches a boat, it will help to know the end of the game
NbBoatTouchIA = 0 #count the amount of time the IA touches a boat, it will help to know the end of the game



"""//----------------fonctions----------------\\"""




def replaceBoat():#replace the '0' by '1' to symbolise a boat  
    grid_playerA_init.pop(position)
    grid_playerA_init.insert(position, 1)


def can_place_boat_here(x,y, direc, length):# check if there is no collision between boats 
    if direc == 0: # verticle direction
        for i in range(length):
            if (x + y*10+10*i) < 100:#if the boat doesn t exceed
                if grid_playerA_init [x +10*y + 10*i] == 1:#if there is already an other boat
                    return False 
            elif (x + y*10 +10*i) >= 100:#if we exceed the grid 
                for j in range(length - i):
                    #print((x + 10 * y) - (10 * i))
                    #print (i)
                    #print("can_place_boat_here")
                    #print(x,y,i)
                    if grid_playerA_init[(x + 10 * y) - (10* i)] == 1: #if there is already a boat
                        return False 
        return True

    if direc == 1: # horizontal direction
        for i in range(length):
            if ((x + 10*y + i)-9) % 10 != 0: 
                if grid_playerA_init [x + 10*y + i] == 1:
                    return False
            elif ((x+10*y)-9) % 10 == 0:
                for j in range(length - i):
                    if x+10*y-i == 1:
                        return False
                    return True
        return True



def place_boat_vertically(x,y,direc,lenght):#place a boat vertically with the coordinates
    if can_place_boat_here(x,y, direc,lenght):
        for i in range(lenght):
            global position
            position = x + y*10
            if position >= 100:
                position = position- 10 * i
                for j in range(lenght-i):
                    position = position - 10
            #print("place_boat_vertically")
            #print(x,y)
            replaceBoat()
            y = y + 1
    else:
        #####
        while can_place_boat_here(x,y, direc,lenght) == False:
            print("FALSE VERTC")
            print(lenght)
            """ -------------------------------------TEST-------------------------------------"""
            x = random.randint(0, 9)
            y = random.randint(0, 9)
            print("x")
            print(x)
            print("y")
            print(y)


            for i in range(lenght):
                position = x + y*10
                if position >= 100:
                    position = position- 10 * i
                    for j in range(lenght-i):
                        position = position - 10
                #print("place_boat_vertically")
                #print(x,y)
                replaceBoat()
                y = y + 1





def place_boat_horizontally(x,y,direc,lenght):#place a boat horizontally with the coordinates
    if can_place_boat_here(x,y, direc,lenght):
        for i in range(lenght):
            global position
            position = x + y * 10

            #print("place_boat_horizontally")
            #print(x,y)

            if (position - 9) % 10 == 0:
                replaceBoat()
                position = position -1 * i
                for j in range(lenght - i):
                    position = position - 1
                    replaceBoat()
            else:
                replaceBoat()
                x = x + 1
    else:
       #####
        while can_place_boat_here(x,y, direc,lenght) == False:
            print("FALSE HORIZ")
            print(lenght)
            """ -------------------------------------TEST-------------------------------------"""
            x = random.randint(0, 9)
            y = random.randint(0, 9)
            print("x")
            print(x)
            print("y")
            print(y)



            for i in range(lenght):
                position = x + y * 10

                #print("place_boat_horizontally")
                #print(x,y)

                if (position - 9) % 10 == 0:
                    replaceBoat()
                    position = position -1 * i
                    for j in range(lenght - i):
                        position = position - 1
                        replaceBoat()
                else:
                    replaceBoat()
                    x = x + 1


            """ -------------------------------------END OF THE TEST-------------------------------------"""            

def check_shot(shot):#check if the shot has not already been fired
    for i in range(len(previous_shots)):
        if previous_shots[i] == shot:
            return False
    return True




def replaceshot(shot):#replace the boat touched by an X
    del grid_playerB_init[shot] #delete the touched element
    grid_playerB_init.insert(shot, 'X') #by an X





def IA ():
    shot = random.randint(0, 99)
    if check_shot(shot):
        previous_shots.append(shot)

        if grid_playerB_init[shot] == 1:
            replaceshot(shot)
            touch = True

            i = 1
            nextshot = shot + i
            if grid_playerB_init[nextshot] == 1: # + 1 (right)
                replaceshot(nextshot)
                i = i + 1
                nextshot = shot + i


            elif grid_playerB_init[nextshot] == 0: # - 1(left)
                i = 1 # variable reset
                nextshot = shot - 1
                if grid_playerB_init[nextshot] == 1:
                    replaceshot(nextshot)
                    i = i + 1
                    nextshot = shot - i

            elif grid_playerB_init[nextshot] == 0: # + 10 (upstair)
                i = 1 # variable reset
                nextshot = shot - 10
                if grid_playerB_init[nextshot] == 1:
                    replaceshot(nextshot)
                    i = i + 10
                    nextshot = shot - i

            elif grid_playerB_init[nextshot] == 0: # - 10 (downstair)
                i = 1 # variable reset
                nextshot = shot + 10
                if grid_playerB_init[nextshot] == 1:
                    replaceshot(nextshot)
                    i = i + 10
                    nextshot = shot + i



def IA_pourrie(): # just a simple IA which fires randomly with no logic
    shot = random.randint(0, 99)
    if check_shot(shot):
        previous_shots.append(shot)



def Endgame(): #function which makes end of the game when all the boats are touched by one of the players
    if NbBoatTouchPlayer == 14 or NbBoatTouchIA == 14: #5+4+3+2 = 14
        print("END OF THE GAME")
        print("your grid :")
        print(grid_playerB_init)
        print("IA's grid :")
        print(grid_playerA_init)



"""//----------------corps du programme----------------\\"""


for boat in range(2, 6): # we place the boats of 2;3;4 and 5 cases 
    direction = random.randint(0, 1) # choose a random integer between 0 and 1. 0 stands for vertically and 1 stands for horizontally
    positionX = random.randint(0, 9)
    positionY = random.randint(0, 9)
    if direction == 0:
        place_boat_vertically(positionX, positionY, 0, boat)
    if direction == 1:
        place_boat_horizontally(positionX, positionY, 1, boat)
print (grid_playerA_init)



for loop in range(200):         #IA's launch
    IA()
print("")    
print(grid_playerB_init)
print("")
print(previous_shots)

Version française :

"""//----------------bibliothèques----------------\\"""
import random



"""//----------------initialisation des variables----------------\\"""
tableau_joueurA_init = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0,       #IA EN JOUEUR A
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0,]

tableau_joueurB_init = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0,       #JOUEUR EN JOUEUR B
                        0, 0, 0, 1, 0, 0, 0, 0, 0, 0,
                        0, 1, 0, 1, 0, 0, 0, 1, 1, 1,
                        0, 1, 0, 1, 0, 0, 0, 0, 0, 0,
                        0, 1, 0, 1, 0, 0, 0, 0, 0, 0,
                        0, 1, 0, 0, 0, 0, 1, 1, 0, 0, #exemple : postition x = 3 (compter le 0) et position y = 1 ---> position = 13
                        0, 1, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
                        0, 0, 0, 1, 1, 1, 1, 0, 0, 1,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0,]


previous_shots = []#matrice vide qui contiendra la liste de tous les tirs effectués
touché = False #on initialise la variable touché à False car on a pas deja touché de bateau
NbBateauxTouchésJoueur = 0 #sert à compter le nombre de bateaux touchés par le joueur, et donc de prévoir la fin du jeu
NbBateauxTouchésIA = 0 #sert à compter le nombre de bateaux touchés par l'IA, et donc de prévoir la fin du jeu



"""//----------------fonctions----------------\\"""




def replaceBateau():#remplace les '0' par des '1' pour symboliser un bateau   
    tableau_joueurA_init.pop(position)
    tableau_joueurA_init.insert(position, 1)


def peut_placer_un_bateau_ici(x,y, direc, longueur):# verifie qu'il n'y ai pas de collision au moment de placer un bateau
    if direc == 0: # verticale
        for i in range(longueur):
            if (x + y*10+10*i) < 100:#si le bateau ne dépasse pas
                if tableau_joueurA_init [x +10*y + 10*i] == 1:#si il y a deja un bateau
                    return False
            elif (x + y*10 +10*i) >= 100:#si on depasse du tableau
                for j in range(longueur - i):
                    #print((x + 10 * y) - (10 * i))
                    #print (i)
                    #print("peut_placer_un_bateau_ici")
                    #print(x,y,i)
                    if tableau_joueurA_init[(x + 10 * y) - (10* i)] == 1:
                        return False # il y a deja un bateau !
        return True

    if direc == 1: # horizontale
        for i in range(longueur):
            if ((x + 10*y + i)-9) % 10 != 0: 
                if tableau_joueurA_init [x + 10*y + i] == 1:
                    return False
            elif ((x+10*y)-9) % 10 == 0:
                for j in range(longueur - i):
                    if x+10*y-i == 1:
                        return False
                    return True
        return True



def placer_bateau_verticalement(x,y,direc,lenght):#place un bateau verticalement avec les coordonnés
    if peut_placer_un_bateau_ici(x,y, direc,lenght):
        for i in range(lenght):
            global position
            position = x + y*10
            if position > 100:
                position = position- 10 * i
                for j in range(lenght-i):
                    position = position - 10
            #print("placer_bateau_verticalement")
            #print(x,y)
            replaceBateau()
            x = x + 10
    else:
        #####
        while peut_placer_un_bateau_ici(x,y, direc,lenght) == False:
            print("FALSE VERTC")
            print(lenght)
            """ -------------------------------------TEST-------------------------------------"""
            x = random.randint(0, 9)
            y = random.randint(0, 9)
            print("x")
            print(x)
            print("y")
            print(y)


            for i in range(lenght):
                position = x + y*10
                if position > 100:
                    position = position- 10 * i
                    for j in range(lenght-i):
                        position = position - 10
                #print("placer_bateau_verticalement")
                #print(x,y)
                replaceBateau()
                x = x + 10





def placer_bateau_horizontalement(x,y,direc,lenght):#place un bateau horizontalement avec les coordonnés
    if peut_placer_un_bateau_ici(x,y, direc,lenght):
        for i in range(lenght):
            global position
            position = x + y * 10

            #print("placer_bateau_horizontalement")
            #print(x,y)

            if (position - 9) % 10 == 0:
                replaceBateau()
                position = position -1 * i
                for j in range(lenght - i):
                    position = position - 1
                    replaceBateau()
            else:
                replaceBateau()
                x = x + 1
    else:
       #####
        while peut_placer_un_bateau_ici(x,y, direc,lenght) == False:
            print("FALSE HORIZ")
            print(lenght)
            """ -------------------------------------TEST-------------------------------------"""
            x = random.randint(0, 9)
            y = random.randint(0, 9)
            print("x")
            print(x)
            print("y")
            print(y)



            for i in range(lenght):
                position = x + y * 10

                #print("placer_bateau_horizontalement")
                #print(x,y)

                if (position - 9) % 10 == 0:
                    replaceBateau()
                    position = position -1 * i
                    for j in range(lenght - i):
                        position = position - 1
                        replaceBateau()
                else:
                    replaceBateau()
                    x = x + 1




def check_tir(tir):#verifie que le tir n'a pas deja été fait
    for i in range(len(previous_shots)):
        if previous_shots[i] == tir:
            return False
    return True




def replaceTir(tir):#remplace le bateau touché par une croix
    del tableau_joueurB_init[tir] #supprime l'element touché
    tableau_joueurB_init.insert(tir, 'X') #par un X





def IA ():
    tir = random.randint(0, 99)
    if check_tir(tir):
        previous_shots.append(tir)

        if tableau_joueurB_init[tir] == 1:
            replaceTir(tir)
            touché = True

            i = 1
            nextshot = tir + i
            if tableau_joueurB_init[nextshot] == 1: # + 1 (à droite)
                replaceTir(nextshot)
                i = i + 1
                nextshot = tir + i


            elif tableau_joueurB_init[nextshot] == 0: # - 1(à gauche)
                i = 1 # on reset la variable
                nextshot = tir - 1
                if tableau_joueurB_init[nextshot] == 1:
                    replaceTir(nextshot)
                    i = i + 1
                    nextshot = tir - i

            elif tableau_joueurB_init[nextshot] == 0: # + 10 (en haut)
                i = 1 # on reset la variable
                nextshot = tir - 10
                if tableau_joueurB_init[nextshot] == 1:
                    replaceTir(nextshot)
                    i = i + 10
                    nextshot = tir - i

            elif tableau_joueurB_init[nextshot] == 0: # - 10 (en bas)
                i = 1 # on reset la variable
                nextshot = tir + 10
                if tableau_joueurB_init[nextshot] == 1:
                    replaceTir(nextshot)
                    i = i + 10
                    nextshot = tir + i



def IA_pourrie():
    tir = random.randint(0, 99)
    if check_tir(tir):
        previous_shots.append(tir)



def finDuGame():        #fonction qui met fin au jeu, quand tous les bateaux sont touchés pour n'importe quel joueur
    if NbBateauxTouchésJoueur == 14 or NbBateauxTouchésIA == 14: #5+4+3+2 = 14
        print("FIN DU JEU")
        print("votre jeu :")
        print(tableau_joueurB_init)
        print("jeu de l'IA :")
        print(tableau_joueurA_init)



"""//----------------corps du programme----------------\\"""


for bateau in range(2, 6): # on place les bateaux de 2;3;4 et 5 cases 
    direction = random.randint(0, 1) # choisi un nombre entre 0 et 1. 0 correspond à la verticale et 1 à l'horizontale
    positionX = random.randint(0, 9)
    positionY = random.randint(0, 9)
    if direction == 0:
        placer_bateau_verticalement(positionX, positionY, 0, bateau)
    if direction == 1:
        placer_bateau_horizontalement(positionX, positionY, 1, bateau)
print (tableau_joueurA_init)



for loop in range(200):         #lancement de l'IA
    IA()
print("")    
print(tableau_joueurB_init)
print("")
print(previous_shots)
jejegoodgame
  • 41
  • 1
  • 11
  • 4
    That's a *lot* of code to ask us to debug for you. Can you describe what is going wrong in more detail? Are you getting an exception? If so, [edit] your question to provide the full traceback. Are you getting incorrect results? Please describe them to us (what you're getting and what you expected instead). If at all possible, try to reduce your code to a [mcve] that still has the same problem. – Blckknght Apr 19 '19 at 10:05
  • What's more, if you really think about programming, you should start writing your code in english. I know that it can be a pain for the frenchman but doing that makes your code understandable for many more people. – woockashek Apr 19 '19 at 10:59

2 Answers2

2
  • As said in a comment, always write code and comments in english, unless you have a very good reason (e.g. technical or administrative terms).
  • Use snake_case (PEP8).
  • Avoid global variables: they make the code a lot harder to read.
  • Maybe use two dimensional lists.

Now, the basics:

>>> L = [2]
>>> L.pop(1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: pop index out of range
>>> L[1]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list index out of range

In your code:

def peut_placer_un_bateau_ici(x,y, direc, longueur):# verifie qu'il n'y ai pas de collision au moment de placer un bateau
    ...
            elif (x + y*10 +10*i) >= 100:#si on depasse du tableau
                ...
                    if tableau_joueurA_init[(x + 10 * y) - (10* i)] == 1:

What happens if x + y*10 >= 100 and i == 0? You have (x + 10 * y) - (10* i) = (x + y*10 +10*i) >= 100 and list index out of range!!

Then:

def placer_bateau_verticalement(x,y,direc,lenght):#place un bateau verticalement avec les coordonnés
    ...
            if position > 100:
                # fix position
           replaceBateau()

And

def replaceBateau():#remplace les '0' par des '1' pour symboliser un bateau   
    tableau_joueurA_init.pop(position)
    tableau_joueurA_init.insert(position, 1)

(BTW, a list is mutable: `tableau_joueurA_init[position] = 1` would do the trick)

What happens if position == 100? pop index out of range!

You have to watch out for edges cases. An advice: clean your code, fix the bugs and then post it to https://codereview.stackexchange.com.

EDIT. A little help because you seem lost.

In the first place, you need to learn about Top-down approach:

The technique for writing a program using top–down methods is to write a main procedure that names all the major functions it will need. Later, the programming team looks at the requirements of each of those functions and the process is repeated.

Let's try to apply this to your program in pseudo-code:

def main():
    grid = init_grid()
    put_boats(grid)
    for _ in range(100):
        play(grid)

def put_boats(grid):
    for b in range(2,6):
        x, y, dir = random
        while cant_put_boat(b, x, y, dir):
            x, y, dir = random
        put_boat(b, x, y, dir, grid)

def cant_put_boat(b, x, y, dir, grid):
    # you don't need to test every position
    return dir == HORIZ and x + b >= 10
        or dir == VERT and y + b >= 10

def put_boat(b, x, y, dir, grid):
    # you can write it yourself

def play(grid):
    x, y = random
    while cant_shoot(grid, x, y)
        x, y = random
    shoot(x, y)

def can_shoot(grid, shot)
    # you can write it yourself

def shoot(grid, shot)
    # you can write it yourself

That's very schematic, but should help you. Try to make it simple and clean. Create small functions (4-6 lines of code, no more). Do not switch to classes before you feel comfortable with procedural code.

Use assert and, if possible, test your code with doctest.

Last trick:

tableau_joueurA_init = [0, 0, ..., 0]

Is the same as:

tableau_joueurA_init = [0]*100 # 100 times the element 0
jferard
  • 7,835
  • 2
  • 22
  • 35
  • thanks for your reply, I know I should do this in english but I need it in french for my high school diploma. How can I avoid global variable ? My grid is 10*10 however, it has 0 to 99 values so you can't have position == 100. I'll try to put the code in english and then, repost it to StackExchanges. Thanks for the help ! – jejegoodgame Apr 21 '19 at 11:01
  • @jejegoodgame You have mainly two ways to avoid global varaibles: 1. pass the value as an argument; 2. create objects that have a state. E.g.1: `def replaceBateau(**position**):`. E.g.2: make `placerBateauVerticalement`, ..., methods of a class `Board` that contains the grid. – jferard Apr 21 '19 at 17:51
  • @jejegoodgame You say that *you can't have position == 100*. Imagine you have `x= y = 10`. Then `position = x + y*10 == 100`. Since you decrease `position` only if `position > 100` (note the **>** and not **>=**), you have `position == 100` in `replaceBateau`. – jferard Apr 21 '19 at 17:55
  • how can x and y == 100 ? x is a random integer between 0 and 9, same for y – jejegoodgame Apr 22 '19 at 09:02
  • @jejegoodgame. In your `for i in range(lenght)` loop: `x = x + 10` (why `+10` ??). Start with `x = 0, y = 9`, ie `x + y*10 = 90`, iterate once: `x = 10, y = 9, x + y*10 = 100`. (You added yourself a test `position > 100` (should be `>=`), I guess you noticed it might be true). – jferard Apr 22 '19 at 09:25
  • Yes, stupid idea x = x + 10, I should just write y = y + 1. I don't understand why it should be position >= 100 because the max position is 99, can you explain please ? And how can I upload the code in English ? – jejegoodgame Apr 23 '19 at 10:08
  • @jejegoodgame when I write `position >= 100`, I mean that if a position is greater or equal to `100`, it's time to act! The lines `if position > 100:/position = position- 10 * i/for j in range(lenght-i):/position = position - 10` should be `if position >= 100:...` (I didn't check if the position is correctly fixed). – jferard Apr 23 '19 at 14:59
  • @jejegoodgame just click the edit button at the bottom of your question. Be sure to add the info that the code was editer and why. – jferard Apr 23 '19 at 16:29
  • with `grid_playerA_init = [0]*100` , can we change a value of one "0" (for example the 3rd 0 is now a 1) and then print its value ? – jejegoodgame Apr 24 '19 at 18:20
  • @jejegoodgame yes. See https://stackoverflow.com/a/24557558/6914441 for infos on this syntax. – jferard Apr 24 '19 at 19:54
  • ok I'll try. I've completed your pseudo-code, it's the EDIT 2 – jejegoodgame Apr 25 '19 at 09:20
  • the list = [0] * 100 works correctly as I expected. So, thank you for the advice. – jejegoodgame Apr 25 '19 at 18:24
  • @jejegoodgame Okay. Can you make it work now? ie replace `= random` by a tuple of random numbers, assign a value to ̀`HORIZ` and `VERT`, sert the grid values by `grid[position] = 1` instead of `append` and show the grid at the end? And fix every syntax errror that might remain. You have to try to make a minimal version that works. – jferard Apr 25 '19 at 20:21
  • just post the 3rd edit, it contains the pseudo-code working ... I guess. – jejegoodgame Apr 25 '19 at 21:09
  • post the 4th edit, it works however, it don't put all the boats, only one point and I don't understand why, can you help me ? (if you want to print the IA's board, just write print(gridPlayerA) . ) – jejegoodgame Apr 27 '19 at 20:01
  • @jejegoodgame Nice! In `put_boat`: 1. Don't `pop` and `push`, just `grid[position] = 1`. 2. look at the loop: at the end of the loop, you increase `position` by 1 (h) or 10 (vert), but you always have `position = x + 10*y`. You write `position = x+i + 10*y` or `position = x + 10*(y+i)` and never update `position` directly. I tested and it should work. – jferard Apr 28 '19 at 08:30
  • Big thank you ! It works pretty well, do you know about tkinter ? or should I post another question ? – jejegoodgame Apr 28 '19 at 11:58
  • @jejegoodgame You're welcome. Previous question: No. Compute the position (`position = x + 10*(y+i)`) *then* update the grid (`grid[position] = 1`) for every `i in range(length)`. Now, just accept the answer if it's ok and edit the [question on Code Review](https://codereview.stackexchange.com/questions/217834/battleship-game-in-python-for-a-project-in-school): you have a lot to learn and to refactor before moving to `tkinter`. (And yes, that is another question, but you can't just ask for someone to add a GUI to your code: you have to try by yourself). – jferard Apr 28 '19 at 12:29
  • I will post my code into the section "answer your question", and see with the others who worked with me, if they can add a GUI. – jejegoodgame Apr 28 '19 at 12:52
0

Before I post the entire code of my part, I want to say a big thank you to @jferard who guide me and help me alot

Here's the code :

"""//----------------library----------------\\"""
import random



"""//----------------variables----------------\\"""

gridPlayerA = [0] * 100 #IA's grid
gridPlayerB = [0] * 100 #Player's grid


gridGamePlayerA = [0] * 100 # just create a copy of the IA boards and only show the empty board



OnGame = True # turn on the game

PrevPlayerShoots = []
PrevIAShoots = []





"""//----------------functions----------------\\"""

def main():

    BoatTouchedByPlayer = 0
    BoatTouchedByIA = 0
    print(gridPlayerB)
    
    put_boats(gridPlayerA)#put IA's boats randomly
    put_boats(gridPlayerB)#put Player's boats randomly (temporarily)
    while OnGame == True: #while we're in the game


        print(gridGamePlayerA) # print the IA's grid (with no boats)


        PlayerShoot = input("It's your turn, where do you want to shoot ? (give the coords like this : A,1) ")
        PlayerShoot.split(",") #Split the coords
        Lettre = PlayerShoot[0] # take the lettre
        Number = int(PlayerShoot[2]) # take the number, convert into an integer

        Player_Shoot = int(ord(Lettre)) - 65 + 10*(Number - 1) # transform the lettre and the number into a number coord


        if can_shoot(PrevPlayerShoots, Player_Shoot) == True: #if we can shoot


             
            if gridPlayerA[Player_Shoot] == 1: # if we touch a boat in the IA's board
                shoot_Touched(gridGamePlayerA, Player_Shoot) # call the shoot function that replace the coord by an F on the empty board
                print("touched")
                BoatTouchedByPlayer = BoatTouchedByPlayer + 1# count until 14 (number of boat "parts")
                End_Game("Player") # check if the player won
            

            else:
                shoot(gridGamePlayerA, Player_Shoot)# show the shot next time to remind the player where he has shoot
                print("missed, IA's turn : ")
                IA_shot = random.randint(0, 99) # make the IA shot randomly 
                if gridPlayerB[IA_shot] == 1:# if IA touch a boat in the player's board
                    print("IA touched one of your boat")
                    shoot_Touched(gridPlayerB, IA_shot) # call the shoot function that replace the coord by an F
                    print(gridPlayerB) # print the player's board
                    BoatTouchedByIA = BoatTouchedByIA + 1 # count until 14 (number of boat "parts")
                    End_Game("IA") # check if the IA won

                else:
                    shoot(gridPlayerB, IA_shot) # replace the shoot by an X
                    print("Lucky, IA missed !")

        

    
def shoot_Touched(grid, shot):
    grid.pop(shot)
    grid.insert(shot, 'F')


def shoot(grid, shot):
    grid.pop(shot)
    grid.insert(shot, 'X')
    


def End_Game(grid):
    if grid == "IA":
        if grid == 14: # if IA has touched all the player's boat
            OnGame = False #turn off the game
            print("Too bad, IA has won !")
        
    elif grid == "Player":    
        if grid == 14:
            OnGame = False #turn off the game
            print("Wow, you have won !")



def can_shoot(List, shot):
    for x in range(len(List)):#check if the shoot didnt already done
        if List[x] == shot:
            print("you've already shoot here !")
            return False
    List.append(shot)        
    return True


def put_boats(grid):
    for n in range(2,6):
        x, y, direction = random.randint(0,9), random.randint(0,9), random.randint(0,1)
        while cant_put_boat(n, x, y, direction, grid):
            x, y, direction = random.randint(0,9), random.randint(0,9), random.randint(0,1)
        put_boat(n, x, y, direction, grid)
            


def put_boat(length, x, y, direction, grid):
    if direction == 0: # HORIZ direction
        for i in range(length):
            position = x+i + 10*y
            grid[position] = 1
            

    if direction == 1: # VERT direction
        for i in range(length):
            position = x + 10*(y+i)
            grid[position] = 1
            

def cant_put_boat(length, x, y, direction, grid):
    return direction == 0 and x + length >= 10 or direction == 1 and y + length >= 10





"""//----------------main code----------------\\"""


main()#simply run the main function
jejegoodgame
  • 41
  • 1
  • 11