-
Notifications
You must be signed in to change notification settings - Fork 19
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
Showing
41 changed files
with
642 additions
and
45 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package = [] |
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 @@ | ||
package = [] |
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,5 @@ | ||
.env | ||
*.avm | ||
*.prover | ||
*.verifier | ||
outputs/ |
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,13 @@ | ||
# board.aleo | ||
|
||
## Build Guide | ||
|
||
To compile this Aleo program, run: | ||
```bash | ||
snarkvm build | ||
``` | ||
|
||
To execute this Aleo program, run: | ||
```bash | ||
snarkvm run hello | ||
``` |
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,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; |
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,6 @@ | ||
{ | ||
"program": "board.aleo", | ||
"version": "0.0.0", | ||
"description": "", | ||
"license": "MIT" | ||
} |
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,4 @@ | ||
// The program input for board/src/main.leo | ||
[main] | ||
public a: u32 = 1u32; | ||
b: u32 = 2u32; |
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 @@ | ||
package = [] |
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,6 @@ | ||
{ | ||
"program": "board.aleo", | ||
"version": "0.0.0", | ||
"description": "", | ||
"license": "MIT" | ||
} |
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,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, | ||
}; | ||
} | ||
} |
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
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,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 = [] |
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,5 @@ | ||
.env | ||
*.avm | ||
*.prover | ||
*.verifier | ||
outputs/ |
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,13 @@ | ||
# move.aleo | ||
|
||
## Build Guide | ||
|
||
To compile this Aleo program, run: | ||
```bash | ||
snarkvm build | ||
``` | ||
|
||
To execute this Aleo program, run: | ||
```bash | ||
snarkvm run hello | ||
``` |
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,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; |
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,6 @@ | ||
{ | ||
"program": "move.aleo", | ||
"version": "0.0.0", | ||
"description": "", | ||
"license": "MIT" | ||
} |
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,4 @@ | ||
// The program input for move/src/main.leo | ||
[main] | ||
public a: u32 = 1u32; | ||
b: u32 = 2u32; |
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 @@ | ||
package = [] |
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,6 @@ | ||
{ | ||
"program": "move.aleo", | ||
"version": "0.0.0", | ||
"description": "", | ||
"license": "MIT" | ||
} |
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,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, | ||
}; | ||
} | ||
} | ||
|
||
|
Oops, something went wrong.