-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0d91025
commit 25d1867
Showing
3 changed files
with
99 additions
and
14 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
use crate::arena::GameState; | ||
|
||
use super::{Node, NodeData}; | ||
|
||
|
||
|
||
#[derive(Debug, Clone)] | ||
pub struct CFRState { | ||
pub nodes: Vec<Node>, | ||
next_node_idx: usize, | ||
} | ||
|
||
impl CFRState { | ||
pub fn new(game_state: GameState) -> Self { | ||
CFRState { | ||
nodes: vec![Node::new_root(game_state)], | ||
next_node_idx: 1, | ||
} | ||
} | ||
|
||
pub fn add(&mut self, parent_idx: usize, data: NodeData) -> usize { | ||
let idx = self.next_node_idx; | ||
self.next_node_idx += 1; | ||
|
||
let node = Node::new(idx, parent_idx, data); | ||
self.nodes.push(node); | ||
|
||
idx | ||
} | ||
|
||
pub fn get(&self, idx: usize) -> Option<&Node> { | ||
self.nodes.get(idx) | ||
} | ||
|
||
pub fn get_mut(&mut self, idx: usize) -> Option<&mut Node> { | ||
self.nodes.get_mut(idx) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::arena::cfr::{NodeData, PlayerData}; | ||
|
||
use crate::arena::GameState; | ||
|
||
use super::CFRState; | ||
|
||
#[test] | ||
fn test_add_get_node() { | ||
// Create a | ||
let mut state = CFRState::new(GameState::new_starting( | ||
vec![100.0; 3], | ||
10.0, | ||
5.0, | ||
0.0, | ||
0, | ||
)); | ||
|
||
let player_idx: usize = state.add(0, NodeData::Player(PlayerData { player_idx: 0 })); | ||
|
||
let node = state.get(player_idx).unwrap().clone(); | ||
match node.data { | ||
NodeData::Player(data) => { | ||
assert_eq!(data.player_idx, 0); | ||
} | ||
_ => panic!("Expected player data"), | ||
} | ||
} | ||
} |