ec1117

Untitled

Jun 18th, 2022
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.99 KB | None | 0 0
  1. import logo from './logo.svg';
  2. import './App.css';
  3. import { useEffect, useState } from 'react';
  4. import { BsGearFill } from 'react-icons/bs'
  5. import { MdOutlineLeaderboard } from 'react-icons/md'
  6.  
  7. const API_URL = "https://raw.githubusercontent.com/xangregg/deepwordle/main/common.txt"
  8.  
  9. function App() {
  10. const [answer, setAnswer] = useState('');
  11. const [guesses, setGuesses] = useState(Array(6).fill(null));
  12. const [currentGuess, setCurrentGuess] = useState('');
  13. const [gameOver, setGameOver] = useState(false);
  14.  
  15. useEffect( () => {
  16. async function go_fetch() {
  17. const response = await fetch(API_URL);
  18. const data = await response.text();
  19. var cells = data.split('\n').map(function (el) { return el.split(/\s+/); });
  20. setAnswer(cells[Math.floor(Math.random()*cells.length)][0]);
  21. }
  22. go_fetch();
  23.  
  24. }, [])
  25.  
  26. useEffect( () => {
  27. let fired = false;
  28. // DIFFERENCE BETWEEN CONST AND FUNCTION?
  29. const handleType = (event) => {
  30. if (gameOver) {
  31. console.log("GAME OVER")
  32. return;
  33. }
  34. console.log(event.key)
  35. if (event.key === 'Enter'){
  36. if (currentGuess.length === 5) {
  37. if(currentGuess === answer || guesses.findIndex(x => x===null) === -1){
  38. setGameOver(true);
  39. }
  40. // const tmp = guesses;
  41. const tmp = [...guesses];
  42. tmp[guesses.findIndex(x => x==null)] = currentGuess;
  43. setGuesses(tmp);
  44. setCurrentGuess('');
  45. }
  46. return;
  47. }
  48. if (event.key === 'Backspace') {
  49. setCurrentGuess(currentGuess.slice(0,-1));
  50. return;
  51. }
  52.  
  53. if (event.keyCode >= 65 && event.keyCode <= 90 && currentGuess.length<5) {
  54. setCurrentGuess(oldGuess => oldGuess + event.key)
  55. console.log(currentGuess, currentGuess.length)
  56. return;
  57. }
  58. return;
  59. }
  60. window.addEventListener('keyup', handleType)
  61.  
  62. return () => {
  63. window.removeEventListener('keyup', handleType)
  64. }
  65. }, [currentGuess, guesses, gameOver, answer]);
  66.  
  67.  
  68. return (
  69. <div className="App">
  70. <Navbar/>
  71. {
  72. guesses.map((guess, i) => {
  73. const isCurrentGuess = i === guesses.findIndex( x => x==null)
  74. const isTmp = i >= guesses.findIndex( x=> x===null) && guesses.findIndex(x=>x===null) !== -1
  75. return <Line key={i} guess={isCurrentGuess?currentGuess:(guess ?? '')} answer={answer} isFinal={!isTmp}/>
  76. })
  77. // means passes empty string if null
  78. }
  79. </div>
  80. );
  81. }
  82.  
  83. function Navbar() {
  84. return <div>
  85. <div className='navbar'>
  86. <div className='center-nav'>
  87. Wordle
  88. </div>
  89. <ul>
  90. <li className='rIcons'><BsGearFill/></li>
  91. <li className='rIcons'><MdOutlineLeaderboard/></li>
  92. </ul>
  93. </div>
  94. <hr></hr>
  95. </div>
  96. }
  97.  
  98. function Line({guess, answer, isFinal}) {
  99. let tiles = [];
  100. for(let i=0;i<5;i++){
  101. let cl_name="tile";
  102. if (isFinal) {
  103. if(guess[i] === answer[i]){
  104. cl_name = "tile correct"
  105. }
  106. else if(answer.includes(guess[i])){
  107. cl_name = "tile almost"
  108. }
  109. else {
  110. cl_name = "tile incorrect"
  111. }
  112. }
  113. tiles.push(<div key={i} className={cl_name}>
  114. {guess[i]}
  115. </div>)
  116. }
  117. return <div className="line">
  118. {tiles}
  119. </div>
  120. }
  121.  
  122. export default App;
  123.  
  124.  
  125. import React from 'react';
  126. import ReactDOM from 'react-dom/client';
  127. import './index.css';
  128. import App from './App';
  129. import reportWebVitals from './reportWebVitals';
  130.  
  131. const root = ReactDOM.createRoot(document.getElementById('root'));
  132. root.render(
  133. <React.StrictMode>
  134. <App />
  135. </React.StrictMode>
  136. );
  137.  
  138. // If you want to start measuring performance in your app, pass a function
  139. // to log results (for example: reportWebVitals(console.log))
  140. // or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals
  141. reportWebVitals();
  142.  
  143.  
  144. .App {
  145. text-align: center;
  146. vertical-align: center;
  147. /* height: 50px */
  148. }
  149.  
  150. .navbar {
  151. display: inline-flex;
  152. margin: 0;
  153. padding: 0;
  154. font-family: Arial, Helvetica, sans-serif;
  155. vertical-align: middle;
  156. font-weight: bold;
  157. font-size:xx-large;
  158. }
  159.  
  160. ul {
  161. margin: 0;
  162. float: right;
  163. list-style-type: none;
  164. text-align: center;
  165. vertical-align: middle;
  166. }
  167.  
  168. li {
  169. vertical-align: middle;
  170. }
  171.  
  172. .center-nav {
  173. }
  174.  
  175. .rIcons {
  176. float:right;
  177. }
  178.  
  179. .line {
  180. /* height: 50px; */
  181. display: flex;
  182. vertical-align: center;
  183. align-items: center;
  184. align-content: center;
  185. margin:auto;
  186. justify-content: center;
  187. }
  188.  
  189. .tile {
  190.  
  191. display: inline-block;
  192. font-family: Arial, Helvetica, sans-serif;
  193. height: 50px;
  194. width: 50px;
  195. margin: 5px;
  196. border: 2px solid lightgray;
  197. text-align: center;
  198. font-size: 22px;
  199. font-weight: bold;
  200. text-transform: capitalize;
  201. vertical-align: middle;
  202. line-height: 50px;
  203. }
  204.  
  205. .correct{
  206. background-color: lightgreen;
  207. }
  208.  
  209. .almost{
  210. background-color: yellow;
  211. }
  212.  
  213. .incorrect{
  214. background-color: rgb(243, 236, 236);
  215. }
  216.  
Add Comment
Please, Sign In to add comment