-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathgame_i8.fe
More file actions
109 lines (93 loc) · 3.8 KB
/
Copy pathgame_i8.fe
File metadata and controls
109 lines (93 loc) · 3.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
use std::context::Context
use ingot::challenges::errors::{InvalidIndex, NotMovable}
use ingot::registry::lock_validator::ILockValidator
const EMPTY_FIELD_MARKER: u8 = 0
// We don't have dynamically sized arrays. Since the number of moves
// varies depending on the position of the empty field, we need to mark
// the directions that point outside of the game field as invalid.
const INVALID_FIELD_MARKER: i8 = -1
pub contract GameI8 {
lock_validator: ILockValidator
board: Array<u8, 16>
moves: Map<u8, Array<i8, 4>>
pub fn __init__(mut self, lock_validator: address, board: Array<u8, 16>) {
self.lock_validator = ILockValidator(lock_validator)
self.board = board
// Map each empty slot to a list of possible moves
// It would be nice to have fixed-size maps that work with `const` (baked into code)
self.moves[u8(0)] = [1, 4, INVALID_FIELD_MARKER, INVALID_FIELD_MARKER]
self.moves[u8(1)] = [0, 2, 5, INVALID_FIELD_MARKER]
self.moves[u8(2)] = [1, 3, 6, INVALID_FIELD_MARKER]
self.moves[u8(3)] = [2, 7, INVALID_FIELD_MARKER, INVALID_FIELD_MARKER]
self.moves[u8(4)] = [0, 5, 8, INVALID_FIELD_MARKER]
self.moves[u8(5)] = [1, 4, 6, 9]
self.moves[u8(6)] = [2, 5, 7, 1]
self.moves[u8(7)] = [3, 6, 11, INVALID_FIELD_MARKER]
self.moves[u8(8)] = [4, 9, 12, INVALID_FIELD_MARKER]
self.moves[u8(9)] = [5, 8, 10, 13]
self.moves[u8(10)] = [6, 9, 11, 14]
self.moves[u8(11)] = [7, 10, 15, INVALID_FIELD_MARKER]
self.moves[u8(12)] = [8, 13, INVALID_FIELD_MARKER, INVALID_FIELD_MARKER]
self.moves[u8(13)] = [9, 12, 14, INVALID_FIELD_MARKER]
self.moves[u8(14)] = [10, 13, 15, INVALID_FIELD_MARKER]
self.moves[u8(15)] = [11, 14, INVALID_FIELD_MARKER, INVALID_FIELD_MARKER]
}
pub fn get_board(self) -> Array<u8, 16>{
return self.board.to_mem()
}
pub fn is_solved(self) -> bool{
let mut index: u256 = 0
let current_board: Array<u8, 16> = self.board.to_mem()
// Workaround for: https://github.com/ethereum/fe/issues/528
for _field in current_board {
if current_board[index] != get_winning_state()[index] {
return false
}
index += 1
}
return true
}
pub fn move_field(mut self, ctx: Context, index: u8){
self.lock_validator.validate_owns_lock(owner: ctx.msg_sender())
if not self.is_valid_index(index){
revert InvalidIndex()
} else {
let movable_fields: Array<i8, 4> = self.moves[self.get_index_of_empty_field()].to_mem()
if not is_in(num:index, values: movable_fields) {
revert NotMovable()
} else {
let empty_field_index: u8 = self.get_index_of_empty_field()
let field_value: u8 = self.board[u256(index)]
self.board[u256(index)] = EMPTY_FIELD_MARKER
self.board[u256(empty_field_index)] = field_value
}
}
}
fn get_index_of_empty_field(self) -> u8{
let mut index: u8 = 0
let current_board: Array<u8, 16> = self.board.to_mem()
for field in current_board {
if field == 0 {
break
} else {
index += 1
}
}
return index
}
fn get_winning_state() -> Array<u8, 16>{
// TODO: Make this a constant when complex constants are supported
return [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0]
}
fn is_valid_index(self, index: u8) -> bool{
return index >= 0 and index <= 15
}
fn is_in(num: u8, values: Array<i8, 4>) -> bool{
for val in values {
if val == i8(num) {
return true
}
}
return false
}
}