1. Mancala is a board game for two players. A Mancala board has 12 small
pits, called "houses," and two large pits, called "stores." At the
beginning, the same number of "seeds" (for example, three) is placed in
each
...
1. Mancala is a board game for two players. A Mancala board has 12 small
pits, called "houses," and two large pits, called "stores." At the
beginning, the same number of "seeds" (for example, three) is placed in
each house; the stores are left empty. Each player controls the six
houses on her side of the board, and her store is to the right of her
houses.
The Java class Mancala is used in the computer implementation of
the game. In this question you are asked to write the constructor and
one method of that class.
The board is represented as int[] board with 14 elements. The
first player's houses are represented by the elements with indices from
1 to 6 and her store has index 7. The second player's houses are
represented by the elements with indices form 8 to 13 and her store has
index 0. The constants and instance variables of the Mancala class
are shown below.
public class Mancala
{
/** The total number of pits on the board */
private static final int BOARD_SIZE = 14;/** The indices of the stores */
private static final int store1 = BOARD_SIZE/2;
private static final int store2 = 0;
/** board[k] holds the numbers of seeds in the k-th pi
t */
private int[] board;
...
}
Throughout your solution, use the three symbolic constants defined
above, rather than literal constants (to make it easier to change the
board size, if necessary).
a. Write a constructor for the Mancala class that takes one
integer parameter n and initializes the board, "placing" n seeds in
each house and leaving the stores empty. Complete
the Mancala's constructor below.
Question 1(a)
/** Initializes the board to hold
BOARD_SIZE values;
* places n "seeds into each "house"; leaves both
* "stores" empty.
*/
public Mancala (int n)
{
board = new int[BOARD_SIZE];
for (int i=1; i < BOARD_SIZE; i++) {
board[i]=n;
}
board[store1] = 0;
board[store2] = 0;
}
b. On each move, a player takes all the seeds from one of her
houses and "sows" them, moving along the board
counterclockwise and placing one seed in each pit, including herhouses, her own store, and the opponent's houses, but excluding
the opponent's store. For example:
If the move ends in the player's own store, the player gets another
turn. Write the method move(int k) that implements the
move from the pit with index k. The method should
return true if the move ends in the player's own store;
otherwise it should return false. Complete the
method move below.
Question 1(b)
/** Updates the board for the move
from a given pit.
* @param k the index of the pit in the array board.
* @return true if the move ends in the player's
* own store; otherwise false.
* Precondition: k != store1 && k != store2
*/
public boolean move(int k)
{
if (k==store1 || k==store2) return false;
int n = board[k];
board[k]=0;
for (int i=k; i>=0; i--) {
if (n >0) {
board[i]++;
n--;
}
}
if (n<=0) return false;
for (int i=0; i 0)
board[i]++;
n--;
}}
return true;
}
In one variation of the game, if a player's move ends in one of that
player's houses, and that house is empty, the player captures the last
sown seed and all the seeds in the opponent's house directly opposite.
All the captured seeds are placed into the player's store. For additional
practice, implement this rule in the move method.
2. This question is concerned with the design and implementation of
classes for a school bus transportation system. In this system,
a Student object represents a student in a given school. A student
has a name and an address, represented as a street name
(a String) and a house number on the street (an int). The
class Student implements the following interface:
public interface Person
{
String getName();
String getStreet();
[Show More]