Go to the end of this question for the solution that worked for me!
i'm currently making my own chess game and i want to check if a position already occurred 3 times. I keep the current position in a 2d array. This is what i tried:
Initialize the useState:
const [positionList, setPositionList] = useState([])
structure of the state should look like this
[
{
position: //some 2d array,
counter: 1
},
{
position: //another 2d array,
counter: 1
}
]
Compare the positions from the array with the current position
(if the position already existed, set counter one up)
let found = false
for (let i = 0; i < positionList.length; i++) {
if (JSON.stringify(positionList[i].position) === JSON.stringify(position)) {
positionList[i].counter = positionList[i].counter + 1
found = true;
break;
}
}
if the position never occurred before, add it to the array
if (found === false) {
setPositionList([...positionList, {position: position, counter: 1}]);
}
The 2nd and 3rd code block gets executed after every move (after players turn).
But for some reason, if "found" is false, it wont add the new position to the array but replace the last object in the array. And furthermore, when i log the changes made to "positionList" with a useEffect, it gets reset after every move to the default value (basically empty array), even though im not changing the state in any other part of my code.
I log the positionList like this:
useEffect(() => {
console.log(positionList)
}, [positionList])
I just cant figure out my error, hopefully someone can help.
Thanks!
EDIT:
I decided to try another way of counting the positions. I now only have one array which contains every position that occured in the correct order. Then i just loop through the array and count the amount of times the current position is in the array. My code looks like this:
Note: I think its important to note that you need to create a copy of the array you want to push. Otherwise all entrys in the array will be completly the same and you won't get the expected output (also see here)
function checkForThreefoldRepetition () {
let counter = 0;
for (let i = 0; i < positionList.length; i++) {
if (JSON.stringify(positionList[i]) === JSON.stringify(position)) {
counter++;
}
}
if (counter === 2) {
const updateGameOver = {
gameOver: true,
reason: "threefold repetition",
winner: "draw"
}
setGameOver(updateGameOver)
}
}
useEffect(() => {
let positionCopy = [];
for (let i = 0; i < position.length; i++) {
positionCopy[i] = position[i].slice();
}
setPositionList([...positionList, positionCopy]);
checkForThreefoldRepetition()
}, [playerTurn])
I think your problem is you are modifying current state directly like this one which is not a proper way of updating state :
positionList[i].counter = positionList[i].counter + 1
So this extra render cause problems,I think.
Related
I need after call a function, set to empty an array and immediately add a new one to there.
This is becouse when an #click event is called i need to call a dialog and populate the content of this with a dynamic component (called them with a slug propertie), so the array should be change accordily to pass the slug propertie to the component.
My code is:
slugs: []
slugConversacion(slug) {
if (this.slugs > 0) {
this.slugs = []
// this.slugs.splice(this.slugs.indexOf(slug), 0);
// this.$delete(this.slugs, this.slugs.indexOf(slug))
}
else {
this.slugs.push(slug);
}
}
<Conversacion
v-for="slug in slugs"
:key="slug.id"
:slug="slug.slug"
></Conversacion>
This not work because when i click the event slugConversacion() set and empty array and only when clicked again, populate. I think that is for the if/else conditional.
What would be the right approach ? Thanks!
Just do this:
if (this.slugs.length > 0) {
this.slugs = [];
}
this.slugs.push(slug);
If you have trouble with setting array to empty with assigning [] to array and loosing reactivity, then you can try next thing.
This works for me as the last resort:
var i = slugs.length;
while(i --){
slugs.splice(i, 1);
}
You should do splice in revers mode because of index confusion: every time will be remained array element with index 1 if you will go throw the loop via
slugs.forEach((item, index) => {
slugs.splice(index, 1);
});
And after that you can do:
this.slugs.push(slug);
The Line console.log before if can log a number but the if statement go error with
this.state.question[i] is undefined.
after 4 hours I can't understand why?
enter image description here
You are getting an error because you are iterating your question array one too many times. If you have 5 items in the array, you want indexes 0, 1, 2, 3 and 4. Your loop tries to read index 5 as well because you have: i <= this.state.question.length. Try changing this to i < this.state.question.length instead.
Also, you are setting your state twice which is unnecessary and may trigger a re-render more than needed. Instead work with a temporary variable:
handlePlay = (e) => {
setInterval(function(){
const time = Math.round(e.target.getCurrentTime());
let obj = {currentTime: time}; //temporary variable to build your next state
for(let i = 0;i<=this.state.question.length;i++){
if(time==this.state.question[i].time){
obj.currentQuestion = i; //add stuff to it
}
}
this.setState(obj); //set your final state here
}.bind(this),1000)
}
I'm making a simple game in swift and xcode and I ran into this problem that I can't figure out. Because I have so many levels, the code locks up indexing and slows down the whole program. I get a color wheel spinning for a few minutes but it never crashes. Just takes several minutes everytime I type in a few characters. Strange, but xcode has always had it's bugs right?
Each Button ("button1, button2..." below) gets a single number from "level1:Array". It's like a code that fills in the button's value for the game. There are only 4 buttons, but the numbers should be able to change as they each have their own variables.
I want to generate this for every level. I should be able to generate something like "button1 = level#[0]" where # is replaced by "userLevel". Changing to a string and doing something like "button1 = ("level(userLevel)") as! Array... doesn't seem to work. Look below and use my terminology when giving examples if you can. Thanks!
Here's the example:
let level1:Array = [9,7,4,1] // current puzzle, to go directly into button vars below (button1,button2,ect)
var userLevel = 1 // current user's level
if userLevel == 1 {
print("making Level 1, setting buttons to value from array")
button1 = level1[0]
button2 = level1[1]
button3 = level1[2]
button4 = level1[3]
}
Now, since level# is repeated so often (for each level as the number progresses) I would rather just make it something like this:
//this doesn't work in swift, but what else can I do?
if userLevel > 0 {
button1 = level\(userLevel)[0]
button2 = level\(userLevel)[1]
button3 = level\(userLevel)[2]
button4 = level\(userLevel)[3]
}
Is there an easy way to do this? Thanks!
-GG
Try using a for-in loop. Create an array of the buttons, and then
var index = 0
for button in buttons {
button = level1[index]
index++
}
EDIT since you want both the user level and the level number to increase, I suggest you define the levels like this. (Make sure that the number of buttons is equal to the number of userLevels, otherwise you will have problems)
var array = [1,2,3]
let levels = [1:[1,3,8],2:[3,6,4],3:[4,2,5]]
var index = 0
if array.count == levels.count {
for number in array {
array[index] = levels[index+1]![index]//The second index can be 0 if you want
index++
}
}
//array = [1,6,5]
// You could create a second index to match the number of levels within the main user level.
In this case, assume array to be your array of buttons
EDIT 2 :)
I've made a function that will allow you to assign all the levels to your array for a specific userLevel, since I see that is what you want
let levels = [1:[1,3,8],2:[3,6,4],3:[4,2,5]]
func assignValuesToArray(levelNo:Int) -> [Int] {
var array: [Int] = []
if (levelNo > 0) && (levelNo <= levels.count) {
for (level,levelArray) in levels {
if level == levelNo {
for value in levelArray {
array.append(value)
}
}
}
return array
} else {
print("This Level number does not exist")
return []
}
}
var finalArray = assignValuesToArray(2)
print(finalArray) // Returns [3,6,4]
As you can see from this example, you will return your array of buttons from the function, and you can assign the returned array values to whatever you like.
I have a store and an array. I want to remove records from the store if that record's value matches with values in the array. Following is is the code I am trying but it's not working. Can anyone suggest the correct way?
'store' is the actual store and 'filterItems' is the array of records I want to remove from 'store'.
store.each(function (record) {
for (var i = 0; i < filterItems.length; i++) {
if (record.get('ItemId') === _filterItems[i].get('ItemId')) {
itemIndex = store.data.indexOf(record);
store.removeAt(itemIndex );
}
}
});
Not sure about your code because i dont know all variables. Though its recommended to use the store.getRange() fn and iterate the array via for loop. Its better for performance.
var storeItems = store.getRange(),
i = 0;
for(; i<storeItems.length; i++){
if(Ext.Array.contains(filterItemIds, storeItems[i].get('id')))
store.remove(store.getById(storeItems[i].get('id')));
}
Here is an example which i tried right now and it works well.
https://fiddle.sencha.com/#fiddle/8r2
Try using the remove method of the store (docs)
store.remove(filterItems);
var indexes = [], i = 0;
dataviewStore.each(function(item, index){
if(item) {
if(item.data.columnId == columnId) {
indexes[i++] = index;
}
}
}, this);
dataviewStore.remove(indexes);
this is my example if your record is matches with the value then store the index of that item after storing indexes of all the items and remove them.
Otherwise you have to use for loop and remove them from end of the array.
Here's what I'm currently doing/trying to do to accomplish my goal. But it is not removing the "row" the way I would like it too.
So, I'm making an object, then pushing it into an array. And the adding to the array part works fine and just as I expect.
var nearProfileInfoObj:Object = new Object();
nearProfileInfoObj.type = "userInfo";
nearProfileInfoObj.dowhat = "add";
nearProfileInfoObj.userid = netConnection.nearID;
nearProfileInfoObj.username = username_input_txt.text;
nearProfileInfoObj.sex = sex_input_txt.selectedItem.toString();
nearProfileInfoObj.age = age_input_txt.selectedItem;
nearProfileInfoObj.location = location_input_txt.text;
nearProfileInfoObj.headline = headline_input_txt.text;
theArray.push(nearProfileInfoObj);
So after that later on I need to be able to remove that object from the array, and it's not working the way I'm expecting. I want to take a variable whoLeft and capture their ID and then look in the array for that particular ID in the userid part of the object and if its there DELETE that whole "row".
I know you can do a filter with an array collection but that doesnt actually delete it. I need to delete it because I may be adding the same value again later on.
whoLeft = theiruserIDVariable;
theArray.filter(userLeaving);
public function userLeaving(element:*, index:int, arr:Array):Boolean
{
if (element.userid == whoLeft)
{
return false;
}
else
{
return true;
}
}
But this doesnt seem to be deleting the whole row like it implies. Does anyone know what i'm doing wrong?
Instead of modifying the original array, the new filtered array is returned by the filter method. So you need to assign the returned array to theArray.
Try this
theArray = theArray.filter(userLeaving);
EDIT This turned out to be slower than for loop:
An alternative to the hand coded loop could be something like this:
theArray.every(searchAndDestroy);
public function searchAndDestroy(element:*, index:int, arr:Array):Boolean
{
if (element.userid == whoLeft)
{
arr.splice(index,1);
return false;
}
return true;
}
As far as I know, every() terminates the first time the test function returns false. So the question is: for a big list, which is faster, the for loop or the loop that every() does with the overhead of the test function call.
EDIT #2 But this was faster than a for loop for a test I ran on an array of a million Points:
for each(var element:Object in theArray)
{
if (element.userid==whoLeft)
{
theArray.splice(theArray.indexOf(element),1);
break;
}
}
I think this is what you're looking for:
for(var i:uint = 0, len:uint = theArray.length; i<len; i++)
{
if(thisArray[i].id == whoLeft.id)
{
thisArray.splice(i, 1);
break;
}
}
However, do you really need it in an Array because you could always use a Dictionary which would mean accessing it by id which would be a lot simpler to remove.