我已经编写了一个作业程序,其中我们必须编写一个简单的Gomoku程序。我以为我拥有了所有东西,但是当我编译并运行时,即使我只有4个同类,即使它们彼此不相邻,也会引发胜利。 (只有一种类型的连续有五个... X或O才能引发一场胜利)。我觉得我应该在每转之前将计数器重置为0,但是我不确定应该在哪里进行操作。任何提示将不胜感激!
class Creator extends React.Component {
state = {
news: [{ heading: "", body: "", link: "" }],
name: "",
description: "",
email: ""
};
handleChange = e => {
if (["heading", "body", "Link"].includes(e.target.className)) {
let news = [...this.state.news];
news[e.target.dataset.id][e.target.className] = e.target.value;
this.setState({ news }, () => console.log(this.state.news));
} else {
this.setState({ [e.target.heading]: e.target.value });
}
};
addNewsItem = e => {
this.setState(prevState => ({
news: [...prevState.news, { heading: "", body: "", Link: "" }]
}));
};
removeClick(i) {
let news = [...this.state.news];
news.splice(i, 1);
this.setState({ news });
}
handleSubmit = e => {
e.preventDefault();
};
render() {
let { name, description, news, email } = this.state;
return (
<form onSubmit={this.handleSubmit} onChange={this.handleChange}>
<Wrapper>
<NewsTitle>Newsletter Creator</NewsTitle>
<br />
<CenterDiv>
<Heading htmlFor="name">Newsletter Title:</Heading>
<input type="text" heading="name" id="name" value={name} />
</CenterDiv>
<CenterDiv>
<Heading2 htmlFor="description">Description</Heading2>
<input
type="text"
heading="description"
id="description"
value={description}
/>
</CenterDiv>
<GreenButton onClick={this.addNewsItem}>Add News Item</GreenButton>
{news.map((val, idx) => {
let headingId = `heading-${idx}`,
bodyId = `body-${idx}`,
linkId = `link-${idx}`;
return (
<div key={idx}>
<div key={idx}>
<Heading htmlFor={headingId}>{`News Item #${idx +
1}`}</Heading>
</div>
<Heading2 htmlFor={headingId}>Heading:</Heading2>
<input
type="text"
name={headingId}
data-id={idx}
id={headingId}
value={news[idx].heading}
className="heading"
/>
<Heading2 htmlFor={bodyId}>Body:</Heading2>
<textarea
cols="40"
rows="2"
type="text"
name={bodyId}
data-id={idx}
id={bodyId}
value={news[idx].body}
className="body"
/>
<Heading2 htmlFor={linkId}>Link:</Heading2>
<input
type="text"
name={linkId}
data-id={idx}
id={linkId}
value={news[idx].link}
className="link"
/>
<RedButton onClick={this.removeClick.bind(this, idx)}>
Delete Item
</RedButton>
</div>
);
})}
<CenterDiv>
<Heading2 htmlFor="email">Enter your Email Address</Heading2>
<input type="text" heading="email" id="email" value={email} />
</CenterDiv>
<CenterDiv>
<GreenButton onClick={this.addNewsItem}>Add News Item</GreenButton>
<BlueButton type="submit" value="Submit">
Save Newsletter
</BlueButton>
<YellowButton type="send" value="Send">
Send
</YellowButton>
</CenterDiv>
</Wrapper>
<Footer>
<FooterText>
Developed by Conor Cahalane using React and Firebase
</FooterText>
</Footer>
</form>
);
}
}
export default Creator;
答案 0 :(得分:1)
在检查获胜条件的所有三个功能中都存在问题:isHorizontalWin
,isVerticalWin
和isDiagonalWin
。这三个变量都使变量count
递增,但是此变量永远不会设置回零。此外,检查是否应在循环内进行count == 5
。这是有关如何修复isHorizontalWin
的示例:
public static boolean isHorizontalWin(char[][] map, int player)
{
int count = 0;
int r;
int c;
for (int i = 0; i < map.length; i++)
{
for (int j = 0; j < map.length; j++)
{
if (map[i][j]==(player))
{
r = i;
c = j;
while (r >= 0 && r <= 18 && c >= 0 && c <= 18 && map[r][c] == player)
{
count ++;
r += 0;
c += 1;
}
if (count == 5)
{
return true;
} else {
count = 0;
}
}
}
}
return false;
}