前提・実現したいこと
Typescirptを用いて、任意の値から作成されるマインスイーパ を作成しています。
10*10のマインスイーパ の基本機能は実装できました。
しかし、フォームからデータを受け取るようにしたところ爆弾を枠外に設置するエラー。そして現在のコードでも加算されるはずの爆弾が加算されないといったエラー。そしてそもそも爆弾が設置されず真っ白な枠だけが生成されるエラーが発生しています。
発生している問題・エラーメッセージ
index.js:60 Uncaught TypeError: Cannot read property 'bomb' of undefined at init (index.js:60) at HTMLInputElement.onclick (index.html:33) 最後のエラーが発生する場合に出てくるエラーです。if (!cell[x][y].bomb)の部分とHTMLのボタンの部分に対して出ます。
該当のソースコード
Typescript
1type Cell = HTMLTableCellElement & { 2 x: number, 3 y: number, 4 bomb: boolean, 5 opened: boolean, 6}; 7let cell: Cell[][]=[]; 8let opened: number=0; 9 10 11 12function init() { 13 let removeElem : HTMLTableElement =<HTMLTableElement>document.getElementById("main"); 14 for (var i = removeElem.childNodes.length - 1; i >= 0; i--) { 15 removeElem.removeChild(removeElem.childNodes[i]); 16 } 17 const form = document.forms.namedItem('txtbox'); 18 const bombInput = form && form.elements.namedItem('BOMB') as HTMLInputElement; 19 if(!bombInput) return alert('爆弾入力してください'); 20 let BOMB: number= parseInt(bombInput.value); 21 if (!BOMB){ 22 alert("爆弾入力してください"); 23 return false; 24 } 25 26 const heightInput = form && form.elements.namedItem('H') as HTMLInputElement; 27 if(!heightInput) return alert('高さ入力してください'); 28 let H: number= parseInt(heightInput.value); 29 if (!H){ 30 alert("高さ入力してください"); 31 return false; 32 } 33 34 const widthInput = form && form.elements.namedItem('W') as HTMLInputElement; 35 if(!widthInput) return alert('横幅入力してください'); 36 let W: number= parseInt(widthInput.value); 37 if (!W){ 38 alert("横幅入力してください"); 39 return false; 40 } 41 42 43 44 let main = document.getElementById("main"); 45 if(main == null){ 46 alert('Error'); 47 return; 48 } 49 for (let i=0; i<H; i++){ 50 cell[i]=[]; 51 let tr = document.createElement("tr"); 52 for (let j=0; j<W; j++){ 53 let td = document.createElement("td") as Cell; 54 td.addEventListener('click', function(event: MouseEvent){ 55 click(event); 56 }); 57 td.className="cell"; 58 td.y = i; 59 td.x = j; 60 cell[i][j]=td; 61 tr.appendChild(td); 62 } 63 main.appendChild(tr); 64 } 65 for(let i=0; i<BOMB; i++){ 66 while(true) { 67 let x=Math.floor(Math.random()*W); 68 let y=Math.floor(Math.random()*H); 69 console.log(x); 70 console.log(y); 71 72 if (!cell[x][y].bomb){ 73 cell[x][y].bomb =true; 74 break; 75 } 76 } 77 } 78} 79function count(x: number,y: number){ 80 const form = document.forms.namedItem('txtbox'); 81 82 const heightInput = form && form.elements.namedItem('H') as HTMLInputElement; 83 if(!heightInput) return alert('Error'); 84 let H: number= parseInt(heightInput.value); 85 if (!H){ 86 alert("高さ入力してください"); 87 return false; 88 } 89 90 const widthInput = form && form.elements.namedItem('W') as HTMLInputElement; 91 if(!widthInput) return alert('Error'); 92 let W: number= parseInt(widthInput.value); 93 94 let b =0; 95 96 for(let j= y-1; j<=y+1; j++){ 97 for(let i =x-1; i<=1; i++){ 98 if (cell[j] && cell[j][i]){ 99 if(cell[j][i].bomb) b++; 100 } 101 } 102 } 103 return b; 104} 105 106function openCell(x: number,y: number){ 107 for (let j = y-1; j<= y+1; j++){ 108 for (let i = x-1; i<= x+1; i++){ 109 if (cell[j]&&cell[j][i]){ 110 let c=cell[j][i]; 111 if(c.opened || c.bomb){ 112 continue; 113 } 114 flip(c); 115 let n = count(i,j); 116 if(n==0){ 117 openCell(i,j); 118 }else { 119 c.textContent = String(n); 120 } 121 } 122 } 123 } 124} 125 126 127function flip(cell: Cell) { 128 const form = document.forms.namedItem('txtbox'); 129 const bombInput = form && form.elements.namedItem('BOMB') as HTMLInputElement; 130 if(!bombInput) return alert('Error'); 131 let BOMB: number= parseInt(bombInput.value); 132 if (!BOMB){ 133 alert("爆弾入力してください"); 134 return false; 135 } 136 137 const heightInput = form && form.elements.namedItem('H') as HTMLInputElement; 138 if(!heightInput) return alert('Error'); 139 let H: number= parseInt(heightInput.value); 140 if (!H){ 141 alert("高さ入力してください"); 142 return false; 143 } 144 145 const widthInput = form && form.elements.namedItem('W') as HTMLInputElement; 146 if(!widthInput) return alert('Error'); 147 let W: number= parseInt(widthInput.value); 148 if (!W){ 149 alert("横幅入力してください"); 150 return false; 151 } 152 153 154 cell.className = "cell-open"; 155 cell.opened = true; 156 if (++opened >= (W * H -BOMB)){ 157 setTitle('Open'); 158 } 159} 160 161 162 163function click(e: MouseEvent) { 164 let src = e.currentTarget as Cell; 165 if(src.bomb){ 166 src.textContent = "★"; 167 setTitle('Game Over'); 168 }else{ 169 openCell(src.x,src.y); 170 } 171} 172 173 174 175
試したこと
console.logで爆弾のx,y座標を調べたところ、本来あるはずの場所になかったり、ある場所の座標が入っていなかったりしました。
丸々2日間検索しても原因がわからなかったので投稿させていただきました。ご助力願います。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/06/16 04:17