Last active
July 20, 2018 17:00
-
-
Save ssilva/e30df5156af8dfa0b5ff82a30a50a694 to your computer and use it in GitHub Desktop.
Simple grid: C vs Rust
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extern crate rand; | |
use rand::Rng; | |
const ROWS: usize = 10; | |
const COLS: usize = 80; | |
const ANSI_COLOR_RED: &str = "\x1b[31m"; | |
const ANSI_COLOR_GREEN: &str = "\x1b[32m"; | |
const ANSI_COLOR_BLUE: &str = "\x1b[34m"; | |
const ANSI_COLOR_RESET: &str = "\x1b[0m"; | |
#[derive(Copy, Clone)] // For the array initialization syntax [T; N] to work | |
enum TerrainType { | |
Grass, | |
Hill, | |
River, | |
} | |
fn init_grid(grid: &mut [[TerrainType; COLS]; ROWS]) { | |
let river_row = rand::thread_rng().gen_range(0, ROWS - 1); | |
for (i, row) in grid.iter_mut().enumerate() { | |
for cell in row.iter_mut() { | |
if i == river_row { | |
*cell = TerrainType::River; | |
} else if rand::thread_rng().gen_range(0, 9) == 0 { | |
*cell = TerrainType::Hill; | |
} | |
} | |
} | |
} | |
fn render_grid(grid: &mut [[TerrainType; COLS]; ROWS]) { | |
for row in grid.iter() { | |
for cell in row.iter() { | |
match cell { | |
TerrainType::Grass => print!("{}.{}", ANSI_COLOR_GREEN, ANSI_COLOR_RESET), | |
TerrainType::Hill => print!("{}^{}", ANSI_COLOR_RED, ANSI_COLOR_RESET), | |
TerrainType::River => print!("{}~{}", ANSI_COLOR_BLUE, ANSI_COLOR_RESET), | |
} | |
} | |
println!(); | |
} | |
} | |
fn main() { | |
let mut grid: [[TerrainType; COLS]; ROWS] = [[TerrainType::Grass; COLS]; ROWS]; | |
init_grid(&mut grid); | |
render_grid(&mut grid); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> // printf | |
#include <stdlib.h> // rand | |
#include <stdint.h> // uint8_t | |
#include <time.h> // time | |
#define ROWS 10 | |
#define COLS 80 | |
#define ANSI_COLOR_RED "\x1b[31m" | |
#define ANSI_COLOR_GREEN "\x1b[32m" | |
#define ANSI_COLOR_BLUE "\x1b[34m" | |
#define ANSI_COLOR_RESET "\x1b[0m" | |
// gcc terrain.c -o terrain -std=c11 -Wall -Wextra -Wpedantic | |
enum TerrainType | |
{ | |
TERRAIN_GRASS, | |
TERRAIN_HILL, | |
TERRAIN_RIVER, | |
}; | |
void init_grid(enum TerrainType grid[ROWS][COLS]) | |
{ | |
uint8_t river_row = rand() % ROWS; | |
for (uint8_t i = 0; i < ROWS; ++i) { | |
for (uint8_t j = 0; j < COLS; ++j) { | |
if (i == river_row) { | |
grid[i][j] = TERRAIN_RIVER; | |
} else if ((rand() % 10) == 0) { | |
grid[i][j] = TERRAIN_HILL; | |
} | |
} | |
} | |
} | |
void render_grid(enum TerrainType grid[ROWS][COLS]) | |
{ | |
for (uint8_t i = 0; i < ROWS; ++i) { | |
for (uint8_t j = 0; j < COLS; ++j) { | |
switch (grid[i][j]) { | |
case TERRAIN_GRASS: printf(ANSI_COLOR_GREEN "." ANSI_COLOR_RESET); break; | |
case TERRAIN_HILL: printf(ANSI_COLOR_RED "^" ANSI_COLOR_RESET); break; | |
case TERRAIN_RIVER: printf(ANSI_COLOR_BLUE "~" ANSI_COLOR_RESET); break; | |
default: printf("?"); break; | |
} | |
} | |
printf("\n"); | |
} | |
} | |
int main() | |
{ | |
srand(time(NULL)); | |
enum TerrainType grid[ROWS][COLS] = {0}; | |
init_grid(grid); | |
render_grid(grid); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment