Fix tictactoe

This commit is contained in:
Pranav Gaddamadugu 2022-09-17 19:01:52 +02:00
parent c202878013
commit ab1218043a
2 changed files with 104 additions and 109 deletions

View File

@ -27,7 +27,11 @@ function bounded_iteration_interest(capital: u32,
if i < iterations { if i < iterations {
// Note that the added amount is rounded down. // Note that the added amount is rounded down.
amount += (amount * rate) / 100u32; amount += (amount * rate) / 100u32;
} // Skip the remaining iterations. } // Skip the remaining iterations.
if i == 40u8 {
return amount;
}
} }
return amount; return amount;
} }

View File

@ -4,17 +4,26 @@
// The state of the board is captured by a value of this circuit type. // The state of the board is captured by a value of this circuit type.
// This will work better with arrays, but for now we just expand the 9 cells. // This will work better with arrays, but for now we just expand the 9 cells.
circuit Row {
e1: u8,
e2: u8,
e3: u8
}
circuit Board { circuit Board {
// c(ell){row}{column} (0 for empty, 1 for X, 2 for O): r1: Row,
c11: u8, r2: Row,
c12: u8, r3: Row,
c13: u8, }
c21: u8,
c22: u8, // This function creates a new game board, with all cells empty.
c23: u8, @program
c31: u8, function new() -> Board {
c32: u8, return Board {
c33: u8 r1: Row { e1: 0u8, e2: 0u8, e3: 0u8 },
r2: Row { e1: 0u8, e2: 0u8, e3: 0u8 },
r3: Row { e1: 0u8, e2: 0u8, e3: 0u8 },
};
} }
// This function checks whether a player is a winner on a board. // This function checks whether a player is a winner on a board.
@ -23,20 +32,20 @@ circuit Board {
function win(b: Board, p: u8) -> bool { function win(b: Board, p: u8) -> bool {
return return
(b.c11 == p && b.c12 == p && b.c13 == p) || // row 1 (b.r1.e1 == p && b.r1.e2 == p && b.r1.e3 == p) || // row 1
(b.c21 == p && b.c22 == p && b.c23 == p) || // row 2 (b.r2.e1 == p && b.r2.e2 == p && b.r2.e3 == p) || // row 2
(b.c31 == p && b.c32 == p && b.c33 == p) || // row 3 (b.r3.e1 == p && b.r3.e3 == p && b.r3.e3 == p) || // row 3
(b.c11 == p && b.c21 == p && b.c31 == p) || // column 1 (b.r1.e1 == p && b.r2.e1 == p && b.r3.e1 == p) || // column 1
(b.c12 == p && b.c22 == p && b.c32 == p) || // column 2 (b.r1.e2 == p && b.r2.e3 == p && b.r3.e2 == p) || // column 2
(b.c13 == p && b.c23 == p && b.c33 == p) || // column 3 (b.r1.e3 == p && b.r2.e3 == p && b.r3.e3 == p) || // column 3
(b.c11 == p && b.c22 == p && b.c33 == p) || // diagonal (b.r1.e1 == p && b.r2.e2 == p && b.r3.e3 == p) || // diagonal
(b.c13 == p && b.c22 == p && b.c31 == p); // other diagonal (b.r1.e3 == p && b.r2.e2 == p && b.r3.e1 == p); // other diagonal
} }
// This carries out a single move: a player (1 for X, 2 for O) marking a cell. // This carries out a single move: a player (1 for X, 2 for O) marking a cell.
// The cell must be empty (i.e. contain 0), otherwise this is a no-op. // The cell must be empty (i.e. contain 0), otherwise this is a no-op.
// The u8 result is the winning player, of 0 if there is no winner (yet). // The u8 result is the winning player, of 0 if there is no winner (yet).
//
@program @program
function main(public player: u8, row: u8, col: u8, board: Board) -> (Board, u8) { function main(public player: u8, row: u8, col: u8, board: Board) -> (Board, u8) {
console.assert(player == 1u8 || player == 2u8); // 1 for X, 2 for O console.assert(player == 1u8 || player == 2u8); // 1 for X, 2 for O
@ -44,113 +53,95 @@ function main(public player: u8, row: u8, col: u8, board: Board) -> (Board, u8)
console.assert(1u8 <= col && col <= 3u8); console.assert(1u8 <= col && col <= 3u8);
let new: Board = board; let new: Board = board;
// This will work better when circuit assignment is supported. // This will work better when circuit assignment is supported.
if row == 1u8 && col == 1u8 && new.c11 == 0u8 { if row == 1u8 && col == 1u8 && new.r1.e1 == 0u8 {
new = Board { new = Board {
c11: player, r1: Row {
c12: new.c12, e1: player,
c13: new.c13, e2: new.r1.e2,
c21: new.c21, e3: new.r1.e3
c22: new.c22, },
c23: new.c23, r2: new.r2,
c31: new.c31, r3: new.r3
c32: new.c32,
c33: new.c33,
}; };
} else if row == 1u8 && col == 2u8 && new.c12 == 0u8 { } else if row == 1u8 && col == 2u8 && new.r1.e2 == 0u8 {
new = Board { new = Board {
c11: new.c11, r1: Row {
c12: player, e1: new.r1.e1,
c13: new.c13, e2: player,
c21: new.c21, e3: new.r1.e3
c22: new.c22, },
c23: new.c23, r2: new.r2,
c31: new.c31, r3: new.r3
c32: new.c32,
c33: new.c33,
}; };
} else if row == 1u8 && col == 3u8 && new.c13 == 0u8 { } else if row == 1u8 && col == 3u8 && new.r1.e3 == 0u8 {
new = Board { new = Board {
c11: new.c11, r1: Row {
c12: new.c12, e1: new.r1.e1,
c13: player, e2: new.r1.e2,
c21: new.c21, e3: player
c22: new.c22, },
c23: new.c23, r2: new.r2,
c31: new.c31, r3: new.r3
c32: new.c32,
c33: new.c33,
}; };
} else if row == 2u8 && col == 1u8 && new.c21 == 0u8 { } else if row == 2u8 && col == 1u8 && new.r2.e1 == 0u8 {
new = Board { new = Board {
c11: new.c11, r1: new.r1,
c12: new.c12, r2: Row {
c13: new.c13, e1: player,
c21: player, e2: new.r2.e2,
c22: new.c22, e3: new.r2.e3
c23: new.c23, },
c31: new.c31, r3: new.r3
c32: new.c32,
c33: new.c33,
}; };
} else if row == 2u8 && col == 2u8 && new.c22 == 0u8 { } else if row == 2u8 && col == 2u8 && new.r2.e2 == 0u8 {
new = Board { new = Board {
c11: new.c11, r1: new.r1,
c12: new.c12, r2: Row {
c13: new.c13, e1: new.r2.e1,
c21: new.c21, e2: player,
c22: player, e3: new.r2.e3
c23: new.c23, },
c31: new.c31, r3: new.r3
c32: new.c32,
c33: new.c33,
}; };
} else if row == 2u8 && col == 3u8 && new.c23 == 0u8 { } else if row == 2u8 && col == 3u8 && new.r2.e3 == 0u8 {
new = Board { new = Board {
c11: new.c11, r1: new.r1,
c12: new.c12, r2: Row {
c13: new.c13, e1: new.r2.e1,
c21: new.c21, e2: new.r2.e2,
c22: new.c22, e3: player
c23: player, },
c31: new.c31, r3: new.r3
c32: new.c32,
c33: new.c33,
}; };
} else if row == 3u8 && col == 1u8 && new.c31 == 0u8 { } else if row == 3u8 && col == 1u8 && new.r3.e1 == 0u8 {
new = Board { new = Board {
c11: new.c11, r1: new.r1,
c12: new.c12, r2: new.r2,
c13: new.c13, r3: Row {
c21: new.c21, e1: player,
c22: new.c22, e2: new.r3.e2,
c23: new.c23, e3: new.r3.e3
c31: player, }
c32: new.c32,
c33: new.c33,
}; };
} else if row == 3u8 && col == 2u8 && new.c32 == 0u8 { } else if row == 3u8 && col == 2u8 && new.r3.e2 == 0u8 {
new = Board { new = Board {
c11: new.c11, r1: new.r1,
c12: new.c12, r2: new.r2,
c13: new.c13, r3: Row {
c21: new.c21, e1: new.r3.e1,
c22: new.c22, e2: player,
c23: new.c23, e3: new.r3.e3
c31: new.c31, }
c32: player,
c33: new.c33,
}; };
} else if row == 3u8 && col == 3u8 && new.c33 == 0u8 { } else if row == 3u8 && col == 3u8 && new.r3.e3 == 0u8 {
new = Board { new = Board {
c11: new.c11, r1: new.r1,
c12: new.c12, r2: new.r2,
c13: new.c13, r3: Row {
c21: new.c21, e1: new.r3.e1,
c22: new.c22, e2: new.r3.e2,
c23: new.c23, e3: player
c31: new.c31, }
c32: new.c32,
c33: player,
}; };
} }
if win(new, 1u8) { if win(new, 1u8) {