Código de HTML - Tic Tac Toe

<<>>

5

Publicado el 27 de Diciembre del 2023gráfica de visualizaciones de la versión: 5
277 visualizaciones desde el 27 de Diciembre del 2023
estrellaestrellaestrellaestrellaestrella
estrellaestrellaestrellaestrella
estrellaestrellaestrella
estrellaestrella
estrella

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <link rel="preconnect" href="https://fonts.gstatic.com" />
    <link
      href="https://fonts.googleapis.com/css2?family=Itim&display=swap"
      rel="stylesheet"
    />
 
    <style>
       * {
        padding: 0;
        margin: 0;
        font-family: 'Itim', cursive;
        }
        .background{
          margin-top:50px;
          font-size: 30px;
          color: white;
        }
        .container {
          margin: 0 auto;
          margin-top: 30px;
          display: grid;
          grid-template-columns: 33% 33% 33%;
          grid-template-rows: 33% 33% 33%;
          max-width: 300px;
        }
        .tile {
          border: 1px solid white;
          min-width: 100px;
          min-height: 100px;
          display: flex;
          justify-content: center;
          align-items: center;
          font-size: 50px;
          cursor: pointer;
        }
        .playerX {
          color: #09C372;
        }
 
        .playerO {
          color: #498AFB;
        }
        .announcer {
          color: white;
          text-align: center;
          font-size: 30px;
          margin: 10px 0;
        }
        .controls{
          margin: 30px auto;
          width: max-content;
        }
        #reset {
          outline: none;
          cursor: pointer;
          border: none;
          padding: 0.9rem 2rem;
          margin: 0;
          font-family: inherit;
          font-size: inherit;
          position: relative;
          display: inline-block;
          letter-spacing: 0.05rem;
          font-weight: 700;
          font-size: 17px;
          border-radius: 500px;
          overflow: hidden;
          background: #66ff66;
          color: rgb(0, 0, 0);
        }
 
        #reset span {
          position: relative;
          z-index: 10;
          transition: color 0.4s;
 
        }
 
        #reset:hover span {
          color: rgb(250, 250, 250);
        }
 
        #reset::before,
        #reset::after {
          position: absolute;
          top: 0;
          left: 0;
          width: 100%;
          height: 100%;
          z-index: 0;
        }
 
        #reset::before {
          content: "";
          background: #fff;
          width: 120%;
          left: -10%;
          transform: skew(30deg);
          transition: transform 0.4s cubic-bezier(0.3, 1, 0.8, 1);
        }
 
        #reset:hover::before {
          transform: translate3d(100%, 0, 0);
        }
 
      </style>
  </head>
  <body style="background-color: black;">
    <center>
    <main class="background">
      <section class="title">
        <h1>Tic Tac Toe</h1>
      </section>
      <section class="display">
        Player <span class="display-player playerX">X</span>'s turn
      </section>
    </center>
      <section class="container">
        <div class="tile"></div>
        <div class="tile"></div>
        <div class="tile"></div>
        <div class="tile"></div>
        <div class="tile"></div>
        <div class="tile"></div>
        <div class="tile"></div>
        <div class="tile"></div>
        <div class="tile"></div>
      </section>
      <section class="display announcer hide"></section>
      <section class="controls">
        <button id="reset"> <span>Reset</span></button>
      </section>
    </main>
    <script>
      window.addEventListener("DOMContentLoaded", () => {
        // everything goes here
      const tiles = Array.from(document.querySelectorAll(".tile"));
      const playerDisplay = document.querySelector(".display-player");
      const resetButton = document.querySelector("#reset");
      const announcer = document.querySelector(".announcer");
      announcer.style.className = "result"
      let board = ["", "", "", "", "", "", "", "", ""];
      let currentPlayer = "X";
      let isGameActive = true;
      const PLAYERX_WON = "PLAYERX_WON";
      const PLAYERO_WON = "PLAYERO_WON";
      const TIE = "TIE";
      /*
   Indexes within the board
   [0] [1] [2]
   [3] [4] [5]
   [6] [7] [8]
*/
      const winningConditions = [
        [0, 1, 2],
        [3, 4, 5],
        [6, 7, 8],
        [0, 3, 6],
        [1, 4, 7],
        [2, 5, 8],
        [0, 4, 8],
        [2, 4, 6],
      ];
      const isValidAction = (tile) => {
        if (tile.innerText === "X" || tile.innerText === "O") {
          return false;
        }
        return true;
      };
      const updateBoard = (index) => {
        board[index] = currentPlayer;
      };
      const changePlayer = () => {
        playerDisplay.classList.remove(`player${currentPlayer}`);
        currentPlayer = currentPlayer === "X" ? "O" : "X";
        playerDisplay.innerText = currentPlayer;
        playerDisplay.classList.add(`player${currentPlayer}`);
      };
      const announce = (type) => {
        switch (type) {
          case PLAYERO_WON:
            announcer.innerHTML = 'Player <span class="playerO">O</span> Won';
            break;
          case PLAYERX_WON:
            announcer.innerHTML = 'Player <span class="playerX">X</span> Won';
            break;
          case TIE:
            announcer.innerText = "Tie";
        }
        announcer.classList.remove("hide");
      };
 
      function handleResultValidation() {
        let roundWon = false;
        for (let i = 0; i <= 7; i++) {
          const winCondition = winningConditions[i];
          const a = board[winCondition[0]];
          const b = board[winCondition[1]];
          const c = board[winCondition[2]];
          if (a === "" || b === "" || c === "") {
            continue;
          }
          if (a === b && b === c) {
            roundWon = true;
            break;
          }
        }
 
        if (roundWon) {
          announce(currentPlayer === "X" ? PLAYERX_WON : PLAYERO_WON);
          isGameActive = false;
          return;
        }
 
        if (!board.includes("")) announce(TIE);
      }
 
      const userAction = (tile, index) => {
        if (isValidAction(tile) && isGameActive) {
          tile.innerText = currentPlayer;
          tile.classList.add(`player${currentPlayer}`);
          updateBoard(index);
          handleResultValidation();
          changePlayer();
        }
      };
 
      tiles.forEach((tile, index) => {
        tile.addEventListener("click", () => userAction(tile, index));
      });
 
      const resetBoard = () => {
        board = ["", "", "", "", "", "", "", "", ""];
        isGameActive = true;
        announcer.classList.add("hide");
 
        if (currentPlayer === "O") {
          changePlayer();
        }
 
        tiles.forEach((tile) => {
          tile.innerText = "";
          tile.classList.remove("playerX");
          tile.classList.remove("playerO");
        });
        announcer.innerHTML=""
      };
 
      resetButton.addEventListener("click",resetBoard);
    });
 
    </script>
  </body>
</html>



Comentarios sobre la versión: 5 (0)


No hay comentarios
 

Comentar la versión: 5

Nombre
Correo (no se visualiza en la web)
Valoración
Comentarios...
CerrarCerrar
CerrarCerrar
Cerrar

Tienes que ser un usuario registrado para poder insertar imágenes, archivos y/o videos.

Puedes registrarte o validarte desde aquí.

Codigo
Negrita
Subrayado
Tachado
Cursiva
Insertar enlace
Imagen externa
Emoticon
Tabular
Centrar
Titulo
Linea
Disminuir
Aumentar
Vista preliminar
sonreir
dientes
lengua
guiño
enfadado
confundido
llorar
avergonzado
sorprendido
triste
sol
estrella
jarra
camara
taza de cafe
email
beso
bombilla
amor
mal
bien
Es necesario revisar y aceptar las políticas de privacidad

http://lwp-l.com/s7475