Task: Matrix digital rain




Go: package main

import (
    gc "github.com/rthornton128/goncurses"
    "log"
    "math/rand"
    "time"
)



const rowDelay = 40000

func main() {
    start := time.Now()
    rand.Seed(time.Now().UnixNano())

    
    chars := []byte("0123456789")
    totalChars := len(chars)

    
    stdscr, err := gc.Init()
    if err != nil {
        log.Fatal("init", err)
    }
    defer gc.End()

    gc.Echo(false)
    gc.Cursor(0)

    if !gc.HasColors() {
        log.Fatal("Program requires a colour capable terminal")
    }

    if err := gc.StartColor(); err != nil {
        log.Fatal(err)
    }

    if err := gc.InitPair(1, gc.C_GREEN, gc.C_BLACK); err != nil {
        log.Fatal("InitPair failed: ", err)
    }
    stdscr.ColorOn(1)
    maxY, maxX := stdscr.MaxYX()

    

    
    columnsRow := make([]int, maxX)

    
    
    columnsActive := make([]int, maxX)

    
    for i := 0; i < maxX; i++ {
        columnsRow[i] = -1
        columnsActive[i] = 0
    }

    for {
        for i := 0; i < maxX; i++ {
            if columnsRow[i] == -1 {
                
                
                columnsRow[i] = rand.Intn(maxY + 1)
                columnsActive[i] = rand.Intn(2)
            }
        }

        
        for i := 0; i < maxX; i++ {
            if columnsActive[i] == 1 {
                
                charIndex := rand.Intn(totalChars)
                stdscr.MovePrintf(columnsRow[i], i, "%c", chars[charIndex])
            } else {
                
                stdscr.MovePrintf(columnsRow[i], i, "%c", ' ')
            }

            columnsRow[i]++

            
            if columnsRow[i] >= maxY {
                columnsRow[i] = -1
            }

            
            if rand.Intn(1001) == 0 {
                if columnsActive[i] == 0 {
                    columnsActive[i] = 1
                } else {
                    columnsActive[i] = 0
                }
            }
        }
        time.Sleep(rowDelay * time.Microsecond)
        stdscr.Refresh()
        elapsed := time.Since(start)
        
        if elapsed.Minutes() >= 1 {
            break
        }
    }
}




Python: import curses
import random
import time





ROW_DELAY=.0001

def get_rand_in_range(min, max):
    return random.randrange(min,max+1)

try:
    
    chars = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
    
    total_chars = len(chars)
        
    stdscr = curses.initscr()
    curses.noecho()
    curses.curs_set(False)
        
    curses.start_color()
    curses.init_pair(1, curses.COLOR_GREEN, curses.COLOR_BLACK)
    stdscr.attron(curses.color_pair(1))
    
    max_x = curses.COLS - 1
    max_y = curses.LINES - 1
    
         
    
    
    
    
    columns_row = []
    
    
    
    
    columns_active = []
    
    for i in range(max_x+1):
        columns_row.append(-1)
        columns_active.append(0)
        
    while(True):
        for i in range(max_x):
            if columns_row[i] == -1:
                
                
                columns_row[i] = get_rand_in_range(0, max_y)
                columns_active[i] = get_rand_in_range(0, 1)
     
        
        
        for i in range(max_x):
            if columns_active[i] == 1:
                
                char_index = get_rand_in_range(0, total_chars-1)
                
                stdscr.addstr(columns_row[i], i, chars[char_index])
            else:
                
                
                stdscr.addstr(columns_row[i], i, " ");
                
     
            columns_row[i]+=1
     
            
            if columns_row[i] >= max_y:
                columns_row[i] = -1
    
            
            if get_rand_in_range(0, 1000) == 0:
                if columns_active[i] == 0:      
                    columns_active[i] = 1
                else:
                    columns_active[i] = 0
     
            time.sleep(ROW_DELAY)
            stdscr.refresh()
    
except KeyboardInterrupt as err:
    curses.endwin()