use std::io;
use rand::Rng;
fn board_size(size: u8) -> Vec<u8> {
let mut vec = vec![];
for i in 0..size {
vec.push(&i + 1)
}
return vec;
}
fn create_board() -> Vec<Vec<u8>> {
let mut size = String::new();
let mut board = vec![];
println!("What size board do you want?");
io::stdin().read_line(&mut size)
.expect("Failed to read line.");
let size: usize = match size.trim().parse() {
Ok(num) => num,
Err(_) => panic!("Not a number"),
};
while board.len() < size {
board.push(board_size(size as u8));
}
board
}
fn move_knight(x: usize, y: usize, range: usize) -> (usize, usize) {
let mut move_to = rand::thread_rng();
let move_to = move_to.gen_range(0..=7);
match &move_to {
0 => ( x - 1, y + 2 ),
1 => ( x + 1, y + 2 ),
2 => ( x + 2, y + 1 ),
3 => ( x + 2, y - 1 ),
4 => ( x + 1, y - 2 ),
5 => ( x - 1, y - 2 ),
6 => ( x - 2, y - 1 ),
7 => ( x - 2, y + 1 ),
_ => (x, y),
}
}
fn place_knight(size: u8) -> (usize, usize) {
let mut knight_placement_x = rand::thread_rng();
let mut knight_placement_y = rand::thread_rng();
(knight_placement_y.gen_range(0..size) as usize, knight_placement_x.gen_range(0..size) as usize)
}
#find index of first element in nested vector
34 messages · Page 1 of 1 (latest)
fn main() {
let board: Vec<Vec<u8>> = create_board();
let max_size: &Vec<Vec<u8>> = &board;
let mut current_position = (0usize, 0usize);
current_position = place_knight(board.len().try_into().unwrap());
let mut current_position_y = board.iter().position(|&x| x == (current_position.0 as usize));
// current_position.0 represents the y axis, current_position.1 represents the x axis
println!("position of knight: {:?}{}", board[current_position.0], board[current_position.0][current_position.1]);
for i in 0..(board.len()).pow(2) {
let current_move = move_knight(current_position.0, current_position.1, board.len());
current_position.0 = current_position.0 + current_move.0;
current_position.1 = current_position.1 + current_move.1;
}
}```
im trying to display the position of the knight as "y: {} x: {}, y, x", but right now y displays the full contents of the array instead of just its position of that array
i dont know exactly how let mut current_position_y = board.iter().position(|&x| x == (current_position.0 as usize)); , but this line returns "can't compare Vec<u8> with usize". i understand why, but i dont know how to fix it
this is for the knights tour problem btw, i havent looked into any how-to's, so im just trying to set up the board, placing the knight somewhere random and some prints for debugging
isnt the x and y position already in current_position
current_position.0 contains the whole vector that is current_position.1
i just want the index of current_position.0
it is an index
its not a vector, its the index of a vector
a vector is Vec<_>, the index is usize
you have 2 indexes in current_position
those are for the vectors in board
oh yeah sure, but trying to print current_position.0 just prints the entire array instead of its position in the array
does that make sense?
vector*
no, just current_position.0 prints the index, a single number
but board[current_position.0] would print a vector
What size board do you want?
8
position of knight: [1, 2, 3, 4, 5, 6, 7, 8], 1
this is the current output i get
yes, 8 is the input
then it prints the board because you have board[current_position.0], and then a number
the type of board is Vec<Vec<u8>>. You need a usize to index a vector. After you index board once, you get to Vec<u8>, and after you index that again, you get to u8
if you print the board, you will print a vector of vectors, because that is the entire content with all of its rows
if you index the board once, you will get just 1 row
and if you index it again, you will finally get the u8
that is how coordination works in a 2d vector/array
you can print the x and y coordinates directly if you have those indexes, which you do
println!("{current_position:?}");
yeah i just found that out, im overcomplicating it
thanks for the help, i'd have been stuck on that for a while
i could have just given you the answer, but it was probably better to explain how it works