@@ -48,7 +48,7 @@ $(document).ready(function() {
4848 $ ( "." + square ) . find ( ".txt" ) . html ( " " ) ;
4949 } ;
5050
51- var tagSquare = ( x , y , val , steps ) => {
51+ var tagSquare = ( type , x , y , val , steps , color = 0 ) => {
5252 var sq = null ;
5353 if ( x >= 0 && x < 8 && y >= 0 && y < 8 ) {
5454 sq = board [ x ] [ y ] ;
@@ -62,63 +62,104 @@ $(document).ready(function() {
6262 $ ( "." + sq ) . addClass ( "heat-" + val ) ;
6363 tagged [ sq ] = val ;
6464 if ( steps > 0 ) {
65- return { x : x , y : y , val : ++ val , steps : -- steps } ;
65+ return { type : type , x : x , y : y , val : ++ val , steps : -- steps , color : color } ;
6666 }
6767 }
6868 }
6969 }
7070
71- var renderPath = ( x , y , val , steps ) => {
71+ var renderPath = ( type , x , y , val , steps , color = 0 ) => {
7272 var next = [ ] ;
73+ var move = 7 ;
7374
74- // knight!
75- next . push ( tagSquare ( x + 1 , y - 2 , val , steps ) ) ;
76- next . push ( tagSquare ( x + 1 , y + 2 , val , steps ) ) ;
77- next . push ( tagSquare ( x - 1 , y - 2 , val , steps ) ) ;
78- next . push ( tagSquare ( x - 1 , y + 2 , val , steps ) ) ;
79- next . push ( tagSquare ( x + 2 , y - 1 , val , steps ) ) ;
80- next . push ( tagSquare ( x + 2 , y + 1 , val , steps ) ) ;
81- next . push ( tagSquare ( x - 2 , y - 1 , val , steps ) ) ;
82- next . push ( tagSquare ( x - 2 , y + 1 , val , steps ) ) ;
75+ if ( 'K' . includes ( type ) ) {
76+ move = 1 ;
77+ }
78+ if ( 'N' . includes ( type ) ) {
79+ next . push ( tagSquare ( type , x + 1 , y - 2 , val , steps , color = color ) ) ;
80+ next . push ( tagSquare ( type , x + 1 , y + 2 , val , steps , color = color ) ) ;
81+ next . push ( tagSquare ( type , x - 1 , y - 2 , val , steps , color = color ) ) ;
82+ next . push ( tagSquare ( type , x - 1 , y + 2 , val , steps , color = color ) ) ;
83+ next . push ( tagSquare ( type , x + 2 , y - 1 , val , steps , color = color ) ) ;
84+ next . push ( tagSquare ( type , x + 2 , y + 1 , val , steps , color = color ) ) ;
85+ next . push ( tagSquare ( type , x - 2 , y - 1 , val , steps , color = color ) ) ;
86+ next . push ( tagSquare ( type , x - 2 , y + 1 , val , steps , color = color ) ) ;
87+ }
88+ if ( 'KBQ' . includes ( type ) ) {
89+ for ( var i = 0 ; i <= move ; i ++ ) {
90+ next . push ( tagSquare ( type , x - i , y - i , val , steps , color = color ) ) ;
91+ next . push ( tagSquare ( type , x + i , y + i , val , steps , color = color ) ) ;
92+ next . push ( tagSquare ( type , x - i , y + i , val , steps , color = color ) ) ;
93+ next . push ( tagSquare ( type , x + i , y - i , val , steps , color = color ) ) ;
94+ }
95+ }
96+ if ( 'KRQ' . includes ( type ) ) {
97+ for ( var i = 0 ; i <= move ; i ++ ) {
98+ next . push ( tagSquare ( type , x , y - i , val , steps , color = color ) ) ;
99+ next . push ( tagSquare ( type , x , y + i , val , steps , color = color ) ) ;
100+ next . push ( tagSquare ( type , x - i , y , val , steps , color = color ) ) ;
101+ next . push ( tagSquare ( type , x + i , y , val , steps , color = color ) ) ;
102+ }
103+ }
104+ if ( 'P' . includes ( type ) ) {
83105
106+ if ( color == 0 ) {
107+ move = 1
108+ if ( x > 5 ) {
109+ move = 2
110+ next . push ( tagSquare ( type , x - 1 , y , val , steps , color = color ) ) ;
111+ }
112+ } else if ( color == 1 ) {
113+ move = - 1
114+ if ( x < 2 ) {
115+ move = - 2
116+ next . push ( tagSquare ( type , x + 1 , y , val , steps , color = color ) ) ;
117+ }
118+ }
119+ next . push ( tagSquare ( type , x - move , y , val , steps , color = color ) ) ;
120+
121+ }
84122 next . forEach ( ( sq ) => {
85- if ( sq ) renderPath ( sq . x , sq . y , sq . val , sq . steps ) ;
123+ if ( sq ) renderPath ( sq . type , sq . x , sq . y , sq . val , sq . steps , color = sq . color ) ;
86124 } ) ;
87125 }
88126
89- function refresh ( sq ) {
127+ function refresh ( type , sq , color = 0 ) {
90128 console . log ( 'REFRESH' , sq ) ;
91129 currentPos = sq ;
92130 initBoard ( ) ;
93- initPiece ( 'N' , sq ) ;
131+ initPiece ( type , sq ) ;
94132 var pos = index [ sq ] ;
95133 var x = pos [ 0 ] ;
96134 var y = pos [ 1 ] ;
97- renderPath ( x , y , 1 , 5 ) ;
135+ renderPath ( type , x , y , 1 , 6 , color = color ) ;
136+ var sprite = [ 'K' , 'Q' , 'B' , 'N' , 'R' , 'P' ] . indexOf ( type ) + 1 + color * 6
137+ $ ( '.piece' ) . css ( "background-image" , "url('img/sprites_" + sprite . toString ( ) . padStart ( 2 , '0' ) + ".png')" ) ;
98138 $ ( "." + sq ) . find ( ".txt" ) . addClass ( "hide" ) . html ( " " ) ;
99139 console . log ( tagged ) ;
100140 }
101141
102- function toggleBlock ( sq ) {
142+ function toggleBlock ( type , sq ) {
103143 if ( blocked [ sq ] ) {
104144 blocked [ sq ] = false ;
105145 } else {
106146 blocked [ sq ] = true ;
107147 }
108148 console . log ( "BLOCKED" , blocked ) ;
109- refresh ( currentPos ) ;
149+ refresh ( type , currentPos ) ;
110150 }
111151
112152 function clickHandler ( event ) {
153+ var type = 'K'
113154 var target = $ ( event . currentTarget ) ;
114155 var id = target . find ( ".identifier" ) . text ( ) ;
115156 if ( event . which == 1 ) {
116- refresh ( id ) ;
157+ refresh ( type , id ) ;
117158 } else {
118159 event . preventDefault ( ) ;
119- toggleBlock ( id ) ;
160+ toggleBlock ( type , id ) ;
120161 }
121162 }
122163
123- refresh ( 'g1' ) ;
164+ refresh ( 'N' , ' g1') ;
124165} ) ;
0 commit comments