-
-
Save dashaw92/9072e9f22d10e27eb722529c5ab2dba8 to your computer and use it in GitHub Desktop.
My mahjong board implementation prototype
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 Board as a struct, and TileType as an enum with both an Empty option and a Used struct option | |
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 {pos: (i32, i32), design: u8} , which means I can easily check if a tile is empty, and if it's not, get info on that tile. | |
To test the structure of this code, I also implemented fmt::Display for 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 | |
let layer_one: Vec<TileType> = vec!( | |
TileType::Used{pos: (0, 0), design: 5}, TileType::Empty, TileType::Used {pos: (0, 1), design: 4}, | |
TileType::Empty, TileType::Used {pos: (1, 0), design: 1}, | |
); | |
let layer_two: Vec<TileType> = vec!( | |
TileType::Used{pos: (0, 0), design: 4}, TileType::Empty, TileType::Used{pos: (0, 1), design: 3}, | |
TileType::Empty, TileType::Used{pos: (1, 0), design: 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)] | |
enum TileType { | |
Empty, | |
Used { pos: (i32, i32), design: u8 }, | |
} | |
#[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{ pos, design } => write!(f, "({}, {}, {}) ", pos.0, pos.1, design), | |
}; | |
} | |
let _ = write!(f, "]\n"); | |
} | |
write!(f, "") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment