Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
checkers-canvas
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
Vadym Gidulian
checkers-canvas
Commits
b9a9afab
Commit
b9a9afab
authored
Oct 28, 2016
by
Vadym Gidulian
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added basic rules
parent
6239858c
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
59 additions
and
1 deletion
+59
-1
script.js
script.js
+59
-1
No files found.
script.js
View file @
b9a9afab
...
...
@@ -28,6 +28,7 @@ const COLOR_PIECE_WHITE = '#fff';
const
game
=
{
state
:
''
,
stateData
:
{},
turn
:
'black'
,
pieces
:
[
{
position
:
'A1'
,
color
:
'black'
,
isKing
:
false
},
{
position
:
'A3'
,
color
:
'black'
,
isKing
:
false
},
...
...
@@ -88,6 +89,7 @@ function gameUpdate() {
}
function
moveStart
(
position
)
{
if
(
!
getPieceByPosition
(
position
))
return
;
if
(
game
.
state
==
'moving'
)
return
;
game
.
state
=
'moving'
;
...
...
@@ -105,7 +107,13 @@ function moveGoing(x, y) {
function
moveEnd
(
position
)
{
if
(
game
.
state
!=
'moving'
)
return
;
if
(
move
(
game
.
stateData
.
piece
,
game
.
stateData
.
originalPosition
,
position
))
{
game
.
stateData
.
piece
.
position
=
position
;
becomeKing
(
game
.
stateData
.
piece
);
nextTurn
();
}
else
{
game
.
stateData
.
piece
.
position
=
game
.
stateData
.
originalPosition
;
}
game
.
stateData
=
{};
game
.
state
=
'waiting'
;
gameUpdate
();
...
...
@@ -121,6 +129,46 @@ function getPieceIndexByPosition(position) {
}).
indexOf
(
position
);
}
// Rules
function
move
(
piece
,
oldPosition
,
newPosition
)
{
if
(
piece
.
color
!=
game
.
turn
)
return
false
;
if
(
getPieceByPosition
(
newPosition
))
return
false
;
var
ox
=
oldPosition
.
charCodeAt
(
0
);
var
oy
=
oldPosition
.
charCodeAt
(
1
);
var
nx
=
newPosition
.
charCodeAt
(
0
);
var
ny
=
newPosition
.
charCodeAt
(
1
);
return
piece
.
color
==
'black'
&&
(
ny
-
oy
)
==
1
&&
Math
.
abs
(
nx
-
ox
)
==
1
||
piece
.
color
==
'black'
&&
(
ny
-
oy
)
==
2
&&
Math
.
abs
(
nx
-
ox
)
==
2
&&
beat
(
piece
,
fromCharCodes
((
ox
+
nx
)
/
2
,
(
oy
+
ny
)
/
2
))
||
piece
.
color
==
'white'
&&
(
oy
-
ny
)
==
1
&&
Math
.
abs
(
nx
-
ox
)
==
1
||
piece
.
color
==
'white'
&&
(
oy
-
ny
)
==
2
&&
Math
.
abs
(
nx
-
ox
)
==
2
&&
beat
(
piece
,
fromCharCodes
((
ox
+
nx
)
/
2
,
(
oy
+
ny
)
/
2
))
||
piece
.
isKing
&&
piece
.
color
==
'black'
&&
Math
.
abs
(
ny
-
oy
)
==
1
&&
Math
.
abs
(
nx
-
ox
)
==
1
||
piece
.
isKing
&&
piece
.
color
==
'black'
&&
Math
.
abs
(
ny
-
oy
)
==
2
&&
Math
.
abs
(
nx
-
ox
)
==
2
&&
beat
(
piece
,
fromCharCodes
((
ox
+
nx
)
/
2
,
(
oy
+
ny
)
/
2
))
||
piece
.
isKing
&&
piece
.
color
==
'white'
&&
Math
.
abs
(
oy
-
ny
)
==
1
&&
Math
.
abs
(
nx
-
ox
)
==
1
||
piece
.
isKing
&&
piece
.
color
==
'white'
&&
Math
.
abs
(
oy
-
ny
)
==
2
&&
Math
.
abs
(
nx
-
ox
)
==
2
&&
beat
(
piece
,
fromCharCodes
((
ox
+
nx
)
/
2
,
(
oy
+
ny
)
/
2
));
}
function
beat
(
piece
,
position
)
{
var
index
=
getPieceIndexByPosition
(
position
);
if
(
!~
index
)
return
false
;
if
(
piece
.
color
==
game
.
pieces
[
index
].
color
)
return
false
;
game
.
pieces
.
splice
(
index
,
1
);
return
true
;
}
function
becomeKing
(
piece
)
{
piece
.
isKing
=
piece
.
isKing
||
piece
.
color
==
'black'
&&
piece
.
position
[
1
]
==
'8'
||
piece
.
color
==
'white'
&&
piece
.
position
[
1
]
==
'1'
;
}
function
nextTurn
()
{
game
.
turn
=
game
.
turn
==
'black'
?
'white'
:
'black'
;
console
.
log
(
game
.
turn
);
}
// Graphics
function
init
()
{
...
...
@@ -230,3 +278,13 @@ function getPositionByCoordinates(x, y) {
return
String
.
fromCharCode
(
col
+
65
)
+
String
.
fromCharCode
((
7
-
row
)
+
49
);
}
// Utils
function
fromCharCodes
()
{
var
s
=
''
;
for
(
var
i
=
0
;
i
<
arguments
.
length
;
i
++
)
{
s
+=
String
.
fromCharCode
(
arguments
[
i
]);
}
return
s
;
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment