Compare commits

..

7 Commits

Author SHA1 Message Date
deadvey
10d452ec33 rustsweeper screenshot 2025-01-05 16:46:10 +00:00
deadvey
c0c9e1db28 proper game over screen for rustsweeper 2025-01-05 16:45:03 +00:00
deadvey
90ac4c5e2e readme 2025-01-05 16:44:35 +00:00
deadvey
40dbc40526 rustsweeper 2025-01-05 16:20:39 +00:00
deadvey
31b95ad12e bug fix for prophecies 2025-01-05 03:14:15 +00:00
deadvey
96b28a74c2 added rubiks cube to readme 2025-01-05 03:09:17 +00:00
deadvey
483b383806 rubiks cube thingy (WIP) 2025-01-05 03:04:23 +00:00
12 changed files with 538 additions and 0 deletions

View File

@ -9,6 +9,11 @@ TO DO: documentation, commenting ¬_¬<br/>
BUGS: Can pass between diagonally conencted blocks, is this a 'bug'?<br/> BUGS: Can pass between diagonally conencted blocks, is this a 'bug'?<br/>
![example image for falling](screenshots/falling.png) ![example image for falling](screenshots/falling.png)
# Rustsweeper
A Minesweeper clone written in rust, instead of mines,<br/>
it's rusty nails that give you tetanus<br/>
![example game over screen for rustsweeper, at the top is a game of rustsweeper that has ended then the text 'GAME OVER' followed by ascii art of a man stepping on a nail and a collection of nails](screenshots/rustsweeper.png)
# Prophecies # Prophecies
Generates a random prophecy based on prophecies from <br/> Generates a random prophecy based on prophecies from <br/>
The Underland Chronicles by Suzanne Collins<br/> The Underland Chronicles by Suzanne Collins<br/>
@ -33,6 +38,10 @@ Greet them as we have before.
Watch the nibblers spin and snap. Watch the nibblers spin and snap.
``` ```
# Rubiks Cube thing (WIP)
It's just a rubiks cube in the terminal, written in rust
![example image for rubiks](screenshots/rubiks.png)
# Roman Numerals (WIP) # Roman Numerals (WIP)
Converts numbers between Roman Numerals and Arabic Numerals<br/> Converts numbers between Roman Numerals and Arabic Numerals<br/>
### Example: ### Example:

View File

