Question

In Java, Using arrays. You are going to write a program that will play a solitaire...

In Java, Using arrays.

You are going to write a program that will play a solitaire (one-player) game of Mancala (Links to an external site.) (Links to an external site.). The game of mancala consists of stones that are moved through various buckets towards a goal. In this version of mancala, the user will be able to choose a number of buckets and a number of stones with which to set up the game. The buckets will be created by the program (utilizing an array) and then the stones will be randomly placed into the buckets for set up.

Once the game is set up, the user will be asked to choose a bucket from which to pick up stones. These stones will be removed from the original bucket and then placed one by one into subsequent buckets, including the goal. If stones still remain after filling each bucket to the left and the goal, then you return to the front of the buckets to continue distribution.

When all stones are moved from the buckets to the goal, the game ends.

Methods

At a minimum, your program should have the following static methods in addition to the main:

  • a method to print the introduction text
  • a method that sets up the game board (buckets and initial stone placement)
  • a method that checks if the game is over (all buckets are empty of stones)
  • a method that prints the board
  • a method that moves the stones

Method: Print Intro Text

You need to create a method that will print the intro text for the game.

Method: Game Setup

You should have a method that prompts the user to choose a number of buckets and a number of stones to be randomly placed in the buckets. You should create an array to represent all the "buckets" (the goal should be stored separate from the buckets - not in the same array). Then randomly assign the stones into the buckets by choosing a random bucket and adding a single stone to that bucket for all the stones you need to randomly place.

Method: Check if the Game is Over

This method should return true if all the buckets in the array are empty and false if any of the buckets still contains stones that need to be moved to the goal.

Method: Print Board

This method should print the board to the screen in the format provided below. Printing the board requires printing the stones in each bucket, the bucket number (starting at 1), and the stones in the goal. The format of printing the board should match the following:

Stones: | 0 | 1 | 0 | 0 | 2 | >> 0 <<
Buckets: -1- -2- -3- -4- -5-    GOAL

Note that here, the label "buckets" is presented to the user next to the bucket numbers. And the contents of the buckets (array) is presented to the user next to the word "stones".

Method: Move Stones

This method should:

  • carry out a single move of stones (one round)
  • allow the user to choose the bucket from which they want to remove stones
  • should distribute these stones beginning from the subsequent bucket, until the end of the array of buckets. If stones still remain on reaching the end, a stone should be distributed to the "goal". If stones still remain after that distribution, stones should continue to be distributed returning to the first bucket (index 0).
  • If the user's guess is entered in lowercase but appears in the puzzle in uppercase, it should still be marked to be displayed. Likewise, if the user's guess is entered in uppercase but appears in the puzzle in lowercase, it should also be marked to be displayed.

As an example, consider the first two moves (two executions of moving stones) here:

== Move #1 ==
Stones: | 3 | 0 | 4 | 0 | 2 | >> 0 <<
Buckets: -1- -2- -3- -4- -5-   GOAL
Which bucket? > 3
4 stones moved. 1 stones added to goal.

== Move #2 ==
Stones: | 4 | 0 | 0 | 1 | 3 | >> 1 <<
Buckets: -1- -2- -3- -4- -5-   GOAL
Which bucket? > 5
3 stones moved. 1 stones added to goal.

== Move #3 ==
Stones: | 5 | 1 | 0 | 1 | 0 | >> 2 <<
Buckets: -1- -2- -3- -4- -5-   GOAL
Which bucket? >

In the first move, 4 stones are removed from bucket 3. These stones are distributed to bucket 4, bucket 5, the goal, and then bucket 1.

In the second move, 3 stones are removed from bucket 5. These stones are distributed to the goal, bucket 1, and bucket 2.

Other Notes

  • On each move of the game, the user should be notified as to how many stones were moved, as well as how many stones were added to the goal.
  • At the end of the game, the number of total moves played should be printed to the screen.
  • Remember that arrays are passed by reference to methods, so if you change an array in the method you do NOT need to return it to see the changes take place.
  • Note that if there are a large number of stones in a bucket then the formatting of the board might be a little off. That is OK. Just make sure that the formatting is correct for single-digit numbers of stones.

Sample Output 1

This program will play a modified solitaire version of
the game of Mancala. Your goal is to get all the stones
from the buckets on the left into the goal on the right
in the least number of moves possible.

Before we begin the game, choose:
        How many buckets do you want? > 5
        How many stones do you want? > 9

OK, let's play...

