8
In the game 15, two players take turns selecting numbers from 1 to 9 (without choosing any number that either player has already selected). A player wins if he or she has three numbers that add up to 15. If all the numbers have been selected and no combination of either player's adds up to 15, then the game is a tie.
Your task is to build a function that takes the state of a game of 15 (represented in any form you like) and returns which number to move next, which will act as an AI to play the game with another player. You may assume that the position is legal (no player has more than one number more than the other player, and no player already has three numbers that add up to 15).
The AI must be perfect - that is, if it's given a winning position, it must force a win, and if it's given a non-losing position (a position where its opponent does not have a winning strategy), it must not allow its opponent to give it a losing position (which is possible, as 15 is a solved game).
The shortest code wins.
(note: I will accept the currently shortest answer and change it if a shorter answer appears.)
Do I understand correctly we are allowed to lose if we get into a winnable position that our AI couldn't have got into? – John Dvorak – 2013-08-05T01:50:23.153
Yes, that is correct. If the AI is presented with a game state that it cannot win or tie (e.g. a double trap), then it is allowed to lose. However, the AI should never allow the game to get into said losing state from an empty board. – Joe Z. – 2013-08-05T03:17:24.213
do I assume correctly the AI is always the first to play? – John Dvorak – 2013-08-05T04:07:44.663
No, the AI can play second as well, but either way the game is solved (with perfect play, the game will always end in a tie). – Joe Z. – 2013-08-05T04:12:08.050
1hmm... it seems we are allowed to draw even if we can force a win. Is that true? – John Dvorak – 2013-08-05T06:34:01.013
Well, I guess it should win if given the opportunity. – Joe Z. – 2013-08-05T13:50:43.660
well, mine does :-) – John Dvorak – 2013-08-05T13:57:55.880
I should clarify: "the AI should never allow the game to get into said losing state from an empty board" or from any other non-losing position. – Joe Z. – 2013-08-06T04:30:24.753
"Do I understand correctly we are allowed to lose if we get into a winnable position that our AI couldn't have got into?" – Jan Dvorak yesterday "Yes, that is correct. ..." – Joe Z. yesterday – John Dvorak – 2013-08-06T04:52:13.897
I think I misunderstood your question then. What I meant is, if the AI is presented initially with a position that's "winnable" from the other side, it's allowed to lose because the other side has a winning strategy and there's absolutely nothing the AI can do. Otherwise the AI must prevent a loss if possible, including taking full advantage of the other player screwing up a winning position by making a wrong move. – Joe Z. – 2013-08-06T14:36:09.493
what is the intention of the word "initially" in that statement? I will update my answer, however – John Dvorak – 2013-08-06T14:44:25.830
If the AI is given a garbage input like
[6 7 9][1 2 4 8]
(in which case 3 and 5 is a double trap) without having made any moves itself (but just having[6 7 9][1 2 4 8]
as a starting position), that's considered an "initial losing position". Then it's allowed to make a losing move because it has no other choice. But from an empty board, the AI should never allow[6 7 9][1 2 4 8]
to be reached. – Joe Z. – 2013-08-07T02:12:56.4832Hah. I just learned about this game, and the first thing when I got home was to post it here. Alas, I've been beaten to it. – boothby – 2013-11-24T07:02:53.483