-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgemini_game.jsx
164 lines (137 loc) · 3.71 KB
/
gemini_game.jsx
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import React from "react";
import classNames from "classnames";
const NONE = 0
const GREEN = 1
const YELLOW = 2
const BLACK = 3
const WHITE = 4
class Edge {
constructor({prev, next, side}) {
this.prev = prev
this.next = next
this.side = side
this.color = 0
if (next) next.prev = this
}
makeNext(close = false) {
return this.next = new Edge({
prev: this,
side: this.side && this.side.next,
next: close && this.getFirst(this) || undefined
})
}
makeLast() {
return this.makeNext(true)
}
getFirst(ref) {
if (!this.prev) {
return this
} else if (this.prev === ref) {
throw new Error('Cannot get first link of a ring!')
} else {
return this.prev.getFirst(ref)
}
}
move() {
if (this.side && this.side.color) {
if (this.color && !this.next.color) {
this.next.color = this.side.color
this.side.color = this.color
this.color = NONE
return true
}
if (!this.color && this.next.color) {
this.color = this.side.color
this.side.color = this.next.color
this.next.color = NONE
return true
}
}
return false
}
setRowColor(color) {
this.color = this.side.color = this.next.color = color
}
}
class Square {
constructor(inscribed) {
this.inscribed = inscribed
const edge1 = new Edge({
side: inscribed && inscribed.edges[0],
})
const edge2 = edge1.makeNext()
const edge3 = edge2.makeNext()
const edge4 = edge3.makeLast()
this.edges = [edge1, edge2, edge3, edge4]
}
makeCircumscribed() {
return this.circumscribed = new Square(this)
}
getDepth() {
return this.inscribed && this.inscribed.getDepth() + 1 || 1
}
}
class GeminiGame extends React.Component {
constructor(props) {
super(props)
const square1 = new Square()
.makeCircumscribed()
.makeCircumscribed()
.makeCircumscribed()
this.state = { square: square1 }
this.initGame(1)
}
initGame(number) {
const pivotal = this.state.square.edges[0]
switch (number) {
case 1:
pivotal.color = GREEN
pivotal.prev.side.color = GREEN
pivotal.prev.prev.side.side.color = GREEN
pivotal.next.side.color = YELLOW
pivotal.next.next.color = YELLOW
pivotal.prev.side.side.color = YELLOW
pivotal.next.next.side.color = BLACK
pivotal.next.side.side.color = BLACK
pivotal.next.side.side.side.color = BLACK
pivotal.next.next.side.side.side.color = WHITE
break;
default:
pivotal.setRowColor(GREEN)
pivotal.next.side.setRowColor(YELLOW)
pivotal.next.next.side.side.setRowColor(BLACK)
pivotal.side.side.side.color = WHITE
pivotal.prev.side.side.side.color = WHITE
}
this.setState({square: this.state.square})
}
render() {
return (
<div className='gemini-game'>
<GeminiSquare square={this.state.square} />
</div>)
}
}
class GeminiSquare extends React.Component {
constructor(props) {
super(props)
this.state = { square: props.square }
}
render() {
const inscribed = this.state.square.inscribed
return (
<div className={classNames('gemini-square', {innermost: !inscribed})}>
<ol className='edges'>
{this.state.square.edges.map((edge, index) =>
<li
className={classNames('edge', `edge-${index+1}`, `color-${edge.color}`)}
onClick={() => {this.state.square.edges[index].move(); this.setState({square: this.state.square})}}
key={index}>
</li>
)}
</ol>
{inscribed && <GeminiSquare square={inscribed} />}
</div>)
}
}
export default GeminiGame