Welcome back! In the previous blog post, you drew the game board and made the game playable by clicking on the board columns. In this post, you'll be adding the status area component to your four-in-a-row game.
Then, add renderPlayerTurnIndicator() to the StatusArea class:
exportdefaultclassStatusAreaextendsGameObject{renderPlayerTurnIndicator(indicatorColor){letindicatorColorValue;switch(indicatorColor){caseConstants.PlayerColor.YELLOW:indicatorColorValue=TokenColor.YELLOW;break;caseConstants.PlayerColor.RED:indicatorColorValue=TokenColor.RED;break;default:// Unknown color. Do not attempt to render player turn indicator.return;}this.context.fillStyle=indicatorColorValue;constindicatorY=this.y+StatusAreaConfig.INDICATOR_WIDTH/2+StatusAreaConfig.PADDING_TOP;this.context.beginPath();// Draws a circle using CanvasDrawingContext2D.arc(): https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/arcthis.context.arc(this.width/2,indicatorY,StatusAreaConfig.INDICATOR_WIDTH/2,0,Math.PI*2);this.context.closePath();this.context.fill();}}
Now what you've implemented the rendering logic of the player turn indicator and exposed the StatusArea class through the components directory, you are now ready to start rendering the player turn indicator.
Then, update processMoveResult() so that it also determines the next player's turn and passes in the colour of the next player to a call to the render() method on statusArea:
Lastly, in createBoard(), update the call to the Board constructor so that the board when the game board is rendered, it's shifted down below the status area:
Add the renderMessage() method to the StatusArea class:
exportdefaultclassFrontEnd{// ..renderMessage(message){this.context.fillStyle="white";this.context.font="bold 16px Arial";this.context.textBaseline="top";this.context.textAlign="center";// Default value had vertical alignment issues. "center" fixes those in this caseconstmessageY=this.y+StatusAreaConfig.PADDING_TOP+StatusAreaConfig.INNER_MARGIN;this.context.fillText(message,this.width/2,messageY);}}
Proceed by adding the message parameter to the render() method then calling renderMessage() in render():
Now, add logic that determines which status message to display depending on the current state of the game. Add pickStatusMessage() to the FrontEnd class:
exportdefaultclassFrontEnd{// ..pickStatusMessage(status){switch(status){caseConstants.GameStatus.WIN:// The game is on the the next turn but the somebody has won from the previous turn. The winning player is the opposite of the player who currently has a turn.returnthis.game.currentTurn===Constants.PlayerColor.YELLOW?StatusMessages.RED_WIN:StatusMessages.YELLOW_WIN;caseConstants.GameStatus.DRAW:returnStatusMessages.DRAW;}// At this point, we can assume that the game is either has just started // or is still in progress.returnthis.game.currentTurn===Constants.PlayerColor.YELLOW?StatusMessages.YELLOW_TURN:StatusMessages.RED_TURN;}}
Then in the processMoveResult() method, update the render() call on statusArea so that it has an additional argument passed in. This argument is a method call to pickStatusMessage() with the status value of moveResult passed in: