Skip to content

Commit

Permalink
My First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
pixelscape59 committed Nov 15, 2023
1 parent 1766c45 commit 6718474
Show file tree
Hide file tree
Showing 8 changed files with 622 additions and 0 deletions.
5 changes: 5 additions & 0 deletions tictactoe/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.env
*.avm
*.prover
*.verifier
outputs/
82 changes: 82 additions & 0 deletions tictactoe/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<!-- # ⭕ Tic-Tac-Toe -->

[//]: # (<img alt="workshop/tictactoe" width="1412" src="../.resources/tictactoe.png">)

A standard game of Tic-Tac-Toe in Leo.

⭕ ❕ ⭕ ❕ ❌

➖ ➕ ➖ ➕ ➖

⭕ ❕ ⁣❌ ❕ ⭕

➖ ➕ ➖ ➕ ➖

❌ ❕ ❌ ❕ ⭕

## Representing State
Leo allows users to define composite data types with the `struct` keyword.
The game board is represented by a struct called `Board`, which contains three `Row`s.
An alternative representation would be to use an array, however, these are not yet supported in Leo.

## Language Features
- `struct` declarations
- conditional statements
- early termination. Leo allows users to return from a function early using the `return` keyword.

## Running the Program

Leo provides users with a command line interface for compiling and running Leo programs.
Users may either specify input values via the command line or provide an input file in `inputs/`.

### Providing inputs via the command line.
1. Run
```bash
leo run <function_name> <input_1> <input_2> ...
```
See `./run.sh` for an example.


### Using an input file.
1. Modify `inputs/tictactoe.in` with the desired inputs.
2. Run
```bash
leo run <function_name>
```

## Executing the Program
```bash
leo execute <function_name> <input_1> <input_2> ...
```

## Playing the Game

### 1. Create a new game board
```bash
leo run new
```
| | | |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 0 | 0 |
| 0 | 0 | 0 |

### 2. Player 1 makes a move
```bash
leo run make_move 1u8 1u8 1u8 "{ r1: { c1: 0u8, c2: 0u8, c3: 0u8 }, r2: { c1: 0u8, c2: 0u8, c3: 0u8 }, r3: { c1: 0u8, c2: 0u8, c3: 0u8 } }"
```
| | | |
|---|---|---|
| 1 | 0 | 0 |
| 0 | 0 | 0 |
| 0 | 0 | 0 |

### 3. Player 2 makes a move
```bash
leo run make_move 2u8 2u8 2u8 "{ r1: { c1: 1u8, c2: 0u8, c3: 0u8 }, r2: { c1: 0u8, c2: 0u8, c3: 0u8 }, r3: { c1: 0u8, c2: 0u8, c3: 0u8 } }"
```
| | | |
|---|---|---|
| 1 | 0 | 0 |
| 0 | 2 | 0 |
| 0 | 0 | 0 |
237 changes: 237 additions & 0 deletions tictactoe/build/main.aleo
Original file line number Diff line number Diff line change
@@ -0,0 +1,237 @@
program tictactoe.aleo;

struct Row:
c1 as u8;
c2 as u8;
c3 as u8;

struct Board:
r1 as Row;
r2 as Row;
r3 as Row;


function new:
cast 0u8 0u8 0u8 into r0 as Row;
cast 0u8 0u8 0u8 into r1 as Row;
cast 0u8 0u8 0u8 into r2 as Row;
cast r0 r1 r2 into r3 as Board;
output r3 as Board.private;


closure check_for_win:
input r0 as Board;
input r1 as u8;
is.eq r0.r1.c1 r1 into r2;
is.eq r0.r1.c2 r1 into r3;
and r2 r3 into r4;
is.eq r0.r1.c3 r1 into r5;
and r4 r5 into r6;
is.eq r0.r2.c1 r1 into r7;
is.eq r0.r2.c2 r1 into r8;
and r7 r8 into r9;
is.eq r0.r2.c3 r1 into r10;
and r9 r10 into r11;
or r6 r11 into r12;
is.eq r0.r3.c1 r1 into r13;
is.eq r0.r3.c3 r1 into r14;
and r13 r14 into r15;
is.eq r0.r3.c3 r1 into r16;
and r15 r16 into r17;
or r12 r17 into r18;
is.eq r0.r1.c1 r1 into r19;
is.eq r0.r2.c1 r1 into r20;
and r19 r20 into r21;
is.eq r0.r3.c1 r1 into r22;
and r21 r22 into r23;
or r18 r23 into r24;
is.eq r0.r1.c2 r1 into r25;
is.eq r0.r2.c3 r1 into r26;
and r25 r26 into r27;
is.eq r0.r3.c2 r1 into r28;
and r27 r28 into r29;
or r24 r29 into r30;
is.eq r0.r1.c3 r1 into r31;
is.eq r0.r2.c3 r1 into r32;
and r31 r32 into r33;
is.eq r0.r3.c3 r1 into r34;
and r33 r34 into r35;
or r30 r35 into r36;
is.eq r0.r1.c1 r1 into r37;
is.eq r0.r2.c2 r1 into r38;
and r37 r38 into r39;
is.eq r0.r3.c3 r1 into r40;
and r39 r40 into r41;
or r36 r41 into r42;
is.eq r0.r1.c3 r1 into r43;
is.eq r0.r2.c2 r1 into r44;
and r43 r44 into r45;
is.eq r0.r3.c1 r1 into r46;
and r45 r46 into r47;
or r42 r47 into r48;
output r48 as boolean;


function make_move:
input r0 as u8.private;
input r1 as u8.private;
input r2 as u8.private;
input r3 as Board.private;
is.eq r0 1u8 into r4;
is.eq r0 2u8 into r5;
or r4 r5 into r6;
assert.eq r6 true;
lte 1u8 r1 into r7;
lte r1 3u8 into r8;
and r7 r8 into r9;
assert.eq r9 true;
lte 1u8 r2 into r10;
lte r2 3u8 into r11;
and r10 r11 into r12;
assert.eq r12 true;
is.eq r1 1u8 into r13;
is.eq r2 1u8 into r14;
and r13 r14 into r15;
is.eq r3.r1.c1 0u8 into r16;
and r15 r16 into r17;
is.eq r1 1u8 into r18;
is.eq r2 2u8 into r19;
and r18 r19 into r20;
is.eq r3.r1.c2 0u8 into r21;
and r20 r21 into r22;
is.eq r1 1u8 into r23;
is.eq r2 3u8 into r24;
and r23 r24 into r25;
is.eq r3.r1.c3 0u8 into r26;
and r25 r26 into r27;
is.eq r1 2u8 into r28;
is.eq r2 1u8 into r29;
and r28 r29 into r30;
is.eq r3.r2.c1 0u8 into r31;
and r30 r31 into r32;
is.eq r1 2u8 into r33;
is.eq r2 2u8 into r34;
and r33 r34 into r35;
is.eq r3.r2.c2 0u8 into r36;
and r35 r36 into r37;
is.eq r1 2u8 into r38;
is.eq r2 3u8 into r39;
and r38 r39 into r40;
is.eq r3.r2.c3 0u8 into r41;
and r40 r41 into r42;
is.eq r1 3u8 into r43;
is.eq r2 1u8 into r44;
and r43 r44 into r45;
is.eq r3.r3.c1 0u8 into r46;
and r45 r46 into r47;
is.eq r1 3u8 into r48;
is.eq r2 2u8 into r49;
and r48 r49 into r50;
is.eq r3.r3.c2 0u8 into r51;
and r50 r51 into r52;
is.eq r1 3u8 into r53;
is.eq r2 3u8 into r54;
and r53 r54 into r55;
is.eq r3.r3.c3 0u8 into r56;
and r55 r56 into r57;
ternary r57 r0 r3.r3.c3 into r58;
ternary r52 r0 r3.r3.c2 into r59;
ternary r52 r3.r3.c3 r58 into r60;
ternary r47 r0 r3.r3.c1 into r61;
ternary r47 r3.r3.c2 r59 into r62;
ternary r47 r3.r3.c3 r60 into r63;
ternary r42 r0 r3.r2.c3 into r64;
ternary r42 r3.r3.c1 r61 into r65;
ternary r42 r3.r3.c2 r62 into r66;
ternary r42 r3.r3.c3 r63 into r67;
ternary r37 r0 r3.r2.c2 into r68;
ternary r37 r3.r2.c3 r64 into r69;
ternary r37 r3.r3.c1 r65 into r70;
ternary r37 r3.r3.c2 r66 into r71;
ternary r37 r3.r3.c3 r67 into r72;
ternary r32 r0 r3.r2.c1 into r73;
ternary r32 r3.r2.c2 r68 into r74;
ternary r32 r3.r2.c3 r69 into r75;
ternary r32 r3.r3.c1 r70 into r76;
ternary r32 r3.r3.c2 r71 into r77;
ternary r32 r3.r3.c3 r72 into r78;
ternary r27 r0 r3.r1.c3 into r79;
ternary r27 r3.r2.c1 r73 into r80;
ternary r27 r3.r2.c2 r74 into r81;
ternary r27 r3.r2.c3 r75 into r82;
ternary r27 r3.r3.c1 r76 into r83;
ternary r27 r3.r3.c2 r77 into r84;
ternary r27 r3.r3.c3 r78 into r85;
ternary r22 r0 r3.r1.c2 into r86;
ternary r22 r3.r1.c3 r79 into r87;
ternary r22 r3.r2.c1 r80 into r88;
ternary r22 r3.r2.c2 r81 into r89;
ternary r22 r3.r2.c3 r82 into r90;
ternary r22 r3.r3.c1 r83 into r91;
ternary r22 r3.r3.c2 r84 into r92;
ternary r22 r3.r3.c3 r85 into r93;
ternary r17 r0 r3.r1.c1 into r94;
ternary r17 r3.r1.c2 r86 into r95;
ternary r17 r3.r1.c3 r87 into r96;
ternary r17 r3.r2.c1 r88 into r97;
ternary r17 r3.r2.c2 r89 into r98;
ternary r17 r3.r2.c3 r90 into r99;
ternary r17 r3.r3.c1 r91 into r100;
ternary r17 r3.r3.c2 r92 into r101;
ternary r17 r3.r3.c3 r93 into r102;
cast r94 r95 r96 into r103 as Row;
cast r97 r98 r99 into r104 as Row;
cast r100 r101 r102 into r105 as Row;
cast r103 r104 r105 into r106 as Board;
call check_for_win r106 1u8 into r107;
call check_for_win r106 2u8 into r108;
not r107 into r109;
and r109 r108 into r110;
ternary r110 r106.r1.c1 r106.r1.c1 into r111;
not r107 into r112;
and r112 r108 into r113;
ternary r113 r106.r1.c2 r106.r1.c2 into r114;
not r107 into r115;
and r115 r108 into r116;
ternary r116 r106.r1.c3 r106.r1.c3 into r117;
cast r111 r114 r117 into r118 as Row;
not r107 into r119;
and r119 r108 into r120;
ternary r120 r106.r2.c1 r106.r2.c1 into r121;
not r107 into r122;
and r122 r108 into r123;
ternary r123 r106.r2.c2 r106.r2.c2 into r124;
not r107 into r125;
and r125 r108 into r126;
ternary r126 r106.r2.c3 r106.r2.c3 into r127;
cast r121 r124 r127 into r128 as Row;
not r107 into r129;
and r129 r108 into r130;
ternary r130 r106.r3.c1 r106.r3.c1 into r131;
not r107 into r132;
and r132 r108 into r133;
ternary r133 r106.r3.c2 r106.r3.c2 into r134;
not r107 into r135;
and r135 r108 into r136;
ternary r136 r106.r3.c3 r106.r3.c3 into r137;
cast r131 r134 r137 into r138 as Row;
cast r118 r128 r138 into r139 as Board;
not r107 into r140;
and r140 r108 into r141;
ternary r141 2u8 0u8 into r142;
ternary r107 r106.r1.c1 r139.r1.c1 into r143;
ternary r107 r106.r1.c2 r139.r1.c2 into r144;
ternary r107 r106.r1.c3 r139.r1.c3 into r145;
cast r143 r144 r145 into r146 as Row;
ternary r107 r106.r2.c1 r139.r2.c1 into r147;
ternary r107 r106.r2.c2 r139.r2.c2 into r148;
ternary r107 r106.r2.c3 r139.r2.c3 into r149;
cast r147 r148 r149 into r150 as Row;
ternary r107 r106.r3.c1 r139.r3.c1 into r151;
ternary r107 r106.r3.c2 r139.r3.c2 into r152;
ternary r107 r106.r3.c3 r139.r3.c3 into r153;
cast r151 r152 r153 into r154 as Row;
cast r146 r150 r154 into r155 as Board;
ternary r107 1u8 r142 into r156;
output r155 as Board.private;
output r156 as u8.private;
6 changes: 6 additions & 0 deletions tictactoe/build/program.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"program": "tictactoe.aleo",
"version": "0.0.0",
"description": "",
"license": "MIT"
}
18 changes: 18 additions & 0 deletions tictactoe/inputs/tictactoe.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// The `new` function does not take any inputs.
[new]

// Inputs for the `make_move` function.
// - `player` : A u8 representing the player making the move. 1 for player 1, 2 for player 2.
// - `row` : A u8 representing the row to make the move in.
// - `column` : A u8 representing the column to make the move in.
// - `board` : A representation of the board state.
[make_move]
player: u8 = 1u8;
row: u8 = 1u8;
col: u8 = 1u8;
board: Board = Board {
r1: Row { c1: 0u8, c2: 0u8, c3: 0u8 },
r2: Row { c1: 0u8, c2: 0u8, c3: 0u8 },
r3: Row { c1: 0u8, c2: 0u8, c3: 0u8 },
};

6 changes: 6 additions & 0 deletions tictactoe/program.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"program": "tictactoe.aleo",
"version": "0.0.0",
"description": "",
"license": "MIT"
}
Loading

0 comments on commit 6718474

Please sign in to comment.