== Move #1 ==
Stones: | 3 | 0 | 4 | 0 | 2 | >> 0 <<
Buckets: -1- -2- -3- -4- -5-   GOAL
Which bucket? > 3
4 stones moved. 1 stones added to goal.

== Move #2 ==
Stones: | 4 | 0 | 0 | 1 | 3 | >> 1 <<
Buckets: -1- -2- -3- -4- -5-   GOAL
Which bucket? > 5
3 stones moved. 1 stones added to goal.

== Move #3 ==
Stones: | 5 | 1 | 0 | 1 | 0 | >> 2 <<
Buckets: -1- -2- -3- -4- -5-   GOAL
Which bucket? > 1
5 stones moved. 1 stones added to goal.

== Move #4 ==
Stones: | 0 | 2 | 1 | 2 | 1 | >> 3 <<
Buckets: -1- -2- -3- -4- -5-   GOAL
Which bucket? > 5
1 stones moved. 1 stones added to goal.

== Move #5 ==
Stones: | 0 | 2 | 1 | 2 | 0 | >> 4 <<
Buckets: -1- -2- -3- -4- -5-   GOAL
Which bucket? > 4
2 stones moved. 1 stones added to goal.

== Move #6 ==
Stones: | 0 | 2 | 1 | 0 | 1 | >> 5 <<
Buckets: -1- -2- -3- -4- -5-   GOAL
Which bucket? > 5
1 stones moved. 1 stones added to goal.

== Move #7 ==
Stones: | 0 | 2 | 1 | 0 | 0 | >> 6 <<
Buckets: -1- -2- -3- -4- -5-   GOAL
Which bucket? > 2
2 stones moved. 0 stones added to goal.

== Move #8 ==
Stones: | 0 | 0 | 2 | 1 | 0 | >> 6 <<
Buckets: -1- -2- -3- -4- -5-   GOAL
Which bucket? > 2
That bucket has no stones. 0 stones added to goal.

== Move #9 ==
Stones: | 0 | 0 | 2 | 1 | 0 | >> 6 <<
Buckets: -1- -2- -3- -4- -5-   GOAL
Which bucket? > 3
2 stones moved. 0 stones added to goal.

== Move #10 ==
Stones: | 0 | 0 | 0 | 2 | 1 | >> 6 <<
Buckets: -1- -2- -3- -4- -5-   GOAL
Which bucket? > 5
1 stones moved. 1 stones added to goal.

== Move #11 ==
Stones: | 0 | 0 | 0 | 2 | 0 | >> 7 <<
Buckets: -1- -2- -3- -4- -5-   GOAL
Which bucket? > 4
2 stones moved. 1 stones added to goal.

== Move #12 ==
Stones: | 0 | 0 | 0 | 0 | 1 | >> 8 <<
Buckets: -1- -2- -3- -4- -5-   GOAL
Which bucket? > 5
1 stones moved. 1 stones added to goal.

Nice job! all stones moved to goal in 12 moves.

Sample Output 2

This program will play a modified solitaire version of
the game of Mancala. Your goal is to get all the stones
from the buckets on the left into the goal on the right
in the least number of moves possible.

Before we begin the game, choose:
        How many buckets do you want? > 3
        How many stones do you want? > 1
OK, let's play...

== Move #1 ==
Stones: | 0 | 0 | 1 | >> 0 <<
Buckets: -1- -2- -3-   GOAL
Which bucket? > 2
That bucket has no stones. 0 stones added to goal.

== Move #2 ==
Stones: | 0 | 0 | 1 | >> 0 <<
Buckets: -1- -2- -3-   GOAL
Which bucket? > 3
1 stones moved. 1 stones added to goal.

Nice job! all stones moved to goal in 2 moves.

Sample Output 3

This program will play a modified solitaire version of
the game of Mancala. Your goal is to get all the stones
from the buckets on the left into the goal on the right
in the least number of moves possible.

Before we begin the game, choose:
        How many buckets do you want? > 2
        How many stones do you want? > 12

OK, let's play...

== Move #1 ==
Stones: | 11 | 1 | >> 0 <<
Buckets: -1- -2-    GOAL
Which bucket? > 1
11 stones moved. 4 stones added to goal.

== Move #2 ==
Stones: | 3 | 5 | >> 4 <<
Buckets: -1- -2-    GOAL
Which bucket? > 1
3 stones moved. 1 stones added to goal.

== Move #3 ==
Stones: | 1 | 6 | >> 5 <<
Buckets: -1- -2-    GOAL
Which bucket? > 1
1 stones moved. 0 stones added to goal.

