Winning Strategies and Complexity of Nim-type Computer Game on Plane

A Nim-type computer game of strategy on plane is described in this paper. It is demonstrated that winning strategies of this two-person game are determined by a system of equations with two unknown integer sequences. Properties of winning points/states are discussed and an O(loglogn) algorithm for the winning states is provided. Two varieties of the Game are also introduced and their winning strategies are analyzed.


Introduction
A Nim game is probably one of the most ancient of all known games.There are several varieties of Nim: categorical games in which no draw is possible; futile games which permit a tie (draw); Grundy's game is a special type of Nim.The game is played by the following rules: given a heap of size n, two players alternately select a sub-heap and divide it into two unequal parts.A player loses if he or she cannot make a legal move.The Misere form of Nim is a version in which the player taking the last piece is the loser, [1].
In Fibonacci Nim, two players deal with a pile of n stones, where n>1.The first player may remove any number of stones, provided that at least one stone is left.
Players alternate moves under the condition that if one player removed x stones, then another one may remove at most 2x stones.Some of them are described in [1][2][3][4][5].
Several years ago the author of this paper introduced a Nim game with a heap of N stones, where each player is allowed to take at most m stones, provided that he/she does not repeat the last move of her/his opponent ("do not be a copycat").The player taking the last stone is the winner.However, a player loses if he/she cannot make a feasible move.Winning strategies for an arbitrary m>1 were provided by the author of this paper and implemented in [6] and [7] by his graduate students.
In the late 1980's the author also introduced a variety of the Nim-game that is discussed in this paper.In the paper we study properties of winning points, provide an algorithm for direct computation of winning points and analyze its complexity.It is demonstrated that the algorithm has O(loglogn) time complexity and does not require any storage, save a couple of numbers that are pre-computed at the beginning of the game.Preliminary results of this paper are published in [15].

Two-player Game on Plane
1) The Game starts after two distinct non-negative integers   0 0 , S L are selected randomly; here.; : Remark 1: In the following discussion (S, L) is a point on a two-dimensional plane with integer coordinates; all further points are located in the positive quadrant of the plane; p and q determine a "level" of the Game.It is assumed that 0  S <L holds, otherwise we swap the coordinates.
2) Three types of moves that allowed are: horizontal, vertical and diagonal.
The players on their move may decrease either a).The first coordinate on an integer t, (S, L)  (S -t, L), {horizontal move, h-move, for short} or b).The second coordinate on an integer u, (S, L)  (S, L -u), {vertical move, v-move, for short} or c).Both coordinates on the same integer x, (S, L) (S -x, L -x), {diagonal move, d-move, for short};  The first player that reaches (0,0)-point on her/his move is the winner of the Game.An analogous Nim game was introduced by Wythoff [14].Whytoff' game is played with two heaps of counters: a player is allowed to take any number from either heap or the same number from both.The player taking the last counter wins.
As in every two-person game with complete information, this Game has a winning strategy for one of the players [8][9][10].In the following discussion we consider that a Human (Hugo) plays against a Computer (Cora).
All points can be divided onto two classes: winning points for Cora and losing points for Cora.It is clear that a winning point for Cora is a losing point for Hugo, and vice versa.
Definition 1: We will say that the Game is in a winning state if after Cora's move it is in a winning point.

Seven Properties of Winning Points
P1.It is easy to see that if (c,d) is a winning point, then (d,c) is also a winning point.
P2.With exception of the (0,0)-point, in all other winning points .Indeed, let (c,c) be a winning point for Cora.Then Hugo can reach the (0,0)-point using a diagonal move, {via subtracting the same y = c from both coordinates}.
If the Game is in a winning point w after Cora's move, then there is no move by which Hugo can reach another winning state .On the other hand, if the Game is in a losing point l, then there exists at least one move that transforms the Game into a winning state.For example, if after Hugo's move the Game is in the state (7,9), then there are two winning moves for Cora: (4,7) and (3,5).

