Skip to content

Commit

Permalink
Merge branch 'component_system'
Browse files Browse the repository at this point in the history
  • Loading branch information
mbillingr committed Mar 10, 2018
2 parents 6d555f4 + d39e98a commit 4c1dbb9
Show file tree
Hide file tree
Showing 25 changed files with 1,716 additions and 1,278 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,7 @@ features = ["cargo-resource-root"]

[dependencies]
cpuprofiler = { version = "0.0.3", optional = true }
env_logger = "*"
log = "*"
rand = "*"
sdl2 = "*"
117 changes: 117 additions & 0 deletions src/ai/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
use std::collections::{BinaryHeap, HashSet};

use rules;
use types::{Stack, StackRole, Suite};

pub enum AiResult {
Unknown,
Winable(i32),
Lost
}

#[derive(Clone, Hash, Eq, PartialEq, Debug)]
pub struct AiState {
stacks: Vec<Stack>
}

impl AiState {
pub fn new(stacks: Vec<Stack>) -> AiState {
AiState {
stacks
}
}

pub fn astar(&self, mut iterations: usize) -> AiResult {
let mut queue = BinaryHeap::new();
queue.push((0, self.clone()));

let mut visited = HashSet::new();

while let Some((depth, state)) = queue.pop() {
if visited.contains(&state) {
continue
}

if rules::check_victory(state.stacks.iter()) {
return AiResult::Winable(depth)
}

iterations -= 1;
if iterations == 0 {
return AiResult::Unknown
}

visited.insert(state.clone());

let mut moves = rules::calc_possible_moves(state.stacks.iter());
for m in moves {
let mut newstate = state.apply_move(m);
queue.push((depth + 1, newstate));
}
}
AiResult::Lost
}

fn apply_move(&self, m: rules::Move) -> AiState {
let mut state = self.clone();
match m {
rules::Move::Button(_, t, s) => {
state.stacks[s[0]].pop_card();
state.stacks[s[1]].pop_card();
state.stacks[s[2]].pop_card();
state.stacks[s[3]].pop_card();
state.stacks[t].push_card(Suite::FaceDown);
state.stacks[t].push_card(Suite::FaceDown);
state.stacks[t].push_card(Suite::FaceDown);
state.stacks[t].push_card(Suite::FaceDown);
}
rules::Move::Cards(t, s, n) => {
let i = state.stacks[s].len() - n;
let tmp = state.stacks[s].split(i);
state.stacks[t].extend(tmp);
}
}
state
}

fn score(&self) -> i32 {
let mut score = 0;
for stack in &self.stacks {
match stack.role {
StackRole::Dragon => if let Some(Suite::FaceDown) = stack.top() {score += 100},
StackRole::Target => if let Some(Suite::Number(n, _)) = stack.top() {score += 10 * n as i32}
StackRole::Sorting => score += stack.score(),
_ => {}
}
}
score
}
}

impl Ord for AiState {
fn cmp(&self, other: &Self) -> ::std::cmp::Ordering {
self.score().cmp(&other.score())
}
}

impl PartialOrd for AiState {
fn partial_cmp(&self, other: &Self) -> Option<::std::cmp::Ordering> {
Some(self.cmp(other))
}
}

impl Stack {
fn score(&self) -> i32 {
let a = self.cards.iter();
let b = self.cards.iter().skip(1);
let mut score = 0;
for (l, u) in a.zip(b).rev() {
if rules::is_valid_pair(*l, *u) {
score += 1;
} else {
return score
}
}
score
}
}
170 changes: 0 additions & 170 deletions src/animation.rs

This file was deleted.

62 changes: 0 additions & 62 deletions src/button.rs

This file was deleted.

Loading

0 comments on commit 4c1dbb9

Please sign in to comment.