|
1 | | -import React from "react"; |
| 1 | +import React, { useEffect, useState } from "react"; |
2 | 2 | import User from "./User"; |
3 | 3 | import Score from "./Score"; |
4 | 4 | import "./App.css" |
5 | 5 |
|
6 | | -class App extends React.Component{ |
7 | | - constructor(props){ |
8 | | - super(props) |
9 | | - this.state = { |
10 | | - values : { |
11 | | - 0 : { |
12 | | - value : "rock", |
13 | | - emoji : "✊"}, |
14 | | - 1 : { |
15 | | - value : "scissor", |
16 | | - emoji : "✌️" |
17 | | - }, |
18 | | - 2 : { |
19 | | - value : "paper", |
20 | | - emoji : "🖐" |
21 | | - }}, |
22 | | - show : 0, |
23 | | - gamer : 0, |
24 | | - user : 0 |
25 | | - } |
26 | | - this.checkResult = this.checkResult.bind(this) |
27 | | - this.restart = this.restart.bind(this) |
28 | | - this.clear = this.clear.bind(this) |
| 6 | +export default function App() { |
| 7 | + const [values, setValues] = useState({ |
| 8 | + 0 : { |
| 9 | + value : "rock", |
| 10 | + emoji : "✊"}, |
| 11 | + 1 : { |
| 12 | + value : "scissor", |
| 13 | + emoji : "✌️" |
| 14 | + }, |
| 15 | + 2 : { |
| 16 | + value : "paper", |
| 17 | + emoji : "🖐" |
| 18 | + }}) |
| 19 | + const [show, setShow] = useState(0); |
| 20 | + const [gamer, setGamer] = useState(0) |
| 21 | + const [user, setUser] = useState(0) |
| 22 | + |
| 23 | + let showValues; |
| 24 | + |
| 25 | + const restart = () => { |
| 26 | + showValues = setInterval(() => { |
| 27 | + let newValue = show === 0 ? 1 |
| 28 | + : show === 1 ? 2 |
| 29 | + : show === 2 && 0 |
| 30 | + setShow(newValue) |
| 31 | + }, 100); |
29 | 32 | } |
30 | 33 |
|
31 | | - componentDidMount(){ |
32 | | - this.restart() |
| 34 | + |
| 35 | + const clear = () => { |
| 36 | + setGamer(0) |
| 37 | + setUser(0) |
33 | 38 | } |
34 | 39 |
|
35 | | - componentWillUnmount(){ |
36 | | - clearInterval(this.showValues); |
| 40 | + const result = (winner) => { |
| 41 | + alert(winner === "gamer" ? "졌다!!!!!" : "이겼다!!!!") |
| 42 | + winner === "gamer" ? setGamer(gamer++) : setUser(user++) |
37 | 43 | } |
38 | 44 |
|
39 | | - checkResult(num){ |
40 | | - clearInterval(this.showValues) |
41 | | - if (this.state.show === num) { |
| 45 | + |
| 46 | + const checkResult = (num) => { |
| 47 | + clearInterval(showValues) |
| 48 | + if (show === num) { |
42 | 49 | alert("비겼다.") |
43 | 50 | } |
44 | | - else if(this.state.show < 2){ |
45 | | - this.state.show+1 === num ? this.result("gamer") : this.result("user") |
| 51 | + else if(show < 2){ |
| 52 | + (show+1) === num ? result("gamer") : result("user") |
46 | 53 | } |
47 | 54 | else{ |
48 | | - num === 0 ? this.result("gamer") : this.result("user"); |
| 55 | + num === 0 ? result("gamer") : result("user"); |
49 | 56 | } |
50 | 57 | setTimeout(() => { |
51 | | - this.restart() |
| 58 | + restart() |
52 | 59 | }, 300); |
53 | 60 | } |
54 | | - |
55 | | - |
56 | | - restart(){ |
57 | | - this.showValues = setInterval(() => { |
58 | | - let newValue = this.state.show === 0 ? 1 |
59 | | - : this.state.show === 1 ? 2 |
60 | | - : this.state.show === 2 && 0 |
61 | | - |
62 | | - this.setState({ |
63 | | - show : newValue |
64 | | - }) |
65 | | - }, 100); |
66 | | - } |
67 | 61 |
|
68 | | - // win(){ |
69 | | - // alert("이겼다!!!!"); |
70 | | - // this.setState((prev) => { |
71 | | - // return {user : prev.user + 1} |
72 | | - // }) |
| 62 | + useEffect(() => { |
| 63 | + restart() |
| 64 | + return clearInterval(showValues) |
| 65 | + }) |
73 | 66 |
|
74 | | - // } |
75 | | - |
76 | | - // lose(){ |
77 | | - // alert("졌다!!!!!!!!"); |
78 | | - // this.setState((prev) => { |
79 | | - // return {gamer : prev.gamer + 1} |
80 | | - // }) |
81 | | - |
82 | | - // } |
83 | | - |
84 | | - result(winner){ |
85 | | - alert(winner === "gamer" ? "졌다!!!!!" : "이겼다!!!!") |
86 | | - this.setState((prev) => { |
87 | | - return {[winner] : prev[winner] + 1} |
88 | | - }) |
89 | | - } |
90 | | - |
91 | | - clear(){ |
92 | | - this.setState({ |
93 | | - user : 0, |
94 | | - gamer : 0 |
95 | | - }) |
96 | | - } |
97 | | - |
98 | | - render(){ |
99 | | - return ( |
100 | | - <> |
| 67 | + return ( |
| 68 | + <> |
101 | 69 | <h1><span className = "gameName">가위바위보</span> 게임을 해봅시다.</h1> |
102 | | - <Score player = "gamer">{this.state.gamer}</Score> |
| 70 | + <Score player = "gamer">{gamer}</Score> |
103 | 71 | <div className="gamerValue"> |
104 | | - {this.state.values[this.state.show].emoji} |
| 72 | + {values[show].emoji} |
105 | 73 | </div> |
106 | | - <Score player = "user">{this.state.user}</Score> |
| 74 | + <Score player = "user">{user}</Score> |
107 | 75 | <div className="btns"> |
108 | | - <User onClick = {this.checkResult.bind(this, 0)}>{this.state.values[0].emoji}</User> |
109 | | - <User onClick = {this.checkResult.bind(this, 1)}>{this.state.values[1].emoji}</User> |
110 | | - <User onClick = {this.checkResult.bind(this, 2)}>{this.state.values[2].emoji}</User> |
111 | | - <User onClick = {this.clear} >🔄</User> |
| 76 | + <User onClick = {() => checkResult(0)}>{values[0].emoji}</User> |
| 77 | + <User onClick = {() => checkResult(1)}>{values[1].emoji}</User> |
| 78 | + <User onClick = {() => checkResult(2)}>{values[2].emoji}</User> |
| 79 | + <User onClick = {clear} >🔄</User> |
112 | 80 | </div> |
113 | | - </> |
114 | | - ) |
115 | | - } |
116 | | - |
| 81 | + </> |
| 82 | + ) |
117 | 83 | } |
118 | 84 |
|
| 85 | + |
119 | 86 | export default App; |
0 commit comments