python - 如何在战舰项目的python3中检测碰撞

标签 python

我想用 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)

最佳答案

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

您已了解解决方案,您可以通过使用 peu d'aide pour l'ècrire 来开发。

你的目标是什么? (考虑 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’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!

我是你的助手!

关于python - 如何在战舰项目的python3中检测碰撞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54792231/

相关文章:

python - 对一列中位于其他列值和条件语句之间的值进行分组

python - 在 dask 中设置 Parquet 输出文件的大小

python - 为什么 Openshift 添加 cache-control private?

python - 在(非常大的)pandas 数据框中查找值并将其存储到字典中

python - 如何定义作为函数的枚举值?

python - 计算 Pandas 中不均匀的箱子

python - Scrapy - 如何根据抓取项目中的链接抓取新页面

python - 更新 dict 值不会更新它引用的全局变量

python - 程序停止运行是由python udp 停留在一个地方引起的

python - 使用代理使用 tornado.httpclient 访问 twitter