A O( n2 ) solution written in JavaScript. The board is represented as a matrix.
(Thank you @earlware
for providing the test cases)
Update: Found some flaws in my code and since made some improvements to it. Tried to make it short and sweet but things didn't go as plan. Anyway, I did try to make everything more descriptive and easy to read :-)
Here it is, three more functions are being added to check for 4 consecutive matches in all directions (horizontal, vertical, and diagonal). It should still be O( n2 )
constpieces_position_list={yellowWins:["B_Red","A_Yellow","C_Red","B_Yellow","C_Red","C_Yellow","D_Red","E_Yellow","D_Red","E_Yellow","D_Red","D_Yellow"],redWins:["A_Red","A_Yellow","B_Red","B_Yellow","C_Red","C_Yellow","D_Red","D_Yellow"],draw:["A_Red","G_Yellow","B_Red","F_Yellow","C_Red","E_Yellow"]};constboard_map={A:0,B:1,C:2,D:3,E:4,F:5,G:6};constcheckDiagonal=(boardMatrix,currentRow,currentColumn)=>{lettrackAlign=0;letcount=0;while(count<3){count++;// check up rightif(boardMatrix[currentRow-count]&&boardMatrix[currentRow-count][currentColumn+count]&&boardMatrix[currentRow-count][currentColumn+count]===boardMatrix[currentRow][currentColumn]){console.log(trackAlign);trackAlign++;}// check up leftif(boardMatrix[currentRow-count]&&boardMatrix[currentRow-count][currentColumn-count]&&boardMatrix[currentRow-count][currentColumn-count]===boardMatrix[currentRow][currentColumn]){trackAlign++;}// check down rightif(boardMatrix[currentRow+count]&&boardMatrix[currentRow+count][currentColumn+count]&&boardMatrix[currentRow+count][currentColumn+count]===boardMatrix[currentRow][currentColumn]){trackAlign++;}// check down leftif(boardMatrix[currentRow+count]&&boardMatrix[currentRow+count][currentColumn-count]&&boardMatrix[currentRow+count][currentColumn-count]===boardMatrix[currentRow][currentColumn]){trackAlign++;}}returntrackAlign===3;};constcheckVertical=(boardMatrix,currentRow,currentColumn)=>{lettrackAlign=0;letcount=0;while(count<3){// check downcount++;if(boardMatrix[currentRow+count]&&boardMatrix[currentRow+count][currentColumn]===boardMatrix[currentRow][currentColumn]){trackAlign++;}}returntrackAlign===3;};constcheckHorizontal=(boardMatrix,currentRow,currentColumn)=>{lettrackAlign=0;letcount=0;while(count<3){count++;// check leftif(boardMatrix[currentRow]&&boardMatrix[currentRow][currentColumn-count]===boardMatrix[currentRow][currentColumn]){trackAlign++;}// check rightif(boardMatrix[currentRow]&&boardMatrix[currentRow][currentColumn+count]===boardMatrix[currentRow][currentColumn]){trackAlign++;}}returntrackAlign===3;};constgetCheckerColumn=checker=>{returnchecker&&checker.substring(0,checker.indexOf("_"));};constgetCheckerColor=checker=>{returnchecker&&checker.substring(checker.indexOf("_")+1,checker.length);};constconnectFour=pieces_position_list=>{constboard={matrix:[[0,0,0,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0,0,0,0,0]]};leti=0;while(i<pieces_position_list.length){constcolumn=board_map[getCheckerColumn(pieces_position_list[i])];constcolor=getCheckerColor(pieces_position_list[i]);letj=6;while(j>0){j--;if(board.matrix[j][column]===0){board.matrix[j][column]=color;if(checkDiagonal(board.matrix,j,column)||checkHorizontal(board.matrix,j,column)||checkVertical(board.matrix,j,column)){console.log("Result:");console.log(`The winner is ${color}!`);console.log(board.matrix);console.log("\n");return;}break;}}i++;}console.log("Is a draw !");console.log(board.matrix);console.log("\n");};//Testsconsole.log("Test 1");connectFour(pieces_position_list.yellowWins);console.log("Test 2");connectFour(pieces_position_list.redWins);console.log("Test 3");connectFour(pieces_position_list.draw);
A O( n2 ) solution written in JavaScript. The board is represented as a matrix.
(Thank you @earlware for providing the test cases)
Update: Found some flaws in my code and since made some improvements to it. Tried to make it short and sweet but things didn't go as plan. Anyway, I did try to make everything more descriptive and easy to read :-)
Here it is, three more functions are being added to check for 4 consecutive matches in all directions (horizontal, vertical, and diagonal). It should still be O( n2 )