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
6239858c
Commit
6239858c
authored
Oct 28, 2016
by
Vadym Gidulian
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added ability to move pieces
parent
8a96882c
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
81 additions
and
6 deletions
+81
-6
script.js
script.js
+81
-6
No files found.
script.js
View file @
6239858c
...
...
@@ -26,6 +26,8 @@ const COLOR_PIECE_BLACK = '#000';
const
COLOR_PIECE_WHITE
=
'#fff'
;
const
game
=
{
state
:
''
,
stateData
:
{},
pieces
:
[
{
position
:
'A1'
,
color
:
'black'
,
isKing
:
false
},
{
position
:
'A3'
,
color
:
'black'
,
isKing
:
false
},
...
...
@@ -60,7 +62,24 @@ gameStart();
function
gameStart
()
{
init
();
game
.
state
=
'waiting'
;
gameUpdate
();
canvas
.
addEventListener
(
'mousedown'
,
function
(
e
)
{
var
position
=
getPositionByCoordinates
(
e
.
clientX
-
canvas
.
offsetLeft
,
e
.
clientY
-
canvas
.
offsetTop
);
if
(
position
)
{
moveStart
(
position
);
}
});
canvas
.
addEventListener
(
'mousemove'
,
function
(
e
)
{
moveGoing
(
e
.
clientX
-
canvas
.
offsetLeft
,
e
.
clientY
-
canvas
.
offsetTop
);
});
canvas
.
addEventListener
(
'mouseup'
,
function
(
e
)
{
var
position
=
getPositionByCoordinates
(
e
.
clientX
-
canvas
.
offsetLeft
,
e
.
clientY
-
canvas
.
offsetTop
);
if
(
position
)
{
moveEnd
(
position
);
}
});
}
function
gameUpdate
()
{
...
...
@@ -68,6 +87,40 @@ function gameUpdate() {
drawPieces
();
}
function
moveStart
(
position
)
{
if
(
game
.
state
==
'moving'
)
return
;
game
.
state
=
'moving'
;
game
.
stateData
.
piece
=
getPieceByPosition
(
position
);
game
.
stateData
.
originalPosition
=
game
.
stateData
.
piece
.
position
;
}
function
moveGoing
(
x
,
y
)
{
if
(
game
.
state
!=
'moving'
)
return
;
game
.
stateData
.
piece
.
position
=
x
+
','
+
y
;
gameUpdate
();
}
function
moveEnd
(
position
)
{
if
(
game
.
state
!=
'moving'
)
return
;
game
.
stateData
.
piece
.
position
=
position
;
game
.
stateData
=
{};
game
.
state
=
'waiting'
;
gameUpdate
();
}
function
getPieceByPosition
(
position
)
{
return
game
.
pieces
[
getPieceIndexByPosition
(
position
)];
}
function
getPieceIndexByPosition
(
position
)
{
return
game
.
pieces
.
map
(
function
(
piece
)
{
return
piece
.
position
;
}).
indexOf
(
position
);
}
// Graphics
function
init
()
{
...
...
@@ -113,11 +166,25 @@ function drawPieces() {
game
.
pieces
.
forEach
(
function
(
piece
)
{
drawPiece
(
piece
.
position
,
piece
.
color
,
piece
.
isKing
);
});
if
(
game
.
stateData
.
piece
)
{
drawPiece
(
game
.
stateData
.
piece
.
position
,
game
.
stateData
.
piece
.
color
,
game
.
stateData
.
piece
.
isKing
);
}
}
function
drawPiece
(
position
,
color
,
isKing
)
{
var
col
=
position
.
toUpperCase
().
charCodeAt
(
0
)
-
65
;
var
row
=
position
.
toUpperCase
().
charCodeAt
(
1
)
-
49
;
var
x
,
y
;
if
(
position
.
length
==
2
)
{
var
col
=
position
.
toUpperCase
().
charCodeAt
(
0
)
-
65
;
var
row
=
position
.
toUpperCase
().
charCodeAt
(
1
)
-
49
;
x
=
BORDER
+
SQUARE
/
2
+
col
*
SQUARE
;
y
=
BORDER
+
SQUARE
/
2
+
(
7
-
row
)
*
SQUARE
;
}
else
{
[
x
,
y
]
=
position
.
split
(
','
);
x
=
+
x
;
y
=
+
y
;
}
switch
(
color
)
{
case
'black'
:
...
...
@@ -130,15 +197,15 @@ function drawPiece(position, color, isKing) {
}
ctx
.
beginPath
();
ctx
.
arc
(
BORDER
+
SQUARE
/
2
+
col
*
SQUARE
,
BORDER
+
SQUARE
/
2
+
(
7
-
row
)
*
SQUARE
,
PIECE
/
2
,
0
,
2
*
Math
.
PI
);
ctx
.
arc
(
x
,
y
,
PIECE
/
2
,
0
,
2
*
Math
.
PI
);
ctx
.
fill
();
ctx
.
beginPath
();
ctx
.
arc
(
BORDER
+
SQUARE
/
2
+
col
*
SQUARE
,
BORDER
+
SQUARE
/
2
+
(
7
-
row
)
*
SQUARE
,
PIECE
/
2
,
0
,
2
*
Math
.
PI
);
ctx
.
arc
(
x
,
y
,
PIECE
/
2
,
0
,
2
*
Math
.
PI
);
ctx
.
stroke
();
ctx
.
beginPath
();
ctx
.
arc
(
BORDER
+
SQUARE
/
2
+
col
*
SQUARE
,
BORDER
+
SQUARE
/
2
+
(
7
-
row
)
*
SQUARE
,
PIECE
/
3
,
0
,
2
*
Math
.
PI
);
ctx
.
arc
(
x
,
y
,
PIECE
/
3
,
0
,
2
*
Math
.
PI
);
ctx
.
stroke
();
if
(
isKing
)
{
...
...
@@ -151,7 +218,15 @@ function drawPiece(position, color, isKing) {
}
ctx
.
beginPath
();
ctx
.
arc
(
BORDER
+
SQUARE
/
2
+
col
*
SQUARE
,
BORDER
+
SQUARE
/
2
+
(
7
-
row
)
*
SQUARE
,
PIECE
/
10
,
0
,
2
*
Math
.
PI
);
ctx
.
arc
(
x
,
y
,
PIECE
/
10
,
0
,
2
*
Math
.
PI
);
ctx
.
fill
();
}
}
function
getPositionByCoordinates
(
x
,
y
)
{
if
(
x
<
BORDER
||
y
<
BORDER
||
x
>
(
SIZE
-
BORDER
)
||
y
>
(
SIZE
-
BORDER
))
return
null
;
var
col
=
Math
.
floor
((
x
-
BORDER
)
/
SQUARE
);
var
row
=
Math
.
floor
((
y
-
BORDER
)
/
SQUARE
);
return
String
.
fromCharCode
(
col
+
65
)
+
String
.
fromCharCode
((
7
-
row
)
+
49
);
}
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