Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gui #2

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
GUI with server
RealButterbee committed Oct 14, 2021

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit 405bf9ab0aebba02141b20031024ef76ee60f001
10 changes: 8 additions & 2 deletions chess_gui/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
GUI to move pieces.
GUI with network support.

Functions missing: proper move validation
It can be started in server mode with "cargo run -- --multi".
You can connect to the server and make moves with "d2d3-;", I'm going to change it to "move:d2d3-;"


NOT IMPLEMENTED (YET):
client
some of the standards we set up
21 changes: 0 additions & 21 deletions chess_gui/plan.txt

This file was deleted.

70 changes: 38 additions & 32 deletions chess_gui/src/chess_graphics_multi.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
use crate::fen_handler;
use chess;
use chess::square::Square;
use ggez;
use ggez::event::{self, EventHandler, MouseButton};
use ggez::event::{EventHandler, MouseButton};
use ggez::graphics;
use ggez::graphics::{Image, Mesh};
use ggez::graphics::Mesh;
use ggez::{Context, ContextBuilder, GameResult};
use std::io::prelude::*;
use std::net::{TcpListener, TcpStream};
use std::sync::mpsc::{self, channel, Receiver, Sender};
use std::thread;

use crate::network::Server;
//use crate::network::Server;

const board_letters: [&str; 8] = ["a", "b", "c", "d", "e", "f", "g", "h"];

@@ -38,6 +38,7 @@ pub struct ChessGame_multi {
turn_white: bool,
receiver: Receiver<String>,
response: Sender<String>,
last_move: String,
}

impl ChessGame_multi {
@@ -86,6 +87,7 @@ impl ChessGame_multi {
let update = true;
let window = false;
let turn_white = true;
let last_move = String::new();
ChessGame_multi {
game,
graphics_board,
@@ -96,6 +98,7 @@ impl ChessGame_multi {
turn_white,
receiver,
response,
last_move,
}
}
}
@@ -113,19 +116,21 @@ impl EventHandler<ggez::GameError> for ChessGame_multi {
if input.is_err() {
return Ok(());
}
let input = input.unwrap();
if input.starts_with("move") {
let ((from, i), (to, j)) = verify_input(&input).unwrap();

let ((from, i), (to, j)) = verify_input(&input.unwrap()).unwrap();

let pos1 = Square::from_i8((from as i8 * 8) + i as i8);
let pos2 = Square::from_i8((to as i8 * 8) + j as i8);
let pos1 = Square::from_i8((from as i8 * 8) + i as i8);
let pos2 = Square::from_i8((to as i8 * 8) + j as i8);

self.game.move_piece(pos1, pos2);
self.game.move_piece(pos1, pos2);

let none = "None".to_owned();
self.graphics_pieces[to][j] = self.graphics_pieces[from][i].clone();
self.graphics_pieces[from][i] = none;
self.update = true;
self.turn_white = true;
let none = "None".to_owned();
self.graphics_pieces[to][j] = self.graphics_pieces[from][i].clone();
self.graphics_pieces[from][i] = none;
self.update = true;
self.turn_white = true;
}
}

Ok(())
@@ -207,18 +212,20 @@ impl EventHandler<ggez::GameError> for ChessGame_multi {
if verify_move(self, x as i8, y as i8) {
move_piece_graphics(self, (self.selection.x, self.selection.y), (x, y));
//self.draw(ctx);
let response_string = format!(
"{}{}{}{}-;",
board_letters[self.selection.x],
self.selection.y + 1,
board_letters[x],
y + 1
);
println!("{}", response_string);
// let response_string = format!(
// "move::{}{}{}{}-;",
// board_letters[self.selection.x],
// self.selection.y + 1,
// board_letters[x],
// y + 1
// );
// println!("message sent {}", response_string);
let response_string =
fen_handler::to_fen(&self.graphics_pieces, "w".to_string());

println!("sent fen string: {}", response_string);
let send = self.response.send(response_string);
if send.is_ok() {
println!("sending of message was ok");
}

if send.is_err() {
panic!();
}
@@ -236,7 +243,6 @@ fn verify_move(game: &mut ChessGame_multi, x: i8, y: i8) -> bool {
let to = (x) + (8 * y);

let moves = game.game.board.calculate_all_moves();
println!("{:?}", moves);
if moves[from].contains(&to) {
game.game
.move_piece(Square::from_i8(from as i8), Square::from_i8(to as i8));
@@ -272,13 +278,13 @@ fn verify_input(input: &String) -> Result<((usize, usize), (usize, usize)), Stri

//Convertera user input till index
let table = ["a", "b", "c", "d", "e", "f", "g", "h"];
println!("{},{}", &&input[0..1], &&input[2..3]);
if !table.contains(&&input[0..1]) || !table.contains(&&input[2..3]) {
println!("{},{}", &&input[5..6], &&input[7..8]);
if !table.contains(&&input[5..6]) || !table.contains(&&input[7..8]) {
return Err("Invalid Input".to_owned());
}

//parse the digits and convert them to usize
let parsed = &input[1..2];
let parsed = &input[6..7];
let from: usize = match parsed.parse::<usize>() {
Ok(value) => value.to_owned() - 1,
Err(error) => {
@@ -287,7 +293,7 @@ fn verify_input(input: &String) -> Result<((usize, usize), (usize, usize)), Stri
};
//let from = from.to_owned();
//let from = from - 1;
let parsed = &input[3..4];
let parsed = &input[8..9];
let to: usize = match parsed.parse::<usize>() {
Ok(value) => value.to_owned() - 1,
Err(error) => {
@@ -296,7 +302,7 @@ fn verify_input(input: &String) -> Result<((usize, usize), (usize, usize)), Stri
}
};
//iterate digits from input
let i = table.iter().position(|&s| s == &input[0..1]).unwrap();
let j = table.iter().position(|&s| s == &input[2..3]).unwrap();
let i = table.iter().position(|&s| s == &input[5..6]).unwrap();
let j = table.iter().position(|&s| s == &input[7..8]).unwrap();
return Ok(((from, i), (to, j)));
}
147 changes: 106 additions & 41 deletions chess_gui/src/chess_graphics_multi_client.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
use crate::fen_handler;
use chess;
use chess::fen_parser::FenParser;
use chess::square::Square;
use ggez;
use ggez::event::{self, EventHandler, MouseButton};
use ggez::graphics;
use ggez::graphics::{Image, Mesh};
use ggez::{Context, ContextBuilder, GameResult};
use std::io;
use std::io::prelude::*;
use std::io::BufReader;
use std::net::{TcpListener, TcpStream};
use std::sync::mpsc::{self, channel, Receiver, Sender};
use std::thread;

use crate::network::Server;
//use crate::network::Server;

const board_letters: [&str; 8] = ["a", "b", "c", "d", "e", "f", "g", "h"];

@@ -36,11 +40,16 @@ pub struct ChessGame_multi {
update: bool,
window: bool,
turn_white: bool,
stream: TcpStream,
receiver: Receiver<String>,
response: Sender<String>,
}

impl ChessGame_multi {
pub fn new(_context: &mut Context, receiver: Receiver<String>) -> ChessGame_multi {
pub fn new(
_context: &mut Context,
receiver: Receiver<String>,
response: Sender<String>,
) -> ChessGame_multi {
let mut game = chess::game::Game::new("player1".to_string(), "player2".to_string());
game.initialize();
let mut graphics_board = [[graphics::Color::WHITE; 8]; 8];
@@ -81,23 +90,70 @@ impl ChessGame_multi {
let update = true;
let window = false;
let turn_white = true;
let mut stream = TcpStream::connect("127.0.0.1:1337").unwrap();
ChessGame_client {
ChessGame_multi {
game,
graphics_board,
graphics_pieces,
selection,
update,
window,
turn_white,
stream,
receiver,
response,
}
}
}

impl EventHandler<ggez::GameError> for ChessGame_multi {
fn update(&mut self, _ctx: &mut Context) -> GameResult<()> {
//Update code here...

if self.turn_white && !self.update {
let incoming = self.receiver.try_recv();
if incoming.is_err() {
return Ok(());
}
let incoming = incoming.unwrap();

let string = FenParser::decode(incoming[6..].to_string());
self.game.board.squares = string;
self.graphics_pieces = fen_handler::fen_parse(incoming);

//let ((from, i), (to, j)) = verify_input(&incoming).unwrap();

// let pos1 = Square::from_i8((from as i8 * 8) + i as i8);
// let pos2 = Square::from_i8((to as i8 * 8) + j as i8);
//
// self.game.move_piece(pos1, pos2);
//
// let none = "None".to_owned();
// self.graphics_pieces[to][j] = self.graphics_pieces[from][i].clone();
// self.graphics_pieces[from][i] = none;
self.update = true;
self.turn_white = false;

// while let Some(mssg) = bufsplit.next() {
// println!("message received");
// let mssg = String::from_utf8_lossy(&mssg.unwrap()).to_string();
// let mssg = mssg.replace('\n', "");
// if mssg.starts_with("move") {
// let ((from, i), (to, j)) = verify_input(&mssg).unwrap();
//
// let pos1 = Square::from_i8((from as i8 * 8) + i as i8);
// let pos2 = Square::from_i8((to as i8 * 8) + j as i8);
//
// self.game.move_piece(pos1, pos2);
//
// let none = "None".to_owned();
// self.graphics_pieces[to][j] = self.graphics_pieces[from][i].clone();
// self.graphics_pieces[from][i] = none;
// self.update = true;
// self.turn_white = false;
// break;
// }
// }
}
Ok(())
}

fn draw(&mut self, ctx: &mut Context) -> GameResult<()> {
@@ -176,6 +232,15 @@ impl EventHandler<ggez::GameError> for ChessGame_multi {
move_piece_graphics(self, (self.selection.x, self.selection.y), (x, y));
//self.draw(ctx);
self.turn_white = true;
let player_move = format!(
"{}{}{}{}",
board_letters[self.selection.x],
self.selection.y + 1,
board_letters[x],
y + 1
);
let message = format!("move:{}-;", player_move);
self.response.send(message);
}
self.update = true;
self.selection.selection = false;
@@ -187,7 +252,6 @@ fn verify_move(game: &mut ChessGame_multi, x: i8, y: i8) -> bool {
let to = (x) + (8 * y);

let moves = game.game.board.calculate_all_moves();
println!("{:?}", moves);
if moves[from].contains(&to) {
game.game
.move_piece(Square::from_i8(from as i8), Square::from_i8(to as i8));
@@ -216,37 +280,38 @@ fn draw_selection_square(ctx: &mut Context, x: usize, y: usize) -> Mesh {
square_mesh
}

fn verify_input(input: &String) -> Result<((usize, usize), (usize, usize)), String> {
if input.len() < 4 {
return Err("false input".to_owned());
}

//Convertera user input till index
let table = ["a", "b", "c", "d", "e", "f", "g", "h"];
if !table.contains(&&input[0..1]) || !table.contains(&&input[2..3]) {
return Err("Invalid Input".to_owned());
}

//parse the digits and convert them to usize
let parsed = &input[1..2];
let from: usize = match parsed.parse::<usize>() {
Ok(value) => value.to_owned() - 1,
Err(error) => {
return Err(error.to_string());
}
};
//let from = from.to_owned();
//let from = from - 1;
let parsed = &input[3..4];
let to: usize = match parsed.parse::<usize>() {
Ok(value) => value.to_owned() - 1,
Err(error) => {
println!("{}", error);
return Err("Internal Error".to_owned());
}
};
//iterate digits from input
let i = table.iter().position(|&s| s == &input[0..1]).unwrap();
let j = table.iter().position(|&s| s == &input[2..3]).unwrap();
return Ok(((from, i), (to, j)));
}
// fn verify_input(input: &String) -> Result<((usize, usize), (usize, usize)), String> {
// if input.len() < 4 {
// return Err("false input".to_owned());
// }
//
// //Convertera user input till index
// let table = ["a", "b", "c", "d", "e", "f", "g", "h"];
// println!("{},{}", &&input[7..8], &&input[9..10]);
// if !table.contains(&&input[7..8]) || !table.contains(&&input[9..10]) {
// return Err("Invalid Input".to_owned());
// }
//
// //parse the digits and convert them to usize
// let parsed = &input[8..9];
// let from: usize = match parsed.parse::<usize>() {
// Ok(value) => value.to_owned() - 1,
// Err(error) => {
// return Err(error.to_string());
// }
// };
// //let from = from.to_owned();
// //let from = from - 1;
// let parsed = &input[10..11];
// let to: usize = match parsed.parse::<usize>() {
// Ok(value) => value.to_owned() - 1,
// Err(error) => {
// println!("{}", error);
// return Err("Internal Error".to_owned());
// }
// };
// //iterate digits from input
// let i = table.iter().position(|&s| s == &input[7..8]).unwrap();
// let j = table.iter().position(|&s| s == &input[9..10]).unwrap();
// return Ok(((from, i), (to, j)));
// }
Loading