473,396 Members | 1,768 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to activate another loop from another loop

I am programming a game called Pig that involves a pair of dice. In the game, the user verses a computer. On each turn, the player rolls a pair of dice and adds up his or her points. Whoever reaches 100 points first, wins. If a player rolls a 1, he or she loses all points for that round and the dice goes to the other player. If a player rolls two 1's in one turn, the player loses all points earned so far in the game and loses control of the dice. The player may voluntarily turn over the dice after each roll. So the player must decide to either roll again and risk losing points, or give up the dice, possibly letting the other player win. I also need to make the computer automatically give up the dice if it receives 20 or more points, while it is their turn.

I almost finished coding the user's turn, but I don't know how to allow the user to give up the dice to the computer. I am not exactly sure what I need to to in order for this to happen but I am guessing that you need to somehow activate another loop in the loop that the user is using. If this way won't work, what can I do to make it work?

Here are the variable declarations:

Expand|Select|Wrap|Line Numbers
  1. import java.util.Scanner;
  2. public class FourFive
  3. {
  4.     public static void main (String[] args)
  5.     {
  6.         Pigs myopp = new Pigs();
  7.         Scanner scan = new Scanner (System.in);
  8.         final int Round = 20;
  9.         int num1 = 0, num2 = 0;
  10.  
  11.  
  12.         int playerTotal = 0;
  13.         int compTotal = 0;
  14.         int win = 100;
  15.         int turnOver = 1;
  16.         Pigs die1 = new Pigs();
  17.         Pigs die2 = new Pigs();
  18.         int play;
  19.  
  20.  
  21.  
  22.         int playerTotal = 0;
  23.         int compTotal = 0;
  24.         int win = 100;
  25.         int turnOver = 1;
  26.         Pigs die1 = new Pigs();
  27.         Pigs die2 = new Pigs();
  28.         int play;
Here is the user's loop:

Expand|Select|Wrap|Line Numbers
  1. System.out.println("Type 1 to roll, type 2 to turn over the dice");
  2.         play = scan.nextInt();
  3.  
  4.  while (play != 0)
  5.         {
  6.             for (int roll = 1; roll <= 6; roll++)//6 sided die
  7.             {
  8.                 num1 = die1.roll();
  9.                 num2 = die2.roll();
  10.                 int points = num1 + num2;
  11.                 System.out.println("You roll " + num1 + " and " + num2);
  12.  
  13.                 if (num1 == 1 && num2 == 1) {
  14.                     playerTotal = 0;
  15.                     System.out.println("You lose control of the dice and lose all points");
  16.                     play = 2;// trying to make the computer roll
  17.                 }
  18.                 else if (num1 != 1 && num2 != 1) {
  19.                     playerTotal += points;
  20.                     System.out.println("you earned " + points + " this round");
  21.                     play = 2;//trying to make the computer roll
  22.                 }
  23.                 else 
  24.                     System.out.println("No additional points added, you lose control of the dice");
  25.  
  26.                 if (play == 2)
  27.  
  28.  
Here is the computer's loop (the code is incomplete and I don't know what loops to use):

Expand|Select|Wrap|Line Numbers
  1.  while (play == 2)//computers turn
  2.         {
  3.          int roundTotal = num1 + num2;//Not sure what loops to use
  4.             while (roundTotal <= 20)//total number of points for each round
  5.             {
  6.                 for (int roll = 1; roll <= 6; roll++)//six sided die
  7.                     {
  8.                         num1 = die1.roll();
  9.                         num2 = die2.roll();
  10.  
  11.                         System.out.println("Computer rolls " + num1 + " and " + num2);
  12.  
  13.                         if (num1 == 1 && num2 == 1) {
  14.                             compTotal = 0;
  15.                             System.out.println("Computer loses control of the dice and loses all points, Your turn");
  16.                             play = 1;//trying to make player roll
  17.  
  18.                         }
  19.                         else if (num1 != 1 && num2 != 1) {
  20.                             compTotal += roundTotal;
  21.                             System.out.println("Computer earned " + roundTotal + " this round");
  22.                             play = 1;//trying to make player roll
  23.                         }
  24.                         else {
  25.                             System.out.println("Computer gained no additional points, your turn");
  26.  
  27.                         }
  28.                         System.out.println("Computer has a total of " + compTotal);
  29.  
  30.  
  31.  
  32.  
  33.  
  34.           }
  35.          }
  36.         }
  37.         System.out.println("Type anything to roll (0 to quit)");
  38.         play = scan.nextInt();
  39.     }
  40.  }
  41.  
I am not sure how to switch between the user's loop and computer's loop. If anything is unclear, please let me know. Thank you in advance.
Nov 16 '16 #1
1 1194
Frinavale
9,735 Expert Mod 8TB
Maybe, since you have Players, you should create a Player class that allows the application to run.

This is my suggestion but there are many ways to solve this problem:

Take the code that is in the "players loop" and put it in the Player class as a function (maybe call it PlayersTurn or something). Then take the code that is in the "computer's loop" and put it into another function in the Player class (maybe call it ComputersTurn).

Your main, outer loop will then be modified to first call the PlayersTurn method until the method finishes, and then call the ComputersTurn method until it finishes.

This loop should never exit unless some sort of "quit game" command is provided by the user during their turn maybe? Or maybe after their turn? You'll have to determine this.
Nov 17 '16 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

33
by: Arthur | last post by:
>>>a= >>> for p in a: print p 1 2 3 >>> p 3 My naive expectation was that p would be 'not defined' from outside
2
by: Aggelos | last post by:
Hi Guys!!! Thanks in advance for any kind of help about this problem. The problem: Acces Database One Table 'Orders' and second 'OrderItems' I want to export in txt file using FSO and...
2
by: Tim:.. | last post by:
Can someone please tell me what I'm doing wrong here I'm trying to give the variable sStart a new name on each loo EG sStart1 = value1 sStart2 = value however the following piece of code...
0
by: Tim::.. | last post by:
Hi can someone please give me some help with this little problem I am having with the following loop ...:: CODE ::. < 'Load XM set xml = Server.CreateObject("Microsoft.XMLDOM" xml.async = fals...
47
by: Mountain Bikn' Guy | last post by:
Take some standard code such as shown below. It simply loops to add up a series of terms and it produces the correct result. // sum numbers with a loop public int DoSumLooping(int iterations) {...
2
by: Tim::.. | last post by:
I have an error on one of my loops and am not completely sure why I keep getting the following error message! Insertion index was out of range. Must be non-negative and less than or equal to...
34
by: Frederick Gotham | last post by:
Is the domestic usage of the C "for" loop inefficient when it comes to simple incrementation? Here's a very simple program that prints out the bit-numbers in a byte. #include <stdio.h> #include...
51
by: Tony Sinclair | last post by:
I'm just learning C#. I'm writing a program (using Visual C# 2005 on WinXP) to combine several files into one (HKSplit is a popular freeware program that does this, but it requires all input and...
16
by: Claudio Grondi | last post by:
Sometimes it is known in advance, that the time spent in a loop will be in order of minutes or even hours, so it makes sense to optimize each element in the loop to make it run faster. One of...
5
by: defn noob | last post by:
Im using PyGame to draw images of graphs and trees. Howver right now i am looping using: while 1: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.