JavaScript
1コード 2//変数の設定 3const MS_SIZE=8;//マインスイーパーのサイズ 4var i,j; 5var tate,yoko,x,y; 6const number_of_mines=8;//地雷の数 7var counter=0; 8 9// 配列を用意 10var cell = new Array(MS_SIZE); 11for(i=0; i<MS_SIZE; i++){ 12 cell[i] = new Array(MS_SIZE); 13} 14for(i=0; i<MS_SIZE; i++){ 15 for(j=0; j<MS_SIZE; j++){ 16 cell[i][j]=0; 17 } 18} 19 20//地雷を配置 21for(i=0; i<number_of_mines; i++){ 22 tate=Math.floor(Math.random()*(number_of_mines)); 23 yoko=Math.floor(Math.random()*(number_of_mines)); 24 if(cell[yoko][tate]!=-1){ 25 cell[yoko][tate]=-1; 26 } 27 else{ 28 i--; 29 } 30} 31 32 33//地雷の周囲 34for(tate=0; tate<MS_SIZE; tate++){ 35 for(yoko=0; yoko<MS_SIZE; yoko++){ 36 if(cell[yoko][tate]==-1){ 37 for(j=-1; j<2; j++){ 38 for(i=-1; i<2; i++){ 39 if(yoko+j<MS_SIZE && yoko+j>=0 && tate+i<MS_SIZE && tate+i>=0){ 40 if(cell[yoko+j][tate+i]!=-1){ 41 cell[yoko+j][tate+i]++; 42 } 43 } 44 } 45 } 46 } 47 } 48} 49 50//ボードを開いたとき、クリックした時 51function openBoard(x,y){ 52 id="cell"+x+y; 53 target = document.getElementById(id); 54 if(cell[x][y]!=-1 && cell[x][y]!=0){ 55 target.innerHTML = cell[x][y]; 56 target.style.padding="7px 13.5px"; 57 target.style.textAlign="center"; 58 counter++; 59 if(counter==(MS_SIZE*MS_SIZE-number_of_mines)){ 60 window.alert("クリア"); 61 } 62 } 63 else if(cell[x][y]==0){ 64 target.innerHTML = cell[x][y]; 65 target.style.padding = "7px 13.5px"; 66 target.style.textAlign = "center"; 67 openBoard0(x,y); 68 } 69 else{ 70 target = document.getElementById(id); 71 target.innerHTML = "外れ"; 72 window.alert("あなたは爆死しました。") 73 } 74} 75 76//0が出た時その周りの0も開く関数 77function openBoard0(x,y){ 78 for(j=-1; j<2; j++){ 79 for(i=-1; i<2; i++){ 80 yoko=x+j; 81 tate=y+i; 82 if(yoko<MS_SIZE && yoko>=0 && tate<MS_SIZE && tate>=0){ 83 id="cell"+yoko+tate; 84 target = document.getElementById(id); 85 target.innerHTML = cell[yoko][tate]; 86 target.style.padding = "7px 13.5px"; 87 target.style.textAlign = "center"; 88 if(cell[yoko][tate]==0){ 89 openBoard0(yoko,tate); 90 } 91 } 92 } 93 } 94} 95
openBoard0という関数は0(地雷が周りにゼロ)を開いたら、その周りのcellを開く関数です。これ自体はうまく動作します。
問題はopenBoard0という関数の中の、再帰的にopenBoard0(yoko,tate)を出すところがうまくいきません。
教えていただけたらとても嬉しいです。
回答4件
あなたの回答
tips
プレビュー