在学校的一个项目中用python编写的战舰游戏

在学校的一个项目中用python编写的战舰游戏,python,Python,我是一名编程初学者,我想为我的学校项目用python 3.6构建一个战舰游戏,我有一些错误,但我不知道如何修复它。如果您试图运行我的代码,请运行它直到出现错误,因为有时没有问题。我以前在placer_bateau_垂直缩放功能中有弹出超出范围,还有一个列表索引超出范围错误。这是我的密码: 使用伪代码编辑4:python代码 """//----------------library----------------\\""" import random """//---------------

我是一名编程初学者,我想为我的学校项目用python 3.6构建一个战舰游戏,我有一些错误,但我不知道如何修复它。如果您试图运行我的代码,请运行它直到出现错误,因为有时没有问题。我以前在placer_bateau_垂直缩放功能中有弹出超出范围,还有一个列表索引超出范围错误。这是我的密码:

使用伪代码编辑4:python代码

"""//----------------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
编辑3:伪代码工作?

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()
编辑2:伪代码:

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')
"""//----------------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)
"""//----------------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)
编辑1:我的代码的英文版本:

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')
"""//----------------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)
"""//----------------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)
“”“//--------------圣经问题-----------------------------\“””
随机输入
“”“/--------------变量初始化-------------------\\“”
网格_playerA_init=[0,0,0,0,0,0,0,0,0,#IA的网格(玩家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,]
网格玩家B_init=[0,0,0,0,0,0,0,0,0,0,#玩家网格(玩家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,]
前一个镜头=[]。#将包含所有镜头的空矩阵
因为我们还没有碰过船
NbBoatTouchPlayer=0#计算玩家触碰船只的时间,这将有助于了解游戏的结束
NbBoatTouchIA=0#计算IA触船的时间,这将有助于了解游戏的结束
“/--------------功能-------------------\”
def REPLACEBOOT():#将“0”替换为“1”以表示船
网格\u playerA\u初始弹出(位置)
网格播放初始插入(位置1)
def可以在此处放置船只(x、y、方向、长度):#检查船只之间是否没有碰撞
如果direc==0:#垂直方向
对于范围内的i(长度):
如果(x+y*10+10*i)<100:#如果船不超过
如果grid_playerA_init[x+10*y+10*i]==1:#如果已经有另一条船
返回错误
elif(x+y*10+10*i)>=100:#如果我们超出网格
对于范围内的j(长度-i):
#打印((x+10*y)-(10*i))
#印刷品(一)
#打印(“可在此放置船只”)
#打印(x、y、i)
如果网格_playerA _init[(x+10*y)-(10*i)]==1:#如果已经有船
返回错误
返回真值
如果direc==1:#水平方向
对于范围内的i(长度):
如果((x+10*y+i)-9)%10!=0: 
如果网格_playerA _init[x+10*y+i]==1:
返回错误
elif((x+10*y)-9)%10==0:
对于范围内的j(长度-i):
如果x+10*y-i==1:
返回错误
返回真值
返回真值
def垂直放置(x,y,direc,lenght):#使用坐标垂直放置船只
如果你能把船放在这里(x,y,direc,lenght):
对于范围内的i(长度):
全球地位
位置=x+y*10
如果位置>=100:
位置=位置-10*i
对于范围内的j(长度-i):
位置=位置-10
#打印(“垂直放置船只”)
#打印(x,y)
替换船()
y=y+1
其他:
#####
而可以将船放在这里(x,y,direc,lenght)=False:
打印(“假VERTC”)
打印(长度)
“”-------------------------------------------测试-------------------------------------------“”“
x=random.randint(0,9)
y=random.randint(0,9)
打印(“x”)
打印(x)
打印(“y”)
打印(y)
对于范围内的i(长度):
位置=x+y*10
如果位置>=100:
位置=位置-10*i
对于范围内的j(长度-i):
位置=位置-10
#打印(“垂直放置船只”)
#打印(x,y)
替换船()
y=y+1
def水平放置(x,y,direc,lenght):#使用坐标水平放置船只
如果你能把船放在这里(x,y,direc,lenght):
对于范围内的i(长度):
全球地位
位置=x+y*10
#打印(“水平放置船只”)
#打印(x,y)
如果(位置-9)%10==0:
替换船()
位置=位置-1*i
对于范围内的j(长度-i):
位置=位置-1
替换船()
其他:
替换船()
x=x+1
其他:
#####
而可以将船放在这里(x,y,direc,lenght)=False:
打印(“假水平线”)
打印(长度)
“”-------------------------------------------测试-------------------------------------------“”“
x=random.randint(0,9)
y=random.randint(0,9)
打印(“x”)
打印(x)
P
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)
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
tableau_joueurA_init = [0, 0, ..., 0]
tableau_joueurA_init = [0]*100 # 100 times the element 0
"""//----------------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