I read the Wikipedia entry about "List of NP-complete problems" and found that games like super mario, pokemon, tetris or candy crush saga are np-complete. How can I imagine np-completeness of a game? Answers don't need to be too precise. I just want to get an overview what it means that games can be np-complete.
6 Answers
It just means that you can create levels or puzzles within these games that encode NP-Hard problems. You can take a graph coloring problem, create an associated Super Mario Bros. level, and that level is beatable if and only if the graph is 3-colorable.
If you want to see the specific way the NP-Complete problems are translated into the games, I recommend the paper "Classic Nintendo Games are (Computationally) Hard". It's well written and easy to follow.
An important caveat to keep in mind is that the NP-hardness requires generalizing the games in "obvious" ways. For example, Tetris normally has a fixed size board but the hardness proof requires the game to allow arbitrarily large boards. Another example is off-screen enemies in Super Mario Bros: the proof is for a variant of the game where off-screen enemies continue moving as if they were onscreen, instead of ceasing to exist and being reset to their starting position when Mario comes back.
- 5,992
- 1
- 26
- 51
I honestly don't know exactly what kind of model is used by the people making those claims; however, what seems reasonable to me would be talking about the $\mathcal{NP}$-completeness of deciding something about a game situation.
Let's take as an example Tetris, since it's the only one from those you quote that I understand enough to talk about. Tetris has a rule called "perfect clear", that gives the player a large bonus if a piece drop clears the board entirely. One might wonder if, given an ordered sequence of pieces $\{P_i\}$ and an integer $k$, there exists a legal sequence of moves for the pieces $P$ that achieves at least $k$ perfect clears. Problem statements such as those are sufficiently abstract that can be modeled with the tools of complexity theory.
Long story short, "$\mathcal{NP}$-complete" means one thing and one thing only, fancy claims such as "Super Mario is $\mathcal{NP}$-complete" have to be translated into a formal statement before they make any actual sense.
- 4,272
- 1
- 10
- 21
For single player games, you can always ask the question "is there a winning strategy for the player", and that question often has a "YES" answer that can be verified in polynomial time, and may very well be NP complete.
For two-player games, the answer can very often not be verified in polynomial time, because to verify that a move for A is a winning move, you must demonstrate that for every response of B there would be again a winning move for A and so on.
- 32,238
- 36
- 56
Here's a simplistic hand-waving explanation:
Such games are in NP because "running" a player's behavior over the course of a game and checking whether s/he wins or loses can be done efficiently (we need it to be in polynomial time in the length of the game, but it's probably linear or $O(n \log(n))$-ish).
Such games are NP-hard because the player's behavior is very expressive. While at any given point a player may only have a limited, even a fixed, number of possible actions, that's enough to create a space of behaviors or strategies exponential in the length of the game; and while you may be able to provide a simple condition or logical formula on the validity/benefit/correctness of a player's actions locally, globally you get a similar effect as with a large combinatorial circuit, or a k-CNF formula.
Hopefully this makes some intuitive sense and also rings enough CS theory bells.
PS - Some games are much more (computationally) complex than that. For example, the boardgames Hex, Go and and Reversi are PSPACE-complete. That's essentially because the formula you need to satisfy for a winning strategy is a repeatedly-alternating-quantifier formula: The exists a move by player 1, such that for every move by player 2, there exists a move by player 1 etc. etc. such that with all those moves having been played, either some of player 2's moves are invalid or we have a valid sequence player 1 has won. With NP games, it's typically just one player's behavior/strategy/choice of moves.
- 1,025
- 6
- 19
Well, it's certainly in NP, because a possible solution is just a finite number of inputs (in each input frame you can select any of the k buttons, we represent each selection of the buttons for each frame with a letter) that brings you to the win-screen. We know that this game has been beaten before, so we know, that there exists a solution. An NTM goes over it's tape and guesses magically a correct certificate of length n. Then it simulates Super Mario with the input and verifies it. The verification can be done in polynomial time (linear time actually, if the solution is correct, it will take exactly n frames to win).
To show NP-completeness, we could reduce 3-SAT to it by building a 3-Sat checker with the level generator (which is built through arbitrary code execution https://www.youtube.com/watch?v=IOsvuEA2h4w).
So we have a 3-SAT CNF input, that we check for correct formatting first. If it's badly formatted, we just translate it into one 'jump'-input (it's not possible to to beat Super Mario within one frame by doing a jump).
We call the length of the 3-CNF input n.
If it's correctly formatted, we translate it into a number of inputs, that builds the 3-CNF checker for us (always the same code of length k), translates the 3-CNF into a String of inputs, that builds the specific 3-CNF in the checker (in O(n)),and checks all possible solutions by brute-force. It idles and does nothing, if after going thru all solutions, none is found. It restarts the game and uses a known solution for Super Mario to beat the game (the code to do that is has the length j). Our transformation is thus in O(n), so it is within polynomial time.
If the CNF is badly formatted, we don't win (by definition our input is not winning, if we haven't won one frame after the execution of it). If the CNF is not satisfiable, we don't win (you can't win by idling for one frame in the level generator, we ensured that in our code). If the CNF is satisfiable, the checker finds a solution restarts and wins the game. Thus the polynomial reduction of 3-Sat to Super Mario is complete and we have proven, that Super Mario is NP-complete.
(Hope I haven't messed this up somewhere. We do run into a storage problem, if the 3-CNF is too long, but limited storage is usually ignored in these contexts, I believe)
- 39
- 1
I rewrote this answer to try to address some comments on a previous version.
I assume that you've read the Wikipedia definition for NP-completeness which really doesn't focus on games. I'll water down the exact meaning of NP-completeness and game theory just a bit and explain the essence of a NP-Complete game.
Let's consider a 2-player game with alternate moves, more restrictively this is essentially about Combinatorial games. Basically a game which the you have some number of moves that can be made and you must chose one of them. You would like to play "perfectly" which means that you'd never make a "bad" move. So of the allowable moves you'd like to select the best one. (Of course your opponent has the same goal...)
Note that perfect play doesn't mean that you'll always win. The rules of the game can be such that the first or the second player should win. Also some games like Tic-Tac-Toe should end in a draw. Thus what "perfect play" means in this discussion is:
(1) That you'll never be in a winning position and then lose the game because you made a "bad" move
(2) You'll never miss an opportunity to get into the winning position if such an opportunity arises.
Given the current state of the game what you'd like is to be able to use an "efficient algorithm" to calculate the best move. On the other hand let's note that an algorithm that has to search through the whole game tree is an "inefficient algorithm."
Now lets define "efficiency" a bit more formally. I'm going to simplify this a bit but the essence is correct. Consider the number of calculations, $C$, that must be done to chose the next move, that one the average each move has $B$ possibilities (the branching factor) and that there are $n$ moves left in the game. The notion is also that each calculation takes the same time so that the effort can be translated into time complexity, $T$, instead of raw calculations.
- An "efficient algorithm" will have:
$T \varpropto aB^a + bB^{\alpha-1} + cB^{\alpha-2} + ... + hB^0$
where $\alpha$ is a "small integer," and a-h are some real numbers. Thus the efficient algorithm executes in polynomial time since this is a polynomial expression. - An "inefficient algorithm" will have:
$T \varpropto aB^n$
and this algorithm executes in exponential time (i.e. nonpolynominal time). The point here is that as $n$ gets larger a combinatorial explosion results.
Now the important point is that it is impossible to have an efficient algorithm, polynomial time, which plays perfectly for a game which NP-complete. To play perfectly a NP-complete problem must, by definition, be solved by an inefficient algorithm that runs in nonpolynomial time.
Note that the running time is about the intrinsic number of calculations, not the response time perceived by a human. For a small game like Tic-Tac-Toe the computer could play all possible future moves and still respond quickly as perceived by a human.
For Nim it is possible to create an polynomial time algorithm. At any point in the game the algorithm can calculate which player has a winning move and what that move should be.
On the other hand let's take the game of Qubic. (You're trying to make a line of 4 in a 3D grid. So it is essentially tic-tac-toe on a 4x4x4 grid.) Qubic is NP-complete thus there is no polynomial time algorithm to calculate the next perfect move. The only way to know if you currently have a winning move is to try all the possible moves of both players to verify that a particular move is a winner, or at least not a loser.
Truthfully the whole game tree for Qubic is small enough so that it can be encoded into a computer program which can play perfectly. What encoding means is that the whole game tree has been explored and all the moves worked out in advance. So the program can essentially do a quick database call using the current board state and get back the best move for that board state without having to do the tree search each time a move is to be made. This is really a "cheat" for our purposes here.
Now let's discuss chess to discuss the evaluation function ignoring some of the other features of chess playing programs. Chess is still an unsolved game. It is unknown if the first or second player should win. It is not possible to be given any board position and predict with certainty who will win. In fact chess has such a large game tree that it is just impossible to search the whole game tree. You'd need computers which are not just 10 or 100 times faster but billions of billions of time faster than any current computer. (There is the hope that quantum computing could cut through this Gordian knot.)
Think of the chess evaluation function as giving each possible next move a probability of being the best move. What a chess program does is to combine look aheads with the evaluation function. Thus the program looks at all possible future moves until it gets to a point where a "good" score can be given to the board position. The computer evaluates all possible paths through the tree this way and then chooses the path with the best score. Since the search never got to the end of the game for all the paths being evaluated, all chess programs ultimately use an imperfect evaluation function. (If you're near the end of the game then the computer might be able to look at all possible future moves.) That means that might be possible to beat the program even if the program had a winning position at some point.
- 101
- 2