Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Creating a Tic-Tac-Toe Game Using CSS

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1.02k
    Comment on it

    Hello readers, Well all love to play games whether a child or an adult. So here in my blog I have tried to create a game which we all have played in his/her life in their childhood days.

     

    So, I have created it using CSS properties , hope you all will love to play it.

     

    Rules:-

    In this game basically 2 players can play against each other which therefore will result either win or tie.

     

    The player can also restart the game when required.

     

    Formation :-

    As in this game , there are 9 blocks i.e 3 rows with 3 columns creating a matrix which results in 9 possible turns for each player.

     

    It includes radio and checked buttons for the labels created for each 9 blocks.

     

    Below is the HTML code for the Tic-Tac-Toe Game :-
     

    1. <!DOCTYPE html>
    2. <html>
    3.  
    4. <head>
    5. <meta charset="utf-8">
    6. <meta http-equiv="X-UA-Compatible" content="IE=edge">
    7. <title>Tic-Tac-Toe</title>
    8. <!-- Fonts css file -->
    9. <link href="https://fonts.googleapis.com/css?family=Open+Sans:400,600,700" rel="stylesheet">
    10. <link href="css/style.css" rel="stylesheet" type="text/css" />
    11. <link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css" />
    12. </head>
    13. <body>
    14. <div class="container">
    15. <div class="tic-tac-toe">
    16. <input id="block1-1-1" type="radio" class="player-1 left first-column top first-row first-diagonal turn-1"/>
    17. <label for="block1-1-1" class="turn-1"></label>
    18. <input id="block1-1-2" type="radio" class="player-1 middle second-column top first-row turn-1"/>
    19. <label for="block1-1-2" class="turn-1"></label>
    20. <input id="block1-1-3" type="radio" class="player-1 right third-column top first-row second-diagonal turn-1"/>
    21. <label for="block1-1-3" class="turn-1"></label>
    22. <input id="block1-2-1" type="radio" class="player-1 left first-column center second-row turn-1"/>
    23. <label for="block1-2-1" class="turn-1"></label>
    24. <input id="block1-2-2" type="radio" class="player-1 middle second-column center second-row first-diagonal second-diagonal turn-1"/>
    25. <label for="block1-2-2" class="turn-1"></label>
    26. <input id="block1-2-3" type="radio" class="player-1 right third-column center second-row turn-1"/>
    27. <label for="block1-2-3" class="turn-1"></label>
    28. <input id="block1-3-1" type="radio" class="player-1 left first-column bottom third-row second-diagonal turn-1"/>
    29. <label for="block1-3-1" class="turn-1"></label>
    30. <input id="block1-3-2" type="radio" class="player-1 middle second-column bottom third-row turn-1"/>
    31. <label for="block1-3-2" class="turn-1"></label>
    32. <input id="block1-3-3" type="radio" class="player-1 right third-column bottom third-row first-diagonal turn-1"/>
    33. <label for="block1-3-3" class="turn-1"></label>
    34. <input id="block2-1-1" type="radio" class="player-2 left first-column top first-row first-diagonal turn-2"/>
    35. <label for="block2-1-1" class="turn-2"></label>
    36. <input id="block2-1-2" type="radio" class="player-2 middle second-column top first-row turn-2"/>
    37. <label for="block2-1-2" class="turn-2"></label>
    38. <input id="block2-1-3" type="radio" class="player-2 right third-column top first-row second-diagonal turn-2"/>
    39. <label for="block2-1-3" class="turn-2"></label>
    40. <input id="block2-2-1" type="radio" class="player-2 left first-column center second-row turn-2"/>
    41. <label for="block2-2-1" class="turn-2"></label>
    42. <input id="block2-2-2" type="radio" class="player-2 middle second-column center second-row first-diagonal second-diagonal turn-2"/>
    43. <label for="block2-2-2" class="turn-2"></label>
    44. <input id="block2-2-3" type="radio" class="player-2 right third-column center second-row turn-2"/>
    45. <label for="block2-2-3" class="turn-2"></label>
    46. <input id="block2-3-1" type="radio" class="player-2 left first-column bottom third-row second-diagonal turn-2"/>
    47. <label for="block2-3-1" class="turn-2"></label>
    48. <input id="block2-3-2" type="radio" class="player-2 middle second-column bottom third-row turn-2"/>
    49. <label for="block2-3-2" class="turn-2"></label>
    50. <input id="block2-3-3" type="radio" class="player-2 right third-column bottom third-row first-diagonal turn-2"/>
    51. <label for="block2-3-3" class="turn-2"></label>
    52. <input id="block3-1-1" type="radio" class="player-1 left first-column top first-row first-diagonal turn-3"/>
    53. <label for="block3-1-1" class="turn-3"></label>
    54. <input id="block3-1-2" type="radio" class="player-1 middle second-column top first-row turn-3"/>
    55. <label for="block3-1-2" class="turn-3"></label>
    56. <input id="block3-1-3" type="radio" class="player-1 right third-column top first-row second-diagonal turn-3"/>
    57. <label for="block3-1-3" class="turn-3"></label>
    58. <input id="block3-2-1" type="radio" class="player-1 left first-column center second-row turn-3"/>
    59. <label for="block3-2-1" class="turn-3"></label>
    60. <input id="block3-2-2" type="radio" class="player-1 middle second-column center second-row first-diagonal second-diagonal turn-3"/>
    61. <label for="block3-2-2" class="turn-3"></label>
    62. <input id="block3-2-3" type="radio" class="player-1 right third-column center second-row turn-3"/>
    63. <label for="block3-2-3" class="turn-3"></label>
    64. <input id="block3-3-1" type="radio" class="player-1 left first-column bottom third-row second-diagonal turn-3"/>
    65. <label for="block3-3-1" class="turn-3"></label>
    66. <input id="block3-3-2" type="radio" class="player-1 middle second-column bottom third-row turn-3"/>
    67. <label for="block3-3-2" class="turn-3"></label>
    68. <input id="block3-3-3" type="radio" class="player-1 right third-column bottom third-row first-diagonal turn-3"/>
    69. <label for="block3-3-3" class="turn-3"></label>
    70. <input id="block4-1-1" type="radio" class="player-2 left first-column top first-row first-diagonal turn-4"/>
    71. <label for="block4-1-1" class="turn-4"></label>
    72. <input id="block4-1-2" type="radio" class="player-2 middle second-column top first-row turn-4"/>
    73. <label for="block4-1-2" class="turn-4"></label>
    74. <input id="block4-1-3" type="radio" class="player-2 right third-column top first-row second-diagonal turn-4"/>
    75. <label for="block4-1-3" class="turn-4"></label>
    76. <input id="block4-2-1" type="radio" class="player-2 left first-column center second-row turn-4"/>
    77. <label for="block4-2-1" class="turn-4"></label>
    78. <input id="block4-2-2" type="radio" class="player-2 middle second-column center second-row first-diagonal second-diagonal turn-4"/>
    79. <label for="block4-2-2" class="turn-4"></label>
    80. <input id="block4-2-3" type="radio" class="player-2 right third-column center second-row turn-4"/>
    81. <label for="block4-2-3" class="turn-4"></label>
    82. <input id="block4-3-1" type="radio" class="player-2 left first-column bottom third-row second-diagonal turn-4"/>
    83. <label for="block4-3-1" class="turn-4"></label>
    84. <input id="block4-3-2" type="radio" class="player-2 middle second-column bottom third-row turn-4"/>
    85. <label for="block4-3-2" class="turn-4"></label>
    86. <input id="block4-3-3" type="radio" class="player-2 right third-column bottom third-row first-diagonal turn-4"/>
    87. <label for="block4-3-3" class="turn-4"></label>
    88. <input id="block5-1-1" type="radio" class="player-1 left first-column top first-row first-diagonal turn-5"/>
    89. <label for="block5-1-1" class="turn-5"></label>
    90. <input id="block5-1-2" type="radio" class="player-1 middle second-column top first-row turn-5"/>
    91. <label for="block5-1-2" class="turn-5"></label>
    92. <input id="block5-1-3" type="radio" class="player-1 right third-column top first-row second-diagonal turn-5"/>
    93. <label for="block5-1-3" class="turn-5"></label>
    94. <input id="block5-2-1" type="radio" class="player-1 left first-column center second-row turn-5"/>
    95. <label for="block5-2-1" class="turn-5"></label>
    96. <input id="block5-2-2" type="radio" class="player-1 middle second-column center second-row first-diagonal second-diagonal turn-5"/>
    97. <label for="block5-2-2" class="turn-5"></label>
    98. <input id="block5-2-3" type="radio" class="player-1 right third-column center second-row turn-5"/>
    99. <label for="block5-2-3" class="turn-5"></label>
    100. <input id="block5-3-1" type="radio" class="player-1 left first-column bottom third-row second-diagonal turn-5"/>
    101. <label for="block5-3-1" class="turn-5"></label>
    102. <input id="block5-3-2" type="radio" class="player-1 middle second-column bottom third-row turn-5"/>
    103. <label for="block5-3-2" class="turn-5"></label>
    104. <input id="block5-3-3" type="radio" class="player-1 right third-column bottom third-row first-diagonal turn-5"/>
    105. <label for="block5-3-3" class="turn-5"></label>
    106. <input id="block6-1-1" type="radio" class="player-2 left first-column top first-row first-diagonal turn-6"/>
    107. <label for="block6-1-1" class="turn-6"></label>
    108. <input id="block6-1-2" type="radio" class="player-2 middle second-column top first-row turn-6"/>
    109. <label for="block6-1-2" class="turn-6"></label>
    110. <input id="block6-1-3" type="radio" class="player-2 right third-column top first-row second-diagonal turn-6"/>
    111. <label for="block6-1-3" class="turn-6"></label>
    112. <input id="block6-2-1" type="radio" class="player-2 left first-column center second-row turn-6"/>
    113. <label for="block6-2-1" class="turn-6"></label>
    114. <input id="block6-2-2" type="radio" class="player-2 middle second-column center second-row first-diagonal second-diagonal turn-6"/>
    115. <label for="block6-2-2" class="turn-6"></label>
    116. <input id="block6-2-3" type="radio" class="player-2 right third-column center second-row turn-6"/>
    117. <label for="block6-2-3" class="turn-6"></label>
    118. <input id="block6-3-1" type="radio" class="player-2 left first-column bottom third-row second-diagonal turn-6"/>
    119. <label for="block6-3-1" class="turn-6"></label>
    120. <input id="block6-3-2" type="radio" class="player-2 middle second-column bottom third-row turn-6"/>
    121. <label for="block6-3-2" class="turn-6"></label>
    122. <input id="block6-3-3" type="radio" class="player-2 right third-column bottom third-row first-diagonal turn-6"/>
    123. <label for="block6-3-3" class="turn-6"></label>
    124. <input id="block7-1-1" type="radio" class="player-1 left first-column top first-row first-diagonal turn-7"/>
    125. <label for="block7-1-1" class="turn-7"></label>
    126. <input id="block7-1-2" type="radio" class="player-1 middle second-column top first-row turn-7"/>
    127. <label for="block7-1-2" class="turn-7"></label>
    128. <input id="block7-1-3" type="radio" class="player-1 right third-column top first-row second-diagonal turn-7"/>
    129. <label for="block7-1-3" class="turn-7"></label>
    130. <input id="block7-2-1" type="radio" class="player-1 left first-column center second-row turn-7"/>
    131. <label for="block7-2-1" class="turn-7"></label>
    132. <input id="block7-2-2" type="radio" class="player-1 middle second-column center second-row first-diagonal second-diagonal turn-7"/>
    133. <label for="block7-2-2" class="turn-7"></label>
    134. <input id="block7-2-3" type="radio" class="player-1 right third-column center second-row turn-7"/>
    135. <label for="block7-2-3" class="turn-7"></label>
    136. <input id="block7-3-1" type="radio" class="player-1 left first-column bottom third-row second-diagonal turn-7"/>
    137. <label for="block7-3-1" class="turn-7"></label>
    138. <input id="block7-3-2" type="radio" class="player-1 middle second-column bottom third-row turn-7"/>
    139. <label for="block7-3-2" class="turn-7"></label>
    140. <input id="block7-3-3" type="radio" class="player-1 right third-column bottom third-row first-diagonal turn-7"/>
    141. <label for="block7-3-3" class="turn-7"></label>
    142. <input id="block8-1-1" type="radio" class="player-2 left first-column top first-row first-diagonal turn-8"/>
    143. <label for="block8-1-1" class="turn-8"></label>
    144. <input id="block8-1-2" type="radio" class="player-2 middle second-column top first-row turn-8"/>
    145. <label for="block8-1-2" class="turn-8"></label>
    146. <input id="block8-1-3" type="radio" class="player-2 right third-column top first-row second-diagonal turn-8"/>
    147. <label for="block8-1-3" class="turn-8"></label>
    148. <input id="block8-2-1" type="radio" class="player-2 left first-column center second-row turn-8"/>
    149. <label for="block8-2-1" class="turn-8"></label>
    150. <input id="block8-2-2" type="radio" class="player-2 middle second-column center second-row first-diagonal second-diagonal turn-8"/>
    151. <label for="block8-2-2" class="turn-8"></label>
    152. <input id="block8-2-3" type="radio" class="player-2 right third-column center second-row turn-8"/>
    153. <label for="block8-2-3" class="turn-8"></label>
    154. <input id="block8-3-1" type="radio" class="player-2 left first-column bottom third-row second-diagonal turn-8"/>
    155. <label for="block8-3-1" class="turn-8"></label>
    156. <input id="block8-3-2" type="radio" class="player-2 middle second-column bottom third-row turn-8"/>
    157. <label for="block8-3-2" class="turn-8"></label>
    158. <input id="block8-3-3" type="radio" class="player-2 right third-column bottom third-row first-diagonal turn-8"/>
    159. <label for="block8-3-3" class="turn-8"></label>
    160. <input id="block9-1-1" type="radio" class="player-1 left first-column top first-row first-diagonal turn-9"/>
    161. <label for="block9-1-1" class="turn-9"></label>
    162. <input id="block9-1-2" type="radio" class="player-1 middle second-column top first-row turn-9"/>
    163. <label for="block9-1-2" class="turn-9"></label>
    164. <input id="block9-1-3" type="radio" class="player-1 right third-column top first-row second-diagonal turn-9"/>
    165. <label for="block9-1-3" class="turn-9"></label>
    166. <input id="block9-2-1" type="radio" class="player-1 left first-column center second-row turn-9"/>
    167. <label for="block9-2-1" class="turn-9"></label>
    168. <input id="block9-2-2" type="radio" class="player-1 middle second-column center second-row first-diagonal second-diagonal turn-9"/>
    169. <label for="block9-2-2" class="turn-9"></label>
    170. <input id="block9-2-3" type="radio" class="player-1 right third-column center second-row turn-9"/>
    171. <label for="block9-2-3" class="turn-9"></label>
    172. <input id="block9-3-1" type="radio" class="player-1 left first-column bottom third-row second-diagonal turn-9"/>
    173. <label for="block9-3-1" class="turn-9"></label>
    174. <input id="block9-3-2" type="radio" class="player-1 middle second-column bottom third-row turn-9"/>
    175. <label for="block9-3-2" class="turn-9"></label>
    176. <input id="block9-3-3" type="radio" class="player-1 right third-column bottom third-row first-diagonal turn-9"/>
    177. <label for="block9-3-3" class="turn-9"></label>
    178. <div class="end">
    179. <h3></h3><a href="">Restart</a>
    180. </div>
    181. </div>
    182. <h5>Note: use the Full Page view for the best experience.</h5>
    183. </div>
    184. </body>
    185. </html>

     

    Below is the CSS code for Tic-Tac-Toe Game :-

    1. @charset "UTF-8";
    2. /* Variables
    3. -------------------------------------------------------------- */
    4. /* Body and Notice styling
    5. -------------------------------------------------------------- */
    6. body {
    7. color: #b6b5ca;
    8. font-family: 'Arial', sans-serif;
    9. margin: 0;
    10. text-align: center;
    11. }
    12.  
    13. h5 {
    14. font-weight: 400;
    15. padding: 0 20px;
    16. }
    17.  
    18. /* Tic-tac-toe game
    19. -------------------------------------------------------------- */
    20. .tic-tac-toe {
    21. font-family: 'Open Sans', sans-serif;
    22. height: 300px;
    23. overflow: hidden;
    24. margin: 50px auto 30px auto;
    25. position: relative;
    26. width: 300px;
    27. }
    28. @media (min-width: 450px) {
    29. .tic-tac-toe {
    30. height: 450px;
    31. width: 450px;
    32. }
    33. }
    34. .tic-tac-toe input[type="radio"] {
    35. display: none;
    36. }
    37. .tic-tac-toe input[type="radio"]:checked + label {
    38. cursor: default;
    39. z-index: 10 !important;
    40. }
    41. .tic-tac-toe input[type="radio"].player-1 + label:after {
    42. content: "";
    43. }
    44. .tic-tac-toe input[type="radio"].player-2 + label:after {
    45. content: "";
    46. }
    47. .tic-tac-toe input[type="radio"].player-1:checked + label:after, .tic-tac-toe input[type="radio"].player-2:checked + label:after {
    48. opacity: 1;
    49. }
    50. .tic-tac-toe input[type="radio"].player-1:checked + label {
    51. background-color: #dc685a;
    52. }
    53. .tic-tac-toe input[type="radio"].player-2:checked + label {
    54. background-color: #ecaf4f;
    55. }
    56. .tic-tac-toe input[type="radio"].turn-1 + label {
    57. z-index: 1;
    58. }
    59. .tic-tac-toe input[type="radio"].turn-2 + label {
    60. z-index: 2;
    61. }
    62. .tic-tac-toe input[type="radio"].turn-3 + label {
    63. z-index: 3;
    64. }
    65. .tic-tac-toe input[type="radio"].turn-4 + label {
    66. z-index: 4;
    67. }
    68. .tic-tac-toe input[type="radio"].turn-5 + label {
    69. z-index: 5;
    70. }
    71. .tic-tac-toe input[type="radio"].turn-6 + label {
    72. z-index: 6;
    73. }
    74. .tic-tac-toe input[type="radio"].turn-7 + label {
    75. z-index: 7;
    76. }
    77. .tic-tac-toe input[type="radio"].turn-8 + label {
    78. z-index: 8;
    79. }
    80. .tic-tac-toe input[type="radio"].turn-9 + label {
    81. z-index: 9;
    82. }
    83. .tic-tac-toe input[type="radio"].turn-1 + label {
    84. display: block;
    85. }
    86. .tic-tac-toe input[type="radio"].turn-1:checked ~ .turn-2 + label {
    87. display: block;
    88. }
    89. .tic-tac-toe input[type="radio"].turn-2:checked ~ .turn-3 + label {
    90. display: block;
    91. }
    92. .tic-tac-toe input[type="radio"].turn-3:checked ~ .turn-4 + label {
    93. display: block;
    94. }
    95. .tic-tac-toe input[type="radio"].turn-4:checked ~ .turn-5 + label {
    96. display: block;
    97. }
    98. .tic-tac-toe input[type="radio"].turn-5:checked ~ .turn-6 + label {
    99. display: block;
    100. }
    101. .tic-tac-toe input[type="radio"].turn-6:checked ~ .turn-7 + label {
    102. display: block;
    103. }
    104. .tic-tac-toe input[type="radio"].turn-7:checked ~ .turn-8 + label {
    105. display: block;
    106. }
    107. .tic-tac-toe input[type="radio"].turn-8:checked ~ .turn-9 + label {
    108. display: block;
    109. }
    110. .tic-tac-toe input[type="radio"].left + label {
    111. left: 0;
    112. }
    113. .tic-tac-toe input[type="radio"].top + label {
    114. top: 0;
    115. }
    116. .tic-tac-toe input[type="radio"].middle + label {
    117. left: 100px;
    118. }
    119. .tic-tac-toe input[type="radio"].right + label {
    120. left: 200px;
    121. }
    122. .tic-tac-toe input[type="radio"].center + label {
    123. top: 100px;
    124. }
    125. .tic-tac-toe input[type="radio"].bottom + label {
    126. top: 200px;
    127. }
    128. @media (min-width: 450px) {
    129. .tic-tac-toe input[type="radio"].middle + label {
    130. left: 150px;
    131. }
    132. .tic-tac-toe input[type="radio"].right + label {
    133. left: 300px;
    134. }
    135. .tic-tac-toe input[type="radio"].center + label {
    136. top: 150px;
    137. }
    138. .tic-tac-toe input[type="radio"].bottom + label {
    139. top: 300px;
    140. }
    141. }
    142. .tic-tac-toe input[type="radio"]:checked ~ input[type="radio"]:checked ~ input[type="radio"]:checked ~
    143. input[type="radio"]:checked ~ input[type="radio"]:checked ~ input[type="radio"]:checked ~
    144. input[type="radio"]:checked ~ input[type="radio"]:checked ~ input[type="radio"]:checked ~ .end {
    145. display: block;
    146. }
    147. .tic-tac-toe input[type="radio"]:checked ~ input[type="radio"]:checked ~ input[type="radio"]:checked ~
    148. input[type="radio"]:checked ~ input[type="radio"]:checked ~ input[type="radio"]:checked ~
    149. input[type="radio"]:checked ~ input[type="radio"]:checked ~ input[type="radio"]:checked ~ .end > h3:before {
    150. content: "It is a tie!";
    151. }
    152. .tic-tac-toe .player-1.first-column:checked ~ .player-1.first-column:checked ~ .player-1.first-column:checked ~ .end,
    153. .tic-tac-toe .player-1.second-column:checked ~ .player-1.second-column:checked ~ .player-1.second-column:checked ~ .end,
    154. .tic-tac-toe .player-1.third-column:checked ~ .player-1.third-column:checked ~ .player-1.third-column:checked ~ .end,
    155. .tic-tac-toe .player-1.first-row:checked ~ .player-1.first-row:checked ~ .player-1.first-row:checked ~ .end,
    156. .tic-tac-toe .player-1.second-row:checked ~ .player-1.second-row:checked ~ .player-1.second-row:checked ~ .end,
    157. .tic-tac-toe .player-1.third-row:checked ~ .player-1.third-row:checked ~ .player-1.third-row:checked ~ .end,
    158. .tic-tac-toe .player-1.first-diagonal:checked ~ .player-1.first-diagonal:checked ~ .player-1.first-diagonal:checked ~ .end,
    159. .tic-tac-toe .player-1.second-diagonal:checked ~ .player-1.second-diagonal:checked ~ .player-1.second-diagonal:checked ~ .end {
    160. display: block;
    161. }
    162. .tic-tac-toe .player-1.first-column:checked ~ .player-1.first-column:checked ~ .player-1.first-column:checked ~ .end h3:before,
    163. .tic-tac-toe .player-1.second-column:checked ~ .player-1.second-column:checked ~ .player-1.second-column:checked ~ .end h3:before,
    164. .tic-tac-toe .player-1.third-column:checked ~ .player-1.third-column:checked ~ .player-1.third-column:checked ~ .end h3:before,
    165. .tic-tac-toe .player-1.first-row:checked ~ .player-1.first-row:checked ~ .player-1.first-row:checked ~ .end h3:before,
    166. .tic-tac-toe .player-1.second-row:checked ~ .player-1.second-row:checked ~ .player-1.second-row:checked ~ .end h3:before,
    167. .tic-tac-toe .player-1.third-row:checked ~ .player-1.third-row:checked ~ .player-1.third-row:checked ~ .end h3:before,
    168. .tic-tac-toe .player-1.first-diagonal:checked ~ .player-1.first-diagonal:checked ~ .player-1.first-diagonal:checked ~ .end h3:before,
    169. .tic-tac-toe .player-1.second-diagonal:checked ~ .player-1.second-diagonal:checked ~ .player-1.second-diagonal:checked ~ .end h3:before {
    170. content: "Player 1 wins!" !important;
    171. }
    172. .tic-tac-toe .player-2.first-column:checked ~ .player-2.first-column:checked ~ .player-2.first-column:checked ~ .end,
    173. .tic-tac-toe .player-2.second-column:checked ~ .player-2.second-column:checked ~ .player-2.second-column:checked ~ .end,
    174. .tic-tac-toe .player-2.third-column:checked ~ .player-2.third-column:checked ~ .player-2.third-column:checked ~ .end,
    175. .tic-tac-toe .player-2.first-row:checked ~ .player-2.first-row:checked ~ .player-2.first-row:checked ~ .end,
    176. .tic-tac-toe .player-2.second-row:checked ~ .player-2.second-row:checked ~ .player-2.second-row:checked ~ .end,
    177. .tic-tac-toe .player-2.third-row:checked ~ .player-2.third-row:checked ~ .player-2.third-row:checked ~ .end,
    178. .tic-tac-toe .player-2.first-diagonal:checked ~ .player-2.first-diagonal:checked ~ .player-2.first-diagonal:checked ~ .end,
    179. .tic-tac-toe .player-2.second-diagonal:checked ~ .player-2.second-diagonal:checked ~ .player-2.second-diagonal:checked ~ .end {
    180. display: block;
    181. }
    182. .tic-tac-toe .player-2.first-column:checked ~ .player-2.first-column:checked ~ .player-2.first-column:checked ~ .end h3:before,
    183. .tic-tac-toe .player-2.second-column:checked ~ .player-2.second-column:checked ~ .player-2.second-column:checked ~ .end h3:before,
    184. .tic-tac-toe .player-2.third-column:checked ~ .player-2.third-column:checked ~ .player-2.third-column:checked ~ .end h3:before,
    185. .tic-tac-toe .player-2.first-row:checked ~ .player-2.first-row:checked ~ .player-2.first-row:checked ~ .end h3:before,
    186. .tic-tac-toe .player-2.second-row:checked ~ .player-2.second-row:checked ~ .player-2.second-row:checked ~ .end h3:before,
    187. .tic-tac-toe .player-2.third-row:checked ~ .player-2.third-row:checked ~ .player-2.third-row:checked ~ .end h3:before,
    188. .tic-tac-toe .player-2.first-diagonal:checked ~ .player-2.first-diagonal:checked ~ .player-2.first-diagonal:checked ~ .end h3:before,
    189. .tic-tac-toe .player-2.second-diagonal:checked ~ .player-2.second-diagonal:checked ~ .player-2.second-diagonal:checked ~ .end h3:before {
    190. content: "Player 2 wins!" !important;
    191. }
    192. .tic-tac-toe label {
    193. background-color: #78bec5;
    194. border-radius: 14px;
    195. cursor: pointer;
    196. color: #fff;
    197. display: none;
    198. height: 90px;
    199. margin: 5px;
    200. position: absolute;
    201. width: 90px;
    202. -moz-transition: background-color 0.3s;
    203. -o-transition: background-color 0.3s;
    204. -webkit-transition: background-color 0.3s;
    205. transition: background-color 0.3s;
    206. }
    207. @media (min-width: 450px) {
    208. .tic-tac-toe label {
    209. height: 140px;
    210. width: 140px;
    211. }
    212. }
    213. .tic-tac-toe label:hover {
    214. background-color: #3d4250;
    215. }
    216. .tic-tac-toe label:hover:after {
    217. opacity: .4;
    218. }
    219. .tic-tac-toe label:after {
    220. left: 0;
    221. font-family: "FontAwesome";
    222. font-size: 45px;
    223. margin-top: -22.5px;
    224. opacity: 0;
    225. position: absolute;
    226. text-align: center;
    227. text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.2);
    228. top: 50%;
    229. width: 100%;
    230. }
    231. @media (min-width: 450px) {
    232. .tic-tac-toe label:after {
    233. font-size: 70px;
    234. margin-top: -35px;
    235. }
    236. }
    237. .tic-tac-toe .end {
    238. background: rgba(255, 255, 255, 0.8);
    239. bottom: 5px;
    240. color: #3d4250;
    241. display: none;
    242. left: 5px;
    243. padding-top: 55px;
    244. position: absolute;
    245. right: 5px;
    246. top: 5px;
    247. text-align: center;
    248. z-index: 11;
    249. }
    250. @media (min-width: 450px) {
    251. .tic-tac-toe .end {
    252. padding-top: 110px;
    253. }
    254. }
    255. .tic-tac-toe .end h3 {
    256. font-size: 30px;
    257. font-weight: 300;
    258. }
    259. @media (min-width: 450px) {
    260. .tic-tac-toe .end h3 {
    261. font-size: 40px;
    262. }
    263. }
    264. .tic-tac-toe .end a {
    265. background-color: #3d4250;
    266. border-radius: 4px;
    267. color: #fff;
    268. padding: 14px 45px;
    269. text-decoration: none;
    270. -moz-transition: background-color 0.2s;
    271. -o-transition: background-color 0.2s;
    272. -webkit-transition: background-color 0.2s;
    273. transition: background-color 0.2s;
    274. }
    275. .tic-tac-toe .end a:hover {
    276. background-color: #262934;
    277. cursor: pointer;
    278. }

    How it works :-

    • As it uses labels for the visible part on the block and radio buttons for the hidden part.
    • In this game , there are basically 9 possible turns , in every turn a player can click on one label only.
    • On clicking once on a label , the state changes to checked.
    • On triggering the checked state on the block , by using the z-index the CSS moves to the next turn and therefore the z-index gets changed on each turn.
    • To each label I have added a class turn-x , that help us to know whose turn is know.
    • The 1 player plays on odd turn while the player 2 at an even turn.
    • To determine which player had won the game , we need to check all the possible victories earned by a player .


     

    Conclusion :-

    I have easily created my first game i.e Tic-Tac-Toe by using HTML and CSS properties .

    By using the above code anyone can easily create this game.


    Note:- The above code will run over all modern browsers.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Reset Password
Fill out the form below and reset your password: