|
| 1 | +import React, { Component } from "react"; |
| 2 | +import Row from "./Row"; |
| 3 | +import type { BoardContent, Scoreboard, Player } from "../types/types"; |
| 4 | +import type { BoardText } from "../types/types"; |
| 5 | + |
| 6 | +type BoardState = { |
| 7 | + board: BoardContent; |
| 8 | + currentPlayer: Player; |
| 9 | + gameOver: boolean; |
| 10 | + message: string; |
| 11 | + scoreboard: Scoreboard; |
| 12 | +}; |
| 13 | + |
| 14 | +class Board extends Component<{}, BoardState> { |
| 15 | + constructor(props: any) { |
| 16 | + super(props); |
| 17 | + this.state = { |
| 18 | + board: this.newBoard(), |
| 19 | + currentPlayer: "X", |
| 20 | + gameOver: false, |
| 21 | + message: "", |
| 22 | + scoreboard: { X: 0, O: 0 }, |
| 23 | + }; |
| 24 | + |
| 25 | + this.resetBoard = this.resetBoard.bind(this); |
| 26 | + this.handleBoxClick = this.handleBoxClick.bind(this); |
| 27 | + } |
| 28 | + |
| 29 | + componentDidUpdate() { |
| 30 | + this.checkForWinner(); |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * @method newBoard |
| 35 | + * @description - returns a blank BoardContent array, |
| 36 | + * for the start of a new game |
| 37 | + */ |
| 38 | + newBoard(): BoardContent { |
| 39 | + return [ |
| 40 | + ["-", "-", "-"], |
| 41 | + ["-", "-", "-"], |
| 42 | + ["-", "-", "-"], |
| 43 | + ]; |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * @method resetBoard |
| 48 | + * @description - sets to board object to be all '-', |
| 49 | + * and sets gameOver and message to default state |
| 50 | + */ |
| 51 | + resetBoard(): void { |
| 52 | + this.setState({ |
| 53 | + gameOver: false, |
| 54 | + board: this.newBoard(), |
| 55 | + message: "", |
| 56 | + }); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * @method checkForWinner |
| 61 | + * @description - checks to see if either player has filled a row |
| 62 | + * if so, ends the game and updates the message to declare winner |
| 63 | + */ |
| 64 | + checkForWinner(): void { |
| 65 | + const { board, gameOver, currentPlayer } = this.state; |
| 66 | + |
| 67 | + const spacesLeft = (): boolean => { |
| 68 | + for (let i of board) { |
| 69 | + if (i.includes("-")) return true; |
| 70 | + } |
| 71 | + return false; |
| 72 | + }; |
| 73 | + |
| 74 | + if (!gameOver) { |
| 75 | + // win conditions: matching rows, columns, or diagonals, that are not empty('-') |
| 76 | + if ( |
| 77 | + (board[0][0] === board[0][1] && |
| 78 | + board[0][1] === board[0][2] && |
| 79 | + board[0][2] !== "-") || |
| 80 | + (board[1][0] === board[1][1] && |
| 81 | + board[1][1] === board[1][2] && |
| 82 | + board[1][2] !== "-") || |
| 83 | + (board[2][0] === board[2][1] && |
| 84 | + board[2][1] === board[2][2] && |
| 85 | + board[2][2] !== "-") || |
| 86 | + (board[0][0] === board[1][0] && |
| 87 | + board[1][0] === board[2][0] && |
| 88 | + board[2][0] !== "-") || |
| 89 | + (board[0][1] === board[1][1] && |
| 90 | + board[1][1] === board[2][1] && |
| 91 | + board[2][1] !== "-") || |
| 92 | + (board[0][2] === board[1][2] && |
| 93 | + board[1][2] === board[2][2] && |
| 94 | + board[2][2] !== "-") || |
| 95 | + (board[0][0] === board[1][1] && |
| 96 | + board[1][1] === board[2][2] && |
| 97 | + board[2][2] !== "-") || |
| 98 | + (board[2][0] === board[1][1] && |
| 99 | + board[1][1] === board[0][2] && |
| 100 | + board[0][2] !== "-") |
| 101 | + ) { |
| 102 | + // winner is the person who's turn was previous |
| 103 | + const winner: Player = currentPlayer === "X" ? "O" : "X"; |
| 104 | + |
| 105 | + this.setState({ |
| 106 | + gameOver: true, |
| 107 | + message: `Player ${winner} wins!`, |
| 108 | + }); |
| 109 | + |
| 110 | + // draw condition: no '-' remaining in board without above win condition triggering |
| 111 | + } else if (!spacesLeft()) { |
| 112 | + this.setState({ |
| 113 | + gameOver: true, |
| 114 | + message: "Draw!", |
| 115 | + }); |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + handleBoxClick(row: number, column: number): void { |
| 121 | + const boardCopy: BoardContent = [ |
| 122 | + [...this.state.board[0]], |
| 123 | + [...this.state.board[1]], |
| 124 | + [...this.state.board[2]], |
| 125 | + ]; |
| 126 | + boardCopy[row][column] = this.state.currentPlayer; |
| 127 | + const newPlayer: Player = this.state.currentPlayer === "X" ? "O" : "X"; |
| 128 | + this.setState({ board: boardCopy, currentPlayer: newPlayer }); |
| 129 | + } |
| 130 | + |
| 131 | + render() { |
| 132 | + const rows: Array<JSX.Element> = []; |
| 133 | + for (let i = 0; i < 3; i++) { |
| 134 | + rows.push( |
| 135 | + <Row |
| 136 | + key={i} |
| 137 | + row={i} |
| 138 | + handleBoxClick={this.handleBoxClick} |
| 139 | + values={this.state.board[i]} |
| 140 | + /> |
| 141 | + ); |
| 142 | + } |
| 143 | + const { X, O }: Scoreboard = this.state.scoreboard; |
| 144 | + |
| 145 | + return ( |
| 146 | + <div className="board"> |
| 147 | + <h1>Tic Tac Toe</h1> |
| 148 | + {this.state.gameOver && <h4>{this.state.message}</h4>} |
| 149 | + {rows} |
| 150 | + <button id="reset" onClick={this.resetBoard}> |
| 151 | + Reset |
| 152 | + </button> |
| 153 | + </div> |
| 154 | + ); |
| 155 | + } |
| 156 | +} |
| 157 | + |
| 158 | +export default Board; |
0 commit comments