Created
March 15, 2024 11:14
-
-
Save NoodleSushi/187a6597b1d82c4a9cc92d6408d9bd53 to your computer and use it in GitHub Desktop.
A cursed implementation of TicTacToe in Rust
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::io; | |
struct Board(u32, String); | |
impl Board { | |
fn new() -> Board { Board(0, "".to_string()) } | |
fn run(&mut self) -> () { | |
loop { | |
loop { | |
self.0 &= 0x0FFFFF; | |
println!(" a b c\n ┌───┬───┬───┐"); | |
while self.0 & 0xF00000 < 0x900000 { | |
if (self.0 & 0xF00000) % 0x300000 == 0x000000 { print!(" {} │ ", 3 - self.0 / 0x300000) } else { print!(" │ ") } | |
print!("{}", if self.0 >> 0o11 >> (self.0 >> 0o24) & 1 != 0 { if self.0 >> (self.0 >> 0o24) & 1 == 0 {'O'} else {'X'} } else {' '}); | |
if (self.0 & 0xF00000) % 0x300000 == 0x200000 { print!(" │ {}\n", 3 - self.0 / 0x300000); if self.0 & 0xF00000 < 0x800000 { println!(" ├───┼───┼───┤") } } | |
self.0 += 0x100000; | |
} | |
println!(" └───┴───┴───┘\n a b c"); | |
if self.0 >> 0o23 & 1 == 1 { println!("{} has won!", if self.0 >> 0o22 & 1 == 0 { 'O' } else { 'X' }); break } | |
else if self.0 >> 0o11 & 0x1FF == 0x1FF { println!("It's a draw!"); break } | |
else { self.0 ^= 1 << 0o22; println!("It is {}'s turn!", if self.0 >> 0o22 & 1 == 0 { 'O' } else { 'X' }) } | |
println!("Please enter a cell to place your piece in (e.g. a1):"); | |
loop { | |
self.1.clear(); | |
io::stdin().read_line(&mut self.1).unwrap_or_default(); | |
self.1 = self.1.trim().to_string(); | |
if self.1.len() > 2 { continue } | |
self.0 = self.0 & 0x000FFFFF | (self.1.chars().nth(0).unwrap_or_default() as u32) << 0o30; | |
match self.0 >> 0o30 { 0x61..=0x63 => { self.0 |= (self.0 >> 0o30) - 0x61 << 0o24 }, _ => { continue } } | |
self.0 = self.0 & 0x00FFFFFF | (self.1.chars().nth(1).unwrap_or_default() as u32) << 0o30; | |
match self.0 >> 0o30 { 0x31..=0x33 => { self.0 += (2 - ((self.0 >> 0o30) - 0x31)) * 3 << 0o24 }, _ => { continue } } | |
if self.0 >> 0o11 >> (self.0 >> 0o24 & 0xF) & 1 == 0 { break } | |
} | |
self.0 |= 1 << 0o11 + (self.0 >> 0o24 & 0xF); | |
if self.0 >> 0o22 & 1 == 1 { self.0 |= 1 << (self.0 >> 0o24 & 0xF) } | |
for win in [0b001001001, 0b010010010,0b100100100, 0b000000111, 0b000111000, 0b111000000, 0b100010001, 0b001010100] as [u16; 8] { | |
if (self.0 >> 0o11 & 0x1FF) as u16 & win == win && matches!((self.0 & 0x1FF) as u16 & win, x if x == win || x == 0) { self.0 |= 1 << 0o23 } | |
} | |
print!("\n\n\n"); | |
} | |
println!("Would you like to play again? (y/n)"); | |
loop { | |
self.1.clear(); | |
io::stdin().read_line(&mut self.1).unwrap_or_default(); | |
self.1 = self.1.trim().to_string(); | |
if self.1.len() > 1 || !matches!(self.1.chars().nth(0).unwrap_or_default(), 'y' | 'n') { continue } | |
break; | |
} | |
if self.1.chars().nth(0).unwrap_or_default() == 'n' { break } | |
self.0 = 0; | |
} | |
} | |
} | |
fn main() { Board::new().run() } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment