package main import "core:fmt" import "core:os" import "core:bufio" import "core:io" import "core:strconv" import "core:strings" import "core:math/rand" instructions: string = "Use space to stay still, and HJKL to move." Direction :: enum {still, up, down, left, right} rand_state := rand.create(69) state_type :: struct { gameboard : [16][16]rune, flow : [16]bool, spawning : [16]bool, player_row : int, } block::'O' water::'~' player::'@' difficulty:f32=0.4 log_length::0.6 main :: proc() { //fmt.println("What's your name? ") //name: string = getln() //defer delete(name) //fmt.printf("Hello %v! Welcome to shitty frogger.\n\n", name) fmt.println("Set difficulty [0.0 <-> 1.0]") difficulty=getfloat() fmt.println("Set seed") rand_state=rand.create(u64(getint())) win : bool = false state : state_type populate_board(&state) draw_board(state) for !win { fmt.println(instructions) walk(&state, getdir()) progress_board(&state) draw_board(state) if state.player_row==0 { win = true } } fmt.println("\n You win, brotha.") getln() } getln :: proc() -> string { buf: [256]u8 bytes_read, ok := os.read(os.stdin, buf[:]) //assert(ok) read_input := string(buf[:bytes_read-2]) return strings.clone(read_input) } getint :: proc() -> int { buf: [256]u8 for { read_input := getln() output, ok := strconv.parse_int(read_input) if ok { return output } else { fmt.println("That's not a normal whole number, mate.") } } } getfloat :: proc() -> f32 { buf: [256]u8 for { read_input := getln() output, ok := strconv.parse_f32(read_input) if ok { return output } else { fmt.println("That's not a normal float, mate.") } } } getrune :: proc() -> rune { buf: [256]u8 bytes_read, ok := os.read(os.stdin, buf[:]) //assert(ok) read_input := rune(buf[0]) return read_input } getdir :: proc() -> Direction { for { switch getrune() { case ' ': return .still case 'h': return .left case 'j': return .down case 'k': return .up case 'l': return .right case: // default fmt.println("Not a valid direction.") } } } populate_board :: proc(state:^state_type) { using state for i:=0; i difficulty { state.spawning[i] = !is_spawning } } } } } append :: proc(array:^[16]rune, appendee:rune, front:bool) { if front { // Put appendee at the start of rune-array // X 1 2 3 4 5 // ^ # # # # # for i:=len(array)-1; i!=0; i-=1 { array[i] = array[i-1] } array[0] = appendee return } else { // Put appendee at the end of rune-array // 0 1 2 3 4 X // # # # # # ^ for _, i in array { if i < len(array)-1 { array[i] = array[i+1] } } array[len(array)-1] = appendee return } } find_player :: proc(state:state_type) -> int { for x, i in state.gameboard[state.player_row] { if x==player {return i} } return -1 } walk :: proc(state:^state_type, dir:Direction) { using state player_column : int = find_player(state^) fmt.println(player_column) current_rubric : ^rune = &gameboard[player_row][player_column] target : ^rune = current_rubric switch dir { case .up: if player_row!=0 { target = &gameboard[player_row-1][player_column] } if target^==block {player_row-=1} case .down: if player_row!=len(gameboard)-1 { target = &gameboard[player_row+1][player_column] } if target^==block {player_row+=1} case .left: if player_column!=0 { target = &gameboard[player_row][player_column-1] } case .right: if player_column!=len(gameboard[0])-1 { target = &gameboard[player_row][player_column+1] } case .still: return case: // default fmt.println("Something just went very wrong. That's not a direction you can walk in.") return } if target^==block { target^=player current_rubric^=block } return } draw_board :: proc(state:state_type) { for row, i in state.gameboard { if state.flow[i] { fmt.printf("> ") } else { fmt.printf("< ") } for rubric, j in row { fmt.printf("%v ", state.gameboard[i][j]) } fmt.printf("\n") } }