Fuzion Logo
fuzion-lang.dev — The Fuzion Language Portal
JavaScript seems to be disabled. Functionality is limited.

are_we_fast_yet/bounce.fz


# ported from https://github.com/smarr/are-we-fast-yet/blob/master/benchmarks/Java/src/Bounce.java
public bounce
post result = 1331
=>

  rand is
    seed := mut 74755
    next =>
      seed <- (((seed.get * 1309) + 13849) & 65535)
      seed.get

  r := rand

  ball is
    x     mutate.new i32 := mut ( r.next % 500)
    y     mutate.new i32 := mut ( r.next % 500)
    x_vel mutate.new i32 := mut ((r.next % 300) - 150)
    y_vel mutate.new i32 := mut ((r.next % 300) - 150)

    bounce =>
      x_limit := mut 500
      y_limit := mut 500
      bounced := mut false

      x <- x.get + x_vel.get
      y <- y.get + y_vel.get

      if x.get > x_limit.get
        x <- x_limit.get; x_vel <- 0 - x_vel.get.abs; bounced <- true
      if x.get < 0
        x <- 0; x_vel <- x_vel.get.abs; bounced <- true
      if y.get > y_limit.get
        y <- y_limit.get; y_vel <- 0 - y_vel.get.abs; bounced <- true
      if y.get < 0
        y <- 0; y_vel <- y_vel.get.abs; bounced <- true

      bounced.get

  ball_count := 100

  # the bounces counter
  bounces:= mut 0

  mi : mutate is
  mi.go unit ()->

    # initialize the balls array
    balls := (mutate.array ball).type.new mi ball_count ball
    for i in 1..99 do
      balls[i] := ball

    # bounce the balls
    for i in 0..49 do
      for b in 0..99 do
        # NYI
        cur_ball := balls[b]
        if cur_ball.bounce
          bounces <- bounces.get + 1
        balls[b] := cur_ball

  bounces.get