Skip to content

Commit

Permalink
read pgns, parse moves
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewdhull committed Dec 7, 2020
1 parent 9c2f595 commit 2b859d5
Show file tree
Hide file tree
Showing 23 changed files with 553 additions and 74 deletions.
12 changes: 12 additions & 0 deletions .idea/chess.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions .idea/codeStyles/codeStyleConfig.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

79 changes: 79 additions & 0 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

73 changes: 52 additions & 21 deletions board.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,26 @@
button {
width: 50px;
}

#moves {
width: 400px;
line-height: 25px;
overflow-wrap: break-word;
}


</style>
</head>
<body>
<button id="start">Move</button>
<script src="pieces.js"></script>
<script src="lib/d3/d3.min.js"></script>
<button id="start">Move</button>
<div id="moves"></div>
<script src="scripts/pieces.js"></script>
<script src="d3-lib/d3/d3.min.js"></script>
<script>


const size = 8
const squareStrokeWidth = 3
const rectWidth = 50, rectHeight = rectWidth
const lightSquareColor = '#dfe0e2'
const darkSquareColor = '#a5a6a9'
const squareStrokeColor = '#2f292b'
var move = 0
var margin = {'right':30, 'left':3, 'top':3, 'bottom':30}


Expand All @@ -49,7 +52,7 @@
// scales + axes
var xScale = d3.scaleLinear()
.domain([25,50,400]).range([25,50,400])
let xAxisTickLabels = ['A','B','C','D','E','F','G','H'];
let xAxisTickLabels = ['a','b','c','d','e','f','g','h'];
xAxis = d3.axisBottom(xScale)
.tickFormat((d,i) => xAxisTickLabels[i])

Expand Down Expand Up @@ -173,7 +176,7 @@
.enter()
.append("text")
.attr('x', function(d){
return (Math.floor(d / size) * rectWidth) + 20.4
return (Math.floor(d / size) * rectWidth) + 21.5
})
.attr('y', function(d){
m = d % size
Expand All @@ -186,38 +189,66 @@
// add pieces
svg.append("g")
.selectAll("path")
.data(Array.from(Object.keys(start_positions)))
.data(Array.from(Object.keys(board)))
.enter()
.append("path")
.attr("id", function(d){
return d
})
.attr("transform", function(d){
return "translate("+
(positions[start_positions[d].position][0]-25)
(positions[board[d].position][0]-25)
+","+
(positions[start_positions[d].position][1]-24)
(positions[board[d].position][1]-24)
+")"
})
.attr("fill", function(d){
return start_positions[d].fill
return board[d].fill
})
.attr("stroke", function(d){
return start_positions[d].stroke
return board[d].stroke
})
.attr("d", function(d){
return start_positions[d].path
return board[d].path
})

// piece movement

// load a game
d3.json("data/pgn709.json", function (d) {
// return {
// any necessary parsing
// year: parseTime(d.year),
// running_total: +d.running_total
// }
}).then(function (data) {
d3.select("#moves")
.append("p")
.text(data.moves)

// piece movement
d3.select("#start").on("click", function() {
var newPosition = positions["E4"]
d3.select("#wkp")
this_move = data.moves[move]
var parsedMove = parseMove(move, this_move)
console.log(parsedMove)
var piece = parsedMove[0]
var nextPosition = parsedMove[1]
move +=1 //advance to next move of game

var newPosition = positions[nextPosition] //x,y coords
d3.select("#"+piece) //animate movement
.transition()
.attr("transform", "translate("+(newPosition[0]-25)+","+
(newPosition[1]-24)+")")
start_positions["wkp"].position["E4"]
});

// update state of the board
board[piece].position[nextPosition]

});

}).catch(function (error) {
console.log(error);
});


</script>
</body>
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 2b859d5

Please sign in to comment.