== Move #4 ==
Stones: | 0 | 7 | >> 5 <<
Buckets: -1- -2-    GOAL
Which bucket? > 2
7 stones moved. 3 stones added to goal.

== Move #5 ==
Stones: | 2 | 2 | >> 8 <<
Buckets: -1- -2-    GOAL
Which bucket? > 1
2 stones moved. 1 stones added to goal.

== Move #6 ==
Stones: | 0 | 3 | >> 9 <<
Buckets: -1- -2-    GOAL
Which bucket? > 2
3 stones moved. 1 stones added to goal.

== Move #7 ==
Stones: | 1 | 1 | >> 10 <<
Buckets: -1- -2-    GOAL
Which bucket? > 2
1 stones moved. 1 stones added to goal.

== Move #8 ==
Stones: | 1 | 0 | >> 11 <<
Buckets: -1- -2-    GOAL
Which bucket? > 1
1 stones moved. 0 stones added to goal.

== Move #9 ==
Stones: | 0 | 1 | >> 11 <<
Buckets: -1- -2-    GOAL
Which bucket? > 2
1 stones moved. 1 stones added to goal.

Nice job! all stones moved to goal in 9 moves.

Homework Submission

Comments

Include a comment at the beginning of your program with the following information and a description of the program in your own words:

// Your name here
// CS 141
// HW Core Topics:
//
// This program will...

Additionally, include a comment above each method that you write describing what that method does. Comments should not be in-line with the method header, but instead should come on a separate line immediately before the method.

// This method...
public static void goodMethodName() {
    /* Stuff goes here */
}

Program Output

As always, include the output from a single run of your program as a block comment at the end of the program. This comment should come one blank line after the last closing curly brace in your code.

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

CODE:

package HomeworkLib;

import java.util.*;
public class Mancala {
  
/*public int[] MoveStones(int[] Buckets,Mancala MancalaObj) {
int i=MancalaObj.GOAL;
return Buckets;
}*/
public static boolean checkGameIsOverOrNot(int Buckets[]) {
for(int i=0;i<Buckets.length;i++) {
if(Buckets[i]!=0)
return true;
}
return false;
}
public static void getRandomAllocted(int Buckets[],int count,int sum) {
java.util.Random g = new java.util.Random();
for (int i = 0; i < count-1; ++i) {
Buckets[i] = g.nextInt(sum);
}
Buckets[count-1] = sum;
java.util.Arrays.sort(Buckets);
for (int i = count-1; i > 0; --i) {
Buckets[i] -= Buckets[i-1];
}
}
public static int[] setUpGameBoard() {
Scanner in = new Scanner(System.in);
int numberOfBuckets;
int nunberOfStones;
System.out.print("Enter total number of Buckets Required: ");
numberOfBuckets=in.nextInt();
System.out.print("Enter total number of Stones: ");
nunberOfStones=in.nextInt();
  
int Buckets[]=new int[numberOfBuckets];
getRandomAllocted(Buckets,numberOfBuckets,nunberOfStones);
  
in.close();
return Buckets;
}
public static void printBoard(int Buckets[],int GOAL) {
System.out.print("Stones: |");
for(int i=0;i<Buckets.length;i++) {
System.out.print(Buckets[i]+" | ");
}
System.out.print(">> "+GOAL+" <<\n");
  
System.out.print("Buckets: -");
for(int i=0;i<Buckets.length;i++) {
System.out.print((i+1)+" - ");
}
System.out.print(" GOAL \n");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
int Buckets[]=setUpGameBoard();
int MoveCount=0;
int GOAL=0;
//Mancala MancalaObj=new Mancala();
//MancalaObj.GOAL=0;
//int GOAL=MancalaObj.GOAL;
System.out.println(Arrays.toString(Buckets));
while(checkGameIsOverOrNot(Buckets)) {
MoveCount++;
System.out.println("=="+"Move #"+MoveCount+"==");
printBoard(Buckets,GOAL);
  
System.out.print("Which Bucket? > ");
int BucketNumber=in.nextInt();
int stonesInBucket=Buckets[BucketNumber-1];
while(stonesInBucket!=0) {
GOAL+=1;
for(int i=BucketNumber;i<Buckets.length;i++) {
Buckets[i]+=1;
stonesInBucket--;
}
if(stonesInBucket!=0) {
for(int i=0;i<BucketNumber-2;i++) {
Buckets[i]+=1;
stonesInBucket--;
}
}
}
}
in.close();
}
}Test Chegg.java ArrayFunou. ArrayFunHou.. Expression Testava AwtControl De.. ManikFrame java 1 package chees: 2 3 import java

