Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Dynamic Move Function Implementation #205

Merged
merged 9 commits into from
Feb 9, 2025
33 changes: 25 additions & 8 deletions onchain/src/constants.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -202,30 +202,47 @@ fn get_cap_colors() -> Array<felt252> {
array!['R', 'G', 'Y', 'B']
}

fn pos_reducer(data: Array<u32>, players_length: u32) -> Array<felt252> {
fn pos_reducer(data: Array<u32>, active_colors: Array<u8>) -> Array<felt252> {
let mut game: Array<felt252> = ArrayTrait::new();
let cap_colors = get_cap_colors();

let mut active_piece_count: u32 = 0;
let mut i: u32 = 0;
loop {
if i >= data.len() {
break;
}
if i < players_length * 4 {
let d = *data.at(i);
let color = *cap_colors.at((i / 4).try_into().unwrap());

let d = *data.at(i);

// Determine the color index based on the current index
let color_index = i / 4;
let piece_index = i % 4;

let mut active_colors_u32: Array<u32> = ArrayTrait::new();
for color in active_colors.clone() {
active_colors_u32.append((color).into());
};

// Check if the current color is in the active colors
if contains(active_colors_u32, color_index) {
let color_id = color_index;

let color_char = *cap_colors.at(color_id.into());

let value = if d == 0 {
// Format: color + "0" + (i % 4 + 1)
color * 100 + (i % 4 + 1).into()
let piece_offset = piece_index + 1;
color_char * 100 + piece_offset.into()
} else if d > 1000 {
// Format: color + (d % 1000)
color * 1000 + (d % 1000).into()
let remainder = d % 1000;
color_char * 1000 + remainder.into()
} else {
d.into()
};

game.append(value);

active_piece_count += 1;
}
i += 1;
};
Expand Down
150 changes: 21 additions & 129 deletions onchain/src/models/game.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ pub struct Game {
pub b0: felt252, // blue piece position on board
pub b1: felt252, // blue piece position on board
pub b2: felt252, // blue piece position on board
pub b3: felt252,
pub b3: felt252, // blue piece position on board
}

pub trait GameTrait {
Expand Down Expand Up @@ -138,134 +138,26 @@ impl GameImpl of GameTrait {
0_u32,
0_u32,
],
r0: match number_of_players {
0 => panic!("number of players cannot be 0"),
1 => panic!("number of players cannot be 1"),
2 => 'R01',
3 => 'R01',
4 => 'R01',
_ => panic!("invalid number of players"),
},
r1: match number_of_players {
0 => panic!("number of players cannot be 0"),
1 => panic!("number of players cannot be 1"),
2 => 'R02',
3 => 'R02',
4 => 'R02',
_ => panic!("invalid number of players"),
},
r2: match number_of_players {
0 => panic!("number of players cannot be 0"),
1 => panic!("number of players cannot be 1"),
2 => 'R03',
3 => 'R03',
4 => 'R03',
_ => panic!("invalid number of players"),
},
r3: match number_of_players {
0 => panic!("number of players cannot be 0"),
1 => panic!("number of players cannot be 1"),
2 => 'R04',
3 => 'R04',
4 => 'R04',
_ => panic!("invalid number of players"),
},
g0: match number_of_players {
0 => panic!("number of players cannot be 0"),
1 => panic!("number of players cannot be 1"),
2 => 'G01',
3 => 'G01',
4 => 'G01',
_ => panic!("invalid number of players"),
},
g1: match number_of_players {
0 => panic!("number of players cannot be 0"),
1 => panic!("number of players cannot be 1"),
2 => 'G02',
3 => 'G02',
4 => 'G02',
_ => panic!("invalid number of players"),
},
g2: match number_of_players {
0 => panic!("number of players cannot be 0"),
1 => panic!("number of players cannot be 1"),
2 => 'G03',
3 => 'G03',
4 => 'G03',
_ => panic!("invalid number of players"),
},
g3: match number_of_players {
0 => panic!("number of players cannot be 0"),
1 => panic!("number of players cannot be 1"),
2 => 'GO4',
3 => 'GO4',
4 => 'GO4',
_ => panic!("invalid number of players"),
},
y0: match number_of_players {
0 => panic!("number of players cannot be 0"),
1 => panic!("number of players cannot be 1"),
2 => 0,
3 => 'Y01',
4 => 'Y01',
_ => panic!("invalid number of players"),
},
y1: match number_of_players {
0 => panic!("number of players cannot be 0"),
1 => panic!("number of players cannot be 1"),
2 => 0,
3 => 'Y02',
4 => 'Y02',
_ => panic!("invalid number of players"),
},
y2: match number_of_players {
0 => panic!("number of players cannot be 0"),
1 => panic!("number of players cannot be 1"),
2 => 0,
3 => 'Y03',
4 => 'Y03',
_ => panic!("invalid number of players"),
},
y3: match number_of_players {
0 => panic!("number of players cannot be 0"),
1 => panic!("number of players cannot be 1"),
2 => 0,
3 => 'Y04',
4 => 'Y04',
_ => panic!("invalid number of players"),
},
b0: match number_of_players {
0 => panic!("number of players cannot be 0"),
1 => panic!("number of players cannot be 1"),
2 => 0,
3 => 0,
4 => 'B01',
_ => panic!("invalid number of players"),
},
b1: match number_of_players {
0 => panic!("number of players cannot be 0"),
1 => panic!("number of players cannot be 1"),
2 => 0,
3 => 0,
4 => 'B02',
_ => panic!("invalid number of players"),
},
b2: match number_of_players {
0 => panic!("number of players cannot be 0"),
1 => panic!("number of players cannot be 1"),
2 => 0,
3 => 0,
4 => 'B03',
_ => panic!("invalid number of players"),
},
b3: match number_of_players {
0 => panic!("number of players cannot be 0"),
1 => panic!("number of players cannot be 1"),
2 => 0,
3 => 0,
4 => 'B04',
_ => panic!("invalid number of players"),
},
// default tokens for red pieces
r0: 'R01',
r1: 'R02',
r2: 'R03',
r3: 'R04',
// Default tokens for green pieces
g0: 'G01',
g1: 'G02',
g2: 'G03',
g3: 'G04',
// Default tokens for yellow pieces
y0: 'Y01',
y1: 'Y02',
y2: 'Y03',
y3: 'Y04',
// Default tokens for blue pieces
b0: 'B01',
b1: 'B02',
b2: 'B03',
b3: 'B04',
}
}

Expand Down
Loading
Loading