Skip to content

Commit

Permalink
sorta working
Browse files Browse the repository at this point in the history
  • Loading branch information
Maneren committed Jun 15, 2021
0 parents commit a572fc6
Show file tree
Hide file tree
Showing 7 changed files with 760 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/target
23 changes: 23 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"version": "0.2.0",
"configurations": [
{
"type": "lldb",
"request": "launch",
"name": "Debug executable 'gomoku'",
"cargo": {
"args": [
"build",
"--bin=gomoku",
"--package=gomoku"
],
"filter": {
"name": "gomoku",
"kind": "bin"
}
},
"args": ["test.txt", "x"],
"cwd": "${workspaceFolder}"
}
]
}
83 changes: 83 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "gomoku"
version = "0.1.0"
authors = ["Maneren <[email protected]>"]
edition = "2018"

[dependencies]
rand = "0.8.3"
130 changes: 130 additions & 0 deletions src/board.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
use std::error;
use std::fmt;

#[derive(Debug)]
pub struct BoardError {
msg: String,
}

impl fmt::Display for BoardError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.msg)
}
}

impl error::Error for BoardError {
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
None
}
}

pub type Tile = Option<bool>;
pub type TilePointer = (usize, usize);


pub struct Board {
pub data: Vec<Vec<Tile>>,
}

impl Board {
pub fn new(input_data: Vec<Vec<char>>) -> Result<Board, BoardError> {
if input_data.len() != 10 {
return Err(BoardError {
msg: "Invalid board height".into(),
});
}

for i in 0..input_data.len() {
if input_data.get(i).unwrap().len() != 10 {
return Err(BoardError {
msg: format!("Invalid board width on row {}", i + 1),
});
}
}

let data = input_data
.iter()
.map(|row| {
row
.iter()
.map(|tile| {
if *tile == 'x' {
Some(true)
} else if *tile == 'o' {
Some(false)
} else {
None
}
})
.collect()
})
.collect();

Ok(Board { data })
}

pub fn from_string(input_string: &str) -> Result<Board, BoardError> {
// split string into Vec<Vec<chars>>
let rows = input_string
.trim()
.split('\n')
.map(|row| row.chars().collect())
.collect::<Vec<Vec<char>>>();

// parse Vec<Vec<char>> into Vec<Vec<Tile>>
let parsed_data = rows
.iter()
.map(|row| row.iter().map(|ch| ch.to_owned()).collect())
.collect();

let board = Board::new(parsed_data)?;

Ok(board)
}

pub fn get_tile(&self, ptr: TilePointer) -> &Tile {
let (x, y) = ptr;
&self.data[y][x]
}

pub fn set_tile(&mut self, ptr: TilePointer, value: Tile) {
let (x, y) = ptr;
self.data[y][x] = value;
}
}

impl std::clone::Clone for Board {
fn clone(&self) -> Self {
Board {
data: self.data.clone(),
}
}
}

impl fmt::Display for Board {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut string = String::from(" 0123456789\n");
for i in 0..self.data.len() {
let row = &self.data[i];
string.push_str(&format!("{:?}", i));
string.push_str(
&(row
.iter()
.map(|field| match field {
Some(val) => {
if *val {
'x'
} else {
'o'
}
}
None => '-',
})
.collect::<String>()
+ "\n"),
);
}

write!(f, "{}", string)
}
}
Loading

0 comments on commit a572fc6

Please sign in to comment.