Skip to content

Commit

Permalink
Update examples
Browse files Browse the repository at this point in the history
  • Loading branch information
d0cd committed Mar 19, 2024
1 parent 71e294f commit 9b95ab3
Show file tree
Hide file tree
Showing 41 changed files with 642 additions and 45 deletions.
1 change: 1 addition & 0 deletions auction/leo.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package = []
6 changes: 4 additions & 2 deletions basic_bank/build/main.aleo
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ function deposit:
sub r0.amount r1 into r2;
cast r0.owner r2 into r3 as Token.record;
hash.bhp256 r0.owner into r4 as field;
async deposit r4 r1 into r5; output r3 as Token.record;
async deposit r4 r1 into r5;
output r3 as Token.record;
output r5 as basic_bank.aleo/deposit.future;

finalize deposit:
Expand Down Expand Up @@ -550,7 +551,8 @@ function withdraw:
hash.bhp256 r0 into r4 as field;
call calculate_interest r1 r2 r3 into r5;
cast r0 r5 into r6 as Token.record;
async withdraw r4 r1 into r7; output r6 as Token.record;
async withdraw r4 r1 into r7;
output r6 as Token.record;
output r7 as basic_bank.aleo/withdraw.future;

finalize withdraw:
Expand Down
1 change: 1 addition & 0 deletions basic_bank/leo.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package = []
5 changes: 5 additions & 0 deletions battleship/board/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.env
*.avm
*.prover
*.verifier
outputs/
13 changes: 13 additions & 0 deletions battleship/board/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# board.aleo

## Build Guide

To compile this Aleo program, run:
```bash
snarkvm build
```

To execute this Aleo program, run:
```bash
snarkvm run hello
```
46 changes: 46 additions & 0 deletions battleship/board/build/main.aleo
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
program board.aleo;

record board_state:
owner as address.private;
hits_and_misses as u64.private;
played_tiles as u64.private;
ships as u64.private;
player_1 as address.private;
player_2 as address.private;
game_started as boolean.private;


function new_board_state:
input r0 as u64.private;
input r1 as address.private;
cast self.caller 0u64 0u64 r0 self.caller r1 false into r2 as board_state.record;
output r2 as board_state.record;


function start_board:
input r0 as board_state.record;
not r0.game_started into r1;
assert.eq r1 true;
cast r0.owner r0.hits_and_misses r0.played_tiles r0.ships r0.player_1 r0.player_2 true into r2 as board_state.record;
output r2 as board_state.record;


function update_played_tiles:
input r0 as board_state.record;
input r1 as u64.private;
sub r1 1u64 into r2;
and r1 r2 into r3;
assert.eq r3 0u64;
and r1 r0.played_tiles into r4;
assert.eq r4 0u64;
or r0.played_tiles r1 into r5;
cast r0.owner r0.hits_and_misses r5 r0.ships r0.player_1 r0.player_2 r0.game_started into r6 as board_state.record;
output r6 as board_state.record;


function update_hits_and_misses:
input r0 as board_state.record;
input r1 as u64.private;
or r0.hits_and_misses r1 into r2;
cast r0.owner r2 r0.played_tiles r0.ships r0.player_1 r0.player_2 r0.game_started into r3 as board_state.record;
output r3 as board_state.record;
6 changes: 6 additions & 0 deletions battleship/board/build/program.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"program": "board.aleo",
"version": "0.0.0",
"description": "",
"license": "MIT"
}
4 changes: 4 additions & 0 deletions battleship/board/inputs/board.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// The program input for board/src/main.leo
[main]
public a: u32 = 1u32;
b: u32 = 2u32;
1 change: 1 addition & 0 deletions battleship/board/leo.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package = []
6 changes: 6 additions & 0 deletions battleship/board/program.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"program": "board.aleo",
"version": "0.0.0",
"description": "",
"license": "MIT"
}
110 changes: 110 additions & 0 deletions battleship/board/src/main.leo
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
program board.aleo {
// Battleship boards are represented by 8x8 squares.
// A u64 is all that is required to represent a hit or a miss on a single board.
// Starting from the top row, left to right, a hit is 1 and a miss is 0.
// A first move resulting in a hit in row 1, column 3 would be:
// 00100000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
// A second u64 is needed to represent which squares have been played, with 1s being played squares and 0s being
// unplayed squares.
record board_state {
owner: address,
// The hits and misses registered on the opponent's board.
hits_and_misses: u64,
// The squares that have been played on the opponent's board.
played_tiles: u64,
// The ship bitstring representing all ship positions on your own board
ships: u64,
player_1: address,
player_2: address,
game_started: bool,
}

// Returns a new board_state.
transition new_board_state(
ships: u64,
opponent: address,
) -> board_state {
return board_state {
owner: self.caller,
hits_and_misses: 0u64,
played_tiles: 0u64,
ships,
player_1: self.caller,
player_2: opponent,
game_started: false,
};
}

// Returns a new board state that has been started.
// Fails if this board has been started before.
transition start_board(
// The record of the board to start. A board can only be started once.
board: board_state,
) -> board_state {
// Ensure this board hasn't been used to start a game before.
assert(!board.game_started);

return board_state {
owner: board.owner,
hits_and_misses: board.hits_and_misses,
played_tiles: board.played_tiles,
ships: board.ships,
player_1: board.player_1,
player_2: board.player_2,
game_started: true,
};
}

// Returns a new board state record that includes all the played tiles.
// Fails if r1 has been played before.
transition update_played_tiles(
// The record of the board to update.
board: board_state,
// The u64 equivalent of a bitstring fire coordinate to send to the opponent.
shoot: u64,
) -> board_state {
// Need to make sure r1 is a valid move. Only one bit of r1 should be flipped.
let flip_bit: u64 = shoot - 1u64;
// bitwise and operation
let check_move: u64 = shoot & flip_bit;
assert_eq(check_move, 0u64);

// Need to make sure r1 is a valid move given the played_tiles. no bits should overlap.
let check_tiles: u64 = shoot & board.played_tiles;
assert_eq(check_tiles, 0u64);

// Update played tiles.
let played_tiles: u64 = board.played_tiles | shoot;

return board_state {
owner: board.owner,
hits_and_misses: board.hits_and_misses,
played_tiles,
ships: board.ships,
player_1: board.player_1,
player_2: board.player_2,
game_started: board.game_started,
};
}

// Returns a new board state record that includes all the hits and misses.
transition update_hits_and_misses(
// The record of the board to update.
board: board_state,
// The u64 equivalent of a bitstring of whether this player's previous move was a hit or miss.
hit_or_miss: u64,
) -> board_state {
// Update hits and misses.
let hits_and_misses: u64 = board.hits_and_misses | hit_or_miss;

return board_state {
owner: board.owner,
hits_and_misses,
played_tiles: board.played_tiles,
ships: board.ships,
player_1: board.player_1,
player_2: board.player_2,
game_started: board.game_started,
};
}
}
2 changes: 1 addition & 1 deletion battleship/build/imports/board.aleo
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ record board_state:
function new_board_state:
input r0 as u64.private;
input r1 as address.private;
cast self.signer 0u64 0u64 r0 self.signer r1 false into r2 as board_state.record;
cast self.caller 0u64 0u64 r0 self.caller r1 false into r2 as board_state.record;
output r2 as board_state.record;


Expand Down
2 changes: 1 addition & 1 deletion battleship/build/imports/move.aleo
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ function create_move:

function start_game:
input r0 as address.private;
cast r0 0u64 self.signer r0 0u64 into r1 as move.record;
cast r0 0u64 self.caller r0 0u64 into r1 as move.record;
output r1 as move.record;
19 changes: 18 additions & 1 deletion battleship/build/program.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,22 @@
"program": "battleship.aleo",
"version": "0.0.0",
"description": "",
"license": "MIT"
"license": "MIT",
"dependencies" : [
{
"name": "board.aleo",
"location": "local",
"path": "board"
},
{
"name": "move.aleo",
"location": "local",
"path": "move"
},
{
"name": "verify.aleo",
"location": "local",
"path": "verify"
}
]
}
20 changes: 20 additions & 0 deletions battleship/leo.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[[package]]
name = "board.aleo"
location = "local"
path = "/Users/pranav/work/Aleo/workshop/battleship/board"
checksum = "da94274230d0c0c3deb96d80e07ad9db8bbf53264286c14cc3231b7a8b7ef380"
dependencies = []

[[package]]
name = "move.aleo"
location = "local"
path = "/Users/pranav/work/Aleo/workshop/battleship/move"
checksum = "7d9fef5fe083eb24376e63935855e4ec709c17fb5ee46a0bb4594b0f9ef8eb08"
dependencies = []

[[package]]
name = "verify.aleo"
location = "local"
path = "/Users/pranav/work/Aleo/workshop/battleship/verify"
checksum = "2c2035ebd70500b7e5a9a6198bed1a1163cd1ddfd09128db8f4c16cf23ad2c62"
dependencies = []
5 changes: 5 additions & 0 deletions battleship/move/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.env
*.avm
*.prover
*.verifier
outputs/
13 changes: 13 additions & 0 deletions battleship/move/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# move.aleo

## Build Guide

To compile this Aleo program, run:
```bash
snarkvm build
```

To execute this Aleo program, run:
```bash
snarkvm run hello
```
24 changes: 24 additions & 0 deletions battleship/move/build/main.aleo
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
program move.aleo;

record move:
owner as address.private;
incoming_fire_coordinate as u64.private;
player_1 as address.private;
player_2 as address.private;
prev_hit_or_miss as u64.private;


function create_move:
input r0 as move.record;
input r1 as u64.private;
input r2 as u64.private;
is.eq r0.player_1 r0.owner into r3;
ternary r3 r0.player_2 r0.player_1 into r4;
cast r4 r1 r0.player_2 r0.player_1 r2 into r5 as move.record;
output r5 as move.record;


function start_game:
input r0 as address.private;
cast r0 0u64 self.caller r0 0u64 into r1 as move.record;
output r1 as move.record;
6 changes: 6 additions & 0 deletions battleship/move/build/program.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"program": "move.aleo",
"version": "0.0.0",
"description": "",
"license": "MIT"
}
4 changes: 4 additions & 0 deletions battleship/move/inputs/move.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// The program input for move/src/main.leo
[main]
public a: u32 = 1u32;
b: u32 = 2u32;
1 change: 1 addition & 0 deletions battleship/move/leo.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package = []
6 changes: 6 additions & 0 deletions battleship/move/program.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"program": "move.aleo",
"version": "0.0.0",
"description": "",
"license": "MIT"
}
46 changes: 46 additions & 0 deletions battleship/move/src/main.leo
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
program move.aleo {
record move {
owner: address,
incoming_fire_coordinate: u64,
player_1: address,
player_2: address,
// One flipped bit indicates a hit. No flipped bits indicates a miss.
prev_hit_or_miss: u64,
}

// Returns new move record owned by the opponent.
transition create_move(
// The move record created by the opponent.
move_record: move,
// The u64 representation of incoming_fire_coordinate, the bitstring fire coordinate to send to the opponent.
incoming_fire_coordinate: u64,
// The u64 representation of prev_hit_or_miss, this player's previous fire coordinate as a hit or miss.
prev_hit_or_miss: u64,
) -> move {
// A new move record should be created and owned by the opponent.
let one_is_owner: bool = move_record.player_1 == move_record.owner;
let opponent: address = one_is_owner ? move_record.player_2 : move_record.player_1;

return move {
owner: opponent,
incoming_fire_coordinate,
player_1: move_record.player_2,
player_2: move_record.player_1,
prev_hit_or_miss,
};
}

// Returns the move record owned by the opponent.
// Note, this move record contains dummy fire coordinates and previous hit or miss.
transition start_game(player_2: address) -> move {
return move {
owner: player_2,
incoming_fire_coordinate: 0u64,
player_1: self.caller,
player_2: player_2,
prev_hit_or_miss: 0u64,
};
}
}


Loading

0 comments on commit 9b95ab3

Please sign in to comment.