1. 程式人生 > >js003-4方向8方向函式

js003-4方向8方向函式

1,求四方向或者8方向的周圍的棋子。

 

 1 /**
 2  * pos 1-4, 1-8 4/8方向的周圍
 3  * @param {*} pos 
 4  * @param {*} dir 
 5  */
 6 var _nearChess = function (pos, dir) {
 7         const row = 7; //
 8         const col = 7; //
 9         const x = pos % col;
10         const y = Math.floor(pos / row);
11         switch
(dir) { 12 case 1: //up 13 if (y !== 0) { 14 return pos - col; 15 } 16 break; 17 case 2: //down 18 if (y !== col - 1) { 19 return pos + col; 20 } 21 break
; 22 case 3: //left 23 if (x !== 0) { 24 return pos - 1; 25 } 26 break; 27 case 4: //right 28 if (x !== col - 1) { 29 return pos + 1; 30 } 31 break
; 32 case 5: //left up 33 if (x !== 0 && y !== 0) { 34 return pos - col - 1; 35 } 36 break; 37 case 6: //right up 38 if (x !== col - 1 && y !== 0) { 39 return pos - col + 1; 40 } 41 break; 42 case 7: //left down 43 if (x !== 0 && y !== col - 1) { 44 return pos + col - 1; 45 } 46 break; 47 case 8: //right down 48 if (x !== col - 1 && y !== col - 1) { 49 return pos + col + 1; 50 } 51 break; 52 53 default: 54 break; 55 } 56 return null; 57 }

2, A*等其他的演算法,後面補充。