Skip to content

Commit

Permalink
Added better looking prinout for game board
Browse files Browse the repository at this point in the history
  • Loading branch information
Kapsyloffer committed Jan 22, 2024
1 parent c3bb009 commit b8d20fd
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 10 deletions.
4 changes: 2 additions & 2 deletions html/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
home_colour: "White",
x1: 0,
y1: 0,
x2: 1,
y2: 1,
x2: 2,
y2: 2,
aggr: false
};

Expand Down
14 changes: 7 additions & 7 deletions src/api/web_sockets.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use axum::{
extract::{
ws::{Message, WebSocket, WebSocketUpgrade}, Path, State
ws::{Message, WebSocket, WebSocketUpgrade}, State
},
response::*,
};
Expand Down Expand Up @@ -47,6 +47,7 @@ pub async fn handle_socket(mut socket: WebSocket, game_hodler: GameHodler) {
let msg = if let Ok(msg) = msg {
msg
} else {
println!("Socket ded");
return;
};

Expand Down Expand Up @@ -89,10 +90,9 @@ pub async fn handle_socket(mut socket: WebSocket, game_hodler: GameHodler) {
if socket
.send(Message::Text(state))
.await
.is_err()
{
return;
}
.is_err(){
return;
}
}
GamePacket::Action { id, move_p, move_a } => {
let mut games = game_hodler.games.lock().unwrap();
Expand Down Expand Up @@ -120,9 +120,9 @@ pub async fn handle_socket(mut socket: WebSocket, game_hodler: GameHodler) {
//println!("{:#?}", game);

//DEBUG
let size = games.len();
//let size = games.len();

println!("{}", size);
println!("{}", game.dislay());
}
GamePacket::GameCreated { .. } => (),
}
Expand Down
69 changes: 68 additions & 1 deletion src/rules/game_instance.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use rand::{distributions::Alphanumeric, Rng};
use serde::Serialize;

use super::game_board::{Board, Color};
use super::{game_board::{Board, Color}, game_tile::Tile};

//#[derive(Serialize, Deserialize, Debug)]
#[derive(Debug, Clone, Serialize)]
Expand Down Expand Up @@ -54,6 +54,73 @@ impl Game {
return None;
}

pub fn dislay(&mut self) -> String
{
let mut disp: String = String::from("\n\n\tS H O B U\n\n");
let red = "\x1b[31m";
let green = "\x1b[32m";
let reset = "\x1b[0m";


//TRASH
for i in 0..4 as usize
{
for j in 0..4 as usize
{
disp.push_str(red);
match self.get_board(Color::White, Color::White).unwrap().get_state()[i][j]
{
Tile::White => disp.push_str("[W]"),
Tile::Black => disp.push_str("[B]"),
Tile::Empty => disp.push_str("[ ]"),
}
disp.push_str(reset);
}
disp.push_str(" ");
for j in 0..4 as usize
{
disp.push_str(green);
match self.get_board(Color::White, Color::Black).unwrap().get_state()[i][j]
{
Tile::White => disp.push_str("[W]"),
Tile::Black => disp.push_str("[B]"),
Tile::Empty => disp.push_str("[ ]"),
}
disp.push_str(reset);
}
disp.push_str("\n");
}
disp.push_str("\n---------------------------\n\n");
for i in 0..4 as usize
{
for j in 0..4 as usize
{
disp.push_str(green);
match self.get_board(Color::Black, Color::White).unwrap().get_state()[i][j]
{
Tile::White => disp.push_str("[W]"),
Tile::Black => disp.push_str("[B]"),
Tile::Empty => disp.push_str("[ ]"),
}
disp.push_str(reset);
}
disp.push_str(" ");
for j in 0..4 as usize
{
disp.push_str(red);
match self.get_board(Color::Black, Color::Black).unwrap().get_state()[i][j]
{
Tile::White => disp.push_str("[W]"),
Tile::Black => disp.push_str("[B]"),
Tile::Empty => disp.push_str("[ ]"),
}
disp.push_str(reset);
}
disp.push_str("\n");
}
return String::from(disp);
}

pub fn generate_url() -> String {
let s: String = rand::thread_rng()
.sample_iter(&Alphanumeric)
Expand Down

0 comments on commit b8d20fd

Please sign in to comment.