Click here to Skip to main content
15,893,814 members
Articles / Mobile Apps / Android

Nim Challenge

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
26 Jul 2012Apache7 min read 33.6K   1.2K   15  
Nim game for Android devices
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
    <title>Nim</title>
    <meta http-equiv="content-type" content="text/html; charset=utf-8">
    <meta name="author" content="Kostas Giannakakis">
    <meta name="description" content="Nim game">
    <link rel="stylesheet" type="text/css" href="main.css" />
    <script src="candles.js" type="text/javascript"></script>
</head>

<body>

<div class="mainframe">
    <h1>Nim</h1>

    <h2>The Game</h2>
    <p>Nim is a strategy game, where two players take turns removing objects from distinct heaps.
       A player can remove any number of items, as long as they all come from the same heap and they
       appear in consecutive places. The player who removes the last item from all the heaps wins.</p>

	<h2>Strategy</h2>
	<p>Depending on the arrange of items an algorithm exists that determines, which player is going to
	   win. This algorithm is better presented with an example. Consider of arrange of three heaps, one
	   of size 1, one of size 2 and one of size 3.</p>
	<div align="center">
		<table class="nim">
		<tr><td>&nbsp;</td><td>|</td><td>&nbsp;</td></tr>
		<tr class="odd"><td>|</td><td>|</td><td>&nbsp;</td></tr>
		<tr><td>|</td><td>|</td><td>|</td></tr>
		</table>
	</div>

	<p>This arrange can be represented as follows:</p>
	<div align="center">
		<table>
			<tr><td>1 = </td><td>001<sub>2</sub></td></tr>
			<tr><td>2 = </td><td>010<sub>2</sub></td></tr>
			<tr><td>3 = </td><td>011<sub>2</sub></td></tr>
			<tr><td>&nbsp;</td><td>---</td></tr>
			<tr><td><em class="sum">Sum:</em> </td><td>000</td></tr>
		</table>
	</div>
	<p>For each row we count the number of elements. We convert the number to its binary representation.
	   A binary number consists of 1s and 0s. In each column we count the number of 1s. If the result is
	   an odd number, then in the corresponding column at the <em class="sum">Sum</em> we get an 1. If it
	   is an even number, we get a 0.</p>

	<p>If the <em class="sum">Sum</em> is all zeros, then the player who plays first loses. On the other hand,
	   if there is even a single 1 in the <em class="sum">Sum</em>, then the first player can always find a
	   move that it will convert the arrange of items into a form that has a zero <em class="sum">Sum</em> and
	   win.</p>

	<p>When you play, it is difficult to convert into binary and calculate the sum quickly. Even if you do so,
	   you still need to find the move that it will lead to a zero sum arrange and this is not easy either. In
	   order to play quickly, you can follow a small set of rules:</p>

	<ul>
		<li>An arrange of 1, 2, 3 loses.</li>
		<li>An arrange of 1, 3, 5 wins (convert it to 1, 2, 3).</li>
		<li>An arrange, where each number appears an even number of times loses (e.g 1,1,2,2 and 4,4).
		    You can quickly draw out of the picture duplicating rows.</li>
		<li>A combination of a winning and a losing arrange wins.</li>
	</ul>

	<h2>Game modes</h2>
	<p><em class="appname">NimChallenge</em> offers three different game modes. In <em>Classic</em> mode
	   the original arrange of items is used. The player can select whether to play first or not, but in
	   order to win the player must start first. You can try this mode to practice and follow computer's
	   moves.</p>

 	<p>In <em>Variations</em> mode different arranges of items are used. The player selects, if she wants to
 	   play first or not. This is a crucial decision and the specific arrange of items must be taken into
 	   account. If the player wins, she can continue playing another game. Every time the arrange of items
 	   is getting more complicated. In this mode a score is kept. The player must play as quickly as possible
 	   in order to win more points.</p>

	<p>In <em>Challenge</em> mode you play against time. You only have a limited amount of time to find the
	  best move. If the time expires, you lose. In this mode also a score is kept and faster moves win
	  more points.</p>

 	<h2>Settings</h2>
	<p>The following aspects of the game can be configured:</p>

	<ul>
		<li><em>Single Touch:</em> If checked each move needs not to be confirmed and it is instantly played.
			This is quicker, but doesn't give a chance to correct an error.</li>
		<li><em>Theme: </em> Selects the appearance of the board.</li>
		<li><em>Last wins: </em> Selects if the game will be played as a normal (checked) or misère game (unchecked). 
		    In a normal game the player who removes the last element wins. In a misère game the last player loses.</li>
	</ul>

	<p>The above settings affect all game modes.</p>

    <h2>More Nim</h2>
    <p>Play more Nim here. Select the pegs by
       clicking on them, when you are finished press the button 'Move' to remove them.</p>
    <div align="center">
    <script language="JavaScript">
    //create the pix array
    var pix = new Array();
    var boardTable = new Array (1,0,0,0,0,
                                1,1,0,0,0,
                                1,1,1,0,0,
                                1,1,1,1,0,
                                1,1,1,1,1);

    pix[0] = new Image();
    pix[0].src = 'images/peg_empty.png';
    pix[1] = new Image();
    pix[1].src = 'images/peg.png';
    pix[2] = new Image();
    pix[2].src = 'images/peg_sel.png';
    pix[3] = new Image();
    pix[3].src = 'images/peg_sel_.png';
    pix[4] = new Image();
    pix[4].src = 'images/frame_v.png';
    pix[5] = new Image();
    pix[5].src = 'images/frame_h.png';
    pix[6] = new Image();
    pix[6].src = 'images/frame_c1.png';
    pix[7] = new Image();
    pix[7].src = 'images/frame_c2.png';
    pix[8] = new Image();
    pix[8].src = 'images/frame_c3.png';
    pix[9] = new Image();
    pix[9].src = 'images/frame_c4.png';

    //Place this script wherever you want your calendar
    //The first argument must match the var name
    var thisCandles = new candles('thisCandles', pix, boardTable, false, 5, 5);
    //thisCandles.computerTurn = false;
    document.write(thisCandles.write());
    </script>
    <br>
    <form name="computerMove" >
    <input type="button" value="Move" onClick="thisCandles.move(thisCandles)">
    <input type="button" value="New game" onClick="thisCandles.newGame(thisCandles, pc_first.checked)">
    <br><br>
    <input type="checkbox" name="pc_first" value="0">Computer first
    </form>
    </div>
</div>
</body>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Apache License, Version 2.0


Written By
Software Developer (Senior) Self employed
Greece Greece
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions