mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
move all vlib modules to vlib/
This commit is contained in:
8
examples/.gitignore
vendored
8
examples/.gitignore
vendored
@ -1,8 +0,0 @@
|
||||
game_of_life/life
|
||||
hello_world
|
||||
json
|
||||
links_scraper
|
||||
log
|
||||
news_fetcher
|
||||
tetris/tetris
|
||||
word_counter/word_counter
|
1
examples/VCasino/.gitignore
vendored
1
examples/VCasino/.gitignore
vendored
@ -1 +0,0 @@
|
||||
VCasino
|
@ -1,17 +0,0 @@
|
||||
# VCasino
|
||||
VCasino is a very simple game made to learn V.
|
||||
|
||||
# Compile and Run
|
||||
|
||||
Use this to generate a binary and then launch the game.
|
||||
```bash
|
||||
v VCasino.v
|
||||
./VCasino
|
||||
```
|
||||
|
||||
And this to compile and launch the game directly.
|
||||
```bash
|
||||
v run VCasino.v
|
||||
```
|
||||
|
||||
Created by Thomas Senechal : https://github.com/thomas-senechal/VCasino
|
@ -1,147 +0,0 @@
|
||||
import rand
|
||||
import os
|
||||
|
||||
const (HelpText = ' Usage:\t./VCasino\n
|
||||
Description:\n VCasino is a little game only made to learn V.\n')
|
||||
const (GDesc = ' The object of Roulette is to pick the number where the spinning ball will land on the wheel.
|
||||
If your number is the good one, you\'ll get your bet x3.
|
||||
If your number is the same color as the ball one, you\'ll get your bet /2.
|
||||
Otherwise, you will lose your bet.\n')
|
||||
const (Odd = 'Red' Even = 'Black')
|
||||
|
||||
struct options {
|
||||
long_opt string
|
||||
short_opt string
|
||||
}
|
||||
|
||||
fn display_help() {
|
||||
println(HelpText + GDesc)
|
||||
}
|
||||
|
||||
fn option_parser() bool {
|
||||
help := options{'--help', '-h'}
|
||||
for i := 0; i < os.args.len; i++ {
|
||||
if os.args[i]== help.long_opt || os.args[i]== help.short_opt {
|
||||
display_help()
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
fn str_is_nbr(s string) bool {
|
||||
for i := 0; i < s.len; i++ {
|
||||
if !s[i].is_digit() {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
fn get_bet_nbr() int {
|
||||
mut bet_nbr := -1
|
||||
for bet_nbr < 0 || bet_nbr > 49 {
|
||||
println('Reminder: Odd numbers are red and even are black.')
|
||||
println('Type the number you want to bet on (between 0 and 49):')
|
||||
line := os.get_line().trim_space()
|
||||
if line.len < 1 {
|
||||
println('error: empty line.')
|
||||
continue
|
||||
}
|
||||
if !str_is_nbr(line) {
|
||||
println('error: $line is not a number.')
|
||||
continue
|
||||
}
|
||||
bet_nbr = line.int()
|
||||
if bet_nbr < 0 || bet_nbr > 49 {
|
||||
println('error: $line is not between 0 and 49.')
|
||||
bet_nbr = -1
|
||||
continue
|
||||
}
|
||||
}
|
||||
return bet_nbr
|
||||
}
|
||||
|
||||
fn get_bet(money int) int {
|
||||
mut bet := -1
|
||||
for bet <= 0 || bet > money {
|
||||
println('You\'ve $money V. Type in the amount of your bet:')
|
||||
line := os.get_line().trim_space()
|
||||
if line.len < 1 {
|
||||
println('error: empty line.')
|
||||
continue
|
||||
}
|
||||
if !str_is_nbr(line) {
|
||||
println('error: $line is not a number.')
|
||||
continue
|
||||
}
|
||||
bet = line.int()
|
||||
if bet <= 0 {
|
||||
println('error: $line is not heigher than 1.')
|
||||
continue
|
||||
} else if bet > money {
|
||||
println('error: $line is not heigher than your money.')
|
||||
}
|
||||
}
|
||||
return bet
|
||||
}
|
||||
|
||||
fn run_wheel(bet_nbr int, bet int) int {
|
||||
rand.seed()
|
||||
winning_nbr := rand.next(50)
|
||||
print('Roulette Wheel spinning... and stops on the number $winning_nbr which is a ')
|
||||
if winning_nbr % 2 == 1 {
|
||||
println(Odd)
|
||||
} else {
|
||||
println(Even)
|
||||
}
|
||||
if winning_nbr == bet_nbr {
|
||||
bet *= 3
|
||||
println('Congratulations! You get $bet V!')
|
||||
} else if winning_nbr % 2 == bet_nbr % 2 {
|
||||
bet /= 2
|
||||
println('You bet the right color. You get $bet V!')
|
||||
} else {
|
||||
println('Sorry buddy. You lost $bet V!')
|
||||
bet *= -1
|
||||
}
|
||||
return bet
|
||||
}
|
||||
|
||||
fn is_broke(money int) bool {
|
||||
if money <= 0 {
|
||||
println('You\'broke, the game is over..')
|
||||
return false
|
||||
} else {
|
||||
quit := options{'yes', 'y'}
|
||||
println('You\'ve $money V. Do you want to quit the casino with your winnings? (y/n)')
|
||||
line := os.get_line().trim_space().to_lower()
|
||||
if line== quit.long_opt || line== quit.short_opt {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
fn game_loop() {
|
||||
mut can_play := true
|
||||
mut money := 1000
|
||||
|
||||
println(GDesc)
|
||||
println('You start the game with $money V.\n')
|
||||
for can_play {
|
||||
bet_nbr := get_bet_nbr()
|
||||
bet := get_bet(money)
|
||||
money += run_wheel(bet_nbr, bet)
|
||||
can_play = is_broke(money)
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
if os.args.len >= 2 {
|
||||
if option_parser() {
|
||||
return
|
||||
}
|
||||
}
|
||||
game_loop()
|
||||
}
|
1
examples/game_of_life/.gitignore
vendored
1
examples/game_of_life/.gitignore
vendored
@ -1 +0,0 @@
|
||||
life
|
@ -1,12 +0,0 @@
|
||||
# Conway's Game of Life
|
||||
|
||||

|
||||
|
||||
|
||||
```v
|
||||
v run life.v
|
||||
```
|
||||
|
||||
Created by fuyutarow: https://github.com/fuyutarow/Conways-Game-of-Life-with-Vlang
|
||||
|
||||
|
Binary file not shown.
Before Width: | Height: | Size: 628 KiB |
@ -1,79 +0,0 @@
|
||||
import time
|
||||
|
||||
fn print_field(field[]array_int) {
|
||||
for line in field {
|
||||
mut s := ' '
|
||||
for j, cell in line {
|
||||
if j == 0 || j == line.len - 1{continue}
|
||||
s += if cell == 1{'@'} else { '.'}
|
||||
}
|
||||
println(s)
|
||||
}
|
||||
println('')
|
||||
}
|
||||
|
||||
pub fn gun() []array_int {
|
||||
mut field := []array_int
|
||||
field << [0,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|
||||
field << [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]
|
||||
field << [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]
|
||||
field << [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]
|
||||
field << [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]
|
||||
field << [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]
|
||||
field << [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]
|
||||
return field
|
||||
}
|
||||
|
||||
fn main() {
|
||||
mut field := gun()
|
||||
print_field(field)
|
||||
for {
|
||||
mut new_field := []array_int
|
||||
for i, line in field {
|
||||
new_field << [0; line.len]
|
||||
}
|
||||
for i, line in field {
|
||||
if i == 0 || i == field.len - 1{continue}
|
||||
for j, cell in line {
|
||||
if j == 0 || j == line.len - 1{continue}
|
||||
moore_sum := (
|
||||
field[i - 1] [j - 1] + field[i - 1] [j] + field[i - 1] [j + 1] +
|
||||
field[i] [j - 1] + field[i] [j + 1] +
|
||||
field[i + 1] [j - 1] + field[i + 1] [j] + field[i + 1] [j + 1]
|
||||
)
|
||||
new_field[i] [j] = if cell == 1 {
|
||||
int(moore_sum in [2, 3])
|
||||
}
|
||||
else {
|
||||
int(moore_sum == 3)
|
||||
}
|
||||
}
|
||||
}
|
||||
field = new_field
|
||||
print_field(field)
|
||||
time.sleep_ms(100)
|
||||
}
|
||||
}
|
||||
|
@ -1,2 +0,0 @@
|
||||
println('Hello, World!')
|
||||
|
@ -1,38 +0,0 @@
|
||||
import json
|
||||
|
||||
struct User {
|
||||
name string
|
||||
age int
|
||||
mut:
|
||||
is_registered bool
|
||||
}
|
||||
|
||||
fn main() {
|
||||
s := '[{ "name":"Frodo", "age":25}, {"name":"Bobby", "age":10}]'
|
||||
users := json.decode( []User, s) or {
|
||||
eprintln('Failed to parse json')
|
||||
return
|
||||
}
|
||||
for user in users {
|
||||
println('$user.name: $user.age')
|
||||
}
|
||||
println('')
|
||||
for i, user in users {
|
||||
println('$i) $user.name')
|
||||
if !user.can_register() {
|
||||
println('Cannot register $user.name, they are too young')
|
||||
}
|
||||
}
|
||||
// Let's encode users again just for fun
|
||||
println('')
|
||||
println(json.encode(users))
|
||||
}
|
||||
|
||||
fn (u User) can_register() bool {
|
||||
return u.age >= 16
|
||||
}
|
||||
|
||||
fn (u mut User) register() {
|
||||
u.is_registered = true
|
||||
}
|
||||
|
@ -1,19 +0,0 @@
|
||||
// Copyright (c) 2019 Alexander Medvednikov. All rights reserved.
|
||||
// Use of this source code is governed by an MIT license
|
||||
// that can be found in the LICENSE file.
|
||||
|
||||
import http
|
||||
|
||||
fn main() {
|
||||
html := http.get('https://news.ycombinator.com')
|
||||
mut pos := 0
|
||||
for {
|
||||
pos = html.index_after('https://', pos + 1)
|
||||
if pos == -1 {
|
||||
break
|
||||
}
|
||||
end := html.index_after('"', pos)
|
||||
println(html.substr(pos, end))
|
||||
}
|
||||
}
|
||||
|
@ -1,12 +0,0 @@
|
||||
import log
|
||||
|
||||
fn main(){
|
||||
mut l := log.Log{level:log.INFO}
|
||||
l.info('info')
|
||||
l.warn('warn')
|
||||
l.error('error')
|
||||
l.debug('no debug')
|
||||
l.set_level(log.DEBUG)
|
||||
l.debug('debug')
|
||||
l.fatal('fatal')
|
||||
}
|
@ -1,58 +0,0 @@
|
||||
// Copyright (c) 2019 Alexander Medvednikov. All rights reserved.
|
||||
// Use of this source code is governed by an MIT license
|
||||
// that can be found in the LICENSE file.
|
||||
|
||||
import http
|
||||
import json
|
||||
import sync
|
||||
import time
|
||||
|
||||
const (
|
||||
NR_THREADS = 8
|
||||
)
|
||||
|
||||
struct Story {
|
||||
title string
|
||||
url string
|
||||
}
|
||||
|
||||
struct Fetcher {
|
||||
mut:
|
||||
mu sync.Mutex
|
||||
ids []int
|
||||
cursor int
|
||||
}
|
||||
|
||||
fn (f mut Fetcher) fetch() {
|
||||
for {
|
||||
f.mu.lock()
|
||||
if f.cursor >= f.ids.len {
|
||||
return
|
||||
}
|
||||
id := f.ids[f.cursor]
|
||||
f.cursor++
|
||||
f.mu.unlock()
|
||||
resp := http.get('https://hacker-news.firebaseio.com/v0/item/${id}.json')
|
||||
story := json.decode(Story, resp) or {
|
||||
println('failed to decode a story')
|
||||
exit(1)
|
||||
}
|
||||
println('#$f.cursor) $story.title | $story.url')
|
||||
}
|
||||
}
|
||||
|
||||
// Fetches top HN stories in 8 coroutines
|
||||
fn main() {
|
||||
resp := http.get('https://hacker-news.firebaseio.com/v0/topstories.json')
|
||||
ids := json.decode( []int, resp) or {
|
||||
println('failed to fetch topstories.json')
|
||||
return
|
||||
}
|
||||
fetcher := &Fetcher{ids: ids}
|
||||
for i := 0; i < NR_THREADS; i++ {
|
||||
go fetcher.fetch()
|
||||
}
|
||||
println(fetcher.ids)
|
||||
time.sleep(5)
|
||||
}
|
||||
|
1
examples/tetris/.gitignore
vendored
1
examples/tetris/.gitignore
vendored
@ -1 +0,0 @@
|
||||
tetris
|
@ -1 +0,0 @@
|
||||
<img src='https://raw.githubusercontent.com/vlang/v/master/examples/tetris/screenshot.png' width=300>
|
Binary file not shown.
Binary file not shown.
Before Width: | Height: | Size: 96 KiB |
@ -1,350 +0,0 @@
|
||||
// Copyright (c) 2019 Alexander Medvednikov. All rights reserved.
|
||||
// Use of this source code is governed by an MIT license
|
||||
// that can be found in the LICENSE file.
|
||||
|
||||
import rand
|
||||
import time
|
||||
import gx
|
||||
import gl
|
||||
import gg
|
||||
import glfw
|
||||
import math
|
||||
|
||||
const (
|
||||
BlockSize = 20 // pixels
|
||||
FieldHeight = 20 // # of blocks
|
||||
FieldWidth = 10
|
||||
TetroSize = 4
|
||||
WinWidth = BlockSize * FieldWidth
|
||||
WinHeight = BlockSize * FieldHeight
|
||||
TimerPeriod = 250 // ms
|
||||
)
|
||||
|
||||
const (
|
||||
// Tetros' 4 possible states are encoded in binaries
|
||||
BTetros = [
|
||||
// 0000 0
|
||||
// 0000 0
|
||||
// 0110 6
|
||||
// 0110 6
|
||||
[66, 66, 66, 66],
|
||||
// 0000 0
|
||||
// 0000 0
|
||||
// 0010 2
|
||||
// 0111 7
|
||||
[27, 131, 72, 232],
|
||||
// 0000 0
|
||||
// 0000 0
|
||||
// 0011 3
|
||||
// 0110 6
|
||||
[36, 231, 36, 231],
|
||||
// 0000 0
|
||||
// 0000 0
|
||||
// 0110 6
|
||||
// 0011 3
|
||||
[63, 132, 63, 132],
|
||||
// 0000 0
|
||||
// 0011 3
|
||||
// 0001 1
|
||||
// 0001 1
|
||||
[311, 17, 223, 74],
|
||||
// 0000 0
|
||||
// 0011 3
|
||||
// 0010 2
|
||||
// 0010 2
|
||||
[322, 71, 113, 47],
|
||||
// Special case since 15 can't be used
|
||||
// 1111
|
||||
[1111, 9, 1111, 9],
|
||||
]
|
||||
// Each tetro has its unique color
|
||||
Colors = [
|
||||
gx.rgb(0, 0, 0),
|
||||
gx.rgb(253, 32, 47),
|
||||
gx.rgb(0, 110, 194),
|
||||
gx.rgb(34, 169, 16),
|
||||
gx.rgb(170, 0, 170),
|
||||
gx.rgb(0, 0, 170),
|
||||
gx.rgb(0, 170, 0),
|
||||
gx.rgb(170, 85, 0),
|
||||
gx.rgb(0, 170, 170),
|
||||
]
|
||||
)
|
||||
|
||||
// TODO: type Tetro [TetroSize]struct{ x, y int }
|
||||
struct Block {
|
||||
mut:
|
||||
x int
|
||||
y int
|
||||
}
|
||||
|
||||
struct Game {
|
||||
mut:
|
||||
// Position of the current tetro
|
||||
pos_x int
|
||||
pos_y int
|
||||
// field[y][x] contains the color of the block with (x,y) coordinates
|
||||
// "-1" border is to avoid bounds checking.
|
||||
// -1 -1 -1 -1
|
||||
// -1 0 0 -1
|
||||
// -1 0 0 -1
|
||||
// -1 -1 -1 -1
|
||||
// TODO: field [][]int
|
||||
field array_array_int
|
||||
// TODO: tetro Tetro
|
||||
tetro []Block
|
||||
// TODO: tetros_cache []Tetro
|
||||
tetros_cache []Block
|
||||
// Index of the current tetro. Refers to its color.
|
||||
tetro_idx int
|
||||
// Index of the rotation (0-3)
|
||||
rotation_idx int
|
||||
// gg context for drawing
|
||||
gg *gg.GG
|
||||
}
|
||||
|
||||
fn main() {
|
||||
glfw.init()
|
||||
mut game := &Game{gg: 0} // TODO
|
||||
game.parse_tetros()
|
||||
game.init_game()
|
||||
mut window := glfw.create_window(glfw.WinCfg {
|
||||
width: WinWidth
|
||||
height: WinHeight
|
||||
title: 'V Tetris'
|
||||
ptr: game // glfw user pointer
|
||||
})
|
||||
window.make_context_current()
|
||||
window.onkeydown(key_down)
|
||||
gg.init()
|
||||
game.gg = gg.new_context(gg.Cfg {
|
||||
width: WinWidth
|
||||
height: WinHeight
|
||||
use_ortho: true // This is needed for 2D drawing
|
||||
})
|
||||
go game.run() // Run the game loop in a new thread
|
||||
gl.clear() // For some reason this is necessary to avoid an intial flickering
|
||||
gl.clear_color(255, 255, 255, 255)
|
||||
for {
|
||||
gl.clear()
|
||||
gl.clear_color(255, 255, 255, 255)
|
||||
game.draw_scene()
|
||||
window.swap_buffers()
|
||||
glfw.wait_events()
|
||||
if window.should_close() {
|
||||
window.destroy()
|
||||
glfw.terminate()
|
||||
exit(0)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn (g mut Game) init_game() {
|
||||
rand.seed()
|
||||
g.generate_tetro()
|
||||
g.field = []array_int // TODO: g.field = [][]int
|
||||
// Generate the field, fill it with 0's, add -1's on each edge
|
||||
for i := 0; i < FieldHeight + 2; i++ {
|
||||
mut row := [0; FieldWidth + 2]
|
||||
row[0] = - 1
|
||||
row[FieldWidth + 1] = - 1
|
||||
g.field << row
|
||||
}
|
||||
mut first_row := g.field[0]
|
||||
mut last_row := g.field[FieldHeight + 1]
|
||||
for j := 0; j < FieldWidth + 2; j++ {
|
||||
first_row[j] = - 1
|
||||
last_row[j] = - 1
|
||||
}
|
||||
}
|
||||
|
||||
fn (g mut Game) parse_tetros() {
|
||||
for b_tetros in BTetros {
|
||||
for b_tetro in b_tetros {
|
||||
for t in parse_binary_tetro(b_tetro) {
|
||||
g.tetros_cache << t
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn (g mut Game) run() {
|
||||
for {
|
||||
g.move_tetro()
|
||||
g.delete_completed_lines()
|
||||
glfw.post_empty_event() // force window redraw
|
||||
time.sleep_ms(TimerPeriod)
|
||||
}
|
||||
}
|
||||
|
||||
fn (g mut Game) move_tetro() {
|
||||
// Check each block in current tetro
|
||||
for block in g.tetro {
|
||||
y := block.y + g.pos_y + 1
|
||||
x := block.x + g.pos_x
|
||||
// Reached the bottom of the screen or another block?
|
||||
// TODO: if g.field[y][x] != 0
|
||||
row := g.field[y]
|
||||
if row[x] != 0 {
|
||||
// The new tetro has no space to drop => end of the game
|
||||
if g.pos_y < 2 {
|
||||
g.init_game()
|
||||
return
|
||||
}
|
||||
// Drop it and generate a new one
|
||||
g.drop_tetro()
|
||||
g.generate_tetro()
|
||||
return
|
||||
}
|
||||
}
|
||||
g.pos_y++
|
||||
}
|
||||
|
||||
fn (g mut Game) move_right(dx int) {
|
||||
// Reached left/right edge or another tetro?
|
||||
for i := 0; i < TetroSize; i++ {
|
||||
tetro := g.tetro[i]
|
||||
y := tetro.y + g.pos_y
|
||||
x := tetro.x + g.pos_x + dx
|
||||
row := g.field[y]
|
||||
if row[x] != 0 {
|
||||
// Do not move
|
||||
return
|
||||
}
|
||||
}
|
||||
g.pos_x += dx
|
||||
}
|
||||
|
||||
fn (g mut Game) delete_completed_lines() {
|
||||
for y := FieldHeight; y >= 1; y-- {
|
||||
g.delete_completed_line(y)
|
||||
}
|
||||
}
|
||||
|
||||
fn (g mut Game) delete_completed_line(y int) {
|
||||
for x := 1; x <= FieldWidth; x++ {
|
||||
f := g.field[y]
|
||||
if f[x] == 0 {
|
||||
return
|
||||
}
|
||||
}
|
||||
// Move everything down by 1 position
|
||||
for yy := y - 1; yy >= 1; yy-- {
|
||||
for x := 1; x <= FieldWidth; x++ {
|
||||
mut a := g.field[yy + 1]
|
||||
mut b := g.field[yy]
|
||||
a[x] = b[x]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Place a new tetro on top
|
||||
fn (g mut Game) generate_tetro() {
|
||||
g.pos_y = 0
|
||||
g.pos_x = FieldWidth / 2 - TetroSize / 2
|
||||
g.tetro_idx = rand.next(BTetros.len)
|
||||
g.rotation_idx = 0
|
||||
g.get_tetro()
|
||||
}
|
||||
|
||||
// Get the right tetro from cache
|
||||
fn (g mut Game) get_tetro() {
|
||||
idx := g.tetro_idx * TetroSize * TetroSize + g.rotation_idx * TetroSize
|
||||
g.tetro = g.tetros_cache.slice(idx, idx + TetroSize)
|
||||
}
|
||||
|
||||
fn (g mut Game) drop_tetro() {
|
||||
for i := 0; i < TetroSize; i++ {
|
||||
tetro := g.tetro[i]
|
||||
x := tetro.x + g.pos_x
|
||||
y := tetro.y + g.pos_y
|
||||
// Remember the color of each block
|
||||
// TODO: g.field[y][x] = g.tetro_idx + 1
|
||||
mut row := g.field[y]
|
||||
row[x] = g.tetro_idx + 1
|
||||
}
|
||||
}
|
||||
|
||||
fn (g &Game) draw_tetro() {
|
||||
for i := 0; i < TetroSize; i++ {
|
||||
tetro := g.tetro[i]
|
||||
g.draw_block(g.pos_y + tetro.y, g.pos_x + tetro.x, g.tetro_idx + 1)
|
||||
}
|
||||
}
|
||||
|
||||
fn (g &Game) draw_block(i, j, color_idx int) {
|
||||
g.gg.draw_rect((j - 1) * BlockSize, (i - 1) * BlockSize,
|
||||
BlockSize - 1, BlockSize - 1, Colors[color_idx])
|
||||
}
|
||||
|
||||
fn (g &Game) draw_field() {
|
||||
for i := 1; i < FieldHeight + 1; i++ {
|
||||
for j := 1; j < FieldWidth + 1; j++ {
|
||||
f := g.field[i]
|
||||
if f[j] > 0 {
|
||||
g.draw_block(i, j, f[j])
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn (g &Game) draw_scene() {
|
||||
g.draw_tetro()
|
||||
g.draw_field()
|
||||
}
|
||||
|
||||
fn parse_binary_tetro(t int) []Block {
|
||||
res := [Block{} ; 4]
|
||||
mut cnt := 0
|
||||
horizontal := t == 9// special case for the horizontal line
|
||||
for i := 0; i <= 3; i++ {
|
||||
// Get ith digit of t
|
||||
p := int(math.pow(10, 3 - i))
|
||||
mut digit := int(t / p)
|
||||
t %= p
|
||||
// Convert the digit to binary
|
||||
for j := 3; j >= 0; j-- {
|
||||
bin := digit % 2
|
||||
digit /= 2
|
||||
if bin == 1 || (horizontal && i == TetroSize - 1) {
|
||||
// TODO: res[cnt].x = j
|
||||
// res[cnt].y = i
|
||||
mut point := &res[cnt]
|
||||
point.x = j
|
||||
point.y = i
|
||||
cnt++
|
||||
}
|
||||
}
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
// TODO: this exposes the unsafe C interface, clean up
|
||||
fn key_down(wnd voidptr, key, code, action, mods int) {
|
||||
if action != 2 && action != 1 {
|
||||
return
|
||||
}
|
||||
// Fetch the game object stored in the user pointer
|
||||
mut game := &Game(glfw.get_window_user_pointer(wnd))
|
||||
switch key {
|
||||
case glfw.KEY_ESCAPE:
|
||||
glfw.set_should_close(wnd, true)
|
||||
case glfw.KeyUp:
|
||||
// Rotate the tetro
|
||||
game.rotation_idx++
|
||||
if game.rotation_idx == TetroSize {
|
||||
game.rotation_idx = 0
|
||||
}
|
||||
game.get_tetro()
|
||||
if game.pos_x < 0 {
|
||||
game.pos_x = 1
|
||||
}
|
||||
case glfw.KeyLeft:
|
||||
game.move_right(-1)
|
||||
case glfw.KeyRight:
|
||||
game.move_right(1)
|
||||
case glfw.KeyDown:
|
||||
game.move_tetro() // drop faster when the player presses <down>
|
||||
}
|
||||
}
|
||||
|
1
examples/word_counter/.gitignore
vendored
1
examples/word_counter/.gitignore
vendored
@ -1 +0,0 @@
|
||||
word_counter
|
@ -1,24 +0,0 @@
|
||||
```
|
||||
usage: word_counter [text_file]
|
||||
using cinderella.txt
|
||||
able => 2
|
||||
afterwards => 1
|
||||
after => 1
|
||||
against => 2
|
||||
again => 10
|
||||
allowed => 2
|
||||
allow => 1
|
||||
all => 12
|
||||
along => 1
|
||||
also => 2
|
||||
always => 2
|
||||
and => 140
|
||||
anew => 1
|
||||
anger => 1
|
||||
another => 2
|
||||
answered => 1
|
||||
anyone => 2
|
||||
any => 1
|
||||
an => 4
|
||||
...
|
||||
```
|
@ -1,250 +0,0 @@
|
||||
A rich man's wife became sick, and when she felt that her end was drawing near,
|
||||
she called her only daughter to her bedside and said, "Dear child, remain pious
|
||||
and good, and then our dear God will always protect you, and I will look down
|
||||
on you from heaven and be near you." With this she closed her eyes and died.
|
||||
|
||||
The girl went out to her mother's grave every day and wept, and she remained
|
||||
pious and good. When winter came the snow spread a white cloth over the grave,
|
||||
and when the spring sun had removed it again, the man took himself another
|
||||
wife.
|
||||
|
||||
This wife brought two daughters into the house with her. They were beautiful,
|
||||
with fair faces, but evil and dark hearts. Times soon grew very bad for the
|
||||
poor stepchild.
|
||||
|
||||
"Why should that stupid goose sit in the parlor with us?" they said. "If she
|
||||
wants to eat bread, then she will have to earn it. Out with this kitchen maid!"
|
||||
|
||||
They took her beautiful clothes away from her, dressed her in an old gray
|
||||
smock, and gave her wooden shoes. "Just look at the proud princess! How decked
|
||||
out she is!" they shouted and laughed as they led her into the kitchen.
|
||||
|
||||
There she had to do hard work from morning until evening, get up before
|
||||
daybreak, carry water, make the fires, cook, and wash. Besides this, the
|
||||
sisters did everything imaginable to hurt her. They made fun of her, scattered
|
||||
peas and lentils into the ashes for her, so that she had to sit and pick them
|
||||
out again. In the evening when she had worked herself weary, there was no bed
|
||||
for her. Instead she had to sleep by the hearth in the ashes. And because she
|
||||
always looked dusty and dirty, they called her Cinderella.
|
||||
|
||||
One day it happened that the father was going to the fair, and he asked his two
|
||||
stepdaughters what he should bring back for them.
|
||||
|
||||
"Beautiful dresses," said the one.
|
||||
|
||||
"Pearls and jewels," said the other.
|
||||
|
||||
"And you, Cinderella," he said, "what do you want?"
|
||||
|
||||
"Father, break off for me the first twig that brushes against your hat on your
|
||||
way home."
|
||||
|
||||
So he bought beautiful dresses, pearls, and jewels for his two stepdaughters.
|
||||
On his way home, as he was riding through a green thicket, a hazel twig brushed
|
||||
against him and knocked off his hat. Then he broke off the twig and took it
|
||||
with him. Arriving home, he gave his stepdaughters the things that they had
|
||||
asked for, and he gave Cinderella the twig from the hazel bush.
|
||||
|
||||
Cinderella thanked him, went to her mother's grave, and planted the branch on
|
||||
it, and she wept so much that her tears fell upon it and watered it. It grew
|
||||
and became a beautiful tree.
|
||||
|
||||
Cinderella went to this tree three times every day, and beneath it she wept and
|
||||
prayed. A white bird came to the tree every time, and whenever she expressed a
|
||||
wish, the bird would throw down to her what she had wished for.
|
||||
|
||||
Now it happened that the king proclaimed a festival that was to last three
|
||||
days. All the beautiful young girls in the land were invited, so that his son
|
||||
could select a bride for himself. When the two stepsisters heard that they too
|
||||
had been invited, they were in high spirits.
|
||||
|
||||
They called Cinderella, saying, "Comb our hair for us. Brush our shoes and
|
||||
fasten our buckles. We are going to the festival at the king's castle."
|
||||
|
||||
Cinderella obeyed, but wept, because she too would have liked to go to the
|
||||
dance with them. She begged her stepmother to allow her to go.
|
||||
|
||||
"You, Cinderella?" she said. "You, all covered with dust and dirt, and you want
|
||||
to go to the festival?. You have neither clothes nor shoes, and yet you want to
|
||||
dance!"
|
||||
|
||||
However, because Cinderella kept asking, the stepmother finally said, "I have
|
||||
scattered a bowl of lentils into the ashes for you. If you can pick them out
|
||||
again in two hours, then you may go with us."
|
||||
|
||||
The girl went through the back door into the garden, and called out, "You tame
|
||||
pigeons, you turtledoves, and all you birds beneath the sky, come and help me
|
||||
to gather:
|
||||
|
||||
The good ones go into the pot, The bad ones go into your crop." Two white
|
||||
pigeons came in through the kitchen window, and then the turtledoves, and
|
||||
finally all the birds beneath the sky came whirring and swarming in, and lit
|
||||
around the ashes. The pigeons nodded their heads and began to pick, pick, pick,
|
||||
pick. And the others also began to pick, pick, pick, pick. They gathered all
|
||||
the good grains into the bowl. Hardly one hour had passed before they were
|
||||
finished, and they all flew out again. The girl took the bowl to her
|
||||
stepmother, and was happy, thinking that now she would be allowed to go to the
|
||||
festival with them.
|
||||
|
||||
But the stepmother said, "No, Cinderella, you have no clothes, and you don't
|
||||
know how to dance. Everyone would only laugh at you."
|
||||
|
||||
Cinderella began to cry, and then the stepmother said, "You may go if you are
|
||||
able to pick two bowls of lentils out of the ashes for me in one hour,"
|
||||
thinking to herself, "She will never be able to do that."
|
||||
|
||||
The girl went through the back door into the garden, and called out, "You tame
|
||||
pigeons, you turtledoves, and all you birds beneath the sky, come and help me
|
||||
to gather:
|
||||
|
||||
The good ones go into the pot, The bad ones go into your crop." Two white
|
||||
pigeons came in through the kitchen window, and then the turtledoves, and
|
||||
finally all the birds beneath the sky came whirring and swarming in, and lit
|
||||
around the ashes. The pigeons nodded their heads and began to pick, pick, pick,
|
||||
pick. And the others also began to pick, pick, pick, pick. They gathered all
|
||||
the good grains into the bowls. Before a half hour had passed they were
|
||||
finished, and they all flew out again. The girl took the bowls to her
|
||||
stepmother, and was happy, thinking that now she would be allowed to go to the
|
||||
festival with them.
|
||||
|
||||
But the stepmother said, "It's no use. You are not coming with us, for you have
|
||||
no clothes, and you don't know how to dance. We would be ashamed of you." With
|
||||
this she turned her back on Cinderella, and hurried away with her two proud
|
||||
daughters.
|
||||
|
||||
Now that no one else was at home, Cinderella went to her mother's grave beneath
|
||||
the hazel tree, and cried out:
|
||||
|
||||
Shake and quiver, little tree, Throw gold and silver down to me. Then the bird
|
||||
threw a gold and silver dress down to her, and slippers embroidered with silk
|
||||
and silver. She quickly put on the dress and went to the festival. Her
|
||||
stepsisters and her stepmother did not recognize her. They thought she must be
|
||||
a foreign princess, for she looked so beautiful in the golden dress. They never
|
||||
once thought it was Cinderella, for they thought that she was sitting at home
|
||||
in the dirt, looking for lentils in the ashes.
|
||||
|
||||
The prince approached her, took her by the hand, and danced with her.
|
||||
Furthermore, he would dance with no one else. He never let go of her hand, and
|
||||
whenever anyone else came and asked her to dance, he would say, "She is my
|
||||
dance partner."
|
||||
|
||||
She danced until evening, and then she wanted to go home. But the prince said,
|
||||
"I will go along and escort you," for he wanted to see to whom the beautiful
|
||||
girl belonged. However, she eluded him and jumped into the pigeon coop. The
|
||||
prince waited until her father came, and then he told him that the unknown girl
|
||||
had jumped into the pigeon coop.
|
||||
|
||||
The old man thought, "Could it be Cinderella?"
|
||||
|
||||
He had them bring him an ax and a pick so that he could break the pigeon coop
|
||||
apart, but no one was inside. When they got home Cinderella was lying in the
|
||||
ashes, dressed in her dirty clothes. A dim little oil-lamp was burning in the
|
||||
fireplace. Cinderella had quickly jumped down from the back of the pigeon coop
|
||||
and had run to the hazel tree. There she had taken off her beautiful clothes
|
||||
and laid them on the grave, and the bird had taken them away again. Then,
|
||||
dressed in her gray smock, she had returned to the ashes in the kitchen.
|
||||
|
||||
The next day when the festival began anew, and her parents and her stepsisters
|
||||
had gone again, Cinderella went to the hazel tree and said:
|
||||
|
||||
Shake and quiver, little tree, Throw gold and silver down to me. Then the bird
|
||||
threw down an even more magnificent dress than on the preceding day. When
|
||||
Cinderella appeared at the festival in this dress, everyone was astonished at
|
||||
her beauty. The prince had waited until she came, then immediately took her by
|
||||
the hand, and danced only with her. When others came and asked her to dance
|
||||
with them, he said, "She is my dance partner." When evening came she wanted to
|
||||
leave, and the prince followed her, wanting to see into which house she went.
|
||||
But she ran away from him and into the garden behind the house. A beautiful
|
||||
tall tree stood there, on which hung the most magnificent pears. She climbed as
|
||||
nimbly as a squirrel into the branches, and the prince did not know where she
|
||||
had gone. He waited until her father came, then said to him, "The unknown girl
|
||||
has eluded me, and I believe she has climbed up the pear tree.
|
||||
|
||||
The father thought, "Could it be Cinderella?" He had an ax brought to him and
|
||||
cut down the tree, but no one was in it. When they came to the kitchen,
|
||||
Cinderella was lying there in the ashes as usual, for she had jumped down from
|
||||
the other side of the tree, had taken the beautiful dress back to the bird in
|
||||
the hazel tree, and had put on her gray smock.
|
||||
|
||||
On the third day, when her parents and sisters had gone away, Cinderella went
|
||||
again to her mother's grave and said to the tree:
|
||||
|
||||
Shake and quiver, little tree, Throw gold and silver down to me. This time the
|
||||
bird threw down to her a dress that was more splendid and magnificent than any
|
||||
she had yet had, and the slippers were of pure gold. When she arrived at the
|
||||
festival in this dress, everyone was so astonished that they did not know what
|
||||
to say. The prince danced only with her, and whenever anyone else asked her to
|
||||
dance, he would say, "She is my dance partner." When evening came Cinderella
|
||||
wanted to leave, and the prince tried to escort her, but she ran away from him
|
||||
so quickly that he could not follow her. The prince, however, had set a trap.
|
||||
He had had the entire stairway smeared with pitch. When she ran down the
|
||||
stairs, her left slipper stuck in the pitch. The prince picked it up. It was
|
||||
small and dainty, and of pure gold.
|
||||
|
||||
The next morning, he went with it to the man, and said to him, "No one shall be
|
||||
my wife except for the one whose foot fits this golden shoe."
|
||||
|
||||
The two sisters were happy to hear this, for they had pretty feet. With her
|
||||
mother standing by, the older one took the shoe into her bedroom to try it on.
|
||||
She could not get her big toe into it, for the shoe was too small for her. Then
|
||||
her mother gave her a knife and said, "Cut off your toe. When you are queen you
|
||||
will no longer have to go on foot."
|
||||
|
||||
The girl cut off her toe, forced her foot into the shoe, swallowed the pain,
|
||||
and went out to the prince. He took her on his horse as his bride and rode away
|
||||
with her. However, they had to ride past the grave, and there, on the hazel
|
||||
tree, sat the two pigeons, crying out:
|
||||
|
||||
Rook di goo, rook di goo! There's blood in the shoe. The shoe is too tight,
|
||||
This bride is not right! Then he looked at her foot and saw how the blood was
|
||||
running from it. He turned his horse around and took the false bride home
|
||||
again, saying that she was not the right one, and that the other sister should
|
||||
try on the shoe. She went into her bedroom, and got her toes into the shoe all
|
||||
right, but her heel was too large. Then her mother gave her a knife, and said,
|
||||
"Cut a piece off your heel. When you are queen you will no longer have to go on
|
||||
foot."
|
||||
|
||||
The girl cut a piece off her heel, forced her foot into the shoe, swallowed the
|
||||
pain, and went out to the prince. He took her on his horse as his bride and
|
||||
rode away with her. When they passed the hazel tree, the two pigeons were
|
||||
sitting in it, and they cried out:
|
||||
|
||||
Rook di goo, rook di goo! There's blood in the shoe. The shoe is too tight,
|
||||
This bride is not right! He looked down at her foot and saw how the blood was
|
||||
running out of her shoe, and how it had stained her white stocking all red.
|
||||
Then he turned his horse around and took the false bride home again. "This is
|
||||
not the right one, either," he said. "Don't you have another daughter?"
|
||||
|
||||
"No," said the man. "There is only a deformed little Cinderella from my first
|
||||
wife, but she cannot possibly be the bride."
|
||||
|
||||
The prince told him to send her to him, but the mother answered, "Oh, no, she
|
||||
is much too dirty. She cannot be seen."
|
||||
|
||||
But the prince insisted on it, and they had to call Cinderella. She first
|
||||
washed her hands and face clean, and then went and bowed down before the
|
||||
prince, who gave her the golden shoe. She sat down on a stool, pulled her foot
|
||||
out of the heavy wooden shoe, and put it into the slipper, and it fitted her
|
||||
perfectly.
|
||||
|
||||
When she stood up the prince looked into her face, and he recognized the
|
||||
beautiful girl who had danced with him. He cried out, "She is my true bride."
|
||||
|
||||
The stepmother and the two sisters were horrified and turned pale with anger.
|
||||
The prince, however, took Cinderella onto his horse and rode away with her. As
|
||||
they passed by the hazel tree, the two white pigeons cried out:
|
||||
|
||||
Rook di goo, rook di goo! No blood's in the shoe. The shoe's not too tight,
|
||||
This bride is right! After they had cried this out, they both flew down and
|
||||
lit on Cinderella's shoulders, one on the right, the other on the left, and
|
||||
remained sitting there. When the wedding with the prince was to be held, the
|
||||
two false sisters came, wanting to gain favor with Cinderella and to share her
|
||||
good fortune. When the bridal couple walked into the church, the older sister
|
||||
walked on their right side and the younger on their left side, and the pigeons
|
||||
pecked out one eye from each of them. Afterwards, as they came out of the
|
||||
church, the older one was on the left side, and the younger one on the right
|
||||
side, and then the pigeons pecked out the other eye from each of them. And
|
||||
thus, for their wickedness and falsehood, they were punished with blindness as
|
||||
long as they lived.
|
||||
|
||||
|
@ -1,62 +0,0 @@
|
||||
// Copyright (c) 2019 Alexander Medvednikov. All rights reserved.
|
||||
// Use of this source code is governed by an MIT license
|
||||
// that can be found in the LICENSE file.
|
||||
|
||||
import os
|
||||
|
||||
fn main() {
|
||||
mut path := 'cinderella.txt'
|
||||
if os.args.len != 2 {
|
||||
println('usage: word_counter [text_file]')
|
||||
println('using $path')
|
||||
}
|
||||
else {
|
||||
path = os.args[1]
|
||||
}
|
||||
contents := os.read_file(path.trim_space()) or {
|
||||
println('failed to open $path')
|
||||
return
|
||||
}
|
||||
mut m := map[string]int{}
|
||||
for word in contents.to_lower().split(' ') {
|
||||
key := filter_word(word)
|
||||
if key == '' {
|
||||
continue
|
||||
}
|
||||
m[key] = m[key] + 1// TODO m[key]++
|
||||
}
|
||||
// Sort the keys
|
||||
mut keys := []string
|
||||
for e in m.entries {
|
||||
keys << e.key
|
||||
}
|
||||
keys.sort()
|
||||
// Print the map
|
||||
for key in keys {
|
||||
val := m[key]
|
||||
println('$key => $val')
|
||||
}
|
||||
}
|
||||
|
||||
// Removes punctuation
|
||||
fn filter_word(word string) string {
|
||||
if word == '' || word == ' ' {
|
||||
return ''
|
||||
}
|
||||
mut i := 0
|
||||
for i < word.len && !is_letter(word[i]) {
|
||||
i++
|
||||
}
|
||||
start := i
|
||||
for i < word.len && is_letter(word[i]) {
|
||||
i++
|
||||
}
|
||||
end := i
|
||||
return word.substr(start, end)
|
||||
}
|
||||
|
||||
// TODO remove once it's possible to call word[i].is_letter()
|
||||
fn is_letter(c byte) bool {
|
||||
return c.is_letter()
|
||||
}
|
||||
|
Reference in New Issue
Block a user