Question

In: Computer Science

Write an app that works in both vertical and horizontal orientations. The app is a variety...

Write an app that works in both vertical and horizontal orientations. The app is a variety of the game of nim. Two players take turns removing identical objects from a set of objects. A player can remove one, two, or three objects at a time. The player who takes the last object loses. You can store the current number of objects in a TextView and get user input via an EditText. Include a Model. Generate randomly the starting number of objects, an integer between 10 and 20. The GUI should look nice in both orientations.


Java language using android studio ..my apologies sir.

Solutions

Expert Solution

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
{

    /**gets the computer's move, 1 or 2.  Currently
     * this method gets a random number.  Your job
     * is to make the computer choose such that it
     * wins every time it is possible.  First, solve
     * with recursion.  After successfully completing
     * this, run the program a few times to see if you
     * can recognize the pattern the computer is taking.
     * Then see if you can get the computer to choose 
     * its move without looping or recursion.
     * @param left
     * @return
     */
    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 Solutions

write 500 words on compare the vertical and horizontal organizational structure
write 500 words on compare the vertical and horizontal organizational structure
For each function, find the horizontal intercepts, the vertical intercept, the vertical asymptotes, and the horizontal...
For each function, find the horizontal intercepts, the vertical intercept, the vertical asymptotes, and the horizontal asymptote. 19. p(x)= 2x-3/x+4 21. s(x)=4/(x-2)^2 24. g(x)= 2x^2 +7x - 15/ 3x^2- 14+ 15 26. k(x)= 2x^2- 3x- 20/x-5
Explain horizontal and vertical integration.
Explain horizontal and vertical integration.
compare the following companies in terms of both horizontal boundary and vertical boundary (i) taiwan's htc...
compare the following companies in terms of both horizontal boundary and vertical boundary (i) taiwan's htc (ii) america's apple and (iii)korea's samsung
Consider an SEIR model with both horizontal and vertical transmission. What assumption can you make about...
Consider an SEIR model with both horizontal and vertical transmission. What assumption can you make about the new born of mothers from the E and I compartment. Should the infected new born enter the E compartment or I compartment or both? Think of the possibilities. Draw a transfer diagram according to the different assumptions you made and derive the corresponding differential equations. Note that : S = susceptible E = exposed I = infectious R = recovery
Consider a mathematical pendulum with a support point moving both in vertical (ξ(t)) and horizontal (η(t))...
Consider a mathematical pendulum with a support point moving both in vertical (ξ(t)) and horizontal (η(t)) direction. (a) Write the Lagrangian and determine the equation of motion. Hint: Consider two dimensions. (b) Consider small angle approximation. What are the linearized equations of motion? Discuss two cases: 1. uniformly accelerated vertical motion, 2. periodic horizontal motion
Over the past few decades, Disney has gone through both horizontal and vertical integration through a...
Over the past few decades, Disney has gone through both horizontal and vertical integration through a number of horizontal and vertical mergers. What are these mergers? What does Disney’s business include? What are the pros and cons being so integrated?
Balance Sheet Horizontal and Vertical Analysis 2018 2019 Horizontal Vertical Cash 28,421 1,274 -95.52% 0.13% AR...
Balance Sheet Horizontal and Vertical Analysis 2018 2019 Horizontal Vertical Cash 28,421 1,274 -95.52% 0.13% AR 225,019 333,558 48.24% 34.00% Inventory 319,887 321,006 0.35% 32.72% Prepaid Insurance 3,500 3,750 7.14% 0.38% Total Current Assets 576,827 659,588 14.35% 67.23% Equipment 385,000 385,000 0.00% 39.24% Less: Acc. Dep. 21,150 63,450 200.00% 6.47% Net Fixed Assets 363,850 321,550 -11.63% 32.77% Total Assets 940,677 981,138 4.30% 100.00% Accts Payable 331,822 401,693 21.06% 40.94% W&P Taxes Payable 25,889 26,145 0.99% 2.66% ST Portion MP 21,014...
Discuss the difference between horizontal and vertical analysis.
Discuss the difference between horizontal and vertical analysis.
explain the tradeoff between horizontal and vertical velocity
explain the tradeoff between horizontal and vertical velocity
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT