bataille_navale.py

Created by ph-moutou

Created on June 08, 2018

2.42 KB

Le programme choisis les emplacements de 5 bateaux (longueurs égales à 2, 3, 3, 4 et 5) que le joueur doit couler en un minimum de coups. Successivement il donne les coordonnées d’une case sous la forme de 2 chiffres suivis de “EXE” (18 pour colonne=1 et ligne=8) : si un bateau est touché, le point s’allume en rouge sinon il s’allume en blanc. Lorsqu’un bateau est coulé un message le signale.


from random import *
from math import *
from kandinsky import *

def segmentH(x1,x2,y):
  for i in range(x2-x1):
    set_pixel(x1+i,y,noir) 

def segmentV(x,y1,y2):
  for i in range(y2-y1):
    set_pixel(x,y1+i,noir)

def carre(x,y,c,l):
  for i in range(l):
    for j in range(l):
      set_pixel(x+i,y+j,c) 

def plateau():
  for i in range(10):
    draw_string(str(i),45+i*20,205)
    draw_string(str(9-i),20,2+i*20)
  carre(40,0,bleu,200)
  for i in range(11):
    segmentH(40,240,0+i*20)
    segmentV(40+i*20,0,200)
  set_pixel(240,200,noir)#dernier pt

def cercle(x0,y0,r,c,e):
  for i in range(2*e):
    xd=x0-int((r-i*0.5)/sqrt(2))
    xf=x0+int((r-i*0.5)/sqrt(2))
    for x in range(xd,xf+1):
      x1=x
      y1=y0+int(sqrt((r-i*0.5)**2-(x-x0)**2))
      set_pixel(x,y1,c)
      for j in range(3):
        x2=x0+y1-y0
        y2=y0+x0-x1
        set_pixel(x2,y2,c)
        x1,y1=x2,y2
        
def message(t,c="",m=""):
  carre(245,70,blanc,70)
  draw_string("coup "+c,245,70)
  draw_string("("+t[0]+"; "+t[1]+")",245,105)
  draw_string(m,245,120)
  
def entrer():
  c=input()
  if len(c)==2:return c
  else:entrer()

def bn(s):
  seed(s)
  B=[2,3,3,4,5]
  E=len(B)*[[]]
  T=[]
  X=sum(B)*[0]
  plateau()
  #choix des emplacements
  for i in range(len(B)):
    r=randint(0,1)#0:horiz.,1:vert.
    good=False
    while good==False:
      K=B[i]*[0]
      if r==0:
        c=randint(0,10-B[i])
        l=randint(0,9)
      else:
        c=randint(0,9)
        l=randint(0,10-B[i])
      good=True
      for j in range(B[i]):
        if r==0:K[j]=str(c+j)+str(l)
        else:K[j]=str(c)+str(l+j)
        if K[j] in T:good=False
    E[i]=K
    for j in range(B[i]):T.append(K[j])
  print(E)
  #jeu
  cp=0
  while sum(X)!=len(X):
    coord=entrer()
    cp+=1
    col=int(coord[0])#colonne(0-9)
    lig=int(coord[1])#ligne  (0-9)
    txt="Rate"
    if coord in T:
      coul=rouge
      rg=T.index(coord)
      X[rg]=1
      txt="Touche"
      for i in range(len(E)):
        if coord in E[i]:
          tch=0
          for j in range(len(E[i])):
            rg=T.index(E[i][j])
            if X[rg]==1:tch+=1
          if tch==len(E[i]):
            txt="Coule"
    else: coul=blanc
    message(coord,str(cp),txt)
    cercle(50+20*col,190-20*lig,9,coul,9)
    print(txt)
  draw_string("Bravo!",245,180)
  print("Mission accomplie en {} coups".format(cp))

noir=color(0,0,0)
bleu=color(150,150,255)
blanc=color(255,255,255)
rouge=color(255,55,55)

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.