Question

I have spent so much time on this and I am having trouble getting all the...

I have spent so much time on this and I am having trouble getting all the methods to work together correctly to run the code right, and I am not sure what is wrong with it.

/*
You will recreate one or two versions of the logic game shown in the Algorithms movie
and you will ask which one the user would like to play.
Upon completion of the game, display who won and ask if they would like to play again and which version of the game.
You will need to use one class and a method dedicated to the game(s).

Chili Pepper:

You will create a "bucket" containing 13 "chocolates" and a "chili pepper."
The user can select 1-3 chocolates out of the bucket, the computer will do the same until someone is left with the chili pepper.
Will it matter who goes first? Do you need to change the starting number of chocolates if the user goes first?

Chess Board:

You will also create 5 "lanes" of "pieces" and each lane has that number of pieces on it;
i.e. lane 5 = 5 pieces, lane 4 = 4 pieces, etc.
The user and the computer can take any number of pieces from one lane.
The object is to not take the last piece. Will it matter who goes first?

Make sure you have instructions on how to play your game upon request.
*/

import java.util.*;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.List;
import java.io.IOException;
import java.util.Scanner;

public class LogicGameTest{

//CHILI PEPPER___________________________________________________________________________________
public String Chili() {

int choco = 13;
Random random = new Random();

while (true) {

System.out.println("There are " + choco + " chocolates left in the bucket");
int count = userCount(choco);
choco = choco - count;


if (choco > 3) {
int computerTakes = random.nextInt(3) + 1;
System.out.println("Computer takes " + computerTakes + " chocolates from the bucket");

choco -= computerTakes;
}
else if (choco > 0) {
System.out.println("There are " + choco + " chocolates left in the bucket");
System.out.println("Computer takes " + choco + " chocolates from the bucket");
System.out.println("You have to take the chilli pepper!");
break;
}
else {
System.out.println("There are " + choco + " chocolates left in the bucket");
System.out.println("Computer takes the chilli! ");
break;
}
}
//}
int userCount(int chocolate) {

while (true) {

System.out.print("\nHow many chocolates you want to take? ");
int count = scanner.nextInt();
if (1 <= count && count <= 3) {
if (count <= chocolate) {
return count;
}
else {
System.out.println("ERROR: There are only " + chocolate + " left in the bucket. You cannot take more than that!");
continue;
}
}
else {
System.out.println("ERROR: Incorrect count. You can take 1,2 or 3 chocolate at a time");
continue;
}
}
}
//}

//CHESS BOARD__________________________________________________________________________________
public String Chess() {

Scanner scan = new Scanner (System.in);
  
int lane5[] = {0,0,0,0,0};
int lane4[] = {0,0,0,0};
int lane3[] = {0,0,0};
int lane2[] = {0,0};
int lane1[] = {0};

int lanenum;
int piecenum;

System.out.println("Please select a lane that you want to take from");
lanenum = scan.nextInt();

System.out.println("How many do you want to take? Selecet a number 1 - 3.");
piecenum = scan.nextInt();

if (lanenum == 5 && piecenum <= 5) {
lane5 = removeThePieces(lane5, piecenum);
System.out.println(lane5);
}
else if (lanenum == 4 && piecenum <= 4) {
lane4 = removeThePieces(lane4, piecenum);
System.out.println(lane4);
}
else if (lanenum == 3 && piecenum <= 3) {
lane3 = removeThePieces(lane3, piecenum);
System.out.println(lane3);
}
else if (lanenum == 2 && piecenum <= 2) {
lane2 = removeThePieces(lane2, piecenum);
System.out.println(lane2);
}
else if (lanenum == 1 && piecenum <= 1) {
lane1 = removeThePieces(lane1, piecenum);
System.out.println(lane1);
}
else {
System.out.println("Please enter the number of a lane");
}
  
public int[] removeThePieces(int[] array, int num) {

int[] anotherArray = new int[array.length - num];//create another array

for (int i = 0, j = 0; i < array.length - num; i++) {
anotherArray[j++] = array[i];
}
return anotherArray;
}
}
//MAIN ______________________________________________________________________________
public static main(String str[]) throws IOException   {
  
Scanner scan = new Scanner (System.in);
  
int x;
System.out.println("Do you want to play a game? \n1 for yes \n2 for no");
x = scan.nextInt();
  
if (x == 1) {
int w;
System.out.println("Which game do you want to play? \n1 for Chili Pepper \n2 for Chess Board ");
w = scan.nextInt();

int y;
System.out.println("Do you want instructions? \n1 for yes \n2 for no ");
y = scan.nextInt();

if (y == 1) {
System.out.println("Chili Pepper: \nA bucket is full of 13 chocolate chips and a chili pepper \nYou and the computer will take turns grabbing a number of chocolate chips (1 - 3) \nout of the bucket and whoever ends with the chili pepper in the end loses.");
System.out.println("\nChess Board: \nYou and the computer will take turns grabbing \nany number of pieces from one lane. The object of this game is to \nnot take the last piece. If you end up with the last piece, \nyou lose. \n");
}
else {
System.out.println("Let's Play!\n");
}


if (w == 1) {
//System.out.println("Chili Pepper Game");
System.out.println(Chili());
}
else if (w == 2) {
//System.out.println("Chess Board Game");
String.out.println(Chess());
}
else {
System.out.println("Please selct a game!");
}
}
else {
System.out.println("That's okay, maybe another time!");
}
}
}
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

thanks for the question, here is the first part of the question. I ran out of time to work on the 2nd part, sharing the first part as its working fine.

========================================================================================

import java.util.Random;
import java.util.Scanner;

public class Chilli {