@ -19,9 +19,14 @@ fn main() -> io::Result<()> {
let lines_in_string = split_string.len(); let lines_in_string = split_string.len();
// Iterate for "number_of_lines" and each time pick a random line in the file // Iterate for "number_of_lines" and each time pick a random line in the file
let mut previous_line_index = 0;
for _i in 0..number_of_lines { for _i in 0..number_of_lines {
let line_index = rand::thread_rng().gen_range(0..lines_in_string); // Pick random line let line_index = rand::thread_rng().gen_range(0..lines_in_string); // Pick random line
if split_string[previous_line_index] == split_string[line_index] {
continue;
}
println!("{}",split_string[line_index]); // Print the random line println!("{}",split_string[line_index]); // Print the random line
previous_line_index = line_index;
} }
Ok(()) Ok(())

1
rubiks/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/target

7
rubiks/Cargo.toml Normal file
View File

@ -0,0 +1,7 @@
[package]
name = "rubiks"
version = "0.1.0"
edition = "2021"
[dependencies]
colored = "2.2.0"

BIN
rubiks/cube.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

26
rubiks/cube.txt Normal file
View File

@ -0,0 +1,26 @@
Colors:
WWW
WWW
WWW
OOO GGG RRR BBB
OOO GGG RRR BBB
OOO GGG RRR BBB
YYY
YYY
YYY
Indexs:
012 0
345
678
901 234 567 890 10, 20
123 456 789 012 30
345 678 901 234 40
567 50
890
123

173
rubiks/src/main.rs Normal file
View File

@ -0,0 +1,173 @@
use std::io::{stdin,stdout,Write};
use colored::*;
fn print_coloured(output: char) {
match output {
'R' => print!("{}", "R".on_truecolor(255,0,0)),
'G' => print!("{}", "G".on_truecolor(0,255,0).purple()),
'B' => print!("{}", "B".on_truecolor(0,0,255).red()),
'Y' => print!("{}", "Y".on_truecolor(255,255,0).black()),
'W' => print!("{}", "W".on_truecolor(255,255,255).black()),
'O' => print!("{}", "O".on_truecolor(255, 128, 0)),
_ => print!("{}", output),
}
}
fn output_cube(cube: &Vec<char>) {
// White square
print!(" ");
for i in 0..=2 {
print_coloured(cube[i]);
}
println!();
print!(" ");
for i in 3..=5 {
print_coloured(cube[i]);
}
println!();
print!(" ");
for i in 6..=8 {
print_coloured(cube[i]);
}
// First layer
println!();
for i in 9..=20 {
print_coloured(cube[i]);
}
// Second layer
println!();
for i in 21..=32 {
print_coloured(cube[i]);
}
// Third layer
println!();
for i in 33..=44 {
print_coloured(cube[i]);
}
// Yellow square
println!();
print!(" ");
for i in 45..=47 {
print_coloured(cube[i]);
}
println!();
print!(" ");
for i in 48..=50 {
print_coloured(cube[i]);
}
println!();
print!(" ");
for i in 51..=53 {
print_coloured(cube[i]);
}
println!()
}
fn move_cube(rotation: String, mut cube: &mut Vec<char>) {
let mut initial_cube = cube.clone();
// Up clockwise
if rotation == "U" {
cube[0] = initial_cube[6];
cube[1] = initial_cube[3];
cube[2] = initial_cube[0];
cube[3] = initial_cube[7];
// Cube 4 remains the same
cube[5] = initial_cube[1];
cube[6] = initial_cube[8];
cube[7] = initial_cube[5];
cube[8] = initial_cube[2];
let indices = vec![12, 13, 14, 15, 16, 17, 18, 19, 20, 9, 10, 11];
for (i, &index) in indices.iter().enumerate() {
cube[9 + i] = initial_cube[index];
}
}
if rotation == "D" {
cube[45] = initial_cube[51];
cube[46] = initial_cube[48];
cube[47] = initial_cube[45];
cube[48] = initial_cube[52];
// Cube 49 remains the same
cube[50] = initial_cube[46];
cube[51] = initial_cube[53];
cube[52] = initial_cube[50];
cube[53] = initial_cube[47];
let indices = vec![42, 43, 44, 33, 34, 35, 36, 37, 38, 39, 40, 41];
for (i, &index) in indices.iter().enumerate() {
cube[33 + i] = initial_cube[index];
}
}
if rotation == "R" {
cube[15] = initial_cube[39];
cube[16] = initial_cube[27];
cube[17] = initial_cube[15];
cube[27] = initial_cube[40];
// Cube 28 remains the same
cube[29] = initial_cube[16];
cube[39] = initial_cube[41];
cube[40] = initial_cube[29];
cube[41] = initial_cube[17];
// Other edge peices around the Right side
cube[42] = initial_cube[2];
cube[30] = initial_cube[5];
cube[18] = initial_cube[8];
cube[2] = initial_cube[14];
cube[5] = initial_cube[26];
cube[8] = initial_cube[38];
cube[14] = initial_cube[47];
cube[26] = initial_cube[50];
cube[38] = initial_cube[53];
cube[47] = initial_cube[42];
cube[50] = initial_cube[30];
cube[53] = initial_cube[18];
}
}
fn input() -> String{
let mut s=String::new();
let _=stdout().flush();
stdin().read_line(&mut s).expect("Did not enter a correct string");
if let Some('\n')=s.chars().next_back() {
s.pop();
}
if let Some('\r')=s.chars().next_back() {
s.pop();
}
return s;
}
fn main() {
// Create a net of a cube, see cube.jpeg or cube.txt in the root of this project for a
// visualisation
let mut cube: Vec<char> = vec![
'W', 'W', 'W', 'W', 'W', 'W', 'W', 'W', 'W', // 0-8 (W)
'O', 'O', 'O', // 9-11 (O)
'G', 'G', 'G', // 12-14 (G)
'R', 'R', 'R', // 15-17 (R)
'B', 'B', 'B', // 18-20 (B)
'O', 'O', 'O', // 21-23 (O)
'G', 'G', 'G', // 24-26 (G)
'R', 'R', 'R', // 27-29 (R)
'B', 'B', 'B', // 30-32 (B)
'O', 'O', 'O', // 33-35 (O)
'G', 'G', 'G', // 36-38 (G)
'R', 'R', 'R', // 39-41 (R)
'B', 'B', 'B', // 42-44 (B)
'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', // 45-53 (Y)
];
output_cube(&cube);
'game_loop: loop {
let rotation = input();
if rotation == "q" {
break 'game_loop;
}
move_cube(rotation, &mut cube);
output_cube(&cube);
}
}

