Tic Tac Toe Board

huangapple go评论77阅读模式
英文:

Tic Tac Toe Board

问题

我现在正在进行奥丁计划,并且正在进行井字游戏项目。我相信这对我来说会非常困难,但我期待着。然而,我在这里是要求帮助在HTML和CSS中设置游戏板。我应该如何着手?

<!-- begin snippet: js hide: false console: false babel: false -->

<!-- language: lang-html -->
<div class="board" id="upperLeft"></div>
<div class="board" id="upperCenter"></div>
<div class="board" id="upperRight"></div>
<div class="board" id="middleLeft"></div>
<div class="board" id="middleCenter"></div>
<div class="board" id="middleRight"></div>
<div class="board" id="lowerLeft"></div>
<div class="board" id="lowerMiddle"></div>
<div class="board" id="lowerRight"></div>

<!-- end snippet -->

请注意,上面是HTML代码片段,用于创建游戏板的各个部分。

英文:

I am doing The Odin Project right now and I am on the Tic Tac Toe project. I am sure this will be very difficult for me but I am looking forward to it. However, I am here to ask for help with setting the board up in HTML and CSS. How would I even go about this?

<!-- begin snippet: js hide: false console: false babel: false -->

<!-- language: lang-html -->

&lt;div class=&quot;board&quot; id=&quot;upperLeft&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;board&quot; id=&quot;upperCenter&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;board&quot; id=&quot;upperRight&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;board&quot; id=&quot;middleLeft&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;board&quot; id=&quot;middleCenter&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;board&quot; id=middleRight&gt;&lt;/div&gt;
&lt;div class=&quot;board&quot; id=&quot;lowerLeft&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;board&quot; id=&quot;lowerMiddle&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;board&quot; id=&quot;lowerRight&quot;&gt;&lt;/div&gt;

<!-- end snippet -->

答案1

得分: 1

你可以使用CSS网格进行布局。

.board {
  width: 500px;
  height: 500px;
  margin: 0 auto;
  
  display: grid;
  grid-template-columns: auto auto auto;
}

.box {
  border: 3px solid;
  border-radius: 2px;
  display: flex;
  justify-content: center;
  align-items: center;
}
<div class="board">
  <div class="box" id="upperLeft"></div>
  <div class="box" id="upperCenter"></div>
  <div class="box" id="upperRight"></div>
  <div class="box" id="middleLeft"></div>
  <div class="box" id="middleCenter"></div>
  <div class="box" id="middleRight"></div>
  <div class="box" id="bottomLeft"></div>
  <div class="box" id="bottomCenter"></div>
  <div class="box" id="bottomRight"></div>
</div>

另外,你可以将所有内容都包含在一个大的div中,就像我在board中所做的那样 Tic Tac Toe Board

英文:

You can use CSS grids to lay it out.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.board {
  width: 500px;
  height: 500px;
  margin: 0 auto;
  
  display: grid;
  grid-template-columns: auto auto auto;
}

.box{
  border: 3px solid;
  border-radius: 2px;
  display: flex;
  justify-content: center;
  align-items: center;
}

<!-- language: lang-html -->

&lt;div class=&quot;board&quot;&gt;
  &lt;div class=&quot;box&quot; id=&quot;upperLeft&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;box&quot; id=&quot;upperCenter&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;box&quot; id=&quot;upperRight&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;box&quot; id=&quot;middleLeft&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;box&quot; id=&quot;middleCenter&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;box&quot; id=&quot;middleRight&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;box&quot; id=&quot;bottomLeft&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;box&quot; id=&quot;bottomCenter&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;box&quot; id=&quot;bottomRight&quot;&gt;&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

Also, you can make everything a member of one class by including it in one big div like I did with the board Tic Tac Toe Board

答案2

得分: 0

你可以使用 grid;这样,单元格可以直接成为容器的子元素:

&lt;div id=&quot;board&quot;&gt;
  &lt;div class=&quot;cell&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;cell&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;cell&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;cell&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;cell&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;cell&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;cell&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;cell&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;cell&quot;&gt;&lt;/div&gt;
&lt;/div&gt;
#board {
  display: grid;

  /* 使三行等高 */
  grid-template-rows: repeat(3, 1fr);
  /* 使三列等宽 */
  grid-template-columns: repeat(3, 1fr);

  /* 控制单元格之间的间隔 */
  gap: 1em;

  /* 高度自己选择 */
  height: 400px;
  /* 使其成为完美的正方形 */
  aspect-ratio: 1 / 1;
}

试试看:

<!-- 开始片段: js 隐藏: true -->

<!-- 语言: lang-css -->

#board {
  display: grid;
  grid-template-rows: repeat(3, 1fr);
  grid-template-columns: repeat(3, 1fr);
  gap: 1em;
  margin: 2em auto;
  height: 400px;
  aspect-ratio: 1 / 1;
}

.cell {
  背景: #ddd;
}

<!-- 语言: lang-html -->

&lt;div id=&quot;board&quot;&gt;
  &lt;div class=&quot;cell&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;cell&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;cell&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;cell&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;cell&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;cell&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;cell&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;cell&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;cell&quot;&gt;&lt;/div&gt;
&lt;/div&gt;

<!-- 结束片段 -->

英文:

You can use grid; this way, cells can be direct children of the board:

&lt;div id=&quot;board&quot;&gt;
  &lt;div class=&quot;cell&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;cell&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;cell&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;cell&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;cell&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;cell&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;cell&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;cell&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;cell&quot;&gt;&lt;/div&gt;
&lt;/div&gt;
#board {
  display: grid;

  /* Make three rows of equal height */
  grid-template-rows: repeat(3, 1fr);
  /* Make three columns of equal width */
  grid-template-columns: repeat(3, 1fr);

  /* Controls the gap between cells */
  gap: 1em;

  /* A size of your choice */
  height: 400px;
  /* Make it a perfect square */
  aspect-ratio: 1 / 1;
}

Try it:

<!-- begin snippet: js hide: true -->

<!-- language: lang-css -->

#board {
  display: grid;
  grid-template-rows: repeat(3, 1fr);
  grid-template-columns: repeat(3, 1fr);
  gap: 1em;
  margin: 2em auto;
  height: 400px;
  aspect-ratio: 1 / 1;
}

.cell {
  background: #ddd;
}

<!-- language: lang-html -->

&lt;div id=&quot;board&quot;&gt;
  &lt;div class=&quot;cell&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;cell&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;cell&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;cell&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;cell&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;cell&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;cell&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;cell&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;cell&quot;&gt;&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

答案3

得分: 0

以下是翻译好的内容:

以下示例使用弹性盒子布局和vmin,使其能够在大多数视口大小上自适应。除了一些极端情况(老实说,我想不出任何情况),这些正方形始终保持相等的边长,整个游戏始终在视口垂直和水平居中显示。线条特性通过更改div.line上的类来控制。

作为奖励,还包括一个<dialog>,它在OPTION注释下作为弹出窗口显示消息,显示谁赢了以及用户退出或重新玩的按钮。当然,需要JavaScript才能使其正常工作(参见<dialog>)。

示例中有注释

/**
 * 所有长度(除了轮廓以外)都以“vmin”为单位
 * 例如,5vmin = 5vh(如果视口高度小于视口宽度)
 * 或者5vw(如果视口宽度小于视口高度)。
 */
:root {
  font: 5vmin/1 "Segoe UI";
}

/**
 * 所有元素都垂直和水平居中显示。
 * “form”是<dialog>的一部分(见下面的OPTION),
 * 它不是必需的。
 */
body,
main,
.frame,
.square,
form {
  display: flex;
  justify-content: center;
  align-items: center;
}

body {
  min-height: 100vh;
  padding: 0;
  background: white;
  overflow: hidden;
}

main {
  flex-flow: row wrap; /* ".square"将按行排列,并在填充前一行的3个方块时创建新行 */
  position: relative; /* 这将设置“main”的边框作为“.frame”的定位参考 */
  width: 75vmin;
  height: 75vmin;
}

.square {
  width: 25vmin;
  height: 25vmin;
  margin: 0;
  outline: 3px inset grey;
  font-size: 5rem;
  font-family: Consolas; /* 等宽字体最适合居中对齐 */
  vertical-align: middle;
  text-transform: uppercase;
  cursor: pointer;
}

.frame {
  position: absolute; /* 将“.frame”设置为正常“flow”之外,以防止其尺寸干扰元素的“static” */
  z-index: 1; /* 将“.frame”置于z轴上的“static”元素之上 */
  top: 0; /* 将左上角定位到“main”的顶部 */
  left: 0; /* 将左上角定位到“main”的左侧 */
  min-width: 100%;
  min-height: 100%;
  outline: 3vw solid white; /* 遮罩所有“.square”的外边缘 */
  overflow: hidden; /* 隐藏超出边界的所有内容 */
  pointer-events: none; /* “.frame”位于所有“.square”之上,还会阻止用户点击或悬停在“.square”上,从而允许绕过“.frame”,以使“.square”可点击和可悬停 */
}

.line {
  position: relative; /* “.line”在移动到新位置时参考其原始位置 */
  min-width: 150%;
  height: 3vmin;
  border: 0;
  background: black;
  transform-origin: center center; /* 允许它以中心旋转 */
}

/**
 * 通过类分配线位置到“.line”
 * 每个数字表示“.square”的位置
 * “.s000”隐藏“.line”,这是它的正常状态
 */
/* 没有“.line” */
.s000 {
  display: none;
}
/* 顶部水平 */
.s123 {
  bottom: 25vmin;
}
/* 中间水平 */
.s456 {/* 这是默认位置,可以通过不将任何类分配给“.line”来实现相同的效果 */ }
/* 底部水平 */
.s789 {
  top: 25vmin;
}
/* 左侧垂直 */
.s147 {
  right: 25vmin;
  transform: rotate(90deg);
}
/* 中间垂直 */
.s258 {
  transform: rotate(90deg);
}
/* 右侧垂直 */
.s369 {
  left: 25vmin;
  transform: rotate(90deg);
}
/* 从左到右对角线 */
.s159 {
  transform: rotate(45deg);
}
/* 从右到左对角线 */
.s357 {
  transform: rotate(-45deg);
}

/* OPTION */
dialog {
  position: fixed;
  z-index: 2;
  border-radius: 8px;
}

dialog::backdrop {
  background: rgba(0, 0, 0, 0.3);
}

form {
  flex-flow: column nowrap;
  font-variant: small-caps;
}

fieldset {
  border: 0;
}

#message {
  font-size: 3rem;
  text-align: center;
}

input {
  width: 5rem;
  font: inherit;
  font-size: 2rem;
  vertical-align: middle;
  cursor: pointer;
}
<main>
  <div class="square">x</div>
  <div class="square">o</div>
  <div class="square">o</div>
  <div class="square"></div>
  <div class="square">x</div>
  <div class="square"></div>
  <div class="square"></div>
  <div class="square"></div>
  <div class="square">x</div>
  <section class="frame">
    <div class="line s159"></div>
  </section>
</main>

<!-- OPTION -->
<dialog>
  <form method="dialog">
    <output id="message">X wins!</output>
    <fieldset>
      <input type="submit" value="Exit">
      <input type="submit" value="Play">
    </fieldset>
  </form>
</dialog>

<details>
<summary>英文:</summary>
The following example uses flexbox and `vmin` which makes it fluidly adaptable to most viewport sizes. Excluding edge cases (TBH, I can&#39;t think of any), the squares always maintain even lengths and and the whole game will always be centered vertically and horizontally within the viewport. The line feature is controlled by changing classes on `div.line`.
As a bonus a `&lt;dialog&gt;` is included which is under the ***OPTION*** comment as a popup to display a message as to who won and buttons for the user to exit or play again. JavaScript is required for it to function of course (see [`&lt;dialog&gt;`][1])
**Details are commented in example**
&lt;!-- begin snippet: js hide: false console: false babel: false --&gt;
&lt;!-- language: lang-css --&gt;
/**
* All lengths (with the exception of outlines) are in &quot;vmin&quot;
* ex. 5vmin = 5vh
*             if viewport height is smaller than viewport width.
*                                 OR
*             5vw 
*             if viewport width is smaller than viewport height.
*/
:root {
font: 5vmin/1 &quot;Segoe UI&quot;;
}
/**
* All elements center it&#39;s contents vertically and horizontally.
* &quot;form&quot; is part of the &lt;dialog&gt; (see OPTION below)
* it is not required.
*/
body,
main,
.frame,
.square,
form {
display: flex;
justify-content: center;
align-items: center;
}
body {
min-height: 100vh;
padding: 0;
background: white;
overflow: hidden;
}
main {
flex-flow: row wrap /* &quot;.square&quot;s will orient in rows and will
make a new row when 3 of them have
filled the previous row */ ;
position: relative /* This sets the borders of &quot;main&quot; as the
reference for &quot;.frame&quot; positioning */ ;
width: 75vmin;
height: 75vmin;
}
.square {
width: 25vmin;
height: 25vmin;
margin: 0;
outline: 3px inset grey;
font-size: 5rem;
font-family: Consolas /* Monospaced fonts center the best */ ;
vertical-align: middle;
text-transform: uppercase;
cursor: pointer;
}
.frame {
position: absolute /* Sets &quot;.frame&quot; out of normal &quot;flow&quot; so
that it&#39;s dimensions do not interfere
with the &quot;static&quot; of the elements */ ;
z-index: 1 /* Brings &quot;.frame&quot; above the &quot;static&quot; elements in
the z-axis */ ;
top: 0 /* Position top left corner to the top of &quot;main&quot; */;
left: 0 /* Position top left corner to the left of &quot;main&quot; */ ;
min-width: 100%;
min-height: 100%;
outline: 3vw solid white /* Mask the outer edges of all &quot;.square&quot; */ ;
overflow: hidden /* Hide all content that exceeds it&#39;s borders */ ;
pointer-events: none /* &quot;.frame&quot; sits above all of the &quot;.square&quot;s 
which also blocks the user from clicking 
or hovering over any &quot;.square&quot;. This allows
&quot;.frame&quot; to be bypassed so that &quot;.square&quot; is
clickable and hoverable again */ ;
}
.line {
position: relative /* &quot;.line&quot; references it&#39;s original position
when moving to a new position */ ;
min-width: 150%;
height: 3vmin;
border: 0;
background: black;
transform-origin: center center /* Allows it to spin from it&#39;s
center when it rotates */;
}
/**
* Line positions are assigned by a class to &quot;.line&quot;
* Each number represents the position of a &quot;.square&quot;
* &quot;.s000&quot; hides &quot;.line&quot; which is it&#39;s normal state
*/
/* No &quot;.line&quot; */
.s000 {
display: none;
}
/* Top horizontal */
.s123 {
bottom: 25vmin;
}
/* Middle horizontal */
.s456 {/* This is default position the same effect can be 
accomplished by not assigning any class to &quot;.line&quot;*/ }
/* Bottom horizontal */
.s789 {
top: 25vmin;
}
/* Left vertical */
.s147 {
right: 25vmin;
transform: rotate(90deg);
}
/* Center vertical */  
.s258 {
transform: rotate(90deg);
}
/* Right vertical */
.s369 {
left: 25vmin;
transform: rotate(90deg);
}
/* Left to right diagonal */
.s159 {
transform: rotate(45deg);
}
/* Right to left diagonal */
.s357 {
transform: rotate(-45deg);
}
/* OPTION */
dialog {
position: fixed;
z-index: 2;
border-radius: 8px;
}
dialog::backdrop {
background: rgba(0, 0, 0, 0.3);
}
form {
flex-flow: column nowrap;
font-variant: small-caps;
}
fieldset {
border: 0;
}
#message {
font-size: 3rem;
text-align: center;
}
input {
width: 5rem;
font: inherit;
font-size: 2rem;
vertical-align: middle;
cursor: pointer;
}
&lt;!-- language: lang-html --&gt;
&lt;main&gt;
&lt;div class=&quot;square&quot;&gt;x&lt;/div&gt;
&lt;div class=&quot;square&quot;&gt;o&lt;/div&gt;
&lt;div class=&quot;square&quot;&gt;o&lt;/div&gt;
&lt;div class=&quot;square&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;square&quot;&gt;x&lt;/div&gt;
&lt;div class=&quot;square&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;square&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;square&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;square&quot;&gt;x&lt;/div&gt;
&lt;section class=&quot;frame&quot;&gt;
&lt;div class=&quot;line s159&quot;&gt;&lt;/div&gt;
&lt;/section&gt;
&lt;/main&gt;
&lt;!-- OPTION --&gt;
&lt;dialog&gt;
&lt;form method=&quot;dialog&quot;&gt;
&lt;output id=&quot;message&quot;&gt;X wins!&lt;/output&gt;
&lt;fieldset&gt;
&lt;input type=&quot;submit&quot; value=&quot;Exit&quot;&gt;
&lt;input type=&quot;submit&quot; value=&quot;Play&quot;&gt;
&lt;/fieldset&gt;
&lt;/form&gt;
&lt;/dialog&gt;
&lt;!-- end snippet --&gt;
[1]: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/dialog
</details>
# 答案4
**得分**: 0
1. 这是一个可以帮助的表格。
2. HTML:
```html
<table>
<tr>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
</tr>
</table>
  1. CSS:
td {
    border: 1px solid black;
    height: 50px;
    width: 50px;
}
英文:

1.Here's a table that would help.

2.Html:

    &lt;table&gt;
&lt;tr&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;

</table>
3.Css:

    td {
border: 1px solid black; 
height: 50px;
width: 50px;
}

huangapple
  • 本文由 发表于 2023年6月6日 03:04:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76409297.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定