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