-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ErrorMessage.js
68 lines (66 loc) · 2.61 KB
/
ErrorMessage.js
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
import React from 'react'
import { checkers, eq, dist, direction } from 'aima-checkers'
export default class ErrorMessage extends React.Component {
message (type, state, y, x, player) {
let message
if (type === 'invalid move') {
// invalid click on a square
if (state.selectedChecker.length !== 2) {
message = 'Please select the checker you want to move.'
} else if ((y + x) % 2 === 1) {
message = 'You can only move diagonally – that is, to another black square.'
} else if ((y - state.selectedChecker[0]) * direction(state.state.player) <= 0) {
message = 'You can only move forward (unless you are royal).'
} else if (
checkers.actions(state.state).some(action =>
eq(action[0], state.selectedChecker) && dist(action[0], action[1]) / 2 === 2) &&
dist(state.selectedChecker, [y, x]) / 2 === 1
) {
message = 'There is a jump available, so you need to jump.' // the modal fallacy
} else {
message = 'You can only move one step at a time (unless you jump over your opponent\'s checker).'
}
} else if (type === 'invalid checker') {
// invalid click on a checker
if (state.state.player !== player) {
// checker has the wrong colour
if (state.selectedChecker.length === 2) {
// another checker is already clicked, so this is probably an attempted move
message = 'You can only move to an empty square.'
} else {
// no other checker is clicked, so this is probably an attempted checker selection
message = (state.state.player === 'p' ? ' Brown ' : ' Beige ') + ' is next.'
}
} else if (!checkers.actions(state.state).some(action => eq(action[0], [y, x]))) {
// checker has the right colour
if (
checkers.actions(state.state).some(action =>
dist(action[0], action[1]) / 2 === 2) &&
!checkers.actions(state.state).some(action =>
eq(action[0], [y, x]) && dist(action[0], action[1]) / 2 === 2)
) {
message = 'There is a jump available for one or more different checkers, so you need to perform one of these jumps.'
} else {
message = 'All moves of this checker are blocked.'
}
}
}
return message
}
render () {
const message = this.message(...this.props.error)
return message
? (
<div>
<p>
{message}
</p>
<div>
<span onClick={() => this.props.parentCallback('help')}>
Read the rules.
</span>
</div>
</div>)
: ''
}
}