Skip to content

Commit dad7bef

Browse files
author
Yasin Yaqoobi
committed
semi working react-chess
1 parent 198c748 commit dad7bef

24 files changed

+317
-59
lines changed

Models/Board.js

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import Player from '../Models/Player.js';
2+
import Piece from '../Models/Piece.js';
3+
4+
export default class Board{
5+
6+
constructor(player1,player2){
7+
8+
this.squarePosition = {num: 8, alpha:97};
9+
this.labelPosition = {num: 8, alpha: 97};
10+
11+
this.squares = player1.getPieces().concat(player2.getPieces());
12+
13+
14+
for (let i = 0; i < 64; i++){
15+
16+
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+
}
21+
}
22+
23+
}
24+
25+
26+
getSquares(){
27+
return this.squares;
28+
}
29+
30+
31+
// set the data-square index ie a8, b7, etc so we know which item is on which square
32+
getDataProp(index){
33+
if (index % 8 === 0){
34+
this.squarePosition.num--;
35+
this.squarePosition.alpha = 97;
36+
}
37+
var str = String.fromCharCode(this.squarePosition.alpha) + this.squarePosition.num;
38+
this.squarePosition.alpha++;
39+
return str;
40+
}
41+
42+
43+
/**
44+
setLabels(index){
45+
var label;
46+
47+
if (index % 8 === 0){
48+
label = new Label('number',labelPosition.num);
49+
labelPosition.num--;
50+
}
51+
if (index >=56){
52+
label = new Label('alphabet', String.fromCharCode(labelPosition.alpha));
53+
labelPosition.alpha++;
54+
}
55+
return label;
56+
}
57+
**/
58+
59+
60+
61+
}

Models/Chess.js

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import Player from '../Models/Player.js';
2+
import Board from '../Models/Board.js';
3+
4+
export default class Chess{
5+
6+
constructor(){
7+
this.players = [new Player('player1'), new Player('player2')];
8+
9+
this.currentPlayer = this.players[0];
10+
11+
this.board = new Board(this.getCurrentPlayer(),this.getOppositePlayer());
12+
}
13+
14+
getBoard(){
15+
return this.board;
16+
}
17+
18+
19+
getPlayers(){
20+
return this.players;
21+
}
22+
23+
24+
getCurrentPlayer(){
25+
return this.currentPlayer;
26+
}
27+
28+
getOppositePlayer(){
29+
if (this.currentPlayer.name === 'player1'){
30+
return this.players[1];
31+
}
32+
return this.players[0];
33+
}
34+
35+
36+
}

Models/Piece.js

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
export default class Piece{
2+
3+
constructor(name,image,belongsTo,dataProp,ascii,numeric){
4+
this.name = name;
5+
this.image = image;
6+
this.dataProp = dataProp;
7+
this.asciii = this.setAscii(ascii);
8+
this.numeric = this.setNumeric(numeric);
9+
this.belongsTo = belongsTo;
10+
}
11+
12+
setAscii(ascii){
13+
if (ascii === undefined){
14+
return parseInt(this.dataProp.split('')[0].charCodeAt());
15+
}
16+
return ascii;
17+
}
18+
19+
setNumeric(numeric){
20+
if (numeric === undefined){
21+
return parseInt(this.dataProp.split('')[1]);
22+
}
23+
return numeric;
24+
}
25+
26+
27+
}

Models/PieceFactory.js

+116
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
import Piece from '../Models/Piece.js';
2+
3+
export default class PieceFactory{
4+
5+
6+
constructor(){
7+
}
8+
9+
static getMyPieces(playerName){
10+
11+
var state = [
12+
{
13+
name: 'player2',
14+
pieces: [
15+
{
16+
name: 'rook',
17+
image: 'bR.png',
18+
positions: ['a8','h8']
19+
},
20+
{
21+
name: 'knight',
22+
images: 'bN.png',
23+
positions: ['b8','g8']
24+
},
25+
{
26+
name: 'bishop',
27+
image:'bB.png',
28+
positions: ['c8','f8']
29+
},
30+
{
31+
name: 'queen',
32+
image:'bQ.png',
33+
positions: ['d8']
34+
},
35+
{
36+
name: 'king',
37+
image:'bK.png',
38+
positions: ['e8']
39+
},
40+
{
41+
name: 'pawn',
42+
image:'bP.png',
43+
positions: ['a7','b7','c7','d7','e7','f7','g7','h7']
44+
}
45+
]
46+
},
47+
48+
{
49+
name: 'player1',
50+
pieces: [
51+
{
52+
name: 'rook',
53+
image: 'wR.png',
54+
positions: ['a1','h1']
55+
},
56+
{
57+
name: 'knight',
58+
images: 'wN.png',
59+
positions: ['b1','g1']
60+
},
61+
{
62+
name: 'bishop',
63+
image:'wB.png',
64+
positions: ['c1','f1']
65+
},
66+
{
67+
name: 'queen',
68+
image:'wQ.png',
69+
positions: ['d1']
70+
},
71+
{
72+
name: 'king',
73+
image:'wK.png',
74+
positions: ['e1']
75+
},
76+
{
77+
name: 'pawn',
78+
image:'wP.png',
79+
positions: ['a2','b2','c2','d2','e2','f2','g2','h2']
80+
}
81+
]
82+
}
83+
];
84+
85+
return state.reduce(function(prev,next){
86+
if (prev.name === playerName){ // Get only current player pieces.
87+
return prev.pieces;
88+
}
89+
return next.pieces;
90+
})
91+
.map(function(piece){ // Go through pieces array and create new piece
92+
return piece.positions
93+
.map(function(position){
94+
return new Piece(piece.name,piece.image,playerName,position);
95+
96+
})
97+
})
98+
.reduce(function(prev,next){ // Flatten the array [ [piece,piece],[piece,piece]....]
99+
return prev.concat(next);
100+
});
101+
102+
103+
}
104+
105+
106+
107+
108+
}
109+
110+
111+
112+
113+
114+
115+
116+

Models/Player.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import PieceFactory from '../Models/PieceFactory.js';
2+
export default class Player{
3+
4+
constructor(name){
5+
this.name = name;
6+
this.pieces = PieceFactory.getMyPieces(this.name); // get all the pieces for player from the piece Factory
7+
}
8+
getPieces(){
9+
return this.pieces;
10+
}
11+
12+
getPieceAt(dataProp){
13+
return this.pieces.reduce((prev,next) => {
14+
if (next.dataProp === dataProp){
15+
return next;
16+
}
17+
},this.pieces[0]);
18+
19+
}
20+
21+
}

components/AddProfile.jsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,13 @@ export default class AddProfile extends React.Component{
4141
hobbies: [this.state.hobby]
4242
}
4343
this.props.addUser(newProfile);
44+
4445
}
4546

4647
render(){
4748
return(
4849
<div>
49-
<p>Add a new Profile</p>
50-
50+
<p>Add a new Profile</p>
5151
<input onChange={this.handlename} value={this.state.name} />
5252
<input onChange={this.handleBio} value={this.state.bio} />
5353
<input onChange={this.handleHobby} value={this.state.hobby} />

components/App.jsx

+37-34
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,64 @@
11
import React from 'react';
2-
import Profile from 'Profile.jsx';
3-
import AddProfile from 'AddProfile.jsx';
42

5-
import {getProfiles} from '../utils/profileApi.js';
3+
import Chess from '../Models/Chess.js';
4+
5+
import Square from 'Square.jsx';
6+
7+
68

79
export default class App extends React.Component {
810
constructor(){ // constructor(props)
911
super(); // super(props)
1012
this.state = {
11-
profiles: []
13+
chessBoard:[]
1214
}
13-
this.addUser = this.addUser.bind(this)
15+
1416
}
1517

1618
/** some initial state -> don't use for ajax. **/
1719
componentWillMount(){
18-
console.log('componenet will mount called');
20+
const chess = new Chess();
21+
this.setState({
22+
chessBoard: chess.getBoard(),
23+
});
24+
1925
}
2026

2127
componentDidMount(){
22-
getProfiles()
23-
.then(profiles=>{
24-
this.setState({
25-
profiles: profiles
26-
})
27-
})
28+
29+
2830
}
2931

3032
componentWillUnmount(){
3133
console.log('component unmount called');
3234
}
3335

34-
addUser(newProfile){
35-
this.setState({
36-
profiles: this.state.profiles.concat([newProfile])
37-
});
38-
39-
}
40-
4136

4237

4338
render(){
44-
let profiles = this.state.profiles.map(profile =>{
45-
return (
46-
<Profile
47-
name={profile.name}
48-
age={profile.age}
49-
bio={profile.bio}
50-
hobbies={profile.hobbies} />
51-
)
52-
});
53-
return(
54-
<div>
55-
{profiles}
56-
<AddProfile addUser={this.addUser} />
57-
</div>
58-
);
39+
console.log(this.state);
40+
let squares = this.state.chessBoard.getSquares().map(square =>{
41+
return(
42+
<Square
43+
name = {square.name}
44+
image = {square.image}
45+
belongsTo = {square.belongsTo}
46+
dataProp = {square.dataProp}
47+
numeric = {square.numeric}
48+
ascii = {square.ascii}
49+
/>
50+
);
51+
52+
});
53+
54+
return(
55+
<div>
56+
<ul>
57+
{squares}
58+
</ul>
59+
</div>
60+
);
5961
}
6062

63+
6164
}

0 commit comments

Comments
 (0)