-
Notifications
You must be signed in to change notification settings - Fork 57
Battleship board game implementation in Turtle #246
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
base: master
Are you sure you want to change the base?
Changes from 1 commit
9791ef7
20e18ec
7eb6895
7e3bbb9
51ded43
258591e
098b5b0
92128b1
bd9e640
8974d8d
4a36f02
dcd8c53
658d185
dcdf18b
3a53c82
d3c5d3c
5acbc11
2451f94
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,13 +1,9 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| use crate::ship::ShipKind; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| use std::ops::Deref; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| use std::{fmt::Display, ops::Deref}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #[derive(Debug, Copy, Clone, PartialEq)] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| pub enum Cell { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Carrier, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Battleship, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Cruiser, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Submarine, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Destroyer, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Ship(ShipKind), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// clear cell on ShipGrid | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Empty, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// clear cell on AttackGrid | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -18,18 +14,28 @@ pub enum Cell { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| Destroyed, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+5
to
+15
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it would make sense to nest ship kind here:
Suggested change
That way you have one duplication less when adding a new ship. Of course this change needs some adjustments in the rest of the code - but it should be easy with rust-analyzer as guide :)
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's neat! |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| impl ShipKind { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| pub fn to_cell(self) -> Cell { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| impl Display for Cell { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| match self { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ShipKind::Carrier => Cell::Carrier, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ShipKind::Battleship => Cell::Battleship, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ShipKind::Cruiser => Cell::Cruiser, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ShipKind::Submarine => Cell::Submarine, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ShipKind::Destroyer => Cell::Destroyer, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Cell::Ship(ShipKind::Carrier) => write!(f, "C"), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Cell::Ship(ShipKind::Battleship) => write!(f, "B"), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Cell::Ship(ShipKind::Cruiser) => write!(f, "R"), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Cell::Ship(ShipKind::Submarine) => write!(f, "S"), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Cell::Ship(ShipKind::Destroyer) => write!(f, "D"), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Cell::Empty | Cell::Unattacked => write!(f, "."), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Cell::Missed => write!(f, ","), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Cell::Bombed => write!(f, "*"), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Cell::Destroyed => write!(f, "#"), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| impl ShipKind { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| pub fn to_cell(self) -> Cell { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Cell::Ship(self) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #[derive(Debug, Copy, Clone)] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| pub struct Grid([[Cell; 10]; 10]); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -40,6 +46,18 @@ impl Deref for Grid { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| impl Display for Grid { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for i in 0..10 { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for j in 0..10 { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| write!(f, "{}", self.get(&(j, i)))? | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| writeln!(f)? | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Ok(()) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| impl Grid { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| pub fn new(cell: Cell) -> Self { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Self { 0: [[cell; 10]; 10] } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not really necessary but sometimes it is more readable to nest matches in that case:
But as I said in this case its perfectly readable so no need to change.