|
1 | 1 | //! Fission game
|
2 | 2 |
|
3 | 3 | use crate::{
|
| 4 | + drawing::svg::{self, ImmSvg, Svg}, |
4 | 5 | grid::{small_bit_grid::SmallBitGrid, FiniteGrid, Grid},
|
5 | 6 | short::partizan::partizan_game::PartizanGame,
|
6 | 7 | };
|
7 | 8 | use cgt_derive::Tile;
|
8 |
| -use std::{fmt::Display, hash::Hash, str::FromStr}; |
| 9 | +use std::{ |
| 10 | + fmt::{self, Display}, |
| 11 | + hash::Hash, |
| 12 | + str::FromStr, |
| 13 | +}; |
9 | 14 |
|
10 | 15 | /// Tile in the game of Fission
|
11 | 16 | #[derive(Debug, Hash, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Tile)]
|
@@ -105,6 +110,64 @@ where
|
105 | 110 | }
|
106 | 111 | }
|
107 | 112 |
|
| 113 | +impl<G> Svg for Fission<G> |
| 114 | +where |
| 115 | + G: Grid<Item = Tile> + FiniteGrid, |
| 116 | +{ |
| 117 | + fn to_svg<W>(&self, buf: &mut W) -> fmt::Result |
| 118 | + where |
| 119 | + W: fmt::Write, |
| 120 | + { |
| 121 | + // Chosen arbitrarily |
| 122 | + let tile_size = 48; |
| 123 | + let grid_width = 4; |
| 124 | + |
| 125 | + let offset = grid_width / 2; |
| 126 | + let svg_width = self.grid.width() as u32 * tile_size + grid_width; |
| 127 | + let svg_height = self.grid.height() as u32 * tile_size + grid_width; |
| 128 | + |
| 129 | + ImmSvg::new(buf, svg_width, svg_height, |buf| { |
| 130 | + for y in 0..self.grid.height() { |
| 131 | + for x in 0..self.grid.width() { |
| 132 | + match self.grid.get(x, y) { |
| 133 | + Tile::Empty => { |
| 134 | + ImmSvg::rect( |
| 135 | + buf, |
| 136 | + (x as u32 * tile_size + offset) as i32, |
| 137 | + (y as u32 * tile_size + offset) as i32, |
| 138 | + tile_size, |
| 139 | + tile_size, |
| 140 | + "white", |
| 141 | + )?; |
| 142 | + } |
| 143 | + Tile::Stone => { |
| 144 | + let circle = svg::Circle { |
| 145 | + cx: (x as u32 * tile_size + offset + tile_size / 2) as i32, |
| 146 | + cy: (y as u32 * tile_size + offset + tile_size / 2) as i32, |
| 147 | + r: tile_size / 3, |
| 148 | + stroke: "black".to_owned(), |
| 149 | + stroke_width: 2, |
| 150 | + fill: "gray".to_owned(), |
| 151 | + }; |
| 152 | + ImmSvg::circle(buf, &circle)?; |
| 153 | + } |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + let grid = svg::Grid { |
| 159 | + x1: 0, |
| 160 | + y1: 0, |
| 161 | + x2: svg_width as i32, |
| 162 | + y2: svg_height as i32, |
| 163 | + grid_width, |
| 164 | + tile_size, |
| 165 | + }; |
| 166 | + ImmSvg::grid(buf, &grid) |
| 167 | + }) |
| 168 | + } |
| 169 | +} |
| 170 | + |
108 | 171 | #[cfg(test)]
|
109 | 172 | mod tests {
|
110 | 173 | use super::*;
|
|
0 commit comments