简体   繁体   中英

How to detect collision in python3 for battleship project

I'd like to create a battleship game in python, but I don't have any idea of how to detect collisions between two ships. I use random module to place the ship on my 10*10 grid. Then, I place (randomly) the 5 cases boat. And after, I place (randomly) my 4 cases ship. My question is : How can I detect if when I place another ship, that there is not a boat already ?

Here is my code :

import random

tableau_joueurA_init = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                    0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                    0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                    0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                    0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                    0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
                    0, 0, 0, 0, 0, 0, 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,
                    0, 0, 0, 1, 0, 0, 0, 0, 0, 0,
                    0, 0, 0, 1, 0, 0, 0, 0, 0, 0,
                    0, 0, 0, 1, 0, 0, 0, 0, 0, 0,
                    0, 0, 0, 1, 0, 0, 0, 0, 0, 0,
                    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, #exemple :  postition x = 3 (compter le 0) et position y = 1 ---> position = 13
                    0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                    0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                    0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                    0, 0, 0, 0, 0, 0, 0, 0, 0, 0,]




def replace():#remplace les '0' par des '1' pour symboliser un tir
    del tableau_joueurA_init[position]
    tableau_joueurA_init.insert(position, 1)



#placement du bateau de 5 :
direction = random.randint(0, 1) # choisi un nombre entre 0 et 1. 0 correspond à la verticale et 1 à l'horizontale
positionX = random.randint(0, 10)
positionY = random.randint(0, 10)

if direction == 0: #si direction est verticale
    for i in range(5):
        position = positionX + (positionY * 10) # on combine les 2 coordonnées pour les avoir dans le tableau 
        if position > 100: # si le bateau dépasse
                position = position -10*i
                for j in range(5-i):#on le fait remonter avec les i restants
                    position = position - 10              
                                                    #tableau_joueurA_init[position] = 1
        replace()
        positionX = positionX + 10


if direction == 1: #si direction est horizontale
    for i in range(5):
        position = positionX + (positionY * 10) # on combine les 2 coordonnées pour les avoir dans le tableau 
        if (position - 9) % 10  == 0:#si le bateau est sur une bordure droite du tableau (9, 19, 29, 39 ...)
            replace()
            position = position -1*i
            for j in range(4-i):#4 car on a le replace sur la bordure qui compte comme 1
                position = position - 1
                replace()
        else:
            replace()
            positionX = positionX + 1



#placement du bateau de 4 :
direction = random.randint(0, 1) # choisi un nombre entre 0 et 1. 0 correspond à la verticale et 1 à l'horizontale
positionX = random.randint(0, 10)
positionY = random.randint(0, 10)

if direction == 0: #si direction est verticale
    for i in range(4):        
        position = positionX + (positionY * 10) # on combine les 2 coordonnées pour les avoir dans le tableau
        if position > 100: # si le bateau dépasse
            position = position -10*i
            for j in range(4-i):#on le fait remonter avec les i restants
                    position = position - 10              
                                            #tableau_joueurA_init[position] = 1
        replace()
        positionX = positionX + 10


if direction == 1: #si direction est horizontale
    for i in range(4):
        position = positionX + (positionY * 10) # on combine les 2 coordonnées pour les avoir dans le tableau 
        if (position - 9) % 10  == 0:#si le bateau est sur une bordure droite du tableau (9, 19, 29, 39 ...)
            replace()
            position = position -1*i
            for j in range(3-i):#4 car on a le replace sur la bordure qui compte comme 1
                position = position - 1
                replace()
        else:
            replace()
            positionX = positionX + 1





print (tableau_joueurA_init)

EDIT: I do not speak french but my sister is fluent so here is the answer in french:

Vous comprenez la solution mais vous devez un peu d'aide pour l'ècrire.

Qu'est-ce que nous aimons se passer? (Considerons 1 direction pour ce moment):

if direction == 0:
    # La nouvelle méthode d’aide, dans la programmation, c’est important de casser les 
    # comportements complexes. Les functions du code sont plus façiles à lire.
    if can_place_ship_here(positionX, positionY, direction, 4):
        for i in range(4):        
        position = positionX + (positionY * 10) 
        if position > 100: 
            position = position -10*i
            for j in range(4-i):
                position = position - 10                                                 
        replace()
        positionX = positionX + 10

Donc, nous devons cette function magique qui s'appelle can_place_ship_here(x, y, dir, length):

def can_place_ship_here(x, y, dir, length):
    if dir == 0:
        for i in range(length):
            if tableau_joueurA_init[x + i][y] == 1:
                return False # There's already a ship here!

    # TODO - check for ships in direction == 1

    return True # We didn't find any ships!

J'espère que cela vous aide!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM