diff --git a/package-lock.json b/package-lock.json index 7a19ee0..1d15d4a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -6413,6 +6413,12 @@ "pretty-format": "22.4.0" } }, + "jest-localstorage-mock": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/jest-localstorage-mock/-/jest-localstorage-mock-2.2.0.tgz", + "integrity": "sha512-x+P0vcwr4540bCAYzTEpiD9rs+zh/QZzyiABV+MU6yM2OPwPlrrLyUx/6gValMyt6tg5lX6Z53o2rHWfUht5Xw==", + "dev": true + }, "jest-matcher-utils": { "version": "22.4.0", "resolved": "https://registry.npmjs.org/jest-matcher-utils/-/jest-matcher-utils-22.4.0.tgz", diff --git a/package.json b/package.json index 8bf1391..7ab9635 100644 --- a/package.json +++ b/package.json @@ -104,5 +104,8 @@ }, "eslintConfig": { "extends": "react-app" + }, + "devDependencies": { + "jest-localstorage-mock": "^2.2.0" } } diff --git a/src/App.driver.js b/src/App.driver.js index ec83eb8..90b1f6a 100644 --- a/src/App.driver.js +++ b/src/App.driver.js @@ -23,6 +23,11 @@ const appDriver = () => { .at(index) .text(), getWinnerMessage: () => wrapper.find('[data-hook="winner-message"]').text(), + getTieMessage: () => wrapper.find('[data-hook="tie-message"]').text(), + isP1NameHasClass: klass => wrapper.find('[data-hook="p1-name"]').hasClass(klass), + isP2NameHasClass: klass => wrapper.find('[data-hook="p2-name"]').hasClass(klass), + isRegistrationVisible: () => wrapper.find('[data-hook="registration"]').length > 0, + isGameBoardVisible: () => wrapper.find('[data-hook="game-board"]').length > 0, }; }; diff --git a/src/App.js b/src/App.js index 64b9564..b5f0e78 100644 --- a/src/App.js +++ b/src/App.js @@ -13,36 +13,83 @@ class App extends React.Component { board: [['', '', ''], ['', '', ''], ['', '', '']], winner: '', currentPlayer: 'X', + p1Wins: 0, + p2Wins: 0, }; } onNewGame = ({ p1Name, p2Name }) => { - this.setState({ p1Name, p2Name }); + const p1Wins = localStorage.getItem(p1Name); + const p2Wins = localStorage.getItem(p2Name); + + this.setState({ + p1Name, + p2Name, + p1Wins: p1Wins ? parseInt(p1Wins, 10) : 0, + p2Wins: p2Wins ? parseInt(p2Wins, 10) : 0, + }); }; + isCellAlreadySet(rIndex, cIndex) { + const { board } = this.state; + return board[rIndex][cIndex] === 'X' || board[rIndex][cIndex] === 'O'; + } + handleCellClick = (rIndex, cIndex) => { + if (this.isCellAlreadySet(rIndex, cIndex)) { + return; + } + const board = this.state.board.map(row => [...row]); board[rIndex][cIndex] = this.state.currentPlayer; if (gameStatus(board) === this.state.currentPlayer) { this.setState({ winner: this.state.currentPlayer }); + + if (this.state.currentPlayer === 'X') { + localStorage.setItem(this.state.p1Name, this.state.p1Wins + 1); + } else { + localStorage.setItem(this.state.p2Name, this.state.p2Wins + 1); + } } const nextPlayer = this.state.currentPlayer === 'X' ? 'O' : 'X'; - this.setState({ board, currentPlayer: nextPlayer }); + this.setState({ board, currentPlayer: nextPlayer, tie: gameStatus(board) === 'tie' }); }; + + saveGame = () => { + localStorage.setItem('gameState', JSON.stringify(this.state)); + }; + + loadGame = () => { + this.setState(JSON.parse(localStorage.getItem('gameState'))); + }; + render() { + const shouldShowRegistration = !this.state.p1Name || this.state.tie || this.state.winner; + const winCount = this.state.winner === 'X' ? this.state.p1Wins : this.state.p2Wins; return (