Skip to content

Commit 375b6b8

Browse files
committed
feat: add prisoners dilemma code
1 parent f976e66 commit 375b6b8

File tree

3 files changed

+1422
-135
lines changed

3 files changed

+1422
-135
lines changed

prisoners_dilemma/encrypted-ixs/src/lib.rs

Lines changed: 66 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,74 @@ use arcis_imports::*;
44
mod circuits {
55
use arcis_imports::*;
66

7-
pub struct InputValues {
8-
v1: u8,
9-
v2: u8,
7+
// Consider 0 - split, 1 - steal
8+
pub struct GameMoves {
9+
player_a_move: u8,
10+
player_b_move: u8,
1011
}
1112

1213
#[instruction]
13-
pub fn add_together(input_ctxt: Enc<Shared, InputValues>) -> Enc<Shared, u16> {
14-
let input = input_ctxt.to_arcis();
15-
let sum = input.v1 as u16 + input.v2 as u16;
16-
input_ctxt.owner.from_arcis(sum)
14+
pub fn init_game(mxe: Mxe) -> Enc<Mxe, GameMoves> {
15+
let game_moves = GameMoves {
16+
player_a_move: 2, // invalid move
17+
player_b_move: 2,
18+
};
19+
20+
mxe.from_arcis(game_moves)
21+
}
22+
23+
pub struct PlayerMove {
24+
player: u8,
25+
player_move: u8,
26+
}
27+
28+
#[instruction]
29+
pub fn player_move(
30+
player_move_ctxt: Enc<Shared, PlayerMove>,
31+
game_ctxt: Enc<Mxe, GameMoves>,
32+
) -> Enc<Mxe, GameMoves> {
33+
let player_move = player_move_ctxt.to_arcis();
34+
let mut game_moves = game_ctxt.to_arcis();
35+
36+
// Check which player is moving, if the player hasn't played their move yet, and the move is valid
37+
if player_move.player == 0 && game_moves.player_a_move == 2 && player_move.player_move < 2 {
38+
game_moves.player_a_move = player_move.player_move;
39+
} else if player_move.player == 1
40+
&& game_moves.player_b_move == 2
41+
&& player_move.player_move < 2
42+
{
43+
game_moves.player_b_move = player_move.player_move;
44+
}
45+
46+
game_ctxt.owner.from_arcis(game_moves)
47+
}
48+
49+
#[instruction]
50+
pub fn compare_moves(game_ctxt: Enc<Mxe, GameMoves>) -> u8 {
51+
let game_moves = game_ctxt.to_arcis();
52+
53+
// 0 - both players splits
54+
// 1 - player A steals
55+
// 2 - player B steals
56+
// 3 - both players steal
57+
// 4 - invalid move
58+
59+
let mut result = 4;
60+
61+
if game_moves.player_a_move == game_moves.player_b_move {
62+
if game_moves.player_a_move == 0 {
63+
//both split
64+
result = 0;
65+
} else if game_moves.player_a_move == 1 {
66+
// both steal
67+
result = 3;
68+
}
69+
} else if (game_moves.player_a_move == 1 && game_moves.player_b_move == 0) {
70+
result = 1; // Player A steals
71+
} else if (game_moves.player_a_move == 0 && game_moves.player_b_move == 1) {
72+
result = 2; // Player B steals
73+
}
74+
75+
result.reveal()
1776
}
1877
}

0 commit comments

Comments
 (0)