Add a comment
Know the answer?
Add Answer to:
In Java, Using arrays. You are going to write a program that will play a solitaire...
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
  • Write a Java program to play the game Tic-Tac-Toe. Start off with a human playing a...

    Write a Java program to play the game Tic-Tac-Toe. Start off with a human playing a human, so each player makes their own moves. Follow the design below, creating the methods indicated and invoking them in the main program. Use a char array of size 9 as the board; initialize with the characters 0 to 8 so that it starts out looking something like the board on the left. 0|1|2 3|4|5 6|7|8 and then as moves are entered the board...

  • Write a c program that will allow two users to play a tic-tac-toe game. You should...

    Write a c program that will allow two users to play a tic-tac-toe game. You should write the program such that two people can play the game without any special instructions. (assue they know how to play the game). Prompt first player(X) to enter their first move. .Then draw the gameboard showing the move. .Prompt the second player(O) to enter their first move. . Then draw the gameboard showing both moves. And so on...through 9 moves. You will need to:...

  • Use Java language to create this program Write a program that allows two players to play...

    Use Java language to create this program Write a program that allows two players to play a game of tic-tac-toe. Using a two-dimensional array with three rows and three columns as the game board. Each element of the array should be initialized with a number from 1 - 9 (like below): 1 2 3 4 5 6 7 8 9 The program should run a loop that Displays the contents of the board array allows player 1 to select the...

  • For this lab you will write a Java program using a loop that will play a...

    For this lab you will write a Java program using a loop that will play a simple Guess The Number game. Th gener e program will randomly loop where it prompt the user for a ate an integer between 1 and 200 (including both 1 and 200 as possible choices) and will enter a r has guessed the correct number, the program will end with a message indicating how many guesses it took to get the right answer and a...

  • Write a program in the Codio programming environment that allows you to play the game of...

    Write a program in the Codio programming environment that allows you to play the game of Rock / Paper / Scissors against the computer. Within the Codio starting project you will find starter code as well as tests to run at each stage. There are three stages to the program, as illustrated below. You must pass the tests at each stage before continuing in to the next stage.  We may rerun all tests within Codio before grading your program. Please see...

  • Write a C# program that allows one user to play Rock-Paper-Scissors with computer. The user will...

    Write a C# program that allows one user to play Rock-Paper-Scissors with computer. The user will pick a move (Rock Paper Scissors) from 3 radio buttons and click the play button to play the game. The application will use random number generator to pick a move for the computer. Then, the application display the computer's move and also display whether you won or lost the game, or the game is tie if both moves are the same. The application must...

  • Using very basic Java programming Write a program that lets the user play the game of...

    Using very basic Java programming Write a program that lets the user play the game of Rock, Paper, Scissors against the computer.The program should use an array of Strings with the names of the choices.The program should loop until the human player decides to quit.The game should keep track of how many times the human won, lost, or tied and present those values when the game ends.

  • Assignment C++: Rock-Scissor-Paper & Tic-Tac-Toe i need you to write two different program for RSP and...

    Assignment C++: Rock-Scissor-Paper & Tic-Tac-Toe i need you to write two different program for RSP and TTT: R-S-P Requirement: - write one program that mimics the Rock-Scissor-Paper game. This program will ask user to enter an input (out of R-S-P), and the computer will randomly pick one and print out the result (user wins / computer wins). - User's input along with computer's random pick will be encoded in the following format: -- user's rock: 10 -- user's scissor: 20...

  • (Tic-Tac-Toe) Create a class Tic-Tac-Toe that will enable you to write a program to play Tic-Tac-Toe....

    (Tic-Tac-Toe) Create a class Tic-Tac-Toe that will enable you to write a program to play Tic-Tac-Toe. The class contains a private 3-by-3 two-dimensional array. Use an enumeration to represent the value in each cell of the array. The enumeration’s constants should be named X, O and EMPTY (for a position that does not contain an X or an O). The constructor should initialize the board elements to EMPTY. Allow two human players. Wherever the first player moves, place an X...

  • You are going to write a method (to be called validateBoard) that is going to validate...

    You are going to write a method (to be called validateBoard) that is going to validate whether or not a Tic-Tac-Toe board is possible. Tic- Tac-Toe is played on a 3 x 3 board and players take turns placing either an x or an o on the board. We will assume that in Tic-Tac-Toe the player placing x will go first arld that o will go second. (Learn more about the game here) As the player placing x pieces goes...

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