1
rustsweeper/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/target

9
rustsweeper/Cargo.toml Normal file
View File

@ -0,0 +1,9 @@
[package]
name = "rustsweeper"
version = "0.1.0"
edition = "2021"
[dependencies]
rand = "0.8.5"
colored = "2.2.0"
console = "0.15.10"

307
rustsweeper/src/main.rs Normal file
View File

@ -0,0 +1,307 @@
use std::io::{stdin,stdout,Write};
use rand::Rng;
use colored::*;
use colored::Color::TrueColor;
use console::Term;
fn output_board(board: &Vec<Vec<i64>>, state: &Vec<Vec<char>>, x_hovered: i64, y_hovered: i64) {
print!("{}[2J", 27 as char);
for y in 0..board.len() {
for x in 0..board[y].len() {
let background_color: u8;
if state[y][x] != 'u' {
background_color = 128;
}
else {
background_color = 196;
}
if x == x_hovered as usize && y == y_hovered as usize {
print!("{}", "X".truecolor(0,0,0).on_truecolor(background_color,background_color,background_color));
}
else if state[y][x] == 'u' {
if board[y][x] == -1 {
print!("{}","".truecolor(128,64,0).on_truecolor(background_color,background_color,background_color));
}
else if board[y][x] == 0 {
print!("{}"," ".on_truecolor(background_color,background_color,background_color));
}
else if board[y][x] == 1 {
print!("{}", "1".truecolor(1,0,254).on_truecolor(background_color,background_color,background_color));
}
else if board[y][x] == 2 {
print!("{}", "2".truecolor(1,127,1).on_truecolor(background_color,background_color,background_color));
}
else if board[y][x] == 3 {
print!("{}", "3".truecolor(254,0,0).on_truecolor(background_color,background_color,background_color));
}
else if board[y][x] == 4 {
print!("{}", "4".truecolor(1,0,128).on_truecolor(background_color,background_color,background_color));
}
else if board[y][x] == 5 {
print!("{}", "5".truecolor(129,1,2).on_truecolor(background_color,background_color,background_color));
}
else if board[y][x] == 6 {
print!("{}", "6".truecolor(0,128,129).on_truecolor(background_color,background_color,background_color));
}
else if board[y][x] == 7 {
print!("{}", "7".truecolor(0,0,0).on_truecolor(background_color,background_color,background_color));
}
else if board[y][x] == 8 {
print!("{}", "8".truecolor(128,128,128).on_truecolor(background_color,background_color,background_color));
}
}
else if state[y][x] == 'm' {
print!("{}", "m".truecolor(228,16,32).on_truecolor(background_color,background_color,background_color));
}
else if state[y][x] == '?' {
print!("{}", "?".truecolor(0,0,0).on_truecolor(background_color,background_color,background_color));
}
else {
print!("{}"," ".on_truecolor(background_color,background_color,background_color));
}
}
println!();
}
}
fn input() -> String{
let mut s=String::new();
let _=stdout().flush();
stdin().read_line(&mut s).expect("Did not enter a correct string");
if let Some('\n')=s.chars().next_back() {
s.pop();
}
if let Some('\r')=s.chars().next_back() {
s.pop();
}
return s;
}
fn place_nails(board: &mut Vec<Vec<i64>>, number_of_nails: i64, width: i64, height: i64) {
for _nail in 0..number_of_nails {
// Generate coordinates for nails, if there is already a nail there, then generate new
// coordinates, should add case for if there are more mines than locations, but why would
// someone do that?
let mut x_coord = rand::thread_rng().gen_range(0..width);
let mut y_coord = rand::thread_rng().gen_range(0..height);
while board[y_coord as usize][x_coord as usize] == -1 {
x_coord = rand::thread_rng().gen_range(0..width);
y_coord = rand::thread_rng().gen_range(0..height);
}
board[y_coord as usize][x_coord as usize] = -1;
}
}
fn determine_nails_in_range(board: &mut Vec<Vec<i64>>) {
for x in 0..board.len() {
for y in 0..board[x].len() {
if board[x][y] != -1 {
let mut num_of_nails = 0;
for i in -1..2 {
for j in -1..2 {
// Check if index is in range (>= 0 and < length of vector)
if (x as i64 + i >= 0 && (x as i64 + i) < board.len() as i64) && (y as i64 + j >= 0 && (y as i64 + j) < board[x].len() as i64) {
if board[(x as i64 + i) as usize][(y as i64 + j) as usize] == -1 {
num_of_nails += 1; // Increment the nail count
}
}
}
}
board[x][y] = num_of_nails;
}
}
}
}
fn find_connected_coordinates(grid: &Vec<Vec<i64>>, start_row: usize, start_col: usize) -> Vec<(usize, usize)> {
let rows = grid.len();
let cols = grid[0].len();
let target_value = grid[start_row][start_col];
// Directions for 8 possible moves (vertical, horizontal, and diagonal)
let directions: [(i64, i64); 8] = [
(-1, 0), // Up
(1, 0), // Down
(0, -1), // Left
(0, 1), // Right
(-1, -1),// Top-left diagonal
(-1, 1), // Top-right diagonal
(1, -1), // Bottom-left diagonal
(1, 1), // Bottom-right diagonal
];
// Visited cells
let mut visited = vec![vec![false; cols]; rows];
// Vector to store the reachable coordinates
let mut result = Vec::new();
// DFS function
fn dfs(
grid: &Vec<Vec<i64>>,
row: usize,
col: usize,
target_value: i64,
visited: &mut Vec<Vec<bool>>,
result: &mut Vec<(usize, usize)>,
directions: &[(i64, i64)],
) {
// Check if the position is out of bounds or already visited or doesn't match the target value
if row >= grid.len() || col >= grid[0].len() || visited[row][col] || grid[row][col] != target_value {
return;
}
// Mark the cell as visited and add the coordinate to the result
visited[row][col] = true;
result.push((row, col));
// Flag to check if we're at the boundary of the connected region
let mut is_boundary = false;
for &direction in directions.iter() {
let (dx, dy) = direction;
let new_row = row as i64 + dx;
let new_col = col as i64 + dy;
if new_row >= 0 && new_row < grid.len() as i64 && new_col >= 0 && new_col < grid[0].len() as i64 {
let new_row = new_row as usize;
let new_col = new_col as usize;
if grid[new_row][new_col] != target_value {
is_boundary = true;
}
dfs(grid, new_row, new_col, target_value, visited, result, directions);
}
}
if is_boundary {
for &direction in directions.iter() {
let (dx, dy) = direction;
let new_row = row as i64 + dx;
let new_col = col as i64 + dy;
if new_row >= 0 && new_row < grid.len() as i64 && new_col >= 0 && new_col < grid[0].len() as i64 {
let new_row = new_row as usize;
let new_col = new_col as usize;
// Add cells with a different value to the result
if grid[new_row][new_col] != target_value && !visited[new_row][new_col] {
result.push((new_row, new_col));
}
}
}
}
}
dfs(grid, start_row, start_col, target_value, &mut visited, &mut result, &directions);
result
}
fn uncover(board: &Vec<Vec<i64>>, state: &mut Vec<Vec<char>>, x_hovered: usize, y_hovered: usize, alive: &mut bool) {
state[y_hovered][x_hovered] = 'u';
if board[y_hovered][x_hovered] == -1 {
*alive = false;
}
if board[y_hovered][x_hovered] == 0 {
let connected = find_connected_coordinates(&board, y_hovered, x_hovered);
for (y,x) in connected.iter() {
state[*y][*x] = 'u';
}
}
}
fn main() {
let stdout = Term::buffered_stdout();
let mut board: Vec<Vec<i64>> = Vec::new();
let mut state: Vec<Vec<char>> = Vec::new(); // u = uncovered, c = covered, m = marked
let height: i64 = 10;
let width: i64 = 30;
let number_of_nails: i64 = 35;
let mut x_hovered: i64 = 0;
let mut y_hovered: i64 = 0;
let mut alive: bool = true;
for y in 0..height {
board.push(Vec::new());
state.push(Vec::new());
for x in 0..width {
board[y as usize].push(0);
state[y as usize].push('c');
//board[x as usize][y as usize] = -1;
}
}
place_nails(&mut board, number_of_nails, width, height);
determine_nails_in_range(&mut board);
output_board(&board, &state, x_hovered, y_hovered);
'gameloop: loop {
if let Ok(character) = stdout.read_char() {
match character {
'w' => if y_hovered > 0 { y_hovered -= 1 },
'a' => if x_hovered > 0 { x_hovered -= 1},
's' => if y_hovered < height-1 { y_hovered += 1 },
'd' => if x_hovered < width-1 {x_hovered += 1},
'u' => uncover(&board, &mut state, x_hovered as usize, y_hovered as usize, &mut alive),
'm' => {
if state[y_hovered as usize][x_hovered as usize] == 'm' {
state[y_hovered as usize][x_hovered as usize] = 'c'
}
else {
state[y_hovered as usize][x_hovered as usize] = 'm'
}
},
'?' => {
if state[y_hovered as usize][x_hovered as usize] == '?' {
state[y_hovered as usize][x_hovered as usize] = 'c'
}
else {
state[y_hovered as usize][x_hovered as usize] = '?'
}
},
'q' => break 'gameloop,
_ => (),
}
}
if alive {
output_board(&board, &state, x_hovered, y_hovered);
}
else if alive == false {
println!("GAME OVER!\nYou got Tetanus!");
println!("⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣀⡀⠀⠀⠀⠀⠀⠀⠀");
println!(" ⢀⣾⣿O⣷");
println!(" ⠸⣿⣿⣿C- HOLY SHIT");
println!(" ⣀⣤⡈⠛⢉⠀⠀⠀⠀⠀⠀⠀");
println!(" ⢀⣴⣿⣿⣿⣿⣿⣿⠃⠀⠀⠀⠀⠀⠀");
println!(" ⣴⣿⣿⢿⣿⣿⣿⣿⣿⠀⡄⠀⠀⠀⠀⠀");
println!(" ⠀⠀⠀⠀⢰⣿⡏⠀⢸⣿⣿⣿⣿⡇⢸⣷⣤⣀⠀⠀⠀");
println!(" ⠀⠀⠀⠀⣼⣿⠁⠀⢸⣿⣿⣿⣿⠁⠀⠙⠻⢿⣿⣶⠀");
println!(" ⠀⠀⠀⠀⠛⠋⠀⠀⠸⣿⣿⣿⡏⠀⠀⠀⠀⠀⠈⠉⠀");
println!(" ⠀⠀⠀⠀⠀⠀⠀⠀⣄⠙⣿⣿⣷⡄⠀⠀⠀⠀⠀⠀⠀");
println!(" ⠀⠀⠀⠀⠀⠀⠀⢸⣿⣦⠈⢿⣿⣿⣦⠀⠀⠀⠀⠀⠀");
println!(" ⠀⠀⠀⠀⠀⠀⠀⣼⣿⡟⠀⠀⠻⣿⣿⣧⠀⠀⠀⠀⠀");
println!(" ⠀⠀⠀⠀⠀⣠⣾⣿⠟⠁⠀⠀⠀⠘⢿⣿⣧/");
println!(" ⠀⠀⠀⠀⢾⣿⠟⠁⠀⠀⠀⠀⠀⠀⠈⢻⣿⠇⠀⠀⠀");
println!(" ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣀⣀⠄⠀ ");
println!("BEWARE:⢀⡴⠀⠀⠀⠀⠀⠀⠘⠛⢿⡟⠁⠀⠀ ");
println!(" ⠀⢀⣴⢿⣇⠀⠀⠲⣶⣤⣤⡄⠀⠈⣧⠀⠀⠀ ");
println!(" ⠀⠙⢷⣄⠀⣿⠃⠀⠀⠀⠀⢻⡀⠀⠀ ");
println!(" ⠀⠀⠀⠀⠀⠀⠙⣿⣏⠀⠀⠀⠀⠀⢸⡇⠀⠀ ");
println!(" ⠀⠀⠀⣿⠛⢷⠄⠀⠀⠀⠈⢿⠀ ");
println!(" ⠀⠀⠀⠀⠀⠀⢰⡏⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ");
println!(" ⠀⠀⠀⠀⠀⠀⠘⠀⠀⠀⠀⠀⠀⠀⠀⠀ ");
break 'gameloop;
}
}
}

BIN
screenshots/rubiks.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 156 KiB

BIN
screenshots/rustsweeper.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 501 KiB