I need the source code of an Android app that has: Nim is a mathematical game of
ID: 3874724 • Letter: I
Question
I need the source code of an Android app that has:
Nim is a mathematical game of strategy in which two players take turns removing objects from distinct heaps. On each turn, a player must remove at least one object, and may remove any number of objects provided they all come from the same heap. The goal of the game is to avoid being the player who must remove the last object. (It can also be played with the winning player taking the last object.) The assignment is to create a nim game which initially displays 4 columns of 1, 3, 5 and 7 objects respectively. The game should permit/require each player, in turn, to remove as many objects as he likes (at least one) but from only one column. Your game should initially offer the option to be played so that the last player wins or last player loses.
Explanation / Answer
/**************************************************/
/* The method creates seven ComboBoxes */
/**************************************************/
void CreateNew_ComboBoxes()
{
int i,x=31;
try
{
cmbbox = new ComboBox[7];
for ( i = 0; i < 7; i++)
{
cmbbox[i] = new ComboBox();
cmbbox[i].Parent=this;
cmbbox[i].Width=40;
cmbbox[i].Location=new Point(x,5);
x+=107;
cmbbox[i].ForeColor=Color.Blue;
cmbbox[i].MaxDropDownItems=30;
cmbbox[i].Tag=i;
cmbbox[i].DropDownStyle=ComboBoxStyle.DropDownList;
cmbbox[i].Visible=true;
cmbbox[i].SelectedValueChanged += new System.EventHandler(
this.Change_Select_Data_ComboBox);
}
FlagColor = true;
}
catch (System.IndexOutOfRangeException exc)
{
MessageBox.Show(exc.Message,"Overloaded");
return;
}
}
/**************************************************/
/* The method enters numbers in ComboBoxes */
/**************************************************/
void Fill_ComboBoxes()
{
Random rdm;
int Number_Members_In_One_Row;
SumOfImages=0; // sum of all pencils
for (int i =
0; i < 7; i++)
{
rdm = new Random(unchecked((int)DateTime.Now.Ticks));
// get random number of elements for one heap
Number_Members_In_One_Row = (rdm.Next(30)+1);
SumOfImages+=Number_Members_In_One_Row;
if(cmbbox[i].Items.Count!=0)
{
cmbbox[i].Items.Clear();
}
for(int k=1; k<=Number_Members_In_One_Row; k++)
{
cmbbox[i].Items.Add(k);
}
cmbbox[i].Text=Number_Members_In_One_Row.ToString();
num_choos_pens=0;
System.Threading.Thread.Sleep(35);
}
}
In the same way methods CreateNew_Images() and Fill_Images() create "heaps" with pencils.
/*******************************************************************/
/* The method creates a matrix in the size 7 on 30 for the pencils */
/*******************************************************************/
void CreateNew_Images()
{
int y;
int x=0;
Image image = Image.FromFile("GrayPen.bmp");
images = new PictureBox[7][];
for(int i=0; i<7; i++,x+=107)
{
y=this.Height-(int)(6*(image.Height));
images[i] = new PictureBox [30];
for(int j=0; j<30; j++)
{
images[i][j]= new PictureBox();
images[i][j].Parent=this;
images[i][j].Height=15;
images[i][j].Width=104;
images[i][j].Anchor=AnchorStyles.Bottom|AnchorStyles.Left;
images[i][j].Location=new Point(x,y);
y-=16;
}
}
}
/*************************************************************/
/* The method fills in a matrix gray pencils */
/*************************************************************/
void Fill_Images()
{
int new_number_elements;
int old_number_elements;
Image image = Image.FromFile("GrayPen.bmp");
for(int i=0; i<7; i++)
{
new_number_elements = cmbbox[i].Items.Count;
old_number_elements=(int)numberLoadImages[i];
// if you must insert images
if(old_number_elements < new_number_elements)
{
for(int j=old_number_elements; j < new_number_elements;
j++)
{
images[i][j].Image=image;
}
numberLoadImages.SetValue(new_number_elements,i);
}
// if you must remove images
else if(old_number_elements > new_number_elements)
{
PictureBox img = new PictureBox();
for(int j=old_number_elements-1;
j>=new_number_elements; j--)
{
images[i][j].Image=img.Image;
}
numberLoadImages.SetValue(new_number_elements,i);
}
}
}
The program supports an opportunity for game of two players (one against another), and a mode of game of the person with a computer. The following code illustrates strategy of a computer (if we have, of course, chosen an option to play with a computer):
/*******************************************************/
/* Computer's game strategy */
/*******************************************************/
private void ComputerGame()
{
int [] arr = new int[7];
//Array numbers of "gray" pens in each column
for(int i= 0;i<7; i++ )
{
arr[i]=((int)numberLoadImages[i]-BlueRedImages[i]);
}
int tryResultElements=-1;
int numElements;
int n;
//////////////////////////////////////////////////////////////////
// 3 cycles for find column and number elements //
//////////////////////////////////////////////////////////////////
//for k times
for(int k= 0;k<7 && tryResultElements!=0 ; k++)
{
numElements=arr[k];
for (n=1; n<=numElements; n++)
{
num_choos_pens=n;
index=k;
////////////////////////////////////////////////////
tryResultElements = numElements-n;
//for m columns
for(int m= 0;m<7; m++)
{
if(k!=m)
tryResultElements^=arr[m];
}
if(tryResultElements==0)
{
num_choos_pens=n;
index=k;
break;
}
}
}
//////////////////////////////////////////////////////////////////
// from (k+1) column computer must get n elements //
//////////////////////////////////////////////////////////////////
int j;
int count;
Image imageRed = Image.FromFile("RedPen.bmp");
SumOfImages = SumOfImages-num_choos_pens;
count=num_choos_pens;
j=
((int)numberLoadImages[index]-BlueRedImages[index]-1);while(
count>0)
{
cmbbox[index].Items.Remove(cmbbox[index].Items.Count);
count--;
}
//Save blue/red items in array images[index][]...
BlueRedImages[index]=BlueRedImages[index]+ num_choos_pens;
for(int
i=
0;i < num_choos_pens; i++, j--)
{
images[index][j].Image=imageRed;
}
if(SumOfImages==0)
{
switch(MessageBox.Show("New Game?","The game" +
"is over.Red won",MessageBoxButtons.YesNo))
{
case DialogResult.Yes: //New Game
this.winBlue=true;
this.winRed=false;
this.UpdateGameWin();
this.Fill_ComboBoxes();
this.Fill_Images();
this.btnOK.Focus();
this.num_choos_pens=0;
this.index=-1;
this.FlagColor=false;
return;
case DialogResult.No: //Finish Game
this.Close();
break;
}
}
int newNumElementsInComboBox;
newNumElementsInComboBox=cmbbox[index].Items.Count;
cmbbox[index].Text=(
newNumElementsInComboBox>0)? Convert.ToString(
newNumElementsInComboBox):"";
winBlue=!winBlue;
winRed=!winRed;
num_choos_pens=0;
index=-1;
}
IN JAVA METHOD
import java.util.Scanner;
/**Implements a game of nim, person vs. computer.
* The game starts with a number of elements chosen
* by the person. The computer takes 1 or 2 elements.
* Then the person takes 1 or 2 elements. The game
* continues until there are no elements left. The
* play who takes the last turn wins.
public class Nim
{
public int getComputerMove(int left)
{
return (int)(Math.random()*2)+1;
}
/**
* plays the game of nim, computer versus person
*/
public void play()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter number of elements to start.");
int left = sc.nextInt();
while(left>0)
{
int computer=getComputerMove(left);
System.out.println("Computer takes "+computer);
left-=computer;
System.out.println("Now there are "+left+" left.");
if(left<=0)
{
System.out.println("Computer wins!");
return;
}
System.out.println("What's your move? (1 or 2)");
int person=sc.nextInt();
while(person!=1 && person!=2)
{
System.out.println(person+" not allowed, choose 1 or 2.");
person=sc.nextInt();
}
left-=person;
System.out.println("Now there are "+left+" left.");
if(left<=0)
{
System.out.println("You win!");
return;
}
}
}
public static void main(String[] args)
{
Nim nim=new Nim();
nim.play();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.