.stack{ width: 320px; height: 320px; margin-top: 10px;}分别编写每个按钮的样式,left为指示距边界框左上角的以像素为单位的水平坐标,top为指示距边界框左上角的以像素为单位的垂直坐标,border-color为设置边框颜色,transparent指透明颜色
.bitgrid1{ left:5px; top:5px; width:40px; height:40px; border-color:transparent; background-color:transparent;}.bitgrid2{ left:50px; top:5px; width:40px; height:40px; border-color:transparent; background-color:transparent;}.bitgrid3{ left:95px; top:5px; width:40px; height:40px; border-color:transparent; background-color:transparent;}.bitgrid4{ left:140px; top:5px; width:40px; height:40px; border-color:transparent; background-color:transparent;}.bitgrid5{ left:185px; top:5px; width:40px; height:40px; border-color:transparent; background-color:transparent;}.bitgrid6{ left:230px; top:5px; width:40px; height:40px; border-color:transparent; background-color:transparent;}.bitgrid7{ left:275px; top:5px; width:40px; height:40px; border-color:transparent; background-color:transparent;}.bitgrid8{ left:5px; top:50px; width:40px; height:40px; border-color:transparent; background-color:transparent;}.bitgrid9{ left:50px; top:50px; width:40px; height:40px; border-color:transparent; background-color:transparent;}.bitgrid10{ left:95px; top:50px; width:40px; height:40px; border-color:transparent; background-color:transparent;}.bitgrid11{ left:140px; top:50px; width:40px; height:40px; border-color:transparent; background-color:transparent;}.bitgrid12{ left:185px; top:50px; width:40px; height:40px; border-color:transparent; background-color:transparent;}.bitgrid13{ left:230px; top:50px; width:40px; height:40px; border-color:transparent; background-color:transparent;}.bitgrid14{ left:275px; top:50px; width:40px; height:40px; border-color:transparent; background-color:transparent;}.bitgrid15{ left:5px; top:95px; width:40px; height:40px; border-color:transparent; background-color:transparent;}.bitgrid16{ left:50px; top:95px; width:40px; height:40px; border-color:transparent; background-color:transparent;}.bitgrid17{ left:95px; top:95px; width:40px; height:40px; border-color:transparent; background-color:transparent;}.bitgrid18{ left:140px; top:95px; width:40px; height:40px; border-color:transparent; background-color:transparent;}.bitgrid19{ left:185px; top:95px; width:40px; height:40px; border-color:transparent; background-color:transparent;}.bitgrid20{ left:230px; top:95px; width:40px; height:40px; border-color:transparent; background-color:transparent;}.bitgrid21{ left:275px; top:95px; width:40px; height:40px; border-color:transparent; background-color:transparent;}.bitgrid22{ left:5px; top:140px; width:40px; height:40px; border-color:transparent; background-color:transparent;}.bitgrid23{ left:50px; top:140px; width:40px; height:40px; border-color:transparent; background-color:transparent;}.bitgrid24{ left:95px; top:140px; width:40px; height:40px; border-color:transparent; background-color:transparent;}.bitgrid25{ left:140px; top:140px; width:40px; height:40px; border-color:transparent; background-color:transparent;}.bitgrid26{ left:185px; top:140px; width:40px; height:40px; border-color:transparent; background-color:transparent;}.bitgrid27{ left:230px; top:140px; width:40px; height:40px; border-color:transparent; background-color:transparent;}.bitgrid28{ left:275px; top:140px; width:40px; height:40px; border-color:transparent; background-color:transparent;}.bitgrid29{ left:5px; top:185px; width:40px; height:40px; border-color:transparent; background-color:transparent;}.bitgrid30{ left:50px; top:185px; width:40px; height:40px; border-color:transparent; background-color:transparent;}.bitgrid31{ left:95px; top:185px; width:40px; height:40px; border-color:transparent; background-color:transparent;}.bitgrid32{ left:140px; top:185px; width:40px; height:40px; border-color:transparent; background-color:transparent;}.bitgrid33{ left:185px; top:185px; width:40px; height:40px; border-color:transparent; background-color:transparent;}.bitgrid34{ left:230px; top:185px; width:40px; height:40px; border-color:transparent; background-color:transparent;}.bitgrid35{ left:275px; top:185px; width:40px; height:40px; border-color:transparent; background-color:transparent;}.bitgrid36{ left:5px; top:230px; width:40px; height:40px; border-color:transparent; background-color:transparent;}.bitgrid37{ left:50px; top:230px; width:40px; height:40px; border-color:transparent; background-color:transparent;}.bitgrid38{ left:95px; top:230px; width:40px; height:40px; border-color:transparent; background-color:transparent;}.bitgrid39{ left:140px; top:230px; width:40px; height:40px; border-color:transparent; background-color:transparent;}.bitgrid40{ left:185px; top:230px; width:40px; height:40px; border-color:transparent; background-color:transparent;}.bitgrid41{ left:230px; top:230px; width:40px; height:40px; border-color:transparent; background-color:transparent;}.bitgrid42{ left:275px; top:230px; width:40px; height:40px; border-color:transparent; background-color:transparent;}.bitgrid43{ left:5px; top:275px; width:40px; height:40px; border-color:transparent; background-color:transparent;}.bitgrid44{ left:50px; top:275px; width:40px; height:40px; border-color:transparent; background-color:transparent;}.bitgrid45{ left:95px; top:275px; width:40px; height:40px; border-color:transparent; background-color:transparent;}.bitgrid46{ left:140px; top:275px; width:40px; height:40px; border-color:transparent; background-color:transparent;}.bitgrid47{ left:185px; top:275px; width:40px; height:40px; border-color:transparent; background-color:transparent;}.bitgrid48{ left:230px; top:275px; width:40px; height:40px; border-color:transparent; background-color:transparent;}.bitgrid49{ left:275px; top:275px; width:40px; height:40px; border-color:transparent; background-color:transparent;}至此,增加的组件样式已经全部设定完毕了。
3. 先增加一个函数change(x,y),接受二维数组的下标,用来改变二维数字的值,如果值为0 则改为1,如果值为1则改为0,这样对应的色块颜色也能跟着发生变化
change(x,y){ if(grids[x][y] == 0){ grids[x][y] = 1; }else if(grids[x][y] == 1) { grids[x][y] = 0; } }之后增加一个函数changeOneGrids(x,y),接受二维数组的下标,调用刚才编写的函数change(x,y),改变其上下左右四个色块对应的二维数组的值,并且调用函数drawGrids()重新画图实现颜色的变化,再使当前步数加1
changeOneGrids(x,y){ if(x>-1 && y>-1 && x<7 && y<7){ this.change(x,y); } if(x+1>-1 && y>-1 && x+1<7 && y<7){ this.change(x+1,y); } if(x-1>-1 && y>-1 && x-1<7 && y<7){ this.change(x-1,y); } if(x>-1 && y+1>-1 && x<7 && y+1<7){ this.change(x,y+1); } if(x>-1 && y-1>-1 && x<7 && y-1<7){ this.change(x,y-1); } this.drawGrids(); this.currentSteps+=1; }再编写49个按钮对应的49个函数,作用是读取当前二维数组位置的下标,并且调用函数changeOneGrids(x,y)
getgrid1(){ let x = 0; let y = 0; this.changeOneGrids(x,y); }, getgrid2(){ let x = 0; let y = 1; this.changeOneGrids(x,y); }, getgrid3(){ let x = 0; let y = 2; this.changeOneGrids(x,y); }, getgrid4(){ let x = 0; let y = 3; this.changeOneGrids(x,y); }, getgrid5(){ let x = 0; let y = 4; this.changeOneGrids(x,y); }, getgrid6(){ let x = 0; let y = 5; this.changeOneGrids(x,y); }, getgrid7(){ let x = 0; let y = 6; this.changeOneGrids(x,y); }, getgrid8(){ let x = 1; let y = 0; this.changeOneGrids(x,y); }, getgrid9(){ let x = 1; let y = 1; this.changeOneGrids(x,y); }, getgrid10(){ let x = 1; let y = 2; this.changeOneGrids(x,y); }, getgrid11(){ let x = 1; let y = 3; this.changeOneGrids(x,y); }, getgrid12(){ let x = 1; let y = 4; this.changeOneGrids(x,y); }, getgrid13(){ let x = 1; let y = 5; this.changeOneGrids(x,y); }, getgrid14(){ let x = 1; let y = 6; this.changeOneGrids(x,y); }, getgrid15(){ let x = 2; let y = 0; this.changeOneGrids(x,y); }, getgrid16(){ let x = 2; let y = 1; this.changeOneGrids(x,y); }, getgrid17(){ let x = 2; let y = 2; this.changeOneGrids(x,y); }, getgrid18(){ let x = 2; let y = 3; this.changeOneGrids(x,y); }, getgrid19(){ let x = 2; let y = 4; this.changeOneGrids(x,y); }, getgrid20(){ let x = 2; let y = 5; this.changeOneGrids(x,y); }, getgrid21(){ let x = 2; let y = 6; this.changeOneGrids(x,y); }, getgrid22(){ let x = 3; let y = 0; this.currentSteps += 1; this.changeOneGrids(x,y); }, getgrid23(){ let x = 3; let y = 1; this.changeOneGrids(x,y); }, getgrid24(){ let x = 3; let y = 2; this.changeOneGrids(x,y); }, getgrid25(){ let x = 3; let y = 3; this.changeOneGrids(x,y); }, getgrid26(){ let x = 3; let y = 4; this.changeOneGrids(x,y); }, getgrid27(){ let x = 3; let y = 5; this.changeOneGrids(x,y); }, getgrid28(){ let x = 3; let y = 6; this.changeOneGrids(x,y); }, getgrid29(){ let x = 4; let y = 0; this.changeOneGrids(x,y); }, getgrid30(){ let x = 4; let y = 1; this.changeOneGrids(x,y); }, getgrid31(){ let x = 4; let y = 2; this.changeOneGrids(x,y); }, getgrid32(){ let x = 4; let y = 3; this.changeOneGrids(x,y); }, getgrid33(){ let x = 4; let y = 4; this.changeOneGrids(x,y); }, getgrid34(){ let x = 4; let y = 5; this.changeOneGrids(x,y); }, getgrid35(){ let x = 4; let y = 6; this.changeOneGrids(x,y); }, getgrid36(){ let x = 5; let y = 0; this.changeOneGrids(x,y); }, getgrid37(){ let x = 5; let y = 1; this.changeOneGrids(x,y); }, getgrid38(){ let x = 5; let y = 2; this.currentSteps += 1; this.changeOneGrids(x,y); }, getgrid39(){ let x = 5; let y = 3; this.changeOneGrids(x,y); }, getgrid40(){ let x = 5; let y = 4; this.changeOneGrids(x,y); }, getgrid41(){ let x = 5; let y = 5; this.changeOneGrids(x,y); }, getgrid42(){ let x = 5; let y = 6; this.changeOneGrids(x,y); }, getgrid43(){ let x = 6; let y = 0; this.changeOneGrids(x,y); }, getgrid44(){ let x = 6; let y = 1; this.changeOneGrids(x,y); }, getgrid45(){ let x = 6; let y = 2; this.changeOneGrids(x,y); }, getgrid46(){ let x = 6; let y = 3; this.changeOneGrids(x,y); }, getgrid47(){ let x = 6; let y = 4; this.changeOneGrids(x,y); }, getgrid48(){ let x = 6; let y = 5; this.changeOneGrids(x,y); }, getgrid49(){ let x = 6; let y = 6; this.changeOneGrids(x,y); }最后是随机生成一个色块被打乱的7 7的棋盘,首先我们得先把二维数组的下标放进一个列表中,Math.random()函数是随机[0,1)内的小数,Math.floor(x)为得出小于或等于x的最大整数,每次随机生成一个数后,读取刚才的列表对应的下标,调用函数changeOneGrids(x,y),重复此操作若干次,这样就能随机生成一个色块被打乱的7*7的棋盘
initGrids(){ let array = []; for (let row = 0; row < 7; row++) { for (let column = 0; column < 7; column++) { if (grids[row][column] == 0) { array.push([row, column]) } } } for (let i = 0; i < 20; i++){ let randomIndex = Math.floor(Math.random() * array.length); let row = array[randomIndex][0]; let column = array[randomIndex][1]; this.changeOneGrids(row,column); } }至此,这一部分内容已经全部编写完毕。