Coding since 11yo, that makes it over 30 years now ~~~
Have a PhD in Comp Sci ~~~
Love to go on bike tours ~~~
I try to stay as generalist as I can in this crazy wide place coding is at now.
One in TypeScript that tracks "groups" of connected pieces so all it needs to do is check for when a group grows to 4 pieces
I feel the code ended up overly complex for the problem, but at least it prints the board prettily ¯_(ツ)_/¯
constpieces_position_list=["C_Yellow","E_Red","G_Yellow","B_Red","D_Yellow","B_Red","B_Yellow","G_Red","C_Yellow","C_Red","D_Yellow","F_Red","E_Yellow","A_Red","A_Yellow","G_Red","A_Yellow","F_Red","F_Yellow","D_Red","B_Yellow","E_Red","D_Yellow","A_Red","G_Yellow","D_Red","D_Yellow","C_Red",];typeGroup={count:number;overriddenBy?:Group};typePiece={color:string;groups:{[groupKey:string]:Group};};typeNeighbour={x:number;y:number;groupKey:string;};constpreceedingNeighbours:Neighbour[]=[{x:-1,y:0,groupKey:"-",},{x:-1,y:-1,groupKey:"\\",},{x:0,y:-1,groupKey:"|",},{x:1,y:-1,groupKey:"/",},];functionwhoIsWinner(piecesPositionList:string[]):string{constcolumns:Piece[][]=[];return(piecesPositionList.find(pieceString=>addPiece(pieceString.charCodeAt(0)-"A".charCodeAt(0),pieceString.substring(2)))||"__Draw").substring(2);functionaddPieceToGroup(group:Group|undefined):number{if(group===undefined)return0;if(group.overriddenBy!==undefined)returnaddPieceToGroup(group.overriddenBy);elsegroup.count++;returngroup.count;}functionpieceAt(columnIndex:number,rowIndex:number):Piece|undefined{returncolumns[columnIndex]?columns[columnIndex][rowIndex]:undefined;}functionaddPiece(columnIndex:number,color:string){constcolumn=columns[columnIndex]||(columns[columnIndex]=[]),rowIndex=column.length,piece:Piece={color,groups:{},};column.push(piece);for(const{x,y,groupKey}ofpreceedingNeighbours){constpreceedingNeighbour=pieceAt(columnIndex+x,rowIndex+y);constproceedingNeighbour=pieceAt(columnIndex-x,rowIndex-y);constpreceedingNeighbourGroup=preceedingNeighbour&&preceedingNeighbour.color===color&&preceedingNeighbour.groups[groupKey];letproceedingNeighbourGroup=proceedingNeighbour&&proceedingNeighbour.color===color&&proceedingNeighbour.groups[groupKey];constgroup=(piece.groups[groupKey]=preceedingNeighbourGroup||proceedingNeighbourGroup||{count:0,overriddenBy:undefined,});if(preceedingNeighbourGroup&&proceedingNeighbourGroup){// If we have groups on both sides, merge the proceeding one (eg the one to the right) into the preceeding one// This is done by setting the proceeding group's `overriddenBy` referencewhile(proceedingNeighbourGroup.overriddenBy)proceedingNeighbourGroup=proceedingNeighbourGroup.overriddenBy;group.count+=proceedingNeighbourGroup.count;proceedingNeighbourGroup.overriddenBy=group;}if(group&&addPieceToGroup(group)>=4){logBoard();returncolor;}}returnundefined;}functionlogBoard():void{constrowDivider="-".repeat(1+7*9)+"\n";lets=rowDivider;for(letrowIndex=5;rowIndex>=0;rowIndex--){for(letcolumnIndex=0;columnIndex<7;columnIndex++){constpiece=pieceAt(columnIndex,rowIndex);if(!piece)s+="| ";elses+=`| ${piece.color.substring(0,1)}${["-","\\","|","/"].reduce((acc,groupKey)=>acc+groupCountString(piece.groups[groupKey]),"")} `;}s+="|\n"+rowDivider;}console.log(s);functiongroupCountString(group:Group):string{if(group===undefined)return"";if(group.overriddenBy!==undefined)returngroupCountString(group.overriddenBy);elsereturnString(group.count);}}}console.log(whoIsWinner(pieces_position_list));
->
----------------------------------------------------------------
| | | | | | | |
----------------------------------------------------------------
| | | | | | | |
----------------------------------------------------------------
| R 1111 | Y 1221 | | Y 1111 | | | Y 14 |
----------------------------------------------------------------
| Y 2221 | Y 2223 | R 3311 | R 3111 | R 3112 | Y 1411 | R 1321 |
----------------------------------------------------------------
| Y 1221 | R 1321 | Y 3123 | Y 3221 | Y 3411 | R 2322 | R 2221 |
----------------------------------------------------------------
| R 2311 | R 2121 | Y 2221 | Y 2423 | R 2311 | R 2221 | Y 1111 |
----------------------------------------------------------------
Yellow
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
One in TypeScript that tracks "groups" of connected pieces so all it needs to do is check for when a group grows to 4 pieces
I feel the code ended up overly complex for the problem, but at least it prints the board prettily ¯_(ツ)_/¯
->