    private static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {

        int chocolates = 13;
        Random random = new Random();

        while (true) {

            System.out.println("There are " + chocolates + " chocolates left in the bucket");
            int count = userCount(chocolates);
            chocolates = chocolates - count;


            if (chocolates > 3) {
                int computerTakes = random.nextInt(3) + 1;
                System.out.println("Computer takes " + computerTakes + " chocolates from the bucket");

                chocolates -= computerTakes;
            } else if (chocolates > 0) {
                System.out.println("There are " + chocolates + " chocolates left in the bucket");
                System.out.println("Computer takes " + chocolates + " chocolates from the bucket");
                System.out.println("You have to take the chilli !");
                break;

            } else {
                System.out.println("There are " + chocolates + " chocolates left in the bucket");
                System.out.println("Computer takes the chilli! ");
                break;

            }


        }


    }

    public static int userCount(int chocolate) {

        while (true) {

            System.out.print("How many chocolates you want to take? ");
            int count = scanner.nextInt();
            if (1 <= count && count <= 3) {
                if (count <= chocolate) {
                    return count;
                } else {
                    System.out.println("ERROR: There are only " + chocolate + " left in the bucket. You cannot take more than that!");
                    continue;
                }
            } else {
                System.out.println("ERROR: Incorrect count. You can take 1,2 or 3 chocolate at a time");
                continue;
            }

        }
    }
}

======================================================================================

Add a comment
Know the answer?
Add Answer to:
I have spent so much time on this and I am having trouble getting all the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I have this code done in two methods, but it has to be done in one,...

    I have this code done in two methods, but it has to be done in one, and I can not figure out how to have it work and run correctly in one method. Chess Board: You will also create 5 "lanes" of "pieces" and each lane has that number of pieces on it; i.e. lane 5 = 5 pieces, lane 4 = 4 pieces, etc. The user and the computer can take any number of pieces from one lane. The...

  • I need help on creating a while loop for my Java assignment. I am tasked to...

    I need help on creating a while loop for my Java assignment. I am tasked to create a guessing game with numbers 1-10. I am stuck on creating a code where in I have to ask the user if they want to play again. This is the code I have: package journal3c; import java.util.Scanner; import java.util.Random; public class Journal3C { public static void main(String[] args) { Scanner in = new Scanner(System.in); Random rnd = new Random(); System.out.println("Guess a number between...

  • This is a basic Java Question referencing Chapter 5 methods. The goal is to make a...

    This is a basic Java Question referencing Chapter 5 methods. The goal is to make a quick program that has the computer guess a number, after which the user will input if they would like to play the easy, medium or hard level. After this point the user will be allowed to guess a certain amount of times, before the computer tells them they are correct, incorrect, and gives them the guessed number. The Question is as follows: Define a...

  • Java - Car Dealership Hey, so i am having a little trouble with my code, so...

    Java - Car Dealership Hey, so i am having a little trouble with my code, so in my dealer class each of the setName for the salesman have errors and im not sure why. Any and all help is greatly appreciated. package app5; import java.util.Scanner; class Salesman { private int ID; private String name; private double commRate; private double totalComm; private int numberOfSales; } class Car { static int count = 0; public String year; public String model; public String...

  • Having trouble with the do while/while loop and the switch statement. I got some of the...

    Having trouble with the do while/while loop and the switch statement. I got some of the switch statement but cant get the program to repeat itself like it should.What i have so far for my code is below. Any help is appreciated... i am not sure what I am doing wrong or what i am missing. I am completely lost on the while loop and where and how to use it in this scenario. import java.util.Scanner; public class sampleforchegg {...

  • My Question is: I have to modify this program, even a small modification is fine. Can...

    My Question is: I have to modify this program, even a small modification is fine. Can anyone give any suggestion and solution? Thanks in Advanced. import java.util.*; class arrayQueue { protected int Queue[]; protected int front, rear, size, len; public arrayQueue(int n) { size = n; len = 0; Queue = new int[size]; front = -1; rear = -1; } public boolean isEmpty() { return front == -1; } public boolean isFull() { return front == 0 && rear ==size...

  • I am getting an error from eclipse stating that: Exception in thread "main" java.lang.Error: Unresolved compilation...

    I am getting an error from eclipse stating that: Exception in thread "main" java.lang.Error: Unresolved compilation problem: at EvenOdd.main(EvenOdd.java:10) I am unable to find what I can do to fix this issue. Can you please assist? My code is below: import java.util.InputMismatchException; import java.util.Scanner; public class EvenOdd {    public static void main(String[] args) {        Scanner input = new Scanner(System.in);        System.out.println("Welcome to this Odd program!");        int userInput1 = input.nextInt();    }       public...

  • Need help with this binary tree program. I will give a thumbs up for anybody who...

    Need help with this binary tree program. I will give a thumbs up for anybody who helps. Source Code: import java.util.Random; import java.util.Scanner; import java.util.Arrays; import java.util.Collections; import java.util.ArrayList; public class Trees { public static int[] prepareData(int[] data){ /* Data is prepared by inserting random values between 1 and data.length. Data items may be assumed to be unique. Please refer to lab spec for the problem definiton. */ ArrayList list = new ArrayList(); for (int i=0; i< data.length; i++) {...

  • Make a FLOWCHART for the following JAVA Prime Number Guessing Game. import java.util.Random; import java.util.Scanner; public...

    Make a FLOWCHART for the following JAVA Prime Number Guessing Game. import java.util.Random; import java.util.Scanner; public class Project2 { //Creating an random class object static Random r = new Random(); public static void main(String[] args) {    char compAns,userAns,ans; int cntUser=0,cntComp=0; /* * Creating an Scanner class object which is used to get the inputs * entered by the user */ Scanner sc = new Scanner(System.in);       System.out.println("*************************************"); System.out.println("Prime Number Guessing Game"); System.out.println("Y = Yes , N = No...

  • This is what I have so far. I'm getting an error on the ... case 3:...

    This is what I have so far. I'm getting an error on the ... case 3: System.out.println("Enter the rank of the card you want removed"); cards.remove(); System.out.println(); break; -------------------------------------- package PlayingCards; import java.util.Scanner; public class Driver { public static void main(String[] args) { Scanner sc = new Scanner(System.in); boolean done = false; int menuInput; DeckOfCards cards = new DeckOfCards(); do { System.out.println("Enter the number of one of the choices below:"); System.out.println("1: Shuffle The Deck."); System.out.println("2: Print The Cards Remaining In...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT