-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgame.fe
109 lines (94 loc) · 3.76 KB
/
game.fe
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
use std::context::Context
use ingot::challenges::errors::{InvalidIndex, NotMovable}
use ingot::registry::lock_validator::ILockValidator
const EMPTY_FIELD_MARKER: u256 = 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: u256 = 666
pub contract Game {
lock_validator: ILockValidator
board: Array<u256, 16>
moves: Map<u256, Array<u256, 4>>
pub fn __init__(mut self, lock_validator: address, board: Array<u256, 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[0] = [1, 4, INVALID_FIELD_MARKER, INVALID_FIELD_MARKER]
self.moves[1] = [0, 2, 5, INVALID_FIELD_MARKER]
self.moves[2] = [1, 3, 6, INVALID_FIELD_MARKER]
self.moves[3] = [2, 7, INVALID_FIELD_MARKER, INVALID_FIELD_MARKER]
self.moves[4] = [0, 5, 8, INVALID_FIELD_MARKER]
self.moves[5] = [1, 4, 6, 9]
self.moves[6] = [2, 5, 7, 10]
self.moves[7] = [3, 6, 11, INVALID_FIELD_MARKER]
self.moves[8] = [4, 9, 12, INVALID_FIELD_MARKER]
self.moves[9] = [5, 8, 10, 13]
self.moves[10] = [6, 9, 11, 14]
self.moves[11] = [7, 10, 15, INVALID_FIELD_MARKER]
self.moves[12] = [8, 13, INVALID_FIELD_MARKER, INVALID_FIELD_MARKER]
self.moves[13] = [9, 12, 14, INVALID_FIELD_MARKER]
self.moves[14] = [10, 13, 15, INVALID_FIELD_MARKER]
self.moves[15] = [11, 14, INVALID_FIELD_MARKER, INVALID_FIELD_MARKER]
}
pub fn get_board(self) -> Array<u256, 16>{
return self.board.to_mem()
}
pub fn is_solved(self) -> bool{
let mut index: u256 = 0
let current_board: Array<u256, 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: u256){
self.lock_validator.validate_owns_lock(owner: ctx.msg_sender())
if not self.is_valid_index(index) {
revert InvalidIndex()
}
else {
let movable_fields: Array<u256, 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: u256 = self.get_index_of_empty_field()
let field_value: u256 = self.board[index]
self.board[index] = EMPTY_FIELD_MARKER
self.board[empty_field_index] = field_value
}
}
}
fn get_index_of_empty_field(self) -> u256{
let mut index: u256 = 0
let current_board: Array<u256, 16> = self.board.to_mem()
for field in current_board {
if field == 0 {
break
} else {
index += 1
}
}
return index
}
fn get_winning_state() -> Array<u256, 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: u256) -> bool{
return index >= 0 and index <= 15
}
fn is_in(num: u256, values: Array<u256, 4>) -> bool{
for val in values {
if val == num {
return true
}
}
return false
}
}