首页 > 解决方案 > 如何在战舰项目的python3中检测碰撞

问题描述

我想用python创建一个战舰游戏,但我不知道如何检测两艘船之间的碰撞。我使用随机模块将船放置在我的 10*10 网格上。然后,我(随机)放置 5 箱船。之后,我(随机)放置我的 4 个箱子。我的问题是:如何检测当我放置另一艘船时,是否已经没有船了?

这是我的代码:

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)

标签: python

解决方案


编辑:我不会说法语,但我姐姐很流利,所以这是法语的答案:

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

Qu'est-ce que nous aimons se passer? (考虑1方向倾倒力矩):

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'appellecan_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 助手!


推荐阅读