array2ds

May God let rust have better constant generics

Array2ds is a none-bloated library in rust for rust by rust for handling staticly sized 2d array/grids to run the examples: cargo run --example 'example_name'

Features

```rust use array2ds::array2d::*; use std::io::stdin;

struct Square { data: char, }

impl Default for Square { fn default() -> Self { Square { data: '#' } } }

fn main() { run_game(); }

fn rungame() { let mut board: Array2d = Array2d::filledwithdefault(3, 3); println!("enter location as: row col, for example: 0 1, row: 0 column 1"); printboard(&board);

let mut winner = false;
let mut current_player = 'X';

while !winner {
    let (row, column) = read_grid_location();

    if board[[row, column]].data == '#' {
        board[[row, column]].data = current_player;

        match current_player {
            'X' => current_player = 'O',
            'O' => current_player = 'X',
            _ => panic!("invalid data for square"),
        }
        println!();
        print_board(&board);
        winner = check_horizontal(&board)  || check_vertical(&board) || check_diagnol(&board);
    } else {
        println!("invalid location, try another");
    }
}

}

fn readgridlocation() -> (usize, usize) { let mut buffer = String::new(); stdin().read_line(&mut buffer).unwrap(); let buffer = buffer.trim(); let splitted: Vec<&str> = buffer.split(' ').collect(); if splitted.len() < 2 { panic!("enter data as row col "); } let r = splitted[0].parse::().unwrap(); let c = splitted[1].parse::().unwrap(); (r, c) }

fn check_diagnol(board: &Array2d) -> bool{

let mid = &board[(1,1)];
if mid.data == '#'{
    return false
}
if board[[0,0]].data == mid.data && mid.data == board[(2,2)].data{
    return true
}
if board[[2,0]].data == mid.data && mid.data == board[(0,2)].data{
    return true
}
false

}

fn checkvertical(board: &Array2d) -> bool { for col in 0..3 { let mut xcount = 0; let mut ocount = 0; for row in 0..3 { match board[[row, col]].data { 'X' => xcount += 1, 'O' => o_count += 1, _ => {} } }

    if x_count == 3 || o_count == 3 {
        return true;
    }
}
false

}

fn checkhorizontal(board: &Array2d) -> bool { for row in board.iterrows() { let mut ocount = 0; let mut xcount = 0; for player in row { match player.data { 'O' => ocount += 1, 'X' => xcount += 1, _ => {} } } if ocount == 3 || xcount == 3 { return true; } } false }

fn printboard(board: &Array2d) { for row in board.iterrows() { for square in row { print!("{}", square.data); } println!(); } }

```

License

MIT