Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
I'm not sure what the language is... The assignment operator is "<-".... Hmm according to wikipedia, it appears that it can be oCaml, R, or S....? It's not ocaml (the for loops seem to be wrong, and it just generally does not look like code written in ocaml). I am unfamiliar with R and S.Josan said:The code at the XKCD forums was weird, I didn't even recognize the language, and the concept was hard to wrap my head around.
#allocate a 2-d array, first parameter is a placeholder
# essentially "this is a really big number, much larger than it should be"
x <- matrix(999,102,7)
#fill x[0,1]=0, x[1,1]=1, x[2,1]=2,... etc. with 0 through 102
# this is the "if A (p1) only needs 1 more token, must spend as much money as B has"
x[,1] <- rep(0,102) # win condition
#fill similarly, but instead of fixing number of tokens remaining to 1
# we're fixing amount of money B has to 1, and filling with number of tokens remaing
# this is because if B runs out of money and B is up 3 tokens, then A has to bid 1
# repeatedly to "buy" each of those tokens (in this case, bid one 3 times, so costs 3 to win)
x[1,] <- rep(0,7) # "win" if B spends more than he has
x[2,2:6] <- c(1,2,3,4,5) # money needed if B has nothing
# fill the table up
for (i in 3:102) { # for B's money from 1 to 100 (weird -2 offset)
for (j in 2:6) { # for number tokens B is winning by from -3 to 3 (weird -4 offset)
y <- 999 # default "cost"/"value" of this scenario
for (k in 1:(i-2)) { #loop through possible bids that A can make
z <- max(x[i,j-1], x[i-k-1,j+1])+k # B's turn: pick most expensive win
y <- min(y,z) # A chooses between the best A has seen in this set of moves so far, and this current move
}
x[i,j] <- y # set the value of our table to the best of all the possible moves
}
}
borrofburi said:hmm... Here's what I'd expect to see for the first few entries:
[ 0, 1, 2, 3, 4, 5, 999], $-1
borrofburi said:Hmm I dunno... My guess would be something about the first few rows being wrong... Can you show me the version where you manually set the second row's last element to 999?
You're probably right. You deal with bounds/termination differently than I do. I prevent the computer from overbidding simply with a check via "if" statement: if the computer has enough money, then it can bid, otherwise don't.Josan said:I don't understand this, I would expect the $-1 row to be all 0. As the computer would never (it isn't allowed) choose this row.borrofburi said:hmm... Here's what I'd expect to see for the first few entries:
[ 0, 1, 2, 3, 4, 5, 999], $-1
Put it inside a code tag inside a showmore tag:Josan said:My entire program (can't get indents to work)
Josan said:
Ah, that makes more sense. Glad you figured it outJosan said:And the final table:
Oh I almost forgot to mention that this is based on dynamic programming. It's an algorithm design method used in cases where you can easily solve the problem by taking the max or min (or, potentially, some other function) of a finite number of subproblems that are all very similar to each other.Josan said:I did it! Using the matrix method