我正在尝试在我使用ReacJS创建的井字游戏应用程序中热交换“ X”和“ O”,该应用程序是在选择具有提交按钮的图像的单选按钮时选择的。选择单选按钮后,所有先前的“ X”和“ O”将自动更改为相应的图像。有没有办法做到这一点?我的原始帖子可以找到here,而codesandbox链接可以找到here。
App.js
import React, { Component } from 'react';
import './App.css';
import Status from'./components/Status';
import GameStatus from'./components/GameStatus';
const connery = require("./images/connery.svg");
const square = require("./images/square.svg");
class App extends Component {
constructor(props){
super(props)
this.state = {
board : Array(9).fill(null),
player : null,
winner : null,
gamemode : null,
/* array to store the ndex */
order_ndex : []
}
}
checkWinner(){
let winLines =
[
["0", "1", "2"],
["3", "4", "5"],
["6", "7", "8"],
["0", "3", "6"],
["1", "4", "7"],
["2", "5", "8"],
["0", "4", "8"],
["2", "4", "6"]
]
this.checkmatch(winLines)
}
checkmatch(winLines){
let board = this.state.board;
//let newWinner = this.state.winner
for (let index = 0; index < winLines.length; index++) {
const [a,b,c]=winLines[index];
if(board[a] && board[a] === board[b] && board[a] === board[c] ){
alert('You won!');
this.setState({
winner : this.state.player
})
this.state.winner = this.state.player;
}
}
if(!this.state.winner && !board.includes(null)){
this.setState({
winner : "None"
})
alert('Its a Draw!');
}
}
handleClick(index){
if(this.state.player && !this.state.winner){
let newBoard = this.state.board
if(this.state.board[index]===null){
newBoard[index] = this.state.player
/* push the last index into the array */
this.state.order_ndex.push(index)
this.setState({
board: newBoard,
player: this.state.player==="X" ? "O" : "X"
})
this.checkWinner()
}
}
}
setPlayer(player){
this.setState({player})
}
setGameMode(gamemode){
console.log(gamemode)
this.setState({gamemode})
}
renderBoxes(){
const isFrontend = this.state.gamemode === "Image";
return this.state.board.map(
(box, index) => (
<div
className="box"
key={index}
onClick={() => {
this.handleClick(index);
}}
>
{box === "X" && isFrontend && <img src={connery} />}
{box === "O" && isFrontend && <img src={square} />}
{!isFrontend && box}
</div>
));
}
reset(){
this.setState({
board : Array(9).fill(null),
player : null,
winner : null,
gamemode : null,
order_ndex : []
})
}
undo() {
let ndex = this.state.order_ndex.pop()
let newBoard = this.state.board
let prev = newBoard[ndex]
newBoard[ndex] = null
this.setState({
board: newBoard,
player: prev
})
}
render() {
return (
<div className="container">
<h1>Tic Tac Toe App</h1>
<GameStatus
gamemode ={this.state.gamemode}
setGameMode = {(e)=> this.setGameMode(e)}
/>
<Status
player={this.state.player}
setPlayer={(e) => this.setPlayer(e)}
gamemode ={this.state.gamemode}
winner = {this.state.winner}
/>
<div className="board">
{this.renderBoxes()}
</div>
<div className="btn">
<button className='reset' onClick = {() => this.reset()}>
{" "}
Reset{" "}
</button>
<div className="divider"/>
<button
className='reset'
disabled ={this.state.winner}
onClick = {() => this.undo()}
>
{" "}
Undo{" "}
</button>
</div>
</div>
);
}
}
export default App;
ChooseGameMode.js
import React, { Component } from 'react';
class ChooseGameMode extends Component{
handleForm(e){
e.preventDefault();
this.props.gamemode(e.target.gamemode.value);
}
render(){
return (
<form onSubmit={(e)=> this.handleForm(e)}>
<label>
Classic
<input type="radio" name="gamemode" value="Classic"/>
</label>
<label>
Image
<input type="radio" name="gamemode" value="Image"/>
</label>
<input type="submit" value="Submit" />
</form>
)
}
}
export default ChooseGameMode;
GameStatus.js
import React, { Component } from 'react';
import ChooseGameMode from'./ChooseGameMode';
class GameStatus extends Component {
handleSetGameMode(e){
this.props.setGameMode(e)
}
render(){
return (this.props.gamemode ?
<h4>You are playing the {this.props.gamemode} mode</h4> :
<ChooseGameMode gamemode={(e) => this.handleSetGameMode(e)} />
)
}
}
export default GameStatus;
choosePlayer.js
import React, { Component } from 'react';
class Player extends Component{
handleForm(e){
e.preventDefault();
this.props.player(e.target.player.value);
}
render(){
return (
<form onSubmit={(e)=> this.handleForm(e)}>
<label>
Player X
<input type="radio" name="player" value="X"/>
</label>
<label>
Player O
<input type="radio" name="player" value="O"/>
</label>
<input type="submit" value="Start" />
</form>
)
}
}
export default Player;
Status.js
import React, { Component } from 'react';
import Player from'./choosePlayer';
class Status extends Component {
handleSetPlayer(e){
if (this.props.gamemode) {
this.props.setPlayer(e)
}
}
renderHtml(){
if (this.props.winner){
return (<h5>Winner is {this.props.winner}</h5>)
} else {
return this.props.player ?
<h5>Next player is {this.props.player}</h5> :
<Player player={(e) => this.handleSetPlayer(e)} />
}
}
render(){
return (<span>{this.renderHtml()}</span>)
}
}
export default Status;