How to use square method in stryker-parent

Best JavaScript code snippet using stryker-parent

barriers.js

Source:barriers.js Github

copy

Full Screen

1//barriers.js2//Master Copy - Space Invaders II3//GAM1004//Fall, 20205//Major : Junsu Jang, Minor : Daehyeon Kim6//“All content © 2020 DigiPen (USA) Corporation, all rights reserved.”7class barrier {8 constructor(x, y) {//Input barrier x, y9 this.x = x;10 this.y = y;11 this.up_damaged = 0;//Current situation of middle side damage from top12 this.down_damaged = 0;//Current situation of middle side damage from bottom13 this.right_up_damaged = 0;//Current situation of right side damage from top14 this.right_down_damaged = 0;//Current situation of right side damage from bottom15 this.left_up_damaged = 0;//Current situation of left side damage from top16 this.left_down_damaged = 0;//Current situation of left side damage from bottom17 this.image_size = 16;18 this.left_damage_max = 11;19 this.right_damage_max = 11;20 this.middle_damage_max = 9;21 this.left_image_1 = image_barrier_left[0];//Top of left side barrier image22 this.left_image_2 = image_barrier_square[0];//Middle of left side barrier image23 this.left_image_3 = image_barrier_square[0];//Bottom of left side barrier image24 this.right_image_1 = image_barrier_right[0];//Top of right side barrier image25 this.right_image_2 = image_barrier_square[0];//Middle of right side barrier image26 this.right_image_3 = image_barrier_square[0];//Bottom of right side barrier image27 this.middle_image_1 = image_barrier_square[0];//Top of middle side barrier image28 this.middle_image_2 = image_barrier_square[0];//Bottom of middle sode barrier image29 }30 generate() {//Draw barrier31 var barrier_left_1 = {//Designate top of left side position32 x: this.x - this.image_size,33 y: this.y - this.image_size34 },35 barrier_left_2 = {//Designate middle of left side position36 x: this.x - this.image_size,37 y: this.y38 },39 barrier_left_3 = {//Designate bottom of left side position40 x: this.x - this.image_size,41 y: this.y + this.image_size42 },43 barrier_right_1 = {//Designate top of right side position44 x: this.x + this.image_size,45 y: this.y - this.image_size46 },47 barrier_right_2 = {//Designate middle of right side position48 x: this.x + this.image_size,49 y: this.y50 },51 barrier_right_3 = {//Designate bottom of right side position52 x: this.x + this.image_size,53 y: this.y + this.image_size54 },55 barrier_midle_1 = {//Designate top of middle side position56 x: this.x,57 y: this.y - this.image_size58 },59 barrier_midle_2 = {//Designate bottom of middle side position60 x: this.x,61 y: this.y62 },63 barrier_midle_3 = {//Designate tail of bottom of middle side position64 x: this.x,65 y: this.y + this.image_size66 };67 push();68 imageMode(CENTER);69 image(this.left_image_1, barrier_left_1.x, barrier_left_1.y, this.image_size, this.image_size);70 image(this.left_image_2, barrier_left_2.x, barrier_left_2.y, this.image_size, this.image_size);71 image(this.left_image_3, barrier_left_3.x, barrier_left_3.y, this.image_size, this.image_size);72 image(this.right_image_1, barrier_right_1.x, barrier_right_1.y, this.image_size, this.image_size);73 image(this.right_image_2, barrier_right_2.x, barrier_right_2.y, this.image_size, this.image_size);74 image(this.right_image_3, barrier_right_3.x, barrier_right_3.y, this.image_size, this.image_size);75 image(this.middle_image_1, barrier_midle_1.x, barrier_midle_1.y, this.image_size, this.image_size);76 image(this.middle_image_2, barrier_midle_2.x, barrier_midle_2.y, this.image_size, this.image_size);77 if (this.down_damaged == 0 && this.up_damaged != this.middle_damage_max) {78 image(image_barrier_bottom_edge, barrier_midle_3.x, barrier_midle_3.y, this.image_size, this.image_size);79 }80 pop();81 }82 update() {83 if(this.left_up_damaged + this.left_down_damaged >= this.left_damage_max){//Prevent damage beyond the limit.84 this.left_up_damaged = this.left_damage_max;85 this.left_down_damaged = 0;86 }87 if(this.right_up_damaged + this.right_down_damaged >= this.right_damage_max){88 this.right_up_damaged = this.right_damage_max;89 this.right_down_damaged = 0;90 }91 if(this.up_damaged + this.down_damaged >= this.middle_damage_max){92 this.up_damaged = this.middle_damage_max;93 this.down_damaged = 0;94 }95var square_image_num = [//Number of square image by damage96 [0, 5, 9, 12, 14, 15],//horizontal direction is down_damaged97 [1, 6, 10, 13, 15],98 [2, 7, 11, 15],99 [3, 8, 15],100 [4, 15],101 [15]102 ];//vertical direction is up_damaged103 var left_tri_image_num = [//Number of left_triangle image by damage104 [0, 3, 5, 6],105 [1, 4, 6],106 [2, 6],107 [6]108 ];109 var right_tri_image_num = [//Number of right_triangle image by damage110 [0, 3, 5, 6],111 [1, 4, 6],112 [2, 6],113 [6]114 ];115var middle_barrier_image = [ //up_damaged116 [ //down_damaged117 [image_barrier_square[square_image_num[0][0]]/*Top*/, image_barrier_square[square_image_num[0][0]]]/*Bottom*/,118 [image_barrier_square[square_image_num[0][0]], image_barrier_square[square_image_num[0][1]]],119 [image_barrier_square[square_image_num[0][0]], image_barrier_square[square_image_num[0][2]]],120 [image_barrier_square[square_image_num[0][0]], image_barrier_square[square_image_num[0][3]]],121 [image_barrier_square[square_image_num[0][0]], image_barrier_square[square_image_num[0][4]]],122 [image_barrier_square[square_image_num[0][1]], image_barrier_square[square_image_num[0][5]]],123 [image_barrier_square[square_image_num[0][2]], image_barrier_square[square_image_num[0][5]]],124 [image_barrier_square[square_image_num[0][3]], image_barrier_square[square_image_num[0][5]]],125 [image_barrier_square[square_image_num[0][4]], image_barrier_square[square_image_num[0][5]]],126 [image_barrier_square[square_image_num[0][5]], image_barrier_square[square_image_num[0][5]]],127 ],128 [129 [image_barrier_square[square_image_num[1][0]], image_barrier_square[square_image_num[0][0]]],130 [image_barrier_square[square_image_num[1][0]], image_barrier_square[square_image_num[0][1]]],131 [image_barrier_square[square_image_num[1][0]], image_barrier_square[square_image_num[0][2]]],132 [image_barrier_square[square_image_num[1][0]], image_barrier_square[square_image_num[0][3]]],133 [image_barrier_square[square_image_num[1][0]], image_barrier_square[square_image_num[0][4]]],134 [image_barrier_square[square_image_num[1][1]], image_barrier_square[square_image_num[0][5]]],135 [image_barrier_square[square_image_num[1][2]], image_barrier_square[square_image_num[0][5]]],136 [image_barrier_square[square_image_num[1][3]], image_barrier_square[square_image_num[0][5]]],137 [image_barrier_square[square_image_num[1][4]], image_barrier_square[square_image_num[0][5]]],138 ],139 [140 [image_barrier_square[square_image_num[2][0]], image_barrier_square[square_image_num[0][0]]],141 [image_barrier_square[square_image_num[2][0]], image_barrier_square[square_image_num[0][1]]],142 [image_barrier_square[square_image_num[2][0]], image_barrier_square[square_image_num[0][2]]],143 [image_barrier_square[square_image_num[2][0]], image_barrier_square[square_image_num[0][3]]],144 [image_barrier_square[square_image_num[2][0]], image_barrier_square[square_image_num[0][4]]],145 [image_barrier_square[square_image_num[2][1]], image_barrier_square[square_image_num[0][5]]],146 [image_barrier_square[square_image_num[2][2]], image_barrier_square[square_image_num[0][5]]],147 [image_barrier_square[square_image_num[2][3]], image_barrier_square[square_image_num[0][5]]],148 ],149 [150 [image_barrier_square[square_image_num[3][0]], image_barrier_square[square_image_num[0][0]]],151 [image_barrier_square[square_image_num[3][0]], image_barrier_square[square_image_num[0][1]]],152 [image_barrier_square[square_image_num[3][0]], image_barrier_square[square_image_num[0][2]]],153 [image_barrier_square[square_image_num[3][0]], image_barrier_square[square_image_num[0][3]]],154 [image_barrier_square[square_image_num[3][0]], image_barrier_square[square_image_num[0][4]]],155 [image_barrier_square[square_image_num[3][1]], image_barrier_square[square_image_num[0][5]]],156 [image_barrier_square[square_image_num[3][2]], image_barrier_square[square_image_num[0][5]]],157 ],158 [159 [image_barrier_square[square_image_num[4][0]], image_barrier_square[square_image_num[0][0]]],160 [image_barrier_square[square_image_num[4][0]], image_barrier_square[square_image_num[0][1]]],161 [image_barrier_square[square_image_num[4][0]], image_barrier_square[square_image_num[0][2]]],162 [image_barrier_square[square_image_num[4][0]], image_barrier_square[square_image_num[0][3]]],163 [image_barrier_square[square_image_num[4][0]], image_barrier_square[square_image_num[0][4]]],164 [image_barrier_square[square_image_num[4][1]], image_barrier_square[square_image_num[0][5]]],165 ],166 [167 [image_barrier_square[square_image_num[5][0]], image_barrier_square[square_image_num[1][0]]],168 [image_barrier_square[square_image_num[5][0]], image_barrier_square[square_image_num[1][1]]],169 [image_barrier_square[square_image_num[5][0]], image_barrier_square[square_image_num[1][2]]],170 [image_barrier_square[square_image_num[5][0]], image_barrier_square[square_image_num[1][3]]],171 [image_barrier_square[square_image_num[5][0]], image_barrier_square[square_image_num[1][4]]],172 ],173 [174 [image_barrier_square[square_image_num[5][0]], image_barrier_square[square_image_num[2][0]]],175 [image_barrier_square[square_image_num[5][0]], image_barrier_square[square_image_num[2][1]]],176 [image_barrier_square[square_image_num[5][0]], image_barrier_square[square_image_num[2][2]]],177 [image_barrier_square[square_image_num[5][0]], image_barrier_square[square_image_num[2][3]]],178 ],179 [180 [image_barrier_square[square_image_num[5][0]], image_barrier_square[square_image_num[3][0]]],181 [image_barrier_square[square_image_num[5][0]], image_barrier_square[square_image_num[3][1]]],182 [image_barrier_square[square_image_num[5][0]], image_barrier_square[square_image_num[3][2]]],183 ],184 [185 [image_barrier_square[square_image_num[5][0]], image_barrier_square[square_image_num[4][0]]],186 [image_barrier_square[square_image_num[5][0]], image_barrier_square[square_image_num[4][1]]],187 ],188 [189 [image_barrier_square[square_image_num[5][0]], image_barrier_square[square_image_num[5][0]]],190 ]191 ];192 var left_barrier_image = [ //up_damaged193 [ //down_damaged194 [image_barrier_left[left_tri_image_num[0][0]]/*Top*/, image_barrier_square[square_image_num[0][0]]/*Middle*/, image_barrier_square[square_image_num[0][0]]]/*Bottom*/,195 [image_barrier_left[left_tri_image_num[0][0]], image_barrier_square[square_image_num[0][0]], image_barrier_square[square_image_num[0][1]]],196 [image_barrier_left[left_tri_image_num[0][0]], image_barrier_square[square_image_num[0][0]], image_barrier_square[square_image_num[0][2]]],197 [image_barrier_left[left_tri_image_num[0][0]], image_barrier_square[square_image_num[0][0]], image_barrier_square[square_image_num[0][3]]],198 [image_barrier_left[left_tri_image_num[0][0]], image_barrier_square[square_image_num[0][0]], image_barrier_square[square_image_num[0][4]]],199 [image_barrier_left[left_tri_image_num[0][0]], image_barrier_square[square_image_num[0][1]], image_barrier_square[square_image_num[0][5]]],200 [image_barrier_left[left_tri_image_num[0][0]], image_barrier_square[square_image_num[0][2]], image_barrier_square[square_image_num[0][5]]],201 [image_barrier_left[left_tri_image_num[0][0]], image_barrier_square[square_image_num[0][3]], image_barrier_square[square_image_num[0][5]]],202 [image_barrier_left[left_tri_image_num[0][0]], image_barrier_square[square_image_num[0][4]], image_barrier_square[square_image_num[0][5]]],203 [image_barrier_left[left_tri_image_num[0][1]], image_barrier_square[square_image_num[0][5]], image_barrier_square[square_image_num[0][5]]],204 [image_barrier_left[left_tri_image_num[0][2]], image_barrier_square[square_image_num[0][5]], image_barrier_square[square_image_num[0][5]]],205 [image_barrier_left[left_tri_image_num[0][3]], image_barrier_square[square_image_num[0][5]], image_barrier_square[square_image_num[0][5]]],206 ],207 [208 [image_barrier_left[left_tri_image_num[1][0]], image_barrier_square[square_image_num[0][0]], image_barrier_square[square_image_num[0][0]]],209 [image_barrier_left[left_tri_image_num[1][0]], image_barrier_square[square_image_num[0][0]], image_barrier_square[square_image_num[0][1]]],210 [image_barrier_left[left_tri_image_num[1][0]], image_barrier_square[square_image_num[0][0]], image_barrier_square[square_image_num[0][2]]],211 [image_barrier_left[left_tri_image_num[1][0]], image_barrier_square[square_image_num[0][0]], image_barrier_square[square_image_num[0][3]]],212 [image_barrier_left[left_tri_image_num[1][0]], image_barrier_square[square_image_num[0][0]], image_barrier_square[square_image_num[0][4]]],213 [image_barrier_left[left_tri_image_num[1][0]], image_barrier_square[square_image_num[0][1]], image_barrier_square[square_image_num[0][5]]],214 [image_barrier_left[left_tri_image_num[1][0]], image_barrier_square[square_image_num[0][2]], image_barrier_square[square_image_num[0][5]]],215 [image_barrier_left[left_tri_image_num[1][0]], image_barrier_square[square_image_num[0][3]], image_barrier_square[square_image_num[0][5]]],216 [image_barrier_left[left_tri_image_num[1][0]], image_barrier_square[square_image_num[0][4]], image_barrier_square[square_image_num[0][5]]],217 [image_barrier_left[left_tri_image_num[1][1]], image_barrier_square[square_image_num[0][5]], image_barrier_square[square_image_num[0][5]]],218 [image_barrier_left[left_tri_image_num[1][2]], image_barrier_square[square_image_num[0][5]], image_barrier_square[square_image_num[0][5]]],219 ],220 [221 [image_barrier_left[left_tri_image_num[2][0]], image_barrier_square[square_image_num[0][0]], image_barrier_square[square_image_num[0][0]]],222 [image_barrier_left[left_tri_image_num[2][0]], image_barrier_square[square_image_num[0][0]], image_barrier_square[square_image_num[0][1]]],223 [image_barrier_left[left_tri_image_num[2][0]], image_barrier_square[square_image_num[0][0]], image_barrier_square[square_image_num[0][2]]],224 [image_barrier_left[left_tri_image_num[2][0]], image_barrier_square[square_image_num[0][0]], image_barrier_square[square_image_num[0][3]]],225 [image_barrier_left[left_tri_image_num[2][0]], image_barrier_square[square_image_num[0][0]], image_barrier_square[square_image_num[0][4]]],226 [image_barrier_left[left_tri_image_num[2][0]], image_barrier_square[square_image_num[0][1]], image_barrier_square[square_image_num[0][5]]],227 [image_barrier_left[left_tri_image_num[2][0]], image_barrier_square[square_image_num[0][2]], image_barrier_square[square_image_num[0][5]]],228 [image_barrier_left[left_tri_image_num[2][0]], image_barrier_square[square_image_num[0][3]], image_barrier_square[square_image_num[0][5]]],229 [image_barrier_left[left_tri_image_num[2][0]], image_barrier_square[square_image_num[0][4]], image_barrier_square[square_image_num[0][5]]],230 [image_barrier_left[left_tri_image_num[2][1]], image_barrier_square[square_image_num[0][5]], image_barrier_square[square_image_num[0][5]]],231 ],232 [233 [image_barrier_left[left_tri_image_num[3][0]], image_barrier_square[square_image_num[1][0]], image_barrier_square[square_image_num[0][0]]],234 [image_barrier_left[left_tri_image_num[3][0]], image_barrier_square[square_image_num[1][0]], image_barrier_square[square_image_num[0][1]]],235 [image_barrier_left[left_tri_image_num[3][0]], image_barrier_square[square_image_num[1][0]], image_barrier_square[square_image_num[0][2]]],236 [image_barrier_left[left_tri_image_num[3][0]], image_barrier_square[square_image_num[1][0]], image_barrier_square[square_image_num[0][3]]],237 [image_barrier_left[left_tri_image_num[3][0]], image_barrier_square[square_image_num[1][0]], image_barrier_square[square_image_num[0][4]]],238 [image_barrier_left[left_tri_image_num[3][0]], image_barrier_square[square_image_num[1][1]], image_barrier_square[square_image_num[0][5]]],239 [image_barrier_left[left_tri_image_num[3][0]], image_barrier_square[square_image_num[1][2]], image_barrier_square[square_image_num[0][5]]],240 [image_barrier_left[left_tri_image_num[3][0]], image_barrier_square[square_image_num[1][3]], image_barrier_square[square_image_num[0][5]]],241 [image_barrier_left[left_tri_image_num[3][0]], image_barrier_square[square_image_num[1][4]], image_barrier_square[square_image_num[0][5]]],242 ],243 [244 [image_barrier_left[left_tri_image_num[3][0]], image_barrier_square[square_image_num[2][0]], image_barrier_square[square_image_num[0][0]]],245 [image_barrier_left[left_tri_image_num[3][0]], image_barrier_square[square_image_num[2][0]], image_barrier_square[square_image_num[0][1]]],246 [image_barrier_left[left_tri_image_num[3][0]], image_barrier_square[square_image_num[2][0]], image_barrier_square[square_image_num[0][2]]],247 [image_barrier_left[left_tri_image_num[3][0]], image_barrier_square[square_image_num[2][0]], image_barrier_square[square_image_num[0][3]]],248 [image_barrier_left[left_tri_image_num[3][0]], image_barrier_square[square_image_num[2][0]], image_barrier_square[square_image_num[0][4]]],249 [image_barrier_left[left_tri_image_num[3][0]], image_barrier_square[square_image_num[2][1]], image_barrier_square[square_image_num[0][5]]],250 [image_barrier_left[left_tri_image_num[3][0]], image_barrier_square[square_image_num[2][2]], image_barrier_square[square_image_num[0][5]]],251 [image_barrier_left[left_tri_image_num[3][0]], image_barrier_square[square_image_num[2][3]], image_barrier_square[square_image_num[0][5]]],252 ],253 [254 [image_barrier_left[left_tri_image_num[3][0]], image_barrier_square[square_image_num[3][0]], image_barrier_square[square_image_num[0][0]]],255 [image_barrier_left[left_tri_image_num[3][0]], image_barrier_square[square_image_num[3][0]], image_barrier_square[square_image_num[0][1]]],256 [image_barrier_left[left_tri_image_num[3][0]], image_barrier_square[square_image_num[3][0]], image_barrier_square[square_image_num[0][2]]],257 [image_barrier_left[left_tri_image_num[3][0]], image_barrier_square[square_image_num[3][0]], image_barrier_square[square_image_num[0][3]]],258 [image_barrier_left[left_tri_image_num[3][0]], image_barrier_square[square_image_num[3][0]], image_barrier_square[square_image_num[0][4]]],259 [image_barrier_left[left_tri_image_num[3][0]], image_barrier_square[square_image_num[3][1]], image_barrier_square[square_image_num[0][5]]],260 [image_barrier_left[left_tri_image_num[3][0]], image_barrier_square[square_image_num[3][2]], image_barrier_square[square_image_num[0][5]]],261 ],262 [263 [image_barrier_left[left_tri_image_num[3][0]], image_barrier_square[square_image_num[4][0]], image_barrier_square[square_image_num[0][0]]],264 [image_barrier_left[left_tri_image_num[3][0]], image_barrier_square[square_image_num[4][0]], image_barrier_square[square_image_num[0][1]]],265 [image_barrier_left[left_tri_image_num[3][0]], image_barrier_square[square_image_num[4][0]], image_barrier_square[square_image_num[0][2]]],266 [image_barrier_left[left_tri_image_num[3][0]], image_barrier_square[square_image_num[4][0]], image_barrier_square[square_image_num[0][3]]],267 [image_barrier_left[left_tri_image_num[3][0]], image_barrier_square[square_image_num[4][0]], image_barrier_square[square_image_num[0][4]]],268 [image_barrier_left[left_tri_image_num[3][0]], image_barrier_square[square_image_num[4][1]], image_barrier_square[square_image_num[0][5]]],269 ],270 [271 [image_barrier_left[left_tri_image_num[3][0]], image_barrier_square[square_image_num[5][0]], image_barrier_square[square_image_num[1][0]]],272 [image_barrier_left[left_tri_image_num[3][0]], image_barrier_square[square_image_num[5][0]], image_barrier_square[square_image_num[1][1]]],273 [image_barrier_left[left_tri_image_num[3][0]], image_barrier_square[square_image_num[5][0]], image_barrier_square[square_image_num[1][2]]],274 [image_barrier_left[left_tri_image_num[3][0]], image_barrier_square[square_image_num[5][0]], image_barrier_square[square_image_num[1][3]]],275 [image_barrier_left[left_tri_image_num[3][0]], image_barrier_square[square_image_num[5][0]], image_barrier_square[square_image_num[1][4]]],276 ],277 [278 [image_barrier_left[left_tri_image_num[3][0]], image_barrier_square[square_image_num[5][0]], image_barrier_square[square_image_num[2][0]]],279 [image_barrier_left[left_tri_image_num[3][0]], image_barrier_square[square_image_num[5][0]], image_barrier_square[square_image_num[2][1]]],280 [image_barrier_left[left_tri_image_num[3][0]], image_barrier_square[square_image_num[5][0]], image_barrier_square[square_image_num[2][2]]],281 [image_barrier_left[left_tri_image_num[3][0]], image_barrier_square[square_image_num[5][0]], image_barrier_square[square_image_num[2][3]]],282 ],283 [284 [image_barrier_left[left_tri_image_num[3][0]], image_barrier_square[square_image_num[5][0]], image_barrier_square[square_image_num[3][0]]],285 [image_barrier_left[left_tri_image_num[3][0]], image_barrier_square[square_image_num[5][0]], image_barrier_square[square_image_num[3][1]]],286 [image_barrier_left[left_tri_image_num[3][0]], image_barrier_square[square_image_num[5][0]], image_barrier_square[square_image_num[3][2]]],287 ],288 [289 [image_barrier_left[left_tri_image_num[3][0]], image_barrier_square[square_image_num[5][0]], image_barrier_square[square_image_num[4][0]]],290 [image_barrier_left[left_tri_image_num[3][0]], image_barrier_square[square_image_num[5][0]], image_barrier_square[square_image_num[4][1]]],291 ],292 [293 [image_barrier_left[left_tri_image_num[3][0]], image_barrier_square[square_image_num[5][0]], image_barrier_square[square_image_num[5][0]]],294 ],295 ];296var right_barrier_image = [ //up_damaged297 [ //down_damaged298 [image_barrier_right[right_tri_image_num[0][0]]/*Top*/, image_barrier_square[square_image_num[0][0]]/*Middle*/, image_barrier_square[square_image_num[0][0]]]/*Bottom*/,299 [image_barrier_right[right_tri_image_num[0][0]], image_barrier_square[square_image_num[0][0]], image_barrier_square[square_image_num[0][1]]],300 [image_barrier_right[right_tri_image_num[0][0]], image_barrier_square[square_image_num[0][0]], image_barrier_square[square_image_num[0][2]]],301 [image_barrier_right[right_tri_image_num[0][0]], image_barrier_square[square_image_num[0][0]], image_barrier_square[square_image_num[0][3]]],302 [image_barrier_right[right_tri_image_num[0][0]], image_barrier_square[square_image_num[0][0]], image_barrier_square[square_image_num[0][4]]],303 [image_barrier_right[right_tri_image_num[0][0]], image_barrier_square[square_image_num[0][1]], image_barrier_square[square_image_num[0][5]]],304 [image_barrier_right[right_tri_image_num[0][0]], image_barrier_square[square_image_num[0][2]], image_barrier_square[square_image_num[0][5]]],305 [image_barrier_right[right_tri_image_num[0][0]], image_barrier_square[square_image_num[0][3]], image_barrier_square[square_image_num[0][5]]],306 [image_barrier_right[right_tri_image_num[0][0]], image_barrier_square[square_image_num[0][4]], image_barrier_square[square_image_num[0][5]]],307 [image_barrier_right[right_tri_image_num[0][1]], image_barrier_square[square_image_num[0][5]], image_barrier_square[square_image_num[0][5]]],308 [image_barrier_right[right_tri_image_num[0][2]], image_barrier_square[square_image_num[0][5]], image_barrier_square[square_image_num[0][5]]],309 [image_barrier_right[right_tri_image_num[0][3]], image_barrier_square[square_image_num[0][5]], image_barrier_square[square_image_num[0][5]]],310 ],311 [312 [image_barrier_right[right_tri_image_num[1][0]], image_barrier_square[square_image_num[0][0]], image_barrier_square[square_image_num[0][0]]],313 [image_barrier_right[right_tri_image_num[1][0]], image_barrier_square[square_image_num[0][0]], image_barrier_square[square_image_num[0][1]]],314 [image_barrier_right[right_tri_image_num[1][0]], image_barrier_square[square_image_num[0][0]], image_barrier_square[square_image_num[0][2]]],315 [image_barrier_right[right_tri_image_num[1][0]], image_barrier_square[square_image_num[0][0]], image_barrier_square[square_image_num[0][3]]],316 [image_barrier_right[right_tri_image_num[1][0]], image_barrier_square[square_image_num[0][0]], image_barrier_square[square_image_num[0][4]]],317 [image_barrier_right[right_tri_image_num[1][0]], image_barrier_square[square_image_num[0][1]], image_barrier_square[square_image_num[0][5]]],318 [image_barrier_right[right_tri_image_num[1][0]], image_barrier_square[square_image_num[0][2]], image_barrier_square[square_image_num[0][5]]],319 [image_barrier_right[right_tri_image_num[1][0]], image_barrier_square[square_image_num[0][3]], image_barrier_square[square_image_num[0][5]]],320 [image_barrier_right[right_tri_image_num[1][0]], image_barrier_square[square_image_num[0][4]], image_barrier_square[square_image_num[0][5]]],321 [image_barrier_right[right_tri_image_num[1][1]], image_barrier_square[square_image_num[0][5]], image_barrier_square[square_image_num[0][5]]],322 [image_barrier_right[right_tri_image_num[1][2]], image_barrier_square[square_image_num[0][5]], image_barrier_square[square_image_num[0][5]]],323 ],324 [325 [image_barrier_right[right_tri_image_num[2][0]], image_barrier_square[square_image_num[0][0]], image_barrier_square[square_image_num[0][0]]],326 [image_barrier_right[right_tri_image_num[2][0]], image_barrier_square[square_image_num[0][0]], image_barrier_square[square_image_num[0][1]]],327 [image_barrier_right[right_tri_image_num[2][0]], image_barrier_square[square_image_num[0][0]], image_barrier_square[square_image_num[0][2]]],328 [image_barrier_right[right_tri_image_num[2][0]], image_barrier_square[square_image_num[0][0]], image_barrier_square[square_image_num[0][3]]],329 [image_barrier_right[right_tri_image_num[2][0]], image_barrier_square[square_image_num[0][0]], image_barrier_square[square_image_num[0][4]]],330 [image_barrier_right[right_tri_image_num[2][0]], image_barrier_square[square_image_num[0][1]], image_barrier_square[square_image_num[0][5]]],331 [image_barrier_right[right_tri_image_num[2][0]], image_barrier_square[square_image_num[0][2]], image_barrier_square[square_image_num[0][5]]],332 [image_barrier_right[right_tri_image_num[2][0]], image_barrier_square[square_image_num[0][3]], image_barrier_square[square_image_num[0][5]]],333 [image_barrier_right[right_tri_image_num[2][0]], image_barrier_square[square_image_num[0][4]], image_barrier_square[square_image_num[0][5]]],334 [image_barrier_right[right_tri_image_num[2][1]], image_barrier_square[square_image_num[0][5]], image_barrier_square[square_image_num[0][5]]],335 ],336 [337 [image_barrier_right[right_tri_image_num[3][0]], image_barrier_square[square_image_num[1][0]], image_barrier_square[square_image_num[0][0]]],338 [image_barrier_right[right_tri_image_num[3][0]], image_barrier_square[square_image_num[1][0]], image_barrier_square[square_image_num[0][1]]],339 [image_barrier_right[right_tri_image_num[3][0]], image_barrier_square[square_image_num[1][0]], image_barrier_square[square_image_num[0][2]]],340 [image_barrier_right[right_tri_image_num[3][0]], image_barrier_square[square_image_num[1][0]], image_barrier_square[square_image_num[0][3]]],341 [image_barrier_right[right_tri_image_num[3][0]], image_barrier_square[square_image_num[1][0]], image_barrier_square[square_image_num[0][4]]],342 [image_barrier_right[right_tri_image_num[3][0]], image_barrier_square[square_image_num[1][1]], image_barrier_square[square_image_num[0][5]]],343 [image_barrier_right[right_tri_image_num[3][0]], image_barrier_square[square_image_num[1][2]], image_barrier_square[square_image_num[0][5]]],344 [image_barrier_right[right_tri_image_num[3][0]], image_barrier_square[square_image_num[1][3]], image_barrier_square[square_image_num[0][5]]],345 [image_barrier_right[right_tri_image_num[3][0]], image_barrier_square[square_image_num[1][4]], image_barrier_square[square_image_num[0][5]]],346 ],347 [348 [image_barrier_right[right_tri_image_num[3][0]], image_barrier_square[square_image_num[2][0]], image_barrier_square[square_image_num[0][0]]],349 [image_barrier_right[right_tri_image_num[3][0]], image_barrier_square[square_image_num[2][0]], image_barrier_square[square_image_num[0][1]]],350 [image_barrier_right[right_tri_image_num[3][0]], image_barrier_square[square_image_num[2][0]], image_barrier_square[square_image_num[0][2]]],351 [image_barrier_right[right_tri_image_num[3][0]], image_barrier_square[square_image_num[2][0]], image_barrier_square[square_image_num[0][3]]],352 [image_barrier_right[right_tri_image_num[3][0]], image_barrier_square[square_image_num[2][0]], image_barrier_square[square_image_num[0][4]]],353 [image_barrier_right[right_tri_image_num[3][0]], image_barrier_square[square_image_num[2][1]], image_barrier_square[square_image_num[0][5]]],354 [image_barrier_right[right_tri_image_num[3][0]], image_barrier_square[square_image_num[2][2]], image_barrier_square[square_image_num[0][5]]],355 [image_barrier_right[right_tri_image_num[3][0]], image_barrier_square[square_image_num[2][3]], image_barrier_square[square_image_num[0][5]]],356 ],357 [358 [image_barrier_right[right_tri_image_num[3][0]], image_barrier_square[square_image_num[3][0]], image_barrier_square[square_image_num[0][0]]],359 [image_barrier_right[right_tri_image_num[3][0]], image_barrier_square[square_image_num[3][0]], image_barrier_square[square_image_num[0][1]]],360 [image_barrier_right[right_tri_image_num[3][0]], image_barrier_square[square_image_num[3][0]], image_barrier_square[square_image_num[0][2]]],361 [image_barrier_right[right_tri_image_num[3][0]], image_barrier_square[square_image_num[3][0]], image_barrier_square[square_image_num[0][3]]],362 [image_barrier_right[right_tri_image_num[3][0]], image_barrier_square[square_image_num[3][0]], image_barrier_square[square_image_num[0][4]]],363 [image_barrier_right[right_tri_image_num[3][0]], image_barrier_square[square_image_num[3][1]], image_barrier_square[square_image_num[0][5]]],364 [image_barrier_right[right_tri_image_num[3][0]], image_barrier_square[square_image_num[3][2]], image_barrier_square[square_image_num[0][5]]],365 ],366 [367 [image_barrier_right[right_tri_image_num[3][0]], image_barrier_square[square_image_num[4][0]], image_barrier_square[square_image_num[0][0]]],368 [image_barrier_right[right_tri_image_num[3][0]], image_barrier_square[square_image_num[4][0]], image_barrier_square[square_image_num[0][1]]],369 [image_barrier_right[right_tri_image_num[3][0]], image_barrier_square[square_image_num[4][0]], image_barrier_square[square_image_num[0][2]]],370 [image_barrier_right[right_tri_image_num[3][0]], image_barrier_square[square_image_num[4][0]], image_barrier_square[square_image_num[0][3]]],371 [image_barrier_right[right_tri_image_num[3][0]], image_barrier_square[square_image_num[4][0]], image_barrier_square[square_image_num[0][4]]],372 [image_barrier_right[right_tri_image_num[3][0]], image_barrier_square[square_image_num[4][1]], image_barrier_square[square_image_num[0][5]]],373 ],374 [375 [image_barrier_right[right_tri_image_num[3][0]], image_barrier_square[square_image_num[5][0]], image_barrier_square[square_image_num[1][0]]],376 [image_barrier_right[right_tri_image_num[3][0]], image_barrier_square[square_image_num[5][0]], image_barrier_square[square_image_num[1][1]]],377 [image_barrier_right[right_tri_image_num[3][0]], image_barrier_square[square_image_num[5][0]], image_barrier_square[square_image_num[1][2]]],378 [image_barrier_right[right_tri_image_num[3][0]], image_barrier_square[square_image_num[5][0]], image_barrier_square[square_image_num[1][3]]],379 [image_barrier_right[right_tri_image_num[3][0]], image_barrier_square[square_image_num[5][0]], image_barrier_square[square_image_num[1][4]]],380 ],381 [382 [image_barrier_right[right_tri_image_num[3][0]], image_barrier_square[square_image_num[5][0]], image_barrier_square[square_image_num[2][0]]],383 [image_barrier_right[right_tri_image_num[3][0]], image_barrier_square[square_image_num[5][0]], image_barrier_square[square_image_num[2][1]]],384 [image_barrier_right[right_tri_image_num[3][0]], image_barrier_square[square_image_num[5][0]], image_barrier_square[square_image_num[2][2]]],385 [image_barrier_right[right_tri_image_num[3][0]], image_barrier_square[square_image_num[5][0]], image_barrier_square[square_image_num[2][3]]],386 ],387 [388 [image_barrier_right[right_tri_image_num[3][0]], image_barrier_square[square_image_num[5][0]], image_barrier_square[square_image_num[3][0]]],389 [image_barrier_right[right_tri_image_num[3][0]], image_barrier_square[square_image_num[5][0]], image_barrier_square[square_image_num[3][1]]],390 [image_barrier_right[right_tri_image_num[3][0]], image_barrier_square[square_image_num[5][0]], image_barrier_square[square_image_num[3][2]]],391 ],392 [393 [image_barrier_right[right_tri_image_num[3][0]], image_barrier_square[square_image_num[5][0]], image_barrier_square[square_image_num[4][0]]],394 [image_barrier_right[right_tri_image_num[3][0]], image_barrier_square[square_image_num[5][0]], image_barrier_square[square_image_num[4][1]]],395 ],396 [397 [image_barrier_right[right_tri_image_num[3][0]], image_barrier_square[square_image_num[5][0]], image_barrier_square[square_image_num[5][0]]],398 ],399 ];400 this.middle_image_1 = middle_barrier_image[this.up_damaged][this.down_damaged][0];401 this.middle_image_2 = middle_barrier_image[this.up_damaged][this.down_damaged][1];402 this.left_image_1 = left_barrier_image[this.left_up_damaged][this.left_down_damaged][0];403 this.left_image_2 = left_barrier_image[this.left_up_damaged][this.left_down_damaged][1];404 this.left_image_3 = left_barrier_image[this.left_up_damaged][this.left_down_damaged][2];405 this.right_image_1 = right_barrier_image[this.right_up_damaged][this.right_down_damaged][0];406 this.right_image_2 = right_barrier_image[this.right_up_damaged][this.right_down_damaged][1];407 this.right_image_3 = right_barrier_image[this.right_up_damaged][this.right_down_damaged][2];408 }409 crash_effect(a){//Effect of crash when a bullet collide with a barrier it'll work410 mx = a.bullet[0].position_x411 my = a.bullet[0].position_y412 a.bullet[0].bullet_break = 1;//bullet_break is 1 bullet will deleted413 monsterBulletEffectTimer = frameCount;414 }415 m_hitRange(object, currentlevel) {//Check monster bullet position and if it hits barrier, barrier has 1 up damage. 416 var left_x = this.x - this.image_size;417 var right_x = this.x + this.image_size;418 var Y = this.y;419 var object_size = 16;420 if (object.position_x < left_x + this.image_size / 2 && object.position_x >= left_x - this.image_size / 2 ) {421 this.monster_object_status = 1;422 }423 if (object.position_x <= right_x + this.image_size / 2 && object.position_x > right_x - this.image_size / 2 ) {424 this.monster_object_status = 3;425 }426 if (object.position_x <= this.x + this.image_size / 2 && object.position_x >= this.x - this.image_size / 2 ) {427 this.monster_object_status = 2;428 } //Check left, right, and middle429 switch (this.monster_object_status) {430 case 1://Collision range according to damage and position431 if (this.left_up_damaged < 3 ) {432 Y = this.y - object_size;433 } else if (this.left_up_damaged < 7) {434 Y = this.y - object_size / 2;435 } else if (this.left_up_damaged < 11) {436 Y = this.y + object_size / 2;437 }438 if (object.position_y >= Y && object.position_y <= Y + this.image_size / 2 && (this.left_up_damaged + this.left_down_damaged < 11)) {439 this.crash_effect(currentlevel)440 this.left_up_damaged++;441 this.monster_object_status = 0;442 }443 444 break;445 case 2:446 if (this.up_damaged < 4) {447 Y = this.y - object_size - object_size / 2;448 } else if (this.up_damaged < 8) {449 Y = this.y - object_size / 2;450 } else if (this.up_damaged < 9) {451 Y = this.y + object_size / 2;452 }453 if (object.position_y >= Y && object.position_y <= Y + this.image_size / 2 && (this.up_damaged + this.down_damaged < 9)) {454 this.crash_effect(currentlevel)455 this.up_damaged++;456 this.monster_object_status = 0;457 }458 break;459 case 3:460 if (this.right_up_damaged < 3) {461 Y = this.y - object_size;462 } else if (this.right_up_damaged < 7) {463 Y = this.y - object_size / 2;464 } else if (this.right_up_damaged < 11) {465 Y = this.y + object_size / 2;466 }467 if (object.position_y >= Y && object.position_y <= Y + this.image_size / 2 && (this.right_up_damaged + this.right_down_damaged < 11)) {468 this.crash_effect(currentlevel)469 this.right_up_damaged++;470 this.monster_object_status = 0;471 }472 break;473 }474}475 p_hitRange(object, currentlevel) {//Check player bullet position and if it hits barrier, barrier has 1 down damage. 476 var left_x = this.x - this.image_size;477 var right_x = this.x + this.image_size;478 var Y = this.y;479 var object_size = 16;480 if (object.position_x < left_x + this.image_size / 2 && object.position_x >= left_x - this.image_size / 2 ) {481 this.player_object_status = 1;482 }483 if (object.position_x <= right_x + this.image_size / 2 && object.position_x > right_x - this.image_size / 2 ) {484 this.player_object_status = 3;485 }486 if (object.position_x <= this.x + this.image_size / 2 && object.position_x >= this.x - this.image_size / 2 ) {487 this.player_object_status = 2;488 }//Check left, right, and middle489 switch (this.player_object_status) {490 case 1://collision range according to damage and position491 if (this.left_down_damaged < 3) {492 Y = this.y + object_size + object_size / 2;493 } else if (this.left_down_damaged < 7) {494 Y = this.y + object_size / 2;495 } else if (this.left_down_damaged < 11) {496 Y = this.y - object_size / 2;497 }498 if (object.position_y >= Y - this.image_size / 2 && object.position_y <= Y && (this.left_up_damaged + this.left_down_damaged < 11)) {499 bullet_removed(attackArray)500 this.left_down_damaged++;501 this.player_object_status = 0;502 }503 break;504 case 2:505 if (this.down_damaged < 5) {506 Y = this.y + object_size / 2;507 } else if (this.down_damaged < 9) {508 Y = this.y - object_size / 2;509 }510 if (object.position_y >= Y - this.image_size / 2 && object.position_y <= Y && (this.up_damaged + this.down_damaged < 9)) {511 bullet_removed(attackArray);512 this.down_damaged++;513 this.player_object_status = 0;514 }515 break;516 case 3:517 if (this.right_down_damaged < 3) {518 Y = this.y + object_size + object_size / 2;519 } else if (this.right_down_damaged < 7) {520 Y = this.y + object_size / 2;521 } else if (this.right_down_damaged < 11) {522 Y = this.y - object_size / 2;523 }524 if (object.position_y >= Y - this.image_size / 2 && object.position_y <= Y && (this.right_up_damaged + this.right_down_damaged < 11)) {525 bullet_removed(attackArray)526 this.right_down_damaged++;527 this.player_object_status = 0;528 }529 break;530 }531 }532 monsterCollision(object){//When a monster collide with a barrier barrier has damage533 var barrier_range = {534 left_bar : {535 right_x : this.x - this.image_size+this.image_size/2,536 left_x : this.x - this.image_size-this.image_size/2537 },538 right_bar : {539 right_x : this.x + this.image_size+this.image_size/2,540 left_x : this.x + this.image_size-this.image_size/2541 },542 hor_middle_bar : {543 right_x : this.x + this.image_size/2,544 left_x : this.x - this.image_size/2545 },546 up_bar : {547 up_y : this.y - this.image_size - this.image_size/2,548 down_y : this.y - this.image_size + this.image_size/2549 },550 down_bar : {551 up_y : this.y + this.image_size - this.image_size/2,552 down_y : this.y + this.image_size + this.image_size/2553 },554 ver_middle_bar : {555 up_y : this.y - this.image_size/2,556 down_y : this.y + this.image_size/2557 }558 }559 var object_size = 14;560 var object_range = {561 object_right_x : object.position_x+ object_size/2,562 object_left_x : object.position_x-object_size/2,563 object_down_y : object.position_y+object_size/2,564 object_up_y : object.position_y-object_size/2565 }566 if(collision(object_range,barrier_range.left_bar,barrier_range.up_bar) && this.left_up_damaged<3){567 this.left_up_damaged = 3;568 569 } if(collision(object_range,barrier_range.left_bar,barrier_range.ver_middle_bar) && this.left_up_damaged<7){570 this.left_up_damaged = 7;571 } if(collision(object_range,barrier_range.left_bar,barrier_range.down_bar) && this.left_up_damaged<11){572 this.left_up_damaged = 11;573 }574 if(collision(object_range,barrier_range.right_bar,barrier_range.up_bar) && this.right_up_damaged<3){575 this.right_up_damaged = 3;576 577 } if(collision(object_range,barrier_range.right_bar,barrier_range.ver_middle_bar) && this.right_up_damaged<7){578 this.right_up_damaged = 7;579 } if(collision(object_range,barrier_range.right_bar,barrier_range.down_bar) && this.right_up_damaged<11){580 this.right_up_damaged = 11;581 }582 if(collision(object_range,barrier_range.hor_middle_bar,barrier_range.up_bar) && this.up_damaged<5){583 584 this.up_damaged = 5;585 586 } if(collision(object_range,barrier_range.hor_middle_bar,barrier_range.ver_middle_bar) && this.up_damaged<9){587 this.up_damaged = 9;588 }589 }590}591function collision(object,barr_horsec,barr_versec){//Collision range592 if(object.object_right_x>=barr_horsec.left_x && object.object_left_x <= barr_horsec.right_x && object.object_up_y<=barr_versec.down_y && object.object_down_y>=barr_versec.up_y){593 return true;594 }595 return false;...

Full Screen

Full Screen

index.js

Source:index.js Github

copy

Full Screen

1// let container = document.querySelector('.container')2let board = document.querySelector('#board')3let rows = new Array(8).fill('r');4let column = new Array(8).fill('c');5let posiblemoves;6const whites = {7 king: '♔', queen: '♕', rook: '♖', bishop: '♗', knight: '♘', pawn: '♙',8}9const black = {10 king: '♚', queen: '♛', rook: '♜', bishop: '♝', knight: '♞', pawn: '♟︎',11}12let SELECTED_PIECE = null;13let STARTING_SQR = null;14let CAPTURED_PIECES = [];15let PLAYER_TURN = 'white';16let isEnPassant = false;17let enPassantTarget = null;18function renderPiece(rowIndex, colIndex) {19 /* initial definition array 20 0 -> represents black special pieces starting row21 1 -> represents black pawns starting row22 6 -> represents white pawns starting row23 7 -> represents white special pieces starting row24 */25 const initialRowPositions = [0, 1, 6, 7]26 if(initialRowPositions.includes(rowIndex)){27 // create a piece element, this will be move between squares28 const piece = document.createElement('span');29 piece.classList.add('piece')30 const blackPieces = {31 0: black.rook, 32 1: black.knight,33 2: black.bishop, 34 3: black.queen,35 4: black.king, 36 5: black.bishop, 37 6: black.knight,38 7: black.rook, 39 }40 const whitePieces = {41 0: whites.rook, 42 1: whites.knight,43 2: whites.bishop, 44 3: whites.queen,45 4: whites.king, 46 5: whites.bishop, 47 6: whites.knight,48 7: whites.rook, 49 }50 // check which piece will go to certain squares51 if(rowIndex === 0) {52 piece.innerHTML = blackPieces[colIndex]53 piece.setAttribute('data-color', 'black')54 piece.setAttribute('data-piece', blackPieces[colIndex])55 }56 if(rowIndex === 1) {57 piece.innerHTML = black.pawn;58 piece.setAttribute('data-color', 'black')59 piece.setAttribute('data-piece', black.pawn);60 }61 if(rowIndex === 7) {62 piece.innerHTML = whitePieces[colIndex]63 piece.setAttribute('data-color', 'white')64 piece.setAttribute('data-piece', whitePieces[colIndex]);65 }66 if(rowIndex === 6) {67 piece.innerHTML = whites.pawn68 piece.setAttribute('data-color', 'white')69 piece.setAttribute('data-piece', whites.pawn);70 }71 piece.setAttribute('id', `r${rowIndex}_c${colIndex}`);72 return piece;73 }74 return null75}76function initGame() {77 rows.forEach((row, rowIndex)=> {78 column.forEach((col, colIndex) => {79 80 const piece = renderPiece(rowIndex, colIndex);81 const color = () => {82 if(rowIndex % 2 === 0) {83 return colIndex % 2 === 0 ? 'white' : 'black';84 }85 return colIndex % 2 === 0 ? 'black' : 'white';86 }87 const square = document.createElement('div');88 89 square.setAttribute('data-index', `${row}${rowIndex}_${col}${colIndex}`);90 square.classList.add('cell');91 square.classList.add(color());92 if(piece) {93 94 const pieceColor = piece.getAttribute('data-color')95 square.appendChild(piece);96 if(pieceColor === PLAYER_TURN) {97 square.addEventListener('click', selectPiece)98 }99 }100 board.appendChild(square);101 })102 });103}104// function to help for checkubg pieceType and check possible moves to delete105function possibleMoves(piece, position) {106 const pieceType = piece.getAttribute("data-piece");107 const pieceColor = piece.getAttribute("data-color");108 const row = position.split('_')[0]109 const col = position.split('_')[1]110 if(pieceType == whites.pawn || pieceType == black.pawn) {111 PawnPossibleMoves(row, col);112 }113 if(pieceType == whites.rook || pieceType == black.rook) {114 straightMove(row, col, pieceType, pieceColor);115 }116 if(pieceType == whites.bishop || pieceType == black.bishop) {117 diagonalMove(row, col, pieceType, pieceColor);118 }119 if(pieceType == whites.king || pieceType == black.king) {120 straightMove(row, col, pieceType, pieceColor);121 diagonalMove(row, col, pieceType, pieceColor);122 } 123 if(pieceType == whites.queen || pieceType == black.queen) {124 straightMove(row, col, pieceType, pieceColor);125 diagonalMove(row, col, pieceType, pieceColor);126 }127 if(pieceType == whites.knight || pieceType == black.knight) {128 knightMove(row, col)129 }130}131function selectPiece(e) {132 const square = e.currentTarget;133 const position = square.getAttribute('data-index');134 const piece = document.querySelector(`#${position}`);135 possibleMoves(piece, position)136 STARTING_SQR = square;137 SELECTED_PIECE = piece;138 document.addEventListener('mousemove', movePiece);139}140function movePiece(e) {141 const squares = document.querySelectorAll('[data-index]');142 squares.forEach((sq) => {143 sq.removeAttribute("style");144 if (sq.childNodes[0] && sq.childNodes[0].getAttribute("class") == 'piece'){145 sq.removeEventListener('click', selectPiece);146 }147 })148 SELECTED_PIECE.style.position = 'absolute';149 SELECTED_PIECE.style.cursor = 'move'150 SELECTED_PIECE.style.cursor = 'grabbing'151 SELECTED_PIECE.style['pointer-events'] = 'none'152 SELECTED_PIECE.style.left = `${e.pageX - 25}px`153 SELECTED_PIECE.style.top = `${e.pageY - 25}px`154}155function wouldEnPassant(targetSquare, startingRow) {156 const position = targetSquare.getAttribute('data-index');157 const row = position.split('_')[0];158 const col = position.split('_')[1];159 const leftSquare = document.querySelector(`[data-index=${row}_c${parseInt(col[1]) - 1}]`);160 const rightSquare = document.querySelector(`[data-index=${row}_c${parseInt(col[1]) + 1}]`);161 if(startingRow == 'r1' || startingRow == 'r6') {162 // check if there's an adjacent piece to target square;163 if(leftSquare?.childNodes[0]) {164 const pieceOnLeft = leftSquare.childNodes[0];165 const pieceType = pieceOnLeft.getAttribute('data-piece');166 const pieceOnLeftColor = pieceOnLeft.getAttribute('data-color');167 const pawnPiece = [whites.pawn, black.pawn]168 if(pawnPiece.includes(pieceType) && pieceOnLeftColor !== PLAYER_TURN) {169 isEnPassant = true;170 }171 }172 if(rightSquare?.childNodes[0]) {173 const pieceOnRight = rightSquare.childNodes[0];174 const pieceType = pieceOnRight.getAttribute('data-piece');175 const pieceOnRightColor = pieceOnRight.getAttribute('data-color');176 const pawnPiece = [whites.pawn, black.pawn]177 if(pawnPiece.includes(pieceType) && pieceOnRightColor !== PLAYER_TURN) {178 isEnPassant = true;179 }180 }181 }182}183function dropPiece(e) {184 const targetSquare = e.currentTarget;185 capturePiece(targetSquare, SELECTED_PIECE);186 const pieceData = SELECTED_PIECE.getAttribute('data-piece');187 const startingPostiion = SELECTED_PIECE.getAttribute('id');188 const startingRow = startingPostiion.split('_')[0];189 if(pieceData == whites.pawn || pieceData == black.pawn) {190 promotePawn(targetSquare, SELECTED_PIECE);191 wouldEnPassant(targetSquare, startingRow)192 }193 targetSquare.appendChild(SELECTED_PIECE);194 const position = targetSquare.getAttribute('data-index');195 targetSquare.removeEventListener('click', dropPiece);196 SELECTED_PIECE.removeAttribute("style");197 SELECTED_PIECE.setAttribute('id', position);198 document.removeEventListener('mousemove', movePiece);199 const squares = document.querySelectorAll('[data-index]');200 switchTurn();201 squares.forEach((sq) => {202 sq.removeAttribute("style");203 if (sq.childNodes[0] && sq.childNodes[0].getAttribute("class") == 'highlight'){204 sq.removeChild(sq.childNodes[0])205 sq.removeEventListener('click', selectPiece);206 }207 if (sq.childNodes[0] && sq.childNodes[0].getAttribute("class") == 'piece'){208 if(sq.childNodes[0].getAttribute("data-color") === PLAYER_TURN) {209 sq.addEventListener('click', selectPiece)210 }211 }212 })213 SELECTED_PIECE = null;214}215function capturePiece(targetSquare, newPiece) {216 const piece = targetSquare.childNodes[0];217 if(isEnPassant && enPassantTarget) {218 CAPTURED_PIECES.push(piece.getAttribute('data-piece'));219 enPassantTarget.removeChild(enPassantTarget.childNodes[0]);220 isEnPassant = false;221 enPassantTarget = null;222 }223 const attribute = targetSquare.childNodes[0].getAttribute('class');224 if(attribute == 'piece') {225 CAPTURED_PIECES.push(piece.getAttribute('data-piece'))226 targetSquare.replaceChild(newPiece, piece);227 }228}229function promotePawn(targetSquare, piece) {230 const position = targetSquare.getAttribute('data-index');231 const row = position.split('_')[0];232 233 if(row == 'r0' || row == 'r7') {234 console.log('pawn ready for promotion');235 }236}237function switchTurn() {238 const prevPlayer = PLAYER_TURN;239 const newPlayer = prevPlayer == 'white' ? 'black' : 'white';240 PLAYER_TURN = newPlayer241}242function PawnPossibleMoves(row, col) {243 const direction = (x, num = row[1]) => (PLAYER_TURN == 'white' ? parseInt(num) - x : parseInt(num) + x);244 245 // check if pawn is from starting row246 // if from starting row, add ability for pawn to moves 2 squares247 if(row == 'r6' || row == 'r1') {248 const leftDiagSqr = getTargetSquare(direction(1), col[1] - 1);249 const rightDiagSqr = getTargetSquare(direction(1), parseInt(col[1]) + 1);250 if(leftDiagSqr?.childNodes[0]) {251 highlightPossibleSquare(leftDiagSqr)252 }253 if(rightDiagSqr?.childNodes[0]) {254 highlightPossibleSquare(rightDiagSqr)255 }256 const startingRow = PLAYER_TURN === 'white' ? 6 : 1;257 for(let x = 1; x <= 2; x++) {258 const square = getTargetSquare(direction(x, startingRow), col[1]);259 if(!square.childNodes[0]) {260 highlightPossibleSquare(square)261 }262 }263 } else {264 const square = getTargetSquare(direction(1), col[1]);265 const leftDiagSqr = getTargetSquare(direction(1), col[1] - 1);266 const rightDiagSqr = getTargetSquare(direction(1), parseInt(col[1]) + 1);267 const left = getTargetSquare(row[1], col[1] - 1);268 const right = getTargetSquare(row[1], parseInt(col[1]) + 1);269 if(leftDiagSqr?.childNodes[0] || (left?.childNodes[0] && isEnPassant) ) {270 highlightPossibleSquare(leftDiagSqr)271 enPassantTarget = left;272 }273 274 if(rightDiagSqr?.childNodes[0] || (right?.childNodes[0] && isEnPassant)) {275 highlightPossibleSquare(rightDiagSqr);276 enPassantTarget = right;277 }278 if(!square.childNodes[0]) {279 highlightPossibleSquare(square)280 }281 }282}283// function helper to highlight possible moves and 284// add click listener to drop piece on that square285function highlightPossibleSquare(square) {286 const highlight = document.createElement('div');287 highlight.classList.add('highlight');288 square.appendChild(highlight)289 square.addEventListener('click', dropPiece);290}291// NOTE: break -> forcibly stops the loop;292function diagonalMove(row, col, piece, color) {293 let cUpRight = parseInt(col[1]) + 1;294 let cUpLeft = parseInt(col[1]) - 1;295 let cRight = parseInt(col[1]) + 1;296 let cLeft = parseInt(col[1]) - 1;297 if(piece == whites.king || 298 piece == black.king) {299 const upperLeftSqr = getTargetSquare(row[1] - 1, cUpLeft);300 const upperRightSqr = getTargetSquare(row[1] - 1, cUpRight);301 const lowerLeftSqr = getTargetSquare(parseInt(row[1]) + 1, cLeft);302 const lowerRightSqr = getTargetSquare(parseInt(row[1]) + 1, cRight);303 if(upperLeftSqr && wouldCapture(upperLeftSqr)) {304 highlightPossibleSquare(upperLeftSqr)305 }306 if(upperRightSqr && wouldCapture(upperRightSqr)) {307 highlightPossibleSquare(upperRightSqr)308 }309 if(lowerLeftSqr && wouldCapture(lowerLeftSqr)) {310 highlightPossibleSquare(lowerLeftSqr)311 }312 if(lowerRightSqr && wouldCapture(lowerRightSqr)) {313 highlightPossibleSquare(lowerRightSqr)314 }315 }316 else {317 for(let r = row[1] - 1; r >= 0; r--) {318 const upperLeftSqr = getTargetSquare(r, cUpLeft);319 if(upperLeftSqr && isBlocked(color, upperLeftSqr)) {320 if(upperLeftSqr && wouldCapture(upperLeftSqr)) {321 highlightPossibleSquare(upperLeftSqr)322 }323 // if piece is blocked by a piece break/stop the loop;324 break;325 }326 if(upperLeftSqr && wouldCapture(upperLeftSqr)) {327 highlightPossibleSquare(upperLeftSqr)328 }329 cUpLeft--330 }331 for(let r = row[1] - 1; r >= 0; r--) {332 const upperRightSqr = getTargetSquare(r, cUpRight);333 if(upperRightSqr && isBlocked(color, upperRightSqr)) {334 if(upperRightSqr && wouldCapture(upperRightSqr)) {335 highlightPossibleSquare(upperRightSqr)336 }337 break;338 }339 if(upperRightSqr && wouldCapture(upperRightSqr)) {340 highlightPossibleSquare(upperRightSqr)341 }342 cUpRight++343 }344 // ======= lower side of origin square =======345 for(let r = parseInt(row[1]) + 1; r <= 7; r++) {346 const lowerLeftSqr = getTargetSquare(r, cLeft);347 if(lowerLeftSqr && isBlocked(color, lowerLeftSqr)) {348 if(lowerLeftSqr && wouldCapture(lowerLeftSqr)) {349 highlightPossibleSquare(lowerLeftSqr)350 }351 break;352 }353 if(lowerLeftSqr && wouldCapture(lowerLeftSqr)) {354 highlightPossibleSquare(lowerLeftSqr)355 }356 cLeft--357 }358 for(let r = parseInt(row[1]) + 1; r <= 7; r++) {359 const lowerRightSqr = getTargetSquare(r, cRight);360 if(lowerRightSqr && isBlocked(color, lowerRightSqr)) {361 if(lowerRightSqr && wouldCapture(lowerRightSqr)) {362 highlightPossibleSquare(lowerRightSqr)363 }364 break;365 }366 if(lowerRightSqr && wouldCapture(lowerRightSqr)) {367 highlightPossibleSquare(lowerRightSqr)368 }369 cRight++370 }371 }372}373function straightMove(row, col, piece, color) {374 if(piece == whites.king || 375 piece == black.king) {376 const upperSquare = getTargetSquare(row[1] - 1, col[1]);377 if(upperSquare && wouldCapture(upperSquare)) {378 highlightPossibleSquare(upperSquare)379 }380 const lowerSquare = getTargetSquare(parseInt(row[1]) + 1, col[1]);381 382 if(lowerSquare && wouldCapture(lowerSquare)) {383 highlightPossibleSquare(lowerSquare)384 }385 const left = getTargetSquare(row[1], col[1] - 1);386 387 if(left && wouldCapture(left)) {388 highlightPossibleSquare(left)389 }390 const right = getTargetSquare(row[1], parseInt(col[1]) + 1);391 392 if(right && wouldCapture(right)) {393 highlightPossibleSquare(right)394 }395 }396 else {397 for(let r = row[1] - 1; r >= 0; r--) {398 const upperSquare = getTargetSquare(r, col[1]);399 if(isBlocked(color, upperSquare)) {400 if(wouldCapture(upperSquare)) {401 highlightPossibleSquare(upperSquare)402 }403 break;404 }405 if(upperSquare) {406 highlightPossibleSquare(upperSquare)407 }408 }409 for(let r = parseInt(row[1]) + 1; r <= 7; r++) {410 const lowerSquare = getTargetSquare(r, col[1]);411 if(isBlocked(color, lowerSquare)) {412 if(wouldCapture(lowerSquare)) {413 highlightPossibleSquare(lowerSquare)414 }415 break;416 }417 if(lowerSquare) {418 highlightPossibleSquare(lowerSquare)419 }420 }421 for(let c = col[1] - 1; c >= 0; c--) {422 const left = getTargetSquare(row[1], c);423 if(isBlocked(color, left)) {424 if(wouldCapture(left)) {425 highlightPossibleSquare(left)426 }427 break;428 }429 if(left) {430 highlightPossibleSquare(left)431 }432 }433 for(let c = parseInt(col[1]) + 1; c <= 7; c++) {434 const right = getTargetSquare(row[1], c);435 if(isBlocked(color, right)) {436 if(wouldCapture(right)) {437 highlightPossibleSquare(right)438 }439 break;440 }441 if(right) {442 highlightPossibleSquare(right)443 }444 }445 }446}447function knightMove(row, col) {448 const r = parseInt(row[1]);449 const c = parseInt(col[1]);450 const upperRightSqr = getTargetSquare(r + 2, c + 1);451 const upperLeftSqr = getTargetSquare(r + 2, c - 1);452 const lowerRightSqr = getTargetSquare(r - 2, c + 1);453 const lowerLeftSqr = getTargetSquare(r - 2, c - 1);454 const upperRightSqr2 = getTargetSquare(r + 1, c + 2);455 const upperLeftSqr2 = getTargetSquare(r + 1, c - 2);456 const lowerRightSqr2 = getTargetSquare(r - 1, c + 2);457 const lowerLeftSqr2 = getTargetSquare(r - 1, c - 2);458 if(upperRightSqr && wouldCapture(upperRightSqr)) highlightPossibleSquare(upperRightSqr);459 if(upperLeftSqr && wouldCapture(upperLeftSqr)) highlightPossibleSquare(upperLeftSqr);460 if(lowerRightSqr && wouldCapture(lowerRightSqr)) highlightPossibleSquare(lowerRightSqr);461 if(lowerLeftSqr && wouldCapture(lowerLeftSqr)) highlightPossibleSquare(lowerLeftSqr);462 if(upperRightSqr2 && wouldCapture(upperRightSqr2)) highlightPossibleSquare(upperRightSqr2);463 if(upperLeftSqr2 && wouldCapture(upperLeftSqr2)) highlightPossibleSquare(upperLeftSqr2);464 if(lowerRightSqr2 && wouldCapture(lowerRightSqr2)) highlightPossibleSquare(lowerRightSqr2);465 if(lowerLeftSqr2 && wouldCapture(lowerLeftSqr2)) highlightPossibleSquare(lowerLeftSqr2);466}467// checker if a piece can capture a piece 468// check if piece color of target piece if opposite of player can capture469function wouldCapture(targetSquare) {470 if(targetSquare.childNodes[0]) {471 return targetSquare.childNodes[0].getAttribute('data-color') !== PLAYER_TURN;472 }473 console.log('would capture')474 return !targetSquare.childNodes[0]475}476function getTargetSquare(row, col) {477 return document.querySelector(`[data-index='r${row}_c${col}']`);478}479// checker if a piece path is blocked by another piece480// to prevent SELECTED_PIECE from jumping behind pieces481function isBlocked(color, targetSquare) {482 let targetColor;483 if(targetSquare.childNodes[0]) {484 targetColor = targetSquare.childNodes[0].getAttribute('data-color');485 }486 if(color == targetColor || targetSquare.childNodes[0]?.getAttribute('class') === 'piece') {487 return true488 }489 return false490}...

Full Screen

Full Screen

offBoard.js

Source:offBoard.js Github

copy

Full Screen

1import React from "react";2import { useState, useEffect } from "react";3import Square from "./Square";4import useWindowSize from 'react-use/lib/useWindowSize'5import Confetti from 'react-confetti'6export default function OffBoard() {7 const { width, height } = useWindowSize()8 const [squares, setsquares] = useState(["", "", "", "", "", "", "", "", ""]);9 const [winner, setWinner] = useState("");10 const Patterns = [11 [0, 1, 2],12 [3, 4, 5],13 [6, 7, 8],14 [0, 3, 6],15 [1, 4, 7],16 [2, 5, 8],17 [0, 4, 8],18 [2, 4, 6],19 ];20 const computer_turn = (index) => {21 let newSquares = [...squares];22 newSquares[index] = "o";23 setTimeout(() => setsquares(newSquares), 150);24 console.log("ahmad");25 };26 const winning_lines = (a, b, c) => {27 return Patterns.filter((squareIndexs) => {28 const squareValues = squareIndexs.map((index) => squares[index]);29 return (30 JSON.stringify([a, b, c].sort()) === JSON.stringify(squareValues.sort())31 );32 });33 };34 const chooseSquare = (index) => {35 if (!winner) {36 const isPlayerTurn =37 squares.filter((square) => square !== "").length % 2 === 0;38 if (isPlayerTurn) {39 let newSquares = [...squares];40 newSquares[index] = "x";41 setsquares(newSquares);42 }43 }44 };45 useEffect(() => {46 let isComputerTurn =47 squares.filter((square) => square !== "").length % 2 === 1;48 const playerWon = winning_lines("x", "x", "x").length > 0;49 const computerWon = winning_lines("o", "o", "o").length > 0;50 if (playerWon || computerWon) isComputerTurn = false;51 if (playerWon) {52 setWinner("x");53 }54 if (computerWon) {55 setWinner("o");56 }57 if (isComputerTurn) {58 const winBlock1 = winning_lines("x", "x", "");59 if (winBlock1.length > 0) {60 const blockIndex = winBlock1[0].filter(61 (index) => squares[index] === ""62 )[0];63 computer_turn(blockIndex);64 return;65 }66 const winBlock2 = winning_lines("o", "o", "");67 if (winBlock2.length > 0) {68 const blockIndex = winBlock2[0].filter(69 (index) => squares[index] === ""70 )[0];71 computer_turn(blockIndex);72 return;73 }74 const winBlock3 = winning_lines("o", "", "");75 if (winBlock3.length > 0) {76 const blockIndex = winBlock3[0].filter(77 (index) => squares[index] === ""78 )[0];79 computer_turn(blockIndex);80 return;81 }82 const emptyIndexs = squares83 .map((square, index) => (square === "" ? index : ""))84 .filter((val) => val != "");85 const randomIndex =86 emptyIndexs[Math.ceil(Math.random() * emptyIndexs.length)];87 computer_turn(randomIndex);88 }89 }, [squares]);90 return (91 <div>92 <div className="board-container">93 <div className="row">94 <Square95 chooseSquare={() => chooseSquare(0)}96 x={squares[0] === "x" ? 1 : 0}97 o={squares[0] === "o" ? 1 : 0}98 />99 <Square100 chooseSquare={() => chooseSquare(1)}101 x={squares[1] === "x" ? 1 : 0}102 o={squares[1] === "o" ? 1 : 0}103 />104 <Square105 chooseSquare={() => chooseSquare(2)}106 x={squares[2] === "x" ? 1 : 0}107 o={squares[2] === "o" ? 1 : 0}108 />109 </div>110 <div className="row">111 <Square112 chooseSquare={() => chooseSquare(3)}113 x={squares[3] === "x" ? 1 : 0}114 o={squares[3] === "o" ? 1 : 0}115 />116 <Square117 chooseSquare={() => chooseSquare(4)}118 x={squares[4] === "x" ? 1 : 0}119 o={squares[4] === "o" ? 1 : 0}120 />121 <Square122 chooseSquare={() => chooseSquare(5)}123 x={squares[5] === "x" ? 1 : 0}124 o={squares[5] === "o" ? 1 : 0}125 />126 </div>127 <div className="row">128 <Square129 chooseSquare={() => chooseSquare(6)}130 x={squares[6] === "x" ? 1 : 0}131 o={squares[6] === "o" ? 1 : 0}132 />133 <Square134 chooseSquare={() => chooseSquare(7)}135 x={squares[7] === "x" ? 1 : 0}136 o={squares[7] === "o" ? 1 : 0}137 />138 <Square139 chooseSquare={() => chooseSquare(8)}140 x={squares[8] === "x" ? 1 : 0}141 o={squares[8] === "o" ? 1 : 0}142 />143 </div>144 </div>145 {winner && winner === "x" ? (146 <div>you won</div>147 ) : winner && winner === "o" ? (148 <div>you lost</div>149 ) : null}150 {winner ? <Confetti151 width={width}152 height={height}153 />:null}154 </div>155 );156}import React from "react";157import { useState, useEffect } from "react";158import Square from "./Square";159import useWindowSize from 'react-use/lib/useWindowSize'160import Confetti from 'react-confetti'161export default function OffBoard() {162 const { width, height } = useWindowSize()163 const [squares, setsquares] = useState(["", "", "", "", "", "", "", "", ""]);164 const [winner, setWinner] = useState("");165 const Patterns = [166 [0, 1, 2],167 [3, 4, 5],168 [6, 7, 8],169 [0, 3, 6],170 [1, 4, 7],171 [2, 5, 8],172 [0, 4, 8],173 [2, 4, 6],174 ];175 const computer_turn = (index) => {176 let newSquares = [...squares];177 newSquares[index] = "o";178 setTimeout(() => setsquares(newSquares), 150);179 console.log("ahmad");180 };181 const winning_lines = (a, b, c) => {182 return Patterns.filter((squareIndexs) => {183 const squareValues = squareIndexs.map((index) => squares[index]);184 return (185 JSON.stringify([a, b, c].sort()) === JSON.stringify(squareValues.sort())186 );187 });188 };189 const chooseSquare = (index) => {190 if (!winner) {191 const isPlayerTurn =192 squares.filter((square) => square !== "").length % 2 === 0;193 if (isPlayerTurn) {194 let newSquares = [...squares];195 newSquares[index] = "x";196 setsquares(newSquares);197 }198 }199 };200 useEffect(() => {201 let isComputerTurn =202 squares.filter((square) => square !== "").length % 2 === 1;203 const playerWon = winning_lines("x", "x", "x").length > 0;204 const computerWon = winning_lines("o", "o", "o").length > 0;205 if (playerWon || computerWon) isComputerTurn = false;206 if (playerWon) {207 setWinner("x");208 }209 if (computerWon) {210 setWinner("o");211 }212 if (isComputerTurn) {213 const winBlock1 = winning_lines("x", "x", "");214 if (winBlock1.length > 0) {215 const blockIndex = winBlock1[0].filter(216 (index) => squares[index] === ""217 )[0];218 computer_turn(blockIndex);219 return;220 }221 const winBlock2 = winning_lines("o", "o", "");222 if (winBlock2.length > 0) {223 const blockIndex = winBlock2[0].filter(224 (index) => squares[index] === ""225 )[0];226 computer_turn(blockIndex);227 return;228 }229 const winBlock3 = winning_lines("o", "", "");230 if (winBlock3.length > 0) {231 const blockIndex = winBlock3[0].filter(232 (index) => squares[index] === ""233 )[0];234 computer_turn(blockIndex);235 return;236 }237 const emptyIndexs = squares238 .map((square, index) => (square === "" ? index : ""))239 .filter((val) => val != "");240 const randomIndex =241 emptyIndexs[Math.ceil(Math.random() * emptyIndexs.length)];242 computer_turn(randomIndex);243 }244 }, [squares]);245 return (246 <div>247 <div className="board-container">248 <div className="row">249 <Square250 chooseSquare={() => chooseSquare(0)}251 x={squares[0] === "x" ? 1 : 0}252 o={squares[0] === "o" ? 1 : 0}253 />254 <Square255 chooseSquare={() => chooseSquare(1)}256 x={squares[1] === "x" ? 1 : 0}257 o={squares[1] === "o" ? 1 : 0}258 />259 <Square260 chooseSquare={() => chooseSquare(2)}261 x={squares[2] === "x" ? 1 : 0}262 o={squares[2] === "o" ? 1 : 0}263 />264 </div>265 <div className="row">266 <Square267 chooseSquare={() => chooseSquare(3)}268 x={squares[3] === "x" ? 1 : 0}269 o={squares[3] === "o" ? 1 : 0}270 />271 <Square272 chooseSquare={() => chooseSquare(4)}273 x={squares[4] === "x" ? 1 : 0}274 o={squares[4] === "o" ? 1 : 0}275 />276 <Square277 chooseSquare={() => chooseSquare(5)}278 x={squares[5] === "x" ? 1 : 0}279 o={squares[5] === "o" ? 1 : 0}280 />281 </div>282 <div className="row">283 <Square284 chooseSquare={() => chooseSquare(6)}285 x={squares[6] === "x" ? 1 : 0}286 o={squares[6] === "o" ? 1 : 0}287 />288 <Square289 chooseSquare={() => chooseSquare(7)}290 x={squares[7] === "x" ? 1 : 0}291 o={squares[7] === "o" ? 1 : 0}292 />293 <Square294 chooseSquare={() => chooseSquare(8)}295 x={squares[8] === "x" ? 1 : 0}296 o={squares[8] === "o" ? 1 : 0}297 />298 </div>299 </div>300 {winner && winner === "x" ? (301 <div>you won</div>302 ) : winner && winner === "o" ? (303 <div>you lost</div>304 ) : null}305 {winner ? <Confetti306 width={width}307 height={height}308 />:null}309 </div>310 );...

Full Screen

Full Screen

script.js

Source:script.js Github

copy

Full Screen

1const numCol = 8;2const cellSizeVh = parseInt(getComputedStyle(document.body).getPropertyValue("--board-size")) / 8;3const viewportHeight = Math.max(document.documentElement.clientHeight, window.innerHeight || 0);4const cellSize = cellSizeVh * viewportHeight / 100;5var highlighted = false;6const assets = "neo-assets/";7// based on css margins of .game8const marginX = vw(50) - cellSize*numCol/2;9const marginY = vh(2);10var hovered_square_div;11class Piece {12 constructor(type, square) {13 this.type = type;14 this.squareArray = square;15 this.squareId = this.getSquareId(square);16 this.node = this.getPieceNode();17 this.displayPiece();18 this.element = document.getElementById(this.type);19 this.eventListener();20 }21 getSquareId(square) {22 let squareId = (square[1] - 1) * numCol + (square[0] - 1);23 return squareId;24 }25 getPieceNode() {26 let imgPath = assets + this.type + ".png";27 let node = document.createElement("img");28 node.setAttribute("id", this.type);29 node.setAttribute("class", "piece");30 node.setAttribute("draggable", "false");31 node.setAttribute("src", imgPath);32 node.setAttribute("alt", "Chess piece");33 node.setAttribute("style", "width:var(--cell-size);");34 return node;35 }36 getSquareDiv(square) {37 let allSquares = document.getElementById("html-board").children;38 let squareElement = allSquares[square];39 return squareElement;40 }41 getPiecePosition() {42 let pos = [];43 pos[0] = (this.squareArray[0] - 1) * cellSize;44 pos[1] = (this.squareArray[1] - 1) * cellSize;45 return pos;46 }47 // displays piece img on the html board48 displayPiece() {49 let piecesDiv = document.getElementById("pieces");50 piecesDiv.appendChild(this.node);51 let piece = document.getElementById(this.type);52 let pos = this.getPiecePosition();53 piece.style.left = pos[0].toString() + "px";54 piece.style.top = pos[1].toString() + "px";55 }56 eventListener() {57 let followMouse = true;58 let legalSquares = this.getLegalSquares();59 // click on piece60 this.element.addEventListener("mousedown", e => {61 followMouse = true62 this.element.style.cursor = "grabbing";63 let x = parseInt(e.clientX) - cellSize / 1.5;64 let y = parseInt(e.clientY) - cellSize / 1.5;65 this.element.style.left = x.toString() + "px";66 this.element.style.top = y.toString() + "px";67 if (highlighted == false) {68 this.highlightLegalSquares();69 highlighted = true;70 }71 72 let currentlyHighlightedDivs = [];73 74 // piece follow mouse75 document.addEventListener("mousemove", e => {76 if (followMouse == true) {77 // ** 1. Piece follow mouse **78 let x = parseInt(e.clientX) - cellSize / 1.5;79 let y = parseInt(e.clientY) - cellSize / 1.5;80 this.element.style.left = x.toString() + "px";81 this.element.style.top = y.toString() + "px";82 83 // ** 2. highlight hovered square **84 let hoveredSquareId = this.getHoveredSquareId(e.clientX, e.clientY);85 86 if (hoveredSquareId != undefined) { // undefined when mouse is not over board87 let hoveredSquareDiv = this.getSquareDiv(hoveredSquareId);88 hovered_square_div = hoveredSquareDiv; // future reference for unhighlighting89 if (currentlyHighlightedDivs.length > 0){90 currentlyHighlightedDivs[0].style.background = "transparent";91 currentlyHighlightedDivs.shift();92 }93 hoveredSquareDiv.style.background = "lightyellow";94 currentlyHighlightedDivs.push(hoveredSquareDiv);95 }96 }97 });98 });99 document.addEventListener("mouseup", e => {100 this.element.style.cursor = "pointer";101 if (followMouse) {102 followMouse = false;103 this.putDownPiece(e.clientX, e.clientY);104 }105 });106 }107 // called on "mousedown" in this.eventListener();108 highlightLegalSquares() {109 let legalSquares = this.getLegalSquares();110 // highlight which square the piece came from111 let squareElement = this.getSquareDiv(this.squareId);112 squareElement.style.border = "5px solid teal";113 // create circles to highlight legal squares114 for (let i of legalSquares) {115 var squareDiv = this.getSquareDiv(i);116 117 let svg = document.createElementNS("http://www.w3.org/2000/svg", "svg");118 svg.setAttribute("class", this.type + "-legalcircle");119 svg.setAttribute("width", cellSize);120 svg.setAttribute("height", cellSize);121 122 let circle = document.createElementNS("http://www.w3.org/2000/svg", "circle");123 circle.setAttribute("cx", cellSize/2);124 circle.setAttribute("cy", cellSize/2);125 circle.setAttribute("r", 20);126 circle.setAttribute("fill", "olive");127 circle.setAttribute("opacity", "0.3");128 129 svg.appendChild(circle);130 squareDiv.appendChild(svg);131 }132 }133 // find the square the mouse is on134 getHoveredSquareId(clientX, clientY) {135 let square = [Math.ceil((clientX - marginX) / cellSize), Math.ceil((clientY - marginY) / cellSize)];136 // make sure you're hovering within the board area137 if (square[0] > 8 || square[1] > 8 || square[0] < 1 || square[1] < 1) {138 console.log("returned")139 return undefined;140 }141 let squareId = this.getSquareId(square);142 return squareId;143 }144 // returns in Id format145 getLegalSquares() {146 // ** only works for rook **147 let allLegalSquares = [];148 // rook can move along the row149 for (let i = 1; i <= numCol; i++) {150 let legalSquare = []151 if (i != this.squareArray[0]) {152 legalSquare[0] = i;153 legalSquare[1] = this.squareArray[1];154 allLegalSquares.push(legalSquare);155 }156 }157 // rook can move along the column158 for (let i = 1; i <= numCol; i++) {159 let legalSquare = []160 if (i != this.squareArray[1]) {161 legalSquare[0] = this.squareArray[0];162 legalSquare[1] = i;163 allLegalSquares.push(legalSquare);164 }165 }166 167 // convert squareArray to squareId168 let legalSquaresId = [];169 for (let i = 0; i < allLegalSquares.length; i++) {170 legalSquaresId[i] = this.getSquareId(allLegalSquares[i]);171 }172 return legalSquaresId;173 174 }175 // places the piece image on the square the mouse hovers if legal.176 putDownPiece(mouseX, mouseY) {177 let legalSquares = this.getLegalSquares();178 let boardBound = document.getElementById("html-board").getBoundingClientRect();179 mouseX = Math.ceil(mouseX / cellSize);180 mouseY = Math.ceil(mouseY / cellSize);181 let mouseSquare = [mouseX, mouseY];182 let mouseSquareId = this.getSquareId(mouseSquare);183 // unhighlight squares184 let allSvgs = document.getElementsByClassName(this.type + "-legalcircle");185 let length = allSvgs.length;186 for (let i = 0; i < length; i++) {187 allSvgs[0].parentElement.removeChild(allSvgs[0]);188 }189 // unhighlight home square190 let squareElement = this.getSquareDiv(this.squareId);191 squareElement.style.border = "";192 // unhighlight hovered square193 hovered_square_div.style.background = "";194 highlighted = false;195 // update object properties, make sure mouseSquare is within 8x8 board and its legal196 if (legalSquares.includes(mouseSquareId) && mouseSquare[0] < 9 && mouseSquare[1] < 9) {197 this.squareArray = mouseSquare;198 this.squareId = mouseSquareId;199 }200 this.displayPiece();201 }202}203function randomSquare() {204 return [Math.floor(Math.random() * (numCol)) + 1, 205 Math.floor(Math.random() * (numCol)) + 1];206}207//firstPiece = new Piece("white_rook", randomSquare());208wr = new Piece("wr", [1, 8]);209// Utils functions210function vh(v) {211 var h = Math.max(document.documentElement.clientHeight, window.innerHeight || 0);212 return (v * h) / 100;213 }214 215function vw(v) {216 var w = Math.max(document.documentElement.clientWidth, window.innerWidth || 0);217 return (v * w) / 100;...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1var parent = require('stryker-parent');2var square = parent.square;3console.log(square(2));4var parent = require('stryker-parent');5var square = parent.square;6console.log(square(2));7var parent = require('stryker-parent');8var square = parent.square;9console.log(square(2));10var parent = require('stryker-parent');11var square = parent.square;12console.log(square(2));13var parent = require('stryker-parent');14var square = parent.square;15console.log(square(2));16var parent = require('stryker-parent');17var square = parent.square;18console.log(square(2));19var parent = require('stryker-parent');20var square = parent.square;21console.log(square(2));22var parent = require('stryker-parent');23var square = parent.square;24console.log(square(2));25var parent = require('stryker-parent');26var square = parent.square;27console.log(square(2));28var parent = require('stryker-parent');29var square = parent.square;30console.log(square(2));31var parent = require('stryker-parent');32var square = parent.square;33console.log(square(2));34var parent = require('stryker-parent');35var square = parent.square;36console.log(square(2));37var parent = require('stryker-parent');38var square = parent.square;39console.log(square(2));

Full Screen

Using AI Code Generation

copy

Full Screen

1var strykerParent = require('stryker-parent');2var square = strykerParent.square(5);3console.log(square);4var strykerParent = require('stryker-parent');5var square = strykerParent.square(5);6console.log(square);7module.exports = function(config) {8 config.set({9 mochaOptions: {10 },11 });12};

Full Screen

Using AI Code Generation

copy

Full Screen

1var stryker = require('stryker-parent');2var x = stryker.square(3);3console.log(x);4var stryker = require('stryker-parent');5var x = stryker.square(4);6console.log(x);7module.exports = function(config) {8 config.set({9 mochaOptions: {10 }11 });12}13[2017-02-27 16:34:50.853] [INFO] SandboxPool - Creating 5 test runners (based on CPU count)14[2017-02-27 16:34:50.853] [INFO] MutatorFacade - 7 Mutant(s) generated15[2017-02-27 16:34:50.853] [INFO] SandboxPool - Creating 5 test runners (based on CPU count)16[2017-02-27 16:34:50.853] [INFO] MutatorFacade - 7 Mutant(s) generated

Full Screen

Using AI Code Generation

copy

Full Screen

1var stryker = require('stryker-parent');2var square = stryker.square(2);3console.log(square);4var stryker = require('stryker-parent');5var square = stryker.square(2);6console.log(square);

Full Screen

Using AI Code Generation

copy

Full Screen

1var strykerParent = require('stryker-parent');2var square = strykerParent.square(2);3console.log(square);4module.exports = {5 square: function (n) {6 return n * n;7 }8};9{10 "scripts": {11 },12}13{14 "scripts": {15 },16 "dependencies": {17 }18}19module.exports = function(config) {20 config.set({21 });22};

Full Screen

Using AI Code Generation

copy

Full Screen

1var stryker_parent = require('stryker-parent');2var square = stryker_parent.square(5);3console.log(square);4module.exports = {5 square: function (number) {6 return number * number;7 }8};9{10 "scripts": {11 },12}13{14}15var stryker_parent = require('./index.js');16var assert = require('assert');17describe('stryker-parent', function () {18 it('should return 25 when 5 is given', function () {19 assert.equal(stryker_parent.square(5), 25);20 });21});22{23 "scripts": {24 },25 "dependencies": {26 }27}28{29}30var stryker_child = require('./index.js');31var assert = require('assert');32describe('stryker-child', function () {

Full Screen

Using AI Code Generation

copy

Full Screen

1var strykerParent = require('stryker-parent');2var a = 2;3var b = strykerParent.square(a);4console.log(b);5module.exports = {6 square: function (a) {7 return a * a;8 }9};10module.exports = function (config) {11 config.set({12 });13};14[2018-03-15 10:32:43.443] [INFO] Initial test run succeeded. Ran 1 tests in 1 seconds (net 0 ms, overhead

Full Screen

Using AI Code Generation

copy

Full Screen

1var parent = require('stryker-parent');2var square = parent.square(2);3console.log(square);4module.exports.square = function (number) {5 return number * number;6};7{8}9{10}11module.exports = function (config) {12 config.set({13 });14};15[2019-02-15 08:21:24.736] [INFO] Stryker 1.0.0 (cli) initialized

Full Screen

Using AI Code Generation

copy

Full Screen

1var stryker = require('stryker-parent');2console.log(stryker.square(5));3module.exports = {4 square: function (num) {5 return num * num;6 }7};

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run stryker-parent automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful