Created
February 1, 2017 15:37
-
-
Save anonymous/c6b2de98cb81a0ef78ec4eff2de3397e to your computer and use it in GitHub Desktop.
Shared via Rust Playground
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
use std::collections::HashMap; | |
use std::fmt; | |
/* | |
Hello, this is my prototype for storing boards in my mahjong project. | |
I defined the following structs: Board, Tile, TileType | |
The entire game board is stored in a HashMap<i32, Vec<TileType>> | |
^-layer ^-Allows me to easily see if a tile is empty or not (empty spot on board) | |
The layer refers to what level of the game board the Vec<TileType> is on. | |
TileType is an enum with the options Empty and Used(Tile), which means I can easily check if a tile is empty, and if it's not, get that tile. | |
Tiles have an (x, y) tuple and a design field. | |
To test the structure of this code, I also implemented fmt::Display for Tile and Board (so I can print out the two types to stdout in my own format) | |
*/ | |
fn main() { | |
let mut board = Board::new((2,2)); //This is just filler at the moment, the size doesn't matter (might even omit it from the project) | |
{ //Just cleans up the scope, nothing else | |
//I wish I could figure out a better way to do this | |
//Obviously, I will be reading in boards programmatically, so it doesn't matter _that_ much | |
let layer_one: Vec<TileType> = vec!( | |
TileType::Used(Tile::new((0,0), 5)), TileType::Empty, TileType::Used(Tile::new((0,1), 4)), | |
TileType::Empty, TileType::Used(Tile::new((1,0), 1)), | |
); | |
let layer_two: Vec<TileType> = vec!( | |
TileType::Used(Tile::new((0,0), 4)), TileType::Empty, TileType::Used(Tile::new((0,1), 3)), | |
TileType::Empty, TileType::Used(Tile::new((1,0), 6)), | |
); | |
//Insert the layers (can be out of order, but this looks nicer) | |
board.add_layer(0, layer_one); | |
board.add_layer(1, layer_two); | |
} | |
println!("{}", board); //Run the custom output, and it works as intended! | |
} | |
#[allow(dead_code)] | |
struct Board { | |
size: (i32, i32), //Dimensions, might remove, don't know. Might need it to help with converting the flat list of tiles to a square board | |
tiles: HashMap<i32, Vec<TileType>>, //Allows me to have a dynamic board which I can edit at runtime (:D) | |
} | |
//The methods for Board | |
#[allow(dead_code)] | |
impl Board { | |
fn new(size: (i32, i32)) -> Board { | |
Board { | |
size: size, | |
tiles: HashMap::new(), | |
} | |
} | |
fn get_size(&self) -> (i32, i32) { | |
self.size | |
} | |
fn add_layer(&mut self, layer: i32, tiles: Vec<TileType>) { | |
self.tiles.insert(layer, tiles); | |
} | |
fn get_layer(&self, layer: i32) -> &Vec<TileType> { | |
self.tiles.get(&layer).unwrap() | |
} | |
} | |
#[allow(dead_code)] | |
struct Tile { | |
pos: (i32, i32), | |
design: u8, | |
} | |
#[allow(dead_code)] | |
impl Tile { | |
fn new(pos: (i32, i32), design: u8) -> Tile { | |
Tile { | |
pos: pos, | |
design: design, | |
} | |
} | |
fn get_position(&self) -> (i32, i32) { | |
self.pos | |
} | |
fn get_design(&self) -> u8 { | |
self.design | |
} | |
} | |
#[allow(dead_code)] | |
enum TileType { | |
Empty, | |
Used(Tile), | |
} | |
#[allow(unused_must_use)] | |
impl fmt::Display for Board { | |
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
for i in 0..self.tiles.len() { | |
let _ = write!(f, "Layer {}: [", i); | |
for tile_type in self.get_layer(i as i32) { | |
match *tile_type { | |
TileType::Empty => write!(f, "( EMPTY ) "), | |
TileType::Used(ref tile) => write!(f, "{} ", tile), | |
}; | |
} | |
let _ = write!(f, "]\n"); | |
} | |
write!(f, "") | |
} | |
} | |
impl fmt::Display for Tile { | |
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
write!(f, "({}, {}, {})", self.pos.0, self.pos.1, self.design) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment