How to use Square method in storybook-root

Best JavaScript code snippet using storybook-root

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

...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

...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}...

Full Screen

Full Screen

script.js

Source:script.js Github

copy

Full Screen

...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

1import { Square } from 'storybook-root'2import { Square } from 'storybook-root'3import { Square } from 'storybook-root'4import { Square } from 'storybook-root'5import { Square } from 'storybook-root'6import React from "react";7import { storiesOf } from "@storybook/react";8import { text, boolean, number } from "@storybook/addon-knobs";9import Square from "./Square";10storiesOf("Square", module)11 .add("default", () => (12 value={text("value", "X")}13 onClick={boolean("onClick", true)}14 isWin={boolean("isWin", false)}15 .add("with isWin", () => (16 value={text("value", "X")}17 onClick={boolean("onClick", true)}18 isWin={boolean("isWin", true)}19 ));20import React from "react";21import renderer from "react-test-renderer";22import Square from "./Square";23describe("Square", () => {24 it("renders correctly", () => {25 const tree = renderer.create(<Square />).toJSON();26 expect(tree).toMatchSnapshot();27 });28});29import React from "react";30import { connect } from "react

Full Screen

Using AI Code Generation

copy

Full Screen

1const storybookRoot = require('storybook-root');2const square = storybookRoot.square;3const circle = storybookRoot.circle;4const triangle = storybookRoot.triangle;5const trapezoid = storybookRoot.trapezoid;6const rhombus = storybookRoot.rhombus;7const rectangle = storybookRoot.rectangle;8const parallelogram = storybookRoot.parallelogram;9const ellipse = storybookRoot.ellipse;10const pentagon = storybookRoot.pentagon;11const hexagon = storybookRoot.hexagon;12const heptagon = storybookRoot.heptagon;13const octagon = storybookRoot.octagon;14const nonagon = storybookRoot.nonagon;15console.log(nonagon(5

Full Screen

Using AI Code Generation

copy

Full Screen

1ReactDOM.render(2 document.getElementById('root')3);4function loadStories() {5 req.keys().forEach(filename => req(filename));6}7configure(loadStories, module);8storiesOf('Button', module)9 .add('with text', () => <Button onClick={action('clicked')}>Hello Button</Button>)10 .add('with some emoji', () => (11 <Button onClick={action('clicked')}>12 ));13storiesOf('Welcome', module).add('to Storybook', () => <Welcome showApp={linkTo('Button')} />);14import { Welcome } from '@storybook/react/demo

Full Screen

Using AI Code Generation

copy

Full Screen

1import { Square } from 'storybook-root';2module.exports = {3 webpackFinal: async (config, { configType }) => {4 config.resolve.alias['storybook-root'] = require.resolve('storybook-root');5 return config;6 },7};8import { Square } from 'storybook-root';9module.exports = {10 webpackFinal: async (config, { configType }) => {11 config.resolve.alias['storybook-root'] = require.resolve('storybook-root');12 return config;13 },14};15import { Square } from 'storybook-root';

Full Screen

Using AI Code Generation

copy

Full Screen

1import { Square } from 'storybook-root';2export default function Test() {3 return <Square />;4}5module.exports = {6 stories: ['../packages/**/src/**/*.stories.@(js|jsx|ts|tsx|mdx)'],7};8module.exports = {9 stories: ['../packages/**/src/**/*.stories.@(js|jsx|ts|tsx|mdx)'],10 webpackFinal: async (config) => {11 config.module.rules = config.module.rules.filter(12 (f) => f.test.toString() !== '/\\.css$/'13 );14 config.module.rules.push({15 });16 return config;17 },18};19module.exports = {20 stories: ['../packages/**/src/**/*.stories.@(js|jsx|ts|tsx|mdx)'],21 webpackFinal: async (config) => {22 config.module.rules = config.module.rules.filter(23 (f) => f.test.toString() !== '/\\.css$/'24 );25 config.module.rules.push({26 });27 return config;28 },29};30module.exports = {31 stories: ['../packages/**/src/**/*.stories.@(js|jsx|ts|tsx|mdx)'],

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 storybook-root 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