webtest.py

Created by andreanx

Created on December 04, 2020

10.7 KB

Script support de participation pour le concours de rentrée 2020 TI-Planet et Planète Casio. https://tiplanet.org/forum/viewtopic.php?f=49&t=24387 Version 3.


#cas
from kandinsky import set_pixel as poly_set_pixel
from kandinsky import fill_rect as poly_fill_rect
from kandinsky import draw_string as poly_draw_string

screen_w, screen_h, font_w, font_h = 320, 222, 11, 18

def poly_pause():
  import ion
  def key_down():
    for k in range(53):
      if ion.keydown(k):
        return 1
    return 0
  while key_down():
    pass
  while not (key_down()):
    pass

def poly_draw_line(x1, y1, x2, y2, c):
  m, a1, b1, a2, b2 = 0, int(x1), int(y1), int(x2), int(y2)
  if (x2 - x1) ** 2 < (y2 - y1) ** 2:
    m, a1, a2, b1, b2 = 1, b1, b2, a1, a2
  if min(a1, a2) != a1: a1, b1, a2, b2 = a2, b2, a1, b1
  for k in range(a2 - a1 + 1):
    a, b = a1 + k, int(b1 + (b2 - b1) * k / ((a2 - a1) or 1))
    poly_set_pixel((a, b)[m], (b, a)[m], c)

#cas
from random import random, randint, choice
from math import pi, cos, sin, floor
#from polycal3 import *

#screen_w, screen_h, font_w, font_h, poly_draw_line, poly_fill_rect, poly_draw_string, poly_show, poly_pause = get_infos()

# def color2int(r, g, b):
#  return r | (g * 256) | (b * 65536)
def int2color(v):
  return [v & 255, (v // 256) & 255, (v // 65536) & 255]
color_back = 0
color_front = 16777215
sprites=(
  [16, 16, 1696283181465067017015386769703278171010768577886963022407551036942422377408, 32767, 0],
  [16, 16, 90406913605304324316311450749470454119920054623686274066983603664472111106016, 255, 8355711],
  [16, 7, 1142449095824479245067099987647496, 65535, 65280],
  [16, 8, 15961181869632440322022688966142263820, 16711680, 16711935],
  [11, 16, 95780240586838128711004227852982791444309999934708792, 16255, 0],
  [5, 16, 680099750626616230187012, 65280, 8355711],
  [9, 16, 5153358514263728141002102549763850240, 16776960, 8355711]
)

mwspr, mhspr, zoom, mwc = 0, 0, 1, 1

m_p, m_l, m_k, m_b, m_d, m_a, m_m = 1, 4, 16, 64, 256, 1024, 4096

def xy(s, n, w=0, h=0):
  a = s * 2 * pi / n
  x, y = round(xweb + rweb*cos(a)), round(yweb + rweb*sin(a))
  x += [[not(not(w)), -w][x < xweb], -w//2][s * 4 == n or s * 4 == 3 * n]
  y += [[not(not(h)), -h][y < yweb], -h // 2][s == 0 or s * 2 == n]
  return x, y

def draw_sprite(i_spr, x, y, i_c, z=1):
  spr = sprites[i_spr]
  w, h, v, dx, dy = spr[0], spr[1], spr[2], 0, 0
  while v > 0:
    if v & 1:
      poly_fill_rect(int(x + z*dx),int(y + z*dy), z, z, int2color(spr[3 + i_c]))
    dy = (dy + 1) % h
    x += z * (not(dy))
    v = v // 2

def draw_connection(s1, s2, c):
  x1, y1 = xy(s1, dimweb)  
  x2, y2 = xy(s2, dimweb)  
  poly_draw_line(x1, y1, x2, y2, int2color(c))

def draw_connections(s, s0, c):
  for i in range(s0, dimweb):
    if web[s][i]:
      draw_connection(s, i, c)

def refresh_platform(i):
  x, y = xy(i, dimweb, wpf, hpf)
  poly_fill_rect(x, y, wpf, hpf, int2color(color_back))
  pfi = platforms[i]
  if pfi:
    for k in range(len(sprites)):
      if pfi & (3 * 2**(2 * k)):
        x, y = xy(i, dimweb, sprites[k][0], sprites[k][1])
        draw_sprite(k, x, y, (pfi // 2**(2*k + 1)) & 1)
  else:
    x, y = xy(i, dimweb, font_w, font_h)
    poly_draw_string([chr(i + 55), str(i)][i <= 9], x, y, int2color(color_front), int2color(color_back))

def insertinto(l1, l2):
  for v in l1:
    if v not in l2:
      l2.append(v)
  return l2

def removefrom(l1, l2):
  for v in l1:
    try:
      l2.remove(v)
    except:
      pass
  return l2

def connectPlatforms(s1, s2):
  global web
  web[s1][s2], web[s2][s1] = 1, 1

def get_reachable_platforms_from_platforms(l, safe):
  lv = []
  for s in l:
    for i in range(dimweb):
      if web[s][i]:
        if i not in lv and (not(safe) or not (platforms[i] & m_p)):
          lv.append(i)
  return lv

def cango(s1, s2, safe):
  lvo1, lvi1, lvo2, lvi2, t_inter, k = [], [s1], [], [s2], 0, 0
  while not (t_inter) and len(lvi1) and len(lvi2):
    lvo1, lvo2 = insertinto(lvo1, lvi1), insertinto(lvo2, lvi2)
    for v in lvo1:
      if v in lvo2:
        return k
    lvi1, lvi2 = get_reachable_platforms_from_platforms(lvo1, safe), get_reachable_platforms_from_platforms(lvo2, safe)
    lvi1, lvi2 = removefrom(lvo1, lvi1), removefrom(lvo2, lvi2)
    k += 1
  return 0

def my_bitor(a, b):
  return ~(~a & ~b)

def init_web(d, p_p, p_b):
  global web, xweb, yweb, rweb, platforms, mwspr, mhspr, zoom, mwc, sprites, screen_w, screen_h
  for i in range(1, len(sprites)):
    mwspr, mhspr = max(mwspr, sprites[i][0]), max(mhspr, sprites[i][1])
  yweb, l0 = screen_h / 2, list(range(dimweb))
  rweb = yweb - max(mhspr, font_h)
  l0.remove(0)
  web, xweb, platforms, conn, dconn, i_k = [], rweb + max(mwspr, font_w), [0 for k in range(dimweb)], [0], list(range(1, dimweb)), choice(l0)
  for j in range(dimweb):
    web.append([0 for k in range(dimweb)])
  while len(dconn):
    s = dconn[randint(0, len(dconn) - 1)]
    connectPlatforms(conn[randint(0, len(conn) - 1)], s)
    dconn.remove(s)
    conn.append(s)
  for j in range(dimweb-1):
    for i in range(j + 1, dimweb):
      if floor(d + random()):
        connectPlatforms(i, j)
  i_d = choice(l0)
  platforms[i_d] = my_bitor(platforms[i_d], m_d)
  l1 = list(l0)
  for v in get_reachable_platforms_from_platforms([0], 0):
    l1.remove(v)
  if not(len(l1)):
    l1 = l0
  l2 = list(l1)
  for v in get_reachable_platforms_from_platforms(get_reachable_platforms_from_platforms([0], 0), 0):
    try:
      l2.remove(v)
    except:
      pass
  if not(len(l2)):
    l2 = l1
  i_l = choice(l2)
  platforms[i_l] = my_bitor(platforms[i_l], m_l)
  platforms[i_k] = my_bitor(platforms[i_k], m_k)
  for i in l1:
    if i != i_k and i != i_d and floor(p_p*dimweb/len(l1) + random()):
      platforms[i] = my_bitor(platforms[i], m_p)
      if not cango(0, i_k, 1) or not cango(0, i_d, 1):
        platforms[i] = platforms[i] & ~m_p
    if floor(p_b*dimweb/len(l1) + random()):
      platforms[i] = my_bitor(platforms[i], m_b)
  for k in range(len(sprites), 0, -1):
    tzoom = int(min(((screen_w - screen_h) / mwspr) // k, (screen_h * k / mhspr) // len(sprites)))
    if tzoom >= zoom:
      zoom, mwc = tzoom, k

def parcourir_selon(ia):
  global dimweb, wpf, hpf, platforms, sprites, screen_w, screen_h
  t_coeff = min(1, screen_h / 240)
  dimweb = int(t_coeff * 36)
  maxcoups = dimweb**2 * 2
  init_web(t_coeff * .05, .1, .15)
  wpf, hpf = max(font_w, mwspr), max(font_h, mhspr)
  poly_fill_rect(0, 0, screen_w, screen_h, int2color(color_back))
  for i in range(dimweb):
    draw_connections(i, i + 1, color_front)
    refresh_platform(i)
  s0, s1, s2, s3, s4, s5, s6, s7 = 0, 0, m_a, 0, 1, -1, 0, 0
  pfs0, pfs5 = platforms[s0], 0
  while s4 > 0  and (not (s2 & (2 * m_k)) or not (pfs0 & m_d)):
    if s5 < 0:
      s5 = 0
    else:
      try:
        k, k2 = ia(s0, voisines, dimweb, s1, s2)
        if pfs5 & (2 * m_b):
          while s0 == s5:
            s0 = randint(0, dimweb - 1)
          pfs0, pfs5 = my_bitor(platforms[s0], m_b), pfs5 & ~(3 * m_b) & ~m_m
        else:
          if k2:
            if s2 & m_a:
              v = platforms[k]
              if v & m_l:
                v, s2 = v & ~m_l, my_bitor(s2, 2 * m_l)
                platforms[k] = my_bitor(v, 2 * m_l)
                refresh_platform(k)
              s2 = s2 & ~m_a
              s2 = my_bitor(s2, 2 * m_a)
          else:
            if k in voisines:
              s0 = k
              if pfs5 & m_b:
                pfs5 = my_bitor(pfs5, 2 * m_b)
              pfs0, pfs5 = platforms[s0], pfs5 & ~m_m
          s3 += 1
          if s3 >= maxcoups:
            s4 = 0
        if pfs0 & m_k:
          pfs0 = pfs0 & ~m_k
          s2 = my_bitor(s2, 2 * m_k)
        if pfs0 & my_bitor(m_p, m_l):
          s4 = 0
          pfs0 = my_bitor(pfs0, 2 * m_m)
        platforms[s5] = pfs5
      except Exception as t_excpt:
        s4 = -1
        print(t_excpt)
    pfs0 = my_bitor(pfs0, m_m)
    s1, voisines = pfs0, get_reachable_platforms_from_platforms([s0], 0)
    platforms[s0] = pfs0
    for v in voisines:
      t = my_bitor(m_p, m_l)
      t = platforms[v] & my_bitor(t, m_k)
      s1 = my_bitor(s1, t)
    for v in get_reachable_platforms_from_platforms(voisines, 0):
      t = platforms[v] & m_l
      s1 = my_bitor(s1, t)
    draw_connections(s5, 0, color_front)
    draw_connections(s0, 0, 16776960)
    if cango(s0, s5, 0) == 1:
      draw_connection(s0, s5, 65280)
    refresh_platform(s5)
    refresh_platform(s0)
    s, sold = my_bitor(s1, s2), my_bitor(s6, s7)
    for k in range(len(sprites) - 1):
      if s & 3 != sold & 3:
        x, y = screen_w - 1 - zoom*mwspr*(1 + k%mwc) + (zoom * (mwspr - sprites[k][0]))//2, (k // mwc)*mhspr*zoom + zoom*(mhspr - sprites[k][1])//2
        if s & 3:
          draw_sprite(k, x, y, (s & 3) // 2, zoom)
        else:
          poly_fill_rect(x, y, sprites[k][0] * zoom, sprites[k][1] * zoom, int2color(color_back))
      s, sold = s // 4, sold // 4
    poly_pause()
    s5, s6, s7, pfs5 = s0, s1, s2, pfs0
  v, r = 0, s4 > 0 and s3 < maxcoups
  if r:
    print("Gagne ! :-)")
  else:
    print("Perdu ! :-(")
    if pfs0 & m_p:
      print("Puits")
      v = 1
    if pfs0 & m_l:
      print("Leviathan")
      v = v | 2
    if s3 >= maxcoups:
      print("Energie")
    elif s4 < 0:
      print("Exception")
      v = v | 4
  print("En " + str(s3) + " coups")
  return r, v, s2, s3

#cas
#from web import *

def ia(corniche, voisines, taille, capteurs, evenements):
  # Nous sommes dans un cratere de volcan.
  # Il y a un nombre {taille} de corniches.
  # Les corniches sont numerotees de 0 a {taille - 1}.

  # Nous sommes sur la corniche numero {corniche}.
  #if capteurs & m_b:
    # Une chauve-souris dort sur cette corniche. Elle se reveillera des que tu
    # seras parti.e au prochain tour !
    #pass
  #if capteurs & (2 * m_b):
    # Une chauve-souris est prete a se saisir de toi et t'emmener sur une autre
    # corniche sans te demander ton avis. Tu ne peux pas choisir ta destination.
    #pass
  #if capteurs & m_d:
    # Cette corniche donne sur la porte de sortie.
    #pass
  #if capteurs & m_p:
    # Une des corniches voisines dissimule un puits. Progresse prudemment !
    #pass
  #if capteurs & m_k:
    # Une des corniches voisines emet de la lumiere. La cle de la sortie doit
    # y etre !
    #pass
  #if capteurs & m_l:
    # Le Leviathan gronde. Il est a 2 pas ou moins d'ici. S'il n'est pas dans
    # une des corniches voisines, il est sur une voisine d'une voisine...
    #pass
  #if evenements & (2 * m_k):
    # Bravo, tu as trouve la cle de la porte de sortie !
    #pass
  #if evenements & m_a:
    # Tu peux tirer une fleche vers une des corniches voisines.
    #pass
  #if evenements & (2 * m_l):
    # Le Leviathan a ete touche mortellement, il ne pose plus de danger !
    #pass

  # renvoie 2 valeurs :
  # * la corniche choisie parmi les voisines
  # * l'action relative desiree :
  #   - 0 pour aller sur cette corcniche
  #   - 1 pour tirer une fleche vers cette corcniche
  return voisines[randint(0, len(voisines) - 1)], 0

parcourir_selon(ia)