- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在 React 中创建一个 Tic Tac Toe 板。当有人单击我的应用程序组件中的休息按钮时,我试图让板休息。我正在尝试找出处理这种情况的最佳方法。我一直在阅读有关振奋人心的内容,但如果有更简单的方法,任何建议都会有所帮助。这是我的代码
let data = {
"box":[
[ "", "", "" ],
[ "", "", "" ],
[ "", "", "" ]
],
"players": ['X','O']
}
class DataStore {
constructor(data, players) {
//store that data in the object
this.data = data;
this.players = players;
//empty array for our watchers
//watchers are objects that want to be informed about when the data changes
this.registeredWatchers = [];
}
//add a new watcher to the list
register(watcher) {
this.registeredWatchers.push(watcher);
}
setCrop(newDataState, row, col) {
//update data
this.data[row][col] = newDataState;
//inform all watching objects..
this.registeredWatchers.map((watcher) => {
watcher.dataUpdated();
})
}
}
class Dispatcher {
constructor() {
this.registeredHandlers = []; //D:
}
register(callback) {
this.registeredHandlers.push(callback);
}
dispatch(action) {
//call every method in our registered handlers array
//with this action as an input
this.registeredHandlers.map((handler) => {
//call that individual function in the array..
handler(action);
})
}
}
class BoxComponent extends React.Component {
render() {
return (
<div
className="box"
onClick={this.handleClick.bind(this)}>
<span>{this.props.type}</span>
</div>);
}
handleClick() {
//try a test dispatch
if(this.props.type.length > 0) {
return false;
} else {
boxDispatcher.dispatch({ type: "pick", row: this.props.rowNum, col: this.props.colNum});
}
}
}
class GridComponent extends React.Component {
constructor(props) {
//make sure this stays a react component
super(props);
this.state = {
turn: boxDataStore.players[0],
box: this.props.box
}
//ensure we're listening to the data store
boxDataStore.register(this);
}
dataUpdated() {
this.setState(prevState => ({
box: boxDataStore.data,
// turn: cropDataStore.players,
turn: prevState.turn === 'X'
? boxDataStore.players[1]
: boxDataStore.players[0]
}))
}
render() {
return(
<div>
{
//loop through all the rows...
this.state.box.map((row, rowNum) => {
//and write them out to the page..
return (<div className="row">
{
row.map((cropEntry, colNum) => {
return <BoxComponent type={cropEntry} rowNum={rowNum} colNum={colNum} turn={rowNum}/>
})
}
</div>);
})
}
</div>
)
}
}
class App extends React.Component {
render() {
return (
<div>
<h1>Tic Tac Toe </h1>
<button onClick={this.resetBoard.bind(this)}>reset</button>
<GridComponent box={this.props.data}/>
</div>
);
}
resetBoard() {
}
}
//start of app
var boxDispatcher = new Dispatcher();
var boxDataStore = new DataStore(data.box, data.players);
boxDispatcher.register((action) => {
if(boxDataStore.registeredWatchers["0"].state.turn == "X") {
//actually waint to handle it
boxDataStore.setCrop('X', action.row, action.col);
} else {
boxDataStore.setCrop('O', action.row, action.col);
}
})
最佳答案
如果您正在阅读有关提升状态的内容,您可能已经阅读了 React official documentation 的页面。就说到这了。
There should be a single “source of truth” for any data that changes in a React application. Usually, the state is first added to the component that needs it for rendering. Then, if other components also need it, you can lift it up to their closest common ancestor. Instead of trying to sync the state between different components, you should rely on the top-down data flow.
基本上,如果您不提升状态,则维护单一事实来源会更加困难,因为您必须同步两个状态,但感觉不太对劲。我认为没有“更简单的方法”可以做到这一点,因为它会给您带来很多副作用。
关于javascript - 在不同组件中重置 Tic Tac Toe 的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47274050/
我是初学者,所以我的代码很乱。我还没有完整地评论这个游戏,所以如果你需要澄清一些变量,我可以给你。 (顺便说一句,这是一个要求制作井字游戏的c++项目) 我的主要问题是,我将如何重复我的棋盘(每次有人
我正在为C的Tic Tac Toe代码编写一个简单的游戏。我已经完成了大部分代码,但是我希望AI永不丢失。 我已经阅读了有关minimax算法的信息,但我不理解。如何使用此算法使计算机获胜或平局,但永
感谢这里人们的帮助,我成功地禁用了点击 div 并在已经使用 $(".pos").addClass('already-played'); 选择它们时覆盖它们; 以及 CSS 中的这个: .已经播放{
我有一个井字棋游戏,其中用户(x)玩CPU(o)。游戏开始时,CPU 将 (o) 放置在中心,并在用户之后移动到随机位置。游戏设置为循环,但一旦出现获胜者,它就会重置,并且不会显示“你赢/输的横幅”。
我试图在没有人工智能的情况下实现井字棋游戏。不知怎的,我的点击功能会自动触发。您能帮我理解为什么点击功能会自动触发吗?这是 HTML 代码片段。 Tic Tac Toe Gam
我正在制作一个井字游戏程序。我计划将 minimax 与它一起使用。我制作了一棵树,其中包含所有可能的游戏序列的空间,并且我正在寻找一种方法来填充它。我目前有这种类型: typedef struct
我正在尝试遵循本教程: https://www.youtube.com/watch?v=Db3cC5iPrOM 2:59 我听不懂他在说什么。 我不明白为什么他在构造函数(public static
我在这里为我的java作业编写了井字棋游戏,一切都很好,除了一个小问题,即当您输入最后一步(第九回合)时,最后一个“X”不显示。这不仅很烦人,因为获胜的棋子没有显示,而且还导致了一些问题,即领带方法没
我对编码和 Java 比较陌生,在我的 CS-173 类(class)中,我的任务是创建一个 Tic Tac Toe 游戏。然而,当谈到创建确定获胜者的方法时,每当我获得“胜利”时,代码都不会运行说我
您好,我想尝试制作一个井字游戏,但遇到问题。我仍然是一个初学者,所以请随意提供有关组织和类似内容的提示,但我的问题是我的方法 checkRowWin、checkColoumnWin 和 E.T.C 添
我正在研究 Tic-Tac-Toe 游戏 (3x3) 的 alpha-beta 剪枝算法。目前,对于任何给定的 3x3 网格实例,我都能找出最好的情况: public Best chooseAlpha
我是一名初学者,正在学习 Java super 技能类(class)。我试图尝试 this VS Code 中的 tic tac toe 游戏项目。效果很好。但代码在提交时出错。 代码: packag
我已经研究“死代码”和“无法访问的代码”有一段时间了,但我似乎仍然无法弄清楚我的程序中这个问题是怎么回事。这是我所拥有的一个片段; “gameEnd()”方法检查 Tic Tac Toe 中的获胜者:
我目前正在做一项任务,即创建一个 Tic Tac Toe 游戏。我已经做到了玩家可以在棋盘上放置标记、绘制标记并随后切换回合。但是,只有当玩家将其标记放在左上角(第一个)字段时,我检查是否存在获胜条件
编辑:我注意到,当您为 TicTacToe 表输入错误的数字时,我的程序会输出“无效移动”。什么会导致这种情况呢?我只使用 move(row, col) 方法一次,因此它不会重复无效输入两次。 我一直
import java.util.Scanner; public class TTT{ public static int row, col; public static Scanner scan =
这个问题已经有答案了: Is Java "pass-by-reference" or "pass-by-value"? (91 个回答) 已关闭 7 年前。 我的井字棋程序有一个小问题。我有一个嵌套计
我正在用 python 开发一个 tic-tac-toe 程序。现在,轮到人类了,一切顺利。然而,AI 在玩完第一个回合后,不会再玩任何后续回合。我扫描了代码,似乎找不到任何可能导致此问题的错误。 请
function checkWin(){ if (arro[0] === arro[1] === arro[2] === 1 || arro[3] === arro[4] === arro[5] ==
我尝试更改innerHTML 的所有内容都没有改变任何内容。没有 X 或 O,并且不会显示当前玩家的姓名。我一直在试图解决这个问题。我一直在寻找答案,据我所知,我所做的一切都是我应该做的。我今晚必须交
我是一名优秀的程序员,十分优秀!