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: Implement move() function #195

Merged
merged 3 commits into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
149 changes: 149 additions & 0 deletions onchain/src/constants.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,156 @@ pub enum TileNode {
B04,
}

fn get_safe_positions() -> Array<u32> {
array![
1, 9, 14, 22, 27, 35, 40, 48, 1001, 1002, 1003, 1004, 1005,
2001, 2002, 2003, 2004, 2005, 3001, 3002, 3003, 3004, 3005,
4001, 4002, 4003, 4004, 4005
]
}

fn get_markers() -> Array<felt252> {
array![
'r0', 'r1', 'r2', 'r3',
'g0', 'g1', 'g2', 'g3',
'y0', 'y1', 'y2', 'y3',
'b0', 'b1', 'b2', 'b3'
]
}

fn find_index(value: felt252, a: Array<felt252>) -> usize {
let mut i = 0;
loop {
if (i >= a.len()) {
break 0;
} else if (a.at(i) == @value) {
break i;
}
i += 1;
}
}

fn get_start_points() -> Array<u32> {
array![0, 13, 26, 39]
}

fn board_to_pos(arr: Array<u32>) -> Array<u32> {
let mut new_arr: Array<u32> = ArrayTrait::new();
let mut i: u32 = 0;

loop {
if i >= arr.len() {
break;
}

let val = *arr.at(i);
let color: u32 = i / 4;

let new_val = if val > 52 {
51 + (val % 1000)
} else if val == 0 {
0

} else {
let diff = if val >= *get_start_points().at(color) {
val - *get_start_points().at(color)
} else {
val + 52 - *get_start_points().at(color)
};

if diff < 1 {
diff + 52
} else {
diff
}
};

new_arr.append(new_val);
i += 1;
};

new_arr
}

fn pos_to_board(arr: Array<u32>) -> Array<u32> {
let mut new_arr: Array<u32> = ArrayTrait::new();
let mut i: u32 = 0;

loop {
if i >= arr.len() {
break;
}

let val = *arr.at(i);
let color: u32 = i / 4;

let new_val = if val > 51 {
(color + 1) * 1000 + (val % 50) - 1
} else if val == 0 {
0
} else {
let a = (*get_start_points().at(color) + val) % 52;
if a == 0 {
52
} else {
a
}
};

new_arr.append(new_val);
i += 1;
};

new_arr
}

fn zero_address() -> ContractAddress {
contract_address_const::<0x0>()
}

fn contains(array: Array<u32>, value: u32) -> bool {
let mut found = false;
for item in array {
if item == value {
found = true;
break;
}
};
return found;

}

fn get_cap_colors() -> Array<felt252> {
array!['R', 'G', 'Y', 'B']
}

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

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 value = if d == 0 {
// Format: color + "0" + (i % 4 + 1)
color * 100 + (i % 4 + 1).into()
} else if d > 1000 {
// Format: color + (d % 1000)
color * 1000 + (d % 1000).into()
} else {
d.into()
};

game.append(value);
}
i += 1;
};

game
}
90 changes: 46 additions & 44 deletions onchain/src/models/game.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -54,22 +54,23 @@ pub struct Game {
pub dice_face: u8, // Last value of dice thrown
pub player_chance: ContractAddress, // Next player to make move
pub has_thrown_dice: bool, // Whether the dice has been thrown or not
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, // blue piece position on board
pub g0: felt252, // green piece position on board
pub g1: felt252, // green piece position on board
pub g2: felt252, // green piece position on board
pub g3: felt252, // green piece position on board
pub game_condition: Array<u32>,
pub r0: felt252, // red piece position on board
pub r1: felt252, // red piece position on board
pub r2: felt252, // red piece position on board
pub r3: felt252, // red piece position on board
pub g0: felt252, // green piece position on board
pub g1: felt252, // green piece position on board
pub g2: felt252, // green piece position on board
pub g3: felt252, // green piece position on board
pub y0: felt252, // yellow piece position on board
pub y1: felt252, // yellow piece position on board
pub y2: felt252, // yellow piece position on board
pub y3: felt252, // yellow piece position on board
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, // blue piece position on board
}

pub trait GameTrait {
Expand Down Expand Up @@ -121,36 +122,37 @@ impl GameImpl of GameTrait {
dice_face: 0,
player_chance: zero_address.into(),
has_thrown_dice: false,
b0: match number_of_players {
game_condition: array![0_u32, 0_u32, 0_u32, 0_u32, 0_u32, 0_u32, 0_u32, 0_u32, 0_u32, 0_u32, 0_u32, 0_u32, 0_u32, 0_u32, 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 => 'B01',
3 => 'B01',
4 => 'B01',
2 => 'R01',
3 => 'R01',
4 => 'R01',
_ => panic!("invalid number of players")
},
b1: match 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 => 'B02',
3 => 'B02',
4 => 'B02',
2 => 'R02',
3 => 'R02',
4 => 'R02',
_ => panic!("invalid number of players")
},
b2: match 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 => 'B03',
3 => 'B03',
4 => 'B03',
2 => 'R03',
3 => 'R03',
4 => 'R03',
_ => panic!("invalid number of players")
},
b3: match 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 => 'B04',
3 => 'B04',
4 => 'B04',
2 => 'R04',
3 => 'R04',
4 => 'R04',
_ => panic!("invalid number of players")
},
g0: match number_of_players {
Expand Down Expand Up @@ -185,68 +187,68 @@ impl GameImpl of GameTrait {
4 => 'GO4',
_ => panic!("invalid number of players")
},
r0: match 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 => 'R01',
4 => 'R01',
3 => 'Y01',
4 => 'Y01',
_ => panic!("invalid number of players")
},
r1: match 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 => 'R02',
4 => 'R02',
3 => 'Y02',
4 => 'Y02',
_ => panic!("invalid number of players")
},
r2: match 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 => 'R03',
4 => 'R03',
3 => 'Y03',
4 => 'Y03',
_ => panic!("invalid number of players")
},
r3: match 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 => 'R04',
4 => 'R04',
3 => 'Y04',
4 => 'Y04',
_ => panic!("invalid number of players")
},
y0: match 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 => 'Y01',
4 => 'B01',
_ => panic!("invalid number of players")
},
y1: match 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 => 'Y02',
4 => 'B02',
_ => panic!("invalid number of players")
},
y2: match 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 => 'Y03',
4 => 'B03',
_ => panic!("invalid number of players")
},
y3: match 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 => 'Y04',
4 => 'B04',
_ => panic!("invalid number of players")
},
}
Expand Down
Loading
Loading