Skip to content

Commit

Permalink
✨ Add transfer army feature
Browse files Browse the repository at this point in the history
  • Loading branch information
bal7hazar committed Sep 17, 2023
1 parent 4f2a440 commit 9ea2328
Showing 1 changed file with 48 additions and 1 deletion.
49 changes: 48 additions & 1 deletion src/entities/tile.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ struct Tile {
mod Errors {
const INVALID_DISPATCHED: felt252 = 'Tile: invalid dispatched';
const INVALID_ARRAY: felt252 = 'Tile: invalid array';
const INVALID_OWNER: felt252 = 'Tile: invalid owner';
const INVALID_ARMY_TRANSFER: felt252 = 'Tile: invalid army transfer';
}

/// Trait to initialize, attack, defend and supply from the Tile.
/// Trait to initialize and manage army from the Tile.
trait TileTrait {
/// Returns a new `Tile` struct.
/// # Arguments
Expand All @@ -50,6 +52,12 @@ trait TileTrait {
/// * `self` - The tile.
/// * `army` - The army to supply.
fn supply(ref self: Tile, army: u8);
/// Transfers an army from the tile to another tile.
/// # Arguments
/// * `self` - The tile.
/// * `to` - The tile to transfer the army to.
/// * `army` - The army to transfer.
fn transfer(ref self: Tile, ref to: Tile, army: u8);
}

/// Implementation of the `TileTrait` for the `Tile` struct.
Expand Down Expand Up @@ -83,6 +91,17 @@ impl TileImpl of TileTrait {
fn supply(ref self: Tile, army: u8) {
self.army += army;
}

fn transfer(ref self: Tile, ref to: Tile, army: u8) {
// [Check] Both tiles are owned by the same player
assert(self.owner == to.owner, Errors::INVALID_OWNER);
// [Check] From tile army is greater than the transfered army
assert(self.army > army, Errors::INVALID_ARMY_TRANSFER);
// [Check] Both tiles are connected by a owned path
// TODO: when neighbors are defined and implemented
self.army -= army;
to.army += army;
}
}

/// Resolves a battle between two armies.
Expand Down Expand Up @@ -250,6 +269,34 @@ mod Tests {
assert(tile.army == 6, 'Tile: wrong tile army');
}

#[test]
#[available_gas(1_000_000)]
fn test_tile_transfer() {
let mut from = TileTrait::new(0, 4, 'a');
let mut to = TileTrait::new(0, 2, 'a');
from.transfer(ref to, 2);
assert(from.army == 2, 'Tile: wrong from army');
assert(to.army == 4, 'Tile: wrong to army');
}

#[test]
#[available_gas(1_000_000)]
#[should_panic(expected: ('Tile: invalid owner',))]
fn test_tile_transfer_revert_invalid_owner() {
let mut from = TileTrait::new(0, 4, 'a');
let mut to = TileTrait::new(0, 2, 'b');
from.transfer(ref to, 2);
}

#[test]
#[available_gas(1_000_000)]
#[should_panic(expected: ('Tile: invalid army transfer',))]
fn test_tile_transfer_revert_invalid_army_transfer() {
let mut from = TileTrait::new(0, 4, 'a');
let mut to = TileTrait::new(0, 2, 'a');
from.transfer(ref to, 5);
}

#[test]
#[available_gas(1_000_000)]
fn test_tile_attack_and_defend() {
Expand Down

0 comments on commit 9ea2328

Please sign in to comment.