Skip to content

Commit a1e68f3

Browse files
Yasin YaqoobiYasin Yaqoobi
Yasin Yaqoobi
authored and
Yasin Yaqoobi
committedSep 8, 2016
chessboard is functional
1 parent dad7bef commit a1e68f3

24 files changed

+141
-82
lines changed
 

‎Models/Board.js

+39-10
Original file line numberDiff line numberDiff line change
@@ -5,37 +5,66 @@ export default class Board{
55

66
constructor(player1,player2){
77

8+
9+
this.defaultSettings = {
10+
selectors: ['#chessBoard'],
11+
colors:['#B58863', '#F0D9B5'],
12+
showlabels: true,
13+
sideBars: true,
14+
animate: true,
15+
showPath: true
16+
};
17+
818
this.squarePosition = {num: 8, alpha:97};
919
this.labelPosition = {num: 8, alpha: 97};
1020

11-
this.squares = player1.getPieces().concat(player2.getPieces());
12-
21+
this.setupSquares(player1,player2);
22+
23+
}
1324

14-
for (let i = 0; i < 64; i++){
25+
setupSquares(player1,player2){
26+
this.squares = []
27+
let count = 0;
28+
for (let i = 1; i < 65; i++){
1529

1630
let dataProp = this.getDataProp(i);
17-
18-
if (player1.getPieceAt(dataProp) === undefined && player2.getPieceAt(dataProp) === undefined){
19-
this.squares.push(new Piece(undefined,undefined,undefined,dataProp));
20-
}
31+
32+
let piece = player1.getPieceAt(dataProp) || player2.getPieceAt(dataProp);
33+
if (!piece){
34+
piece = new Piece(undefined,undefined,undefined,dataProp);
35+
}
36+
37+
piece.setBackground(this.getPieceBackground(count));
38+
this.squares.push(piece);
39+
count++;
2140
}
22-
41+
2342
}
2443

2544

45+
2646
getSquares(){
2747
return this.squares;
2848
}
2949

50+
getPieceBackground(index){
51+
if (index > 0 &&index % 8 === 0){
52+
this.defaultSettings.colors.reverse();
53+
}
54+
return this.defaultSettings.colors[index % 2];
55+
}
56+
3057

3158
// set the data-square index ie a8, b7, etc so we know which item is on which square
3259
getDataProp(index){
60+
var str = String.fromCharCode(this.squarePosition.alpha) + this.squarePosition.num;
61+
this.squarePosition.alpha++;
62+
3363
if (index % 8 === 0){
3464
this.squarePosition.num--;
3565
this.squarePosition.alpha = 97;
3666
}
37-
var str = String.fromCharCode(this.squarePosition.alpha) + this.squarePosition.num;
38-
this.squarePosition.alpha++;
67+
3968
return str;
4069
}
4170

‎Models/Piece.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ export default class Piece{
44
this.name = name;
55
this.image = image;
66
this.dataProp = dataProp;
7-
this.asciii = this.setAscii(ascii);
7+
this.ascii = this.setAscii(ascii);
88
this.numeric = this.setNumeric(numeric);
9-
this.belongsTo = belongsTo;
9+
this.belongsTo = belongsTo;
1010
}
1111

1212
setAscii(ascii){
@@ -22,6 +22,9 @@ export default class Piece{
2222
}
2323
return numeric;
2424
}
25+
setBackground(color){
26+
this.background = color;
27+
}
2528

2629

2730
}

‎Models/PieceFactory.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export default class PieceFactory{
1919
},
2020
{
2121
name: 'knight',
22-
images: 'bN.png',
22+
image: 'bN.png',
2323
positions: ['b8','g8']
2424
},
2525
{
@@ -55,7 +55,7 @@ export default class PieceFactory{
5555
},
5656
{
5757
name: 'knight',
58-
images: 'wN.png',
58+
image: 'wN.png',
5959
positions: ['b1','g1']
6060
},
6161
{

‎Models/Player.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,10 @@ export default class Player{
1010
}
1111

1212
getPieceAt(dataProp){
13-
return this.pieces.reduce((prev,next) => {
14-
if (next.dataProp === dataProp){
15-
return next;
16-
}
17-
},this.pieces[0]);
18-
13+
return this.pieces.find(piece => {
14+
15+
return piece.dataProp === dataProp;
16+
});
1917
}
2018

2119
}

‎build/style.css

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
2+
#app{
3+
width: 1200px;
4+
box-shadow: 5px 5px 35px 0px rgba(0,0,0,0.4);
5+
-webkit-box-shadow: 5px 5px 35px 0px rgba(0,0,0,0.4);
6+
-moz-box-shadow: 5px 5px 35px 0px rgba(0,0,0,0.4);
7+
overflow: hidden;
8+
margin: 0 auto;
9+
padding: 5%;
10+
position: relative;
11+
height: 2000px;
12+
}
13+
14+
#chessBoard{
15+
width: 500px;
16+
height: 500px;
17+
margin: 0 auto;
18+
display: block;
19+
}
20+
21+
#chessBoard ul{
22+
border: 1px solid gray;
23+
list-style-type: none;
24+
margin: 0 auto;
25+
padding: 0;
26+
height: 100%;
27+
width: 100%;
28+
display: block;
29+
box-sizing: border-box;
30+
}
31+
32+
#chessBoard ul li{
33+
display: inline-block;
34+
width: calc(100% / 8);
35+
height: calc(100% / 8);
36+
box-sizing: border-box;
37+
float: left;
38+
position: relative;
39+
}
40+
.label{
41+
display: block;
42+
-webkit-font-smoothing: antialiased;
43+
}
44+
.type-num{
45+
margin-left: 3px;
46+
margin-top: 2px;
47+
position: absolute;
48+
}
49+
50+
.type-alpha{
51+
position: absolute;
52+
bottom: 2px;
53+
right: 4px;
54+
}
55+
56+
.square img{
57+
margin: 0 auto;
58+
float: none;
59+
width: 90%;
60+
height: 90%;
61+
display: block;
62+
}
63+
64+
.messageBoard{
65+
min-height: 60px;
66+
overflow: hidden;
67+
}
68+
69+
h3{
70+
margin: 10px 0;
71+
}

‎components/App.jsx

+5-2
Original file line numberDiff line numberDiff line change
@@ -37,22 +37,25 @@ export default class App extends React.Component {
3737

3838
render(){
3939
console.log(this.state);
40-
let squares = this.state.chessBoard.getSquares().map(square =>{
40+
let squares = this.state.chessBoard.getSquares().map((square,index) =>{
41+
4142
return(
4243
<Square
44+
index = {index}
4345
name = {square.name}
4446
image = {square.image}
4547
belongsTo = {square.belongsTo}
4648
dataProp = {square.dataProp}
4749
numeric = {square.numeric}
4850
ascii = {square.ascii}
51+
background = {square.background}
4952
/>
5053
);
5154

5255
});
5356

5457
return(
55-
<div>
58+
<div id="chessBoard">
5659
<ul>
5760
{squares}
5861
</ul>

‎components/Square.jsx

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
import React from 'react';
22

33
let Square = props =>{
4-
console.log(props);
4+
55
return (
6-
<li className={props.dataProp} data-square={props.dataProp}>
7-
<img className={props.belongTo} src={props.image}></img>
6+
<li className={'square item-' + props.index} data-square={props.dataProp}
7+
style={{background: props.background }}
8+
>
9+
{
10+
props.image ?
11+
<img className={props.belongTo} src={'/images/' + props.image}></img>
12+
: ''
13+
}
814
</li>
915
);
1016
};

‎components/images/bB.png

1.37 KB
Loading

‎components/images/bK.png

2.94 KB
Loading

‎components/images/bN.png

1.83 KB
Loading

‎components/images/bP.png

777 Bytes
Loading

‎components/images/bQ.png

2.59 KB
Loading

‎components/images/bR.png

748 Bytes
Loading

‎components/images/wB.png

2.32 KB
Loading

‎components/images/wK.png

2.76 KB
Loading

‎components/images/wN.png

2.33 KB
Loading

‎components/images/wP.png

1.53 KB
Loading

‎components/images/wQ.png

3.72 KB
Loading

‎components/images/wR.png

1.07 KB
Loading

‎index.html

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
<html>
33
<head>
44
<title>Chess</title>
5+
<link rel="stylesheet" href="/build/style.css">
56
</head>
67
<body>
78
<div id="app"></div>

‎server/index.js

-31
This file was deleted.

‎server/package.json

-14
This file was deleted.

‎utils/profileApi.js

-11
This file was deleted.

‎webpack.config.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@ module.exports = {
2323
query: {
2424
presets: ['es2015', 'react']
2525
}
26-
}
26+
},
27+
{
28+
test: /\.(png|jpg|)$/,
29+
loader: 'url-loader?limit=200000'
30+
}
2731
]
2832
}
2933
}

0 commit comments

Comments
 (0)
Please sign in to comment.