Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | benedictta |
I have a loop to update a value but after the loop finished the variable is not updated?
heres an example code:
var bestScore = -1000
for i in range(3):
score=getScoreinSquare(i)
bestScore = max(bestScore,score)
return bestScore
but it will always return -1000 and when i returned it from inside the loop it will return the updated value but its inefficient and i cant modify it further in the current state… please help… thankyou
Where is score
defined? Other than that oddity, I don’t see anything wrong with the posted code. Though, you say it’s example code. Are you sure it’s equivalent to your actual code? Maybe post that instead?
jgodfrey | 2021-01-05 22:09
You could check in your debugger that “getScoreinSquare(i)” actually returns a value greater than -1000.
egore | 2021-01-05 22:17
hi thank u for replying, this is the full code of the function:
func minimax(board, aiCards, playerCards, depth, isMaximizingPlayer):
var score
var boardState = board.duplicate()
var aiState = aiCards.duplicate()
var playerState = playerCards.duplicate()
if(depth==0):
score = evaluateBoard(board, aiCards)
return score
elif(depth!=0):
var bestScore
if(isMaximizingPlayer):
bestScore = -1000
for i in range(3):
for j in range(3):
var box = boardState[i][j]
if(box[5]==true):
for c in range (aiState.size()):
var card = aiState[c]
for k in range(5):
box[k] = card[k]
box[5] = false
aiState.remove(c)
if(i>0):
var left = boardState[i-1][j]
if left[4]==true:
if box[0]>left[1]:
left[4] = false
if(i<2):
var right = boardState[i+1][j]
if right[4]==true:
if box[1]>right[0]:
right[4] = false
if(j>0):
var top = boardState[i][j-1]
if top[4]==true:
if box[2]>top[3]:
top[4] = false
if(j<2):
var bottom = boardState[i][j+1]
if bottom[4]==true:
if box[3]>bottom[2]:
bottom[4] = false
score = minimax(boardState,aiState,playerState,depth-1,false)
bestScore = max(score,bestScore)
boardState = board.duplicate()
aiState = aiCards.duplicate()
return bestScore
elif(!isMaximizingPlayer):
bestScore = 1000
for i in range(3):
for j in range(3):
var box = boardState[i][j]
if(box[5]==true):
for c in range (playerState.size()):
var card = playerState[c]
for k in range(5):
box[k] = card[k]
box[5] = false
playerState.remove(c)
if(i>0):
var left = boardState[i-1][j]
if left[4]==false:
if box[0]>left[1]:
left[4] = true
if(i<2):
var right = boardState[i+1][j]
if right[4]==false:
if box[1]>right[0]:
right[4] = true
if(j>0):
var top = boardState[i][j-1]
if top[4]==false:
if box[2]>top[3]:
top[4] = true
if(j<2):
var bottom = boardState[i][j+1]
if bottom[4]==false:
if box[3]>bottom[2]:
bottom[4] = true
score = minimax(boardState,aiState,playerState,depth-1,true)
bestScore = min(score,bestScore)
boardState = board.duplicate()
playerState = playerCards.duplicate()
return bestScore
\its currently returned from inside the loop but everytime i change it to return from outside the loop its returning the original bestScore value which is -1000 because the recursion started from maximizing player
benedictta | 2021-01-06 00:23
Can you post the code in the form that doesn’t work as you intend? So,with the return from outside the loop… It’s impossible to say what’s wrong with that code without seeing (exactly) it…
jgodfrey | 2021-01-06 00:33
ok right sorry so this is the one thats not working as intended:
func minimax(board, aiCards, playerCards, depth, isMaximizingPlayer):
var score
var boardState = board.duplicate()
var aiState = aiCards.duplicate()
var playerState = playerCards.duplicate()
if(depth==0):
score = evaluateBoard(board, aiCards)
return score
elif(depth!=0):
var bestScore
if(isMaximizingPlayer):
bestScore = -1000
for i in range(3):
for j in range(3):
var box = boardState[i][j]
if(box[5]==true):
for c in range (aiState.size()):
var card = aiState[c]
for k in range(5):
box[k] = card[k]
box[5] = false
aiState.remove(c)
if(i>0):
var left = boardState[i-1][j]
if left[4]==true:
if box[0]>left[1]:
left[4] = false
if(i<2):
var right = boardState[i+1][j]
if right[4]==true:
if box[1]>right[0]:
right[4] = false
if(j>0):
var top = boardState[i][j-1]
if top[4]==true:
if box[2]>top[3]:
top[4] = false
if(j<2):
var bottom = boardState[i][j+1]
if bottom[4]==true:
if box[3]>bottom[2]:
bottom[4] = false
score = minimax(boardState,aiState,playerState,depth-1,false)
bestScore = max(score,bestScore)
boardState = board.duplicate()
aiState = aiCards.duplicate()
return bestScore
elif(!isMaximizingPlayer):
bestScore = 1000
for i in range(3):
for j in range(3):
var box = boardState[i][j]
if(box[5]==true):
for c in range (playerState.size()):
var card = playerState[c]
for k in range(5):
box[k] = card[k]
box[5] = false
playerState.remove(c)
if(i>0):
var left = boardState[i-1][j]
if left[4]==false:
if box[0]>left[1]:
left[4] = true
if(i<2):
var right = boardState[i+1][j]
if right[4]==false:
if box[1]>right[0]:
right[4] = true
if(j>0):
var top = boardState[i][j-1]
if top[4]==false:
if box[2]>top[3]:
top[4] = true
if(j<2):
var bottom = boardState[i][j+1]
if bottom[4]==false:
if box[3]>bottom[2]:
bottom[4] = true
score = minimax(boardState,aiState,playerState,depth-1,true)
bestScore = min(score,bestScore)
boardState = board.duplicate()
playerState = playerCards.duplicate()
return bestScore
also im pretty sure the one is not working is the loop inside the if(isMaximizingPlayer) condition, because i tried with the other condition and its working normally but i still cant grasp what is the error, again thank you
benedictta | 2021-01-06 00:42
I don’t see anything inherently wrong with your handling of the bestScore
variable. That said, the general flow looks suspicious to me. Are you intentionally updating the boardState
array while you’re iterating over it? Also, most of that logic won’t execute unless box[5] == true
. Is that guaranteed to happen? If not, you’ll be returning your original boardState
value.
Whatever the problem, I don’t think it should be too difficult to find with a few surgical print
statements. For instance, if you print the value of bestScore
immediately after it comes back from the min
and max
calls, and just before you return the final bestScore
value, do those results make sense?
jgodfrey | 2021-01-06 01:40
hmm i think i might have to rewrite the code since it has become too messy and check if that works thank u for answering tho appreciate it!!
benedictta | 2021-01-06 01:44
Yeah… I’d agree with that assessment. There’s much that can/should be cleaned up there. Any time you find yourself (mostly) replicating huge blocks of code (the if
and else
blocks are very similar), that’s usually an indicator of a design that should be reworked…
jgodfrey | 2021-01-06 01:49