Advertisement
loloof64

chess_board.rs

Aug 11th, 2022
1,378
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 4.38 KB | None | 0 0
  1. use pleco::{board::Board, File, Piece, Player, Rank, SQ};
  2. use vizia::prelude::*;
  3.  
  4. #[derive(Lens)]
  5. pub struct ChessBoard {
  6.     board_state: Board,
  7.     white_turn: bool,
  8.     pieces: [[Piece; 8]; 8],
  9. }
  10.  
  11. impl ChessBoard {
  12.     pub fn new<'a>(cx: &'a mut Context /* todo pass init parameters */) -> Handle<'a, Self> {
  13.        let board = Board::from_fen("rnbqkbnr/pp1ppppp/8/2p5/4P3/5N2/PPPP1PPP/RNBQKB1R b KQkq - 1 2").expect("Bad board position.");
  14.        Self {
  15.            board_state: board,
  16.            white_turn: board.turn() == Player::White,
  17.            pieces: get_pieces_from_board(board),
  18.        }
  19.        .build(cx, |cx| {
  20.            let turn_color = ChessBoard::white_turn.map(|turn| {
  21.                if *turn {
  22.                    Color::white()
  23.                } else {
  24.                    Color::black()
  25.                }
  26.            });
  27.            HStack::new(cx, |cx| {
  28.                for i in 0..=9 {
  29.                    for j in 0..=9 {
  30.                        let bg_color = if i > 0 && i < 9 && j > 0 && j < 9 {
  31.                            if (i % 2 == 0) ^ (j % 2 == 0) {
  32.                                Color::rgb(125, 47, 31)
  33.                            } else {
  34.                                Color::rgb(200, 200, 120)
  35.                            }
  36.                        } else {
  37.                            Color::rgb(20, 60, 200)
  38.                        };
  39.  
  40.                        if i > 0 && i < 9 && j > 0 && j < 9 {
  41.                            let file = i - 1 as u8;
  42.                            let rank = 9 - j as u8;
  43.  
  44.                            let piece = ChessBoard::pieces[rank as usize][file as usize];
  45.                        }
  46.  
  47.                        Element::new(cx)
  48.                            .col_index(i)
  49.                            .row_index(j)
  50.                            .background_color(bg_color);
  51.                    }
  52.                }
  53.  
  54.                Element::new(cx)
  55.                    .col_index(9)
  56.                    .row_index(9)
  57.                    .background_color(turn_color)
  58.                    .border_radius(Percentage(50.0));
  59.            })
  60.            .layout_type(LayoutType::Grid)
  61.            .grid_rows(vec![
  62.                Stretch(1.0),
  63.                Stretch(2.0),
  64.                Stretch(2.0),
  65.                Stretch(2.0),
  66.                Stretch(2.0),
  67.                Stretch(2.0),
  68.                Stretch(2.0),
  69.                Stretch(2.0),
  70.                Stretch(2.0),
  71.                Stretch(1.0),
  72.            ])
  73.            .grid_cols(vec![
  74.                Stretch(1.0),
  75.                Stretch(2.0),
  76.                Stretch(2.0),
  77.                Stretch(2.0),
  78.                Stretch(2.0),
  79.                Stretch(2.0),
  80.                Stretch(2.0),
  81.                Stretch(2.0),
  82.                Stretch(2.0),
  83.                Stretch(1.0),
  84.            ])
  85.            .background_color(Color::rgb(20, 60, 200));
  86.        })
  87.        .focusable(false)
  88.    }
  89. }
  90.  
  91. impl View for ChessBoard {
  92.    fn event(&mut self, cx: &mut EventContext, event: &mut Event) {
  93.        event.map(|window_event, meta| match window_event {
  94.            WindowEvent::TriggerDown { .. } => {}
  95.  
  96.            WindowEvent::TriggerUp { .. } => {}
  97.  
  98.            _ => {}
  99.        });
  100.    }
  101. }
  102.  
  103. fn build_file(file: u8) -> Result<File, String> {
  104.    match file {
  105.        0 => Ok(File::A),
  106.        1 => Ok(File::B),
  107.        2 => Ok(File::C),
  108.        3 => Ok(File::D),
  109.        4 => Ok(File::E),
  110.        5 => Ok(File::F),
  111.        6 => Ok(File::G),
  112.        7 => Ok(File::H),
  113.        _ => Err(format!("Not available file {}.", file)),
  114.    }
  115. }
  116.  
  117. fn build_rank(rank: u8) -> Result<Rank, String> {
  118.    match rank {
  119.        0 => Ok(Rank::R1),
  120.        1 => Ok(Rank::R2),
  121.        2 => Ok(Rank::R3),
  122.        3 => Ok(Rank::R4),
  123.        4 => Ok(Rank::R5),
  124.        5 => Ok(Rank::R6),
  125.        6 => Ok(Rank::R7),
  126.        7 => Ok(Rank::R8),
  127.    }
  128. }
  129.  
  130. fn get_pieces_from_board(board: Board) -> [[Piece; 8]; 8] {
  131.    let mut result = [[Piece::None; 8]; 8];
  132.  
  133.    for row in 0..=7 {
  134.        for col in 0..=7 {
  135.            let file = col;
  136.            let rank = row;
  137.  
  138.            let pleco_file = build_file(file).unwrap();
  139.            let pleco_rank = build_rank(rank).unwrap();
  140.  
  141.            result[rank as usize][file as usize] = board.piece_at_sq(SQ::make(pleco_file, pleco_rank));
  142.        }
  143.    }
  144.  
  145.    result
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement