React Trigger Function After Browser Paints
up vote
0
down vote
favorite
I am building a chess engine using react and javascript and I am trying to implement a feature where the engine makes a move after you make your own move. I have a function "change_states" that changes the states of the chess game whenever a user makes a move. I also have a function "engine_move" which makes an engine move on the board based on the current states of the game.
When I call "engine_move" as either a callback for setState, a function in ComponentDidUpdate, or even requestAnimationFrame the "engine_move" function runs before the changes from the "change_state" function are reflected in the browser. This means that the moved piece disappears for as long as the engine_move function is running and then two moves render on the board immediately (the original players move and the engine move). I can't figure out how to get it so the browser repaints in-between the user move and the engine move. Any help would be greatly appreciated. Thanks!
class Chess extends Component {
constructor(props) {
super(props);
this.state = {
history: [{ position: new Position('white', initialize_engine_board(), [95,
25], [1, 1, 1, 1], 0) }],
drag_end: null,
promotion:{class:'hidden',start: null, end: null, player: null},
status:null
}
}
change_states(history, position, start, end, promotion_piece) {
let possible_moves = legal_moves(position);
let move = create_move(start, end, position, promotion_piece);
let status = null;
if (is_legal(move, possible_moves)) {
let new_position = make_move(position, move);
let new_moves = legal_moves(new_position);
if (new_moves.length === 0) {
status = 'Game Over'
}
this.setState({
history: history.concat([{position: new_position}]),
drag_end: null,
status: status,
});
}
}
engine_move() {
const history = this.state.history.slice();
const position = history[history.length - 1].position;
//Time in milliseconds
let search_time = 1000;
let engine_move = alphabeta_search(position,10,search_time).move;
if (engine_move === null) {
this.setState({
status: 'Game Over',
});
return;
}
let new_position = make_move(position, engine_move);
this.setState({
history: history.concat([{position: new_position}]),
engine_turn: false
});
}
javascript reactjs
add a comment |
up vote
0
down vote
favorite
I am building a chess engine using react and javascript and I am trying to implement a feature where the engine makes a move after you make your own move. I have a function "change_states" that changes the states of the chess game whenever a user makes a move. I also have a function "engine_move" which makes an engine move on the board based on the current states of the game.
When I call "engine_move" as either a callback for setState, a function in ComponentDidUpdate, or even requestAnimationFrame the "engine_move" function runs before the changes from the "change_state" function are reflected in the browser. This means that the moved piece disappears for as long as the engine_move function is running and then two moves render on the board immediately (the original players move and the engine move). I can't figure out how to get it so the browser repaints in-between the user move and the engine move. Any help would be greatly appreciated. Thanks!
class Chess extends Component {
constructor(props) {
super(props);
this.state = {
history: [{ position: new Position('white', initialize_engine_board(), [95,
25], [1, 1, 1, 1], 0) }],
drag_end: null,
promotion:{class:'hidden',start: null, end: null, player: null},
status:null
}
}
change_states(history, position, start, end, promotion_piece) {
let possible_moves = legal_moves(position);
let move = create_move(start, end, position, promotion_piece);
let status = null;
if (is_legal(move, possible_moves)) {
let new_position = make_move(position, move);
let new_moves = legal_moves(new_position);
if (new_moves.length === 0) {
status = 'Game Over'
}
this.setState({
history: history.concat([{position: new_position}]),
drag_end: null,
status: status,
});
}
}
engine_move() {
const history = this.state.history.slice();
const position = history[history.length - 1].position;
//Time in milliseconds
let search_time = 1000;
let engine_move = alphabeta_search(position,10,search_time).move;
if (engine_move === null) {
this.setState({
status: 'Game Over',
});
return;
}
let new_position = make_move(position, engine_move);
this.setState({
history: history.concat([{position: new_position}]),
engine_turn: false
});
}
javascript reactjs
It is very similar to this question here: stackoverflow.com/questions/50569064/…
– Chris Atkeson
Nov 11 at 22:51
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I am building a chess engine using react and javascript and I am trying to implement a feature where the engine makes a move after you make your own move. I have a function "change_states" that changes the states of the chess game whenever a user makes a move. I also have a function "engine_move" which makes an engine move on the board based on the current states of the game.
When I call "engine_move" as either a callback for setState, a function in ComponentDidUpdate, or even requestAnimationFrame the "engine_move" function runs before the changes from the "change_state" function are reflected in the browser. This means that the moved piece disappears for as long as the engine_move function is running and then two moves render on the board immediately (the original players move and the engine move). I can't figure out how to get it so the browser repaints in-between the user move and the engine move. Any help would be greatly appreciated. Thanks!
class Chess extends Component {
constructor(props) {
super(props);
this.state = {
history: [{ position: new Position('white', initialize_engine_board(), [95,
25], [1, 1, 1, 1], 0) }],
drag_end: null,
promotion:{class:'hidden',start: null, end: null, player: null},
status:null
}
}
change_states(history, position, start, end, promotion_piece) {
let possible_moves = legal_moves(position);
let move = create_move(start, end, position, promotion_piece);
let status = null;
if (is_legal(move, possible_moves)) {
let new_position = make_move(position, move);
let new_moves = legal_moves(new_position);
if (new_moves.length === 0) {
status = 'Game Over'
}
this.setState({
history: history.concat([{position: new_position}]),
drag_end: null,
status: status,
});
}
}
engine_move() {
const history = this.state.history.slice();
const position = history[history.length - 1].position;
//Time in milliseconds
let search_time = 1000;
let engine_move = alphabeta_search(position,10,search_time).move;
if (engine_move === null) {
this.setState({
status: 'Game Over',
});
return;
}
let new_position = make_move(position, engine_move);
this.setState({
history: history.concat([{position: new_position}]),
engine_turn: false
});
}
javascript reactjs
I am building a chess engine using react and javascript and I am trying to implement a feature where the engine makes a move after you make your own move. I have a function "change_states" that changes the states of the chess game whenever a user makes a move. I also have a function "engine_move" which makes an engine move on the board based on the current states of the game.
When I call "engine_move" as either a callback for setState, a function in ComponentDidUpdate, or even requestAnimationFrame the "engine_move" function runs before the changes from the "change_state" function are reflected in the browser. This means that the moved piece disappears for as long as the engine_move function is running and then two moves render on the board immediately (the original players move and the engine move). I can't figure out how to get it so the browser repaints in-between the user move and the engine move. Any help would be greatly appreciated. Thanks!
class Chess extends Component {
constructor(props) {
super(props);
this.state = {
history: [{ position: new Position('white', initialize_engine_board(), [95,
25], [1, 1, 1, 1], 0) }],
drag_end: null,
promotion:{class:'hidden',start: null, end: null, player: null},
status:null
}
}
change_states(history, position, start, end, promotion_piece) {
let possible_moves = legal_moves(position);
let move = create_move(start, end, position, promotion_piece);
let status = null;
if (is_legal(move, possible_moves)) {
let new_position = make_move(position, move);
let new_moves = legal_moves(new_position);
if (new_moves.length === 0) {
status = 'Game Over'
}
this.setState({
history: history.concat([{position: new_position}]),
drag_end: null,
status: status,
});
}
}
engine_move() {
const history = this.state.history.slice();
const position = history[history.length - 1].position;
//Time in milliseconds
let search_time = 1000;
let engine_move = alphabeta_search(position,10,search_time).move;
if (engine_move === null) {
this.setState({
status: 'Game Over',
});
return;
}
let new_position = make_move(position, engine_move);
this.setState({
history: history.concat([{position: new_position}]),
engine_turn: false
});
}
javascript reactjs
javascript reactjs
asked Nov 11 at 22:40
Chris Atkeson
143
143
It is very similar to this question here: stackoverflow.com/questions/50569064/…
– Chris Atkeson
Nov 11 at 22:51
add a comment |
It is very similar to this question here: stackoverflow.com/questions/50569064/…
– Chris Atkeson
Nov 11 at 22:51
It is very similar to this question here: stackoverflow.com/questions/50569064/…
– Chris Atkeson
Nov 11 at 22:51
It is very similar to this question here: stackoverflow.com/questions/50569064/…
– Chris Atkeson
Nov 11 at 22:51
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53253969%2freact-trigger-function-after-browser-paints%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
It is very similar to this question here: stackoverflow.com/questions/50569064/…
– Chris Atkeson
Nov 11 at 22:51