w
In general, let W be a set of all winning points and L be a set of all losing points.Then after one move the Game is transformed from W to L. However, if the Game is in L, then there exists at least one move that transforms the game into W. Formally it means that, if ( , )  S L W  , then for any positive integer u, or ( , ) . But in this case Cora cannot make either a horizontal move or a diagonal move that transforms the Game into a winning state.The latter implies that ( ,  is a winning state, which in its turn contradicts the earlier assumption that is the winning state.Q.E.D.
StepL6: : For the sake of simplicity of further discussion, we assume that in every point (c,d) c<d.
Then the conditions ( 9)-( 11) also hold for k = n.Q.E.D. Applying the Steps L1-L6, we sequentially generate the winning points

Alternative Formulation of L1-L6 Algorithm
2).Let the sequences A and B satisfy the following system of equations: W1: where in ( 7) is a sequence of pair-wise differences of corresponding elements of B and A, and N is the set of all natural numbers {1,2,3,…}.Then the system of Equations ( 6)-( 8) with unknown sequences A and B has a solution.

B A 
Proof {by induction}: The following algorithm is a constructive proof that a solution of ( 6)-( 8) exists.Indeed, the sequences A={ n can be iteratively generated using an analogue of the Sieve of Eratosthenes: be sequences such that for every k<n the following conditions hold: and for every 1 StepL3: Let J = {j: ; Here, n stands for the largest index k of k that was used in , and n stands for the largest number of the set {1,2,...,k} which we cover for

Sequences A, B and Winning Points
Theorem 2: For every integer Proof: The following sequence of steps is a constructive proof of Theorem1.Indeed, let T1.If imply that k < m and k .Indeed, an assumption that leads to a contradiction, because implies that S <

Iterative Algorithm and its Complexity
In applications for computer games, an iterative computation of n a for a large n is time consuming, since its time complexity T(n) and space complexities S(n) are both of order O(n).For instance, if , then we need to generate and store one trillion pairs of integers.A brief analysis shows that this is well beyond of current size of memory for PC.A more efficient algorithm is described below.Theorem 3: Conjecture 1 implies that z is an irrational number.
Proof: An assumption that z is a rational number leads to contradiction.Assume that z = q/s, where both q and s are relatively prime integers.Then there exists an infinite number of pairs and such that = .Indeed, select Then for the integer t = 1,2,3,… it follows from ( 16) and ( 14) that which is a violation of the conditions ( 6) and (10).Then for large n's, z is a positive solution of the equation.); where either or ; However, from the condition (11) alone we do not know yet in which of the states A, B, C, D or E the Game is.

Solution of Equation with Unknown Index
On the step A4 of the algorithm we must solve either equation k or i in order to respectively determine the indices k or i.In order to determine the indices we must solve either the equation or the equation This can be done by using ( 16) Then from ( 14) and ( 29) Example 6: Find an integer index k such that 102 k a  .Then * k  64 is the smallest integer for which holds * k  102/(g+1) {see (29)}.

Required Accuracy for g
It is assumed that in the Examples 3-6 and 8 we know the exact value of an irrational number g.However, to find an integer solution of (17) for an arbitrary large index k or i we must compute g with a high precision.Let /10 /10 .. /10 ..

O(loglogn) Time Complexity for Winning Strategies
It is easy to verify that a positive root of (24) can be computed using a Newton iterative process The process (38) has the following properties: a).It converges to (1 5  )/2, i.e., for large r where r  is a degree of accuracy (error) after r iterations.

The Algorithm
It is assumed that Hugo makes the first move by randomly generating positive integers and such that 0 where

Randomization
Let a and be the number of integers on interval [1, M] such that and 1 respectively, i.e., + =M.
Hence, if a pair of integers (S, L) is generated randomly, then it is more likely that they will be elements of the sequence A, than the sequence B.
Remark 4: The sequence of the operations (50) and (51) in the Algorithm is based on the observation that for every M, .

    a b n M n M 
That is why on the A4 we first check whether there is a solution of and only then whether there is a solution of i .This sequence of verifications decreases the average complexity of the algorithm.Another approach is to randomize the sequence of these operators: Namely, with the probability g = 0.618 to execute (50) and then, if necessary, to execute (51).And with the probability g = 0.382 to execute (51) and only then, if necessary, to execute (50).

The First Move
Without a third independent party, it seems impossible to introduce a random and trustworthy mechanism for deciding whose move is the first.As a palliative solution, the following procedure is suggested: immediately, after the first point ( 0 , 0 ) is generated, Hugo has a short period of time (say, a couple of seconds) to decide who must make the first move.One way to preclude Hugo from cheating and to introduce more variety to the Game, select Q := 2Q on every consecutive run of the Game with the same player.More detailed analysis of possible alternatives is beyond the scope of this paper.

Varieties of Nim-Game on Plane
Of many possible varieties I consider only two: the Attrition game and the Flip-Flop game.
In both games the moves are the same as in the Game described above in this paper.Only the goals are different.
Attrition game: The first player that reaches point (0, 0) is a loser.

Winning Strategies
Let the winning points k in the Attrition game.It is clear that both 1 = (0,1) and 2 = (2,2) are the winning points for Cora.Indeed, after Cora's move (0, 1) Hugo is losing the Game.The same is with (2,2): after that move Hugo is forced to reach (0,0), because Hugo can make either (0,2) or (1,1) or (1,2) move.Then Cora moves (0,1) and Hugo has no other choice but move (0,0).(56) From the (56) winning strategy it follows that "Only once during the Game" -requirement is inessential and it is introduced for a psychological reason only.The Game can be further modified if the flipper must pay for every flip, and the winner gets the "bank".
After this paper was completed, the author discovered that the Game has been described in [13,14].

Acknowledgements
I appreciate H. Wozniakowski for his suggestion, an anonymous reviewer for several corrections and B. Blake for comments that improved the style of this paper.


m S a , then by (3),(7) and (13) m b L  ; {Hugo is now in the winning point}.T2.If m , then Cora selects y := S -a m ; S := S -y and L := L -y; since S > a m implies that L > b m .Indeed , then Cora finds either an index k < m such that k a S  or an index i < m such that i b S  }.T3a.If there exists an integer k < m such that k a

9 . 2 :
Direct Computation of a n and b nTo decrease the complexity of computation of a n and b n and avoid excessive storage, let's find a closed-form expression for := v(n).Then from (11) 16) and the asymptotic behavior(15) are observed in numerous computer experiments.ConjectureFor large n/ n b a = z + o(n) (17)Remark 2: The Conjecture 2 is also based on extensive computer experiments.

Conjecture 3 :
z=g+1, where g is a golden ratio, 21) is observed in numerous computer experiments.It plausibility follows from the following: Since for a large n = nz + o(n) i.e., z  ( 5 1  )/2, {the value of the golden ratio +1}.Let the Game be in the state (S, L) after Hugo's move and let m := (13).L S Then the Game is implicitly in one of five states {where by convention holds that S < L}:A.(S, L) = ( ), {the game is in a winning state for Hugo};

)
Find the smallest integer satisfying the inequality kg > S; if then If is the smallest integer satisfying the ine-has an integer solution, then from (29) we find the smallest integer k = satisfying

Example 7 :
Let at the beginning of the Game s := 2,718,282 and l := 3,141,593.

Example 8 :
If M = 50, then if u is an arbitrary selected integer on the interval[1, 50], then with probability g there exists an index k such that k , and with probability

Flip-flop game :
Only once during the Game players on their move can change the goal of the Game if

1 (
-Flop game: Although it seems confusing, actually the winning points for the Flip-Flop Game are very simple.It follows from an observation that for all 2 ., 2 w  (3,5); 3 w  (4,7); and only and the Game is in the attrition phase, then

Proposition 1: There are no two
,L 0 ) = (29,51) be a randomly generated initial point; Hugo makes the 1st move; in italics are shown Hugo's moves; in bold are Cora's winning points Table1.
shows how many Newton iterations r(n) are required to compute as a function of n.In addition, we do not need to store any winning points.Instead, as it is demonstrated below, only a single real value of n * i * i ( * g +1) > S (47)