Cursed Esoteric let-Phobic Tic-Tac-Toe Implementation

Hiya! I've been learning Rust for a few days, got my hands dirty and wanted to share my quirky take of Tic-Tac-Toe in Rust just for the funsies lol.

rn it has 52 lines, much more if I haven't compressed it. I've avoided using let statements, only relying on the tuple struct's values. Relied on the struct's u32 value to store long-term and temporary values with bit shifting, and its String value for user input.

Tried to find a category for sharing snippets or projects here and this is the closest one I could think of, so if this topic isn't suited for this category feel free to close it! otherwise feel free to share dem thoughts kekek

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() }
2 Likes

Nice. Since the playground supports interactive sessions nowadays, it’s even easily tested out online … though (at least for me) the box drawing characters unfortunately don’t properly render mono-spaced there, so I’ve replaced them with the uglier but pragmatic alternative of ASCII art boxes: Rust Playground

1 Like

I'm not aware of a Rust equivalent to IOCCC (International Obfuscated C Code Contest). Perhaps you should start something like it for Rust. IORCC is not a very catchy name though.

(Recreational programming is something I enjoy reading, but haven't really done much with myself (apart from writing a few compilers and interpreters for esoteric languages).)

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.