-
Notifications
You must be signed in to change notification settings - Fork 5
Add 4-bit Two’s Complement mode #9
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
Changes from all commits
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 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -190,7 +190,13 @@ impl BinaryNumbersPuzzle { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Block::bordered().border_type(border_type).fg(border_color).render(area, buf); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let suggestion_str = format!("{suggestion}"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let suggestion_str = if self.bits.is_twos_complement() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Convert raw bit pattern to signed value for display | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let signed_val = self.bits.raw_to_signed(*suggestion); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| format!("{signed_val}") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| format!("{suggestion}") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #[allow(clippy::cast_possible_truncation)] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Paragraph::new(suggestion_str.to_string()) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -642,6 +648,7 @@ enum GuessResult { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #[derive(Clone)] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| pub enum Bits { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Four, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| FourTwosComplement, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| FourShift4, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| FourShift8, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| FourShift12, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -653,7 +660,11 @@ pub enum Bits { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| impl Bits { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| pub const fn to_int(&self) -> u32 { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| match self { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Self::Four | Self::FourShift4 | Self::FourShift8 | Self::FourShift12 => 4, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Self::Four | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| | Self::FourShift4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| | Self::FourShift8 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| | Self::FourShift12 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| | Self::FourTwosComplement => 4, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Self::Eight => 8, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Self::Twelve => 12, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Self::Sixteen => 16, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -662,6 +673,7 @@ impl Bits { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| pub const fn scale_factor(&self) -> u32 { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| match self { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Self::Four => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Self::FourTwosComplement => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Self::FourShift4 => 16, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Self::FourShift8 => 256, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Self::FourShift12 => 4096, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -673,6 +685,7 @@ impl Bits { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| pub const fn high_score_key(&self) -> u32 { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| match self { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Self::Four => 4, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Self::FourTwosComplement => 42, // separate key for two's complement | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Self::FourShift4 => 44, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Self::FourShift8 => 48, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Self::FourShift12 => 412, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -686,7 +699,11 @@ impl Bits { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| pub const fn suggestion_count(&self) -> usize { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| match self { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Self::Four | Self::FourShift4 | Self::FourShift8 | Self::FourShift12 => 3, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Self::Four | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| | Self::FourShift4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| | Self::FourShift8 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| | Self::FourShift12 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| | Self::FourTwosComplement => 3, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Self::Eight => 4, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Self::Twelve => 5, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Self::Sixteen => 6, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -695,6 +712,7 @@ impl Bits { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| pub const fn label(&self) -> &'static str { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| match self { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Self::Four => "4 bits", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Self::FourTwosComplement => "4 bits (Two's complement)", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Self::FourShift4 => "4 bits*16", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Self::FourShift8 => "4 bits*256", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Self::FourShift12 => "4 bits*4096", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -703,6 +721,21 @@ impl Bits { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Self::Sixteen => "16 bits", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// Convert raw bit pattern to signed value for two's complement mode | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| pub const fn raw_to_signed(&self, raw: u32) -> i32 { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| match self { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Self::FourTwosComplement => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // 4-bit two's complement: range -8 to +7 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if raw >= 8 { (raw as i32) - 16 } else { raw as i32 } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| _ => raw as i32, // other modes use unsigned | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| pub const fn is_twos_complement(&self) -> bool { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| matches!(self, Self::FourTwosComplement) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| pub struct BinaryNumbersPuzzle { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -725,21 +758,44 @@ impl BinaryNumbersPuzzle { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let mut suggestions = Vec::new(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let scale = bits.scale_factor(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| while suggestions.len() < bits.suggestion_count() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let raw = rng.random_range(0..u32::pow(2, bits.to_int())); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let num = raw * scale; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if !suggestions.contains(&num) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| suggestions.push(num); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if bits.is_twos_complement() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // For two's complement, generate unique raw bit patterns (0-15) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // For two's complement, generate unique raw bit patterns (0-15) | |
| // For two's complement, generate unique raw bit patterns in the range 0..16 (i.e., 0 to 15 inclusive) |
Copilot
AI
Nov 29, 2025
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.
[nitpick] The two's complement branch duplicates the same logic pattern as the else branch (lines 771-777) with only the scaling difference. Consider refactoring to eliminate duplication, such as: generate raw_values first, then apply scaling conditionally when building suggestions.
| if bits.is_twos_complement() { | |
| // For two's complement, generate unique raw bit patterns (0-15) | |
| let mut raw_values: Vec<u32> = Vec::new(); | |
| while raw_values.len() < bits.suggestion_count() { | |
| let raw = rng.random_range(0..u32::pow(2, bits.to_int())); | |
| if !raw_values.contains(&raw) { | |
| raw_values.push(raw); | |
| } | |
| } | |
| // Store raw bit patterns directly | |
| suggestions = raw_values; | |
| } else { | |
| // For unsigned modes | |
| while suggestions.len() < bits.suggestion_count() { | |
| let raw = rng.random_range(0..u32::pow(2, bits.to_int())); | |
| let num = raw * scale; | |
| if !suggestions.contains(&num) { | |
| suggestions.push(num); | |
| } | |
| } | |
| // Generate unique raw bit patterns for suggestions | |
| let mut raw_values: Vec<u32> = Vec::new(); | |
| while raw_values.len() < bits.suggestion_count() { | |
| let raw = rng.random_range(0..u32::pow(2, bits.to_int())); | |
| if !raw_values.contains(&raw) { | |
| raw_values.push(raw); | |
| } | |
| } | |
| // Apply scaling if not two's complement | |
| if bits.is_twos_complement() { | |
| suggestions = raw_values.clone(); | |
| } else { | |
| suggestions = raw_values.iter().map(|raw| raw * scale).collect(); |
Copilot
AI
Nov 29, 2025
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.
Corrected spelling of 'twos' to 'two's' in comment for consistency with the mode name.
| let current_number = suggestions[0]; // scaled value or raw for twos complement | |
| let current_number = suggestions[0]; // scaled value or raw for two's complement |
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.
The new
is_twos_complementhelper method lacks test coverage. Consider adding a test to verify it returns true only forFourTwosComplementand false for all other bit modes.