Brief Rule Overview
- Mancala is played on a 6 x 2 board with 1 home space on each side (see picture above)
- At the start of the game each space contains 4 "beans"
- Player 1 can only pick up "beans" from the lower half of the board and Player 2 can only pick up "beans" from the upper half of the board; Player 1's home space is the home space on the right, Player 2's home space is the one on the left
- The game is played by each player alternating turns; each turn consists of picking up all the "beans" in a space and adding 1 "bean" to each following spaces (including the player's home space, but not their opponents home space) until the player runs out of "beans"
- The object of the game is to collect as many "beans" in your respective home space; the winner is the one who has the most "beans" in their respective home space
- The game ends when either row of spaces has no more "beans" left to pick up and move
- **BONUS => If a player's last "bean" lands in their home space => ** GO AGAIN
Mancala.Java; Program Introduction
- In order to re-create the game of mancala we used StdDraw, most importantly the StdDraw.line, StdDraw.circle etc… methods to recreate the board and the StdDraw.picture method to recreate the number of beads in each space
- We drew each of the individual number values using photoshop and saved them as .gif files to later be used as pictures for the array.
- For the actual code we used a 1D array to house all the integer values for the number of beads in each space
- We broke up our code into several functions to implement the game of Mancala in java.
- We coded our updatearray and updatepicture functions which update the array values and the pictures on our board representing the number of beads in each space
UpdateArray Function
board = updatearray(board, c, beans); --> calls updatearray Function, pass in our 1D array (board), column number where player clicked (c), and the value of the cell in the array that corresponds to where the player clicked
public static int[] updatearray(int []board, int c, int beans){
int curr_pos = 0;
c = c + 1; // move to the right from beginning point;
if(c == 13)
c = 0; //in case you are at then end of the array
wrap around and start back at the begining of the array
for(curr_pos = c; beans > 0; curr_pos++ )
{
if(curr_pos > 13) //reset
curr_pos = 0;
board[curr_pos] = board[curr_pos] + 1; // 1 is added to the value in the next cell
beans--; // this is a counter that decreases the move value (beans) by 1
}
// function keeps adding 1 to the following cell until the move value (beans) reaches zero
return board; // returns int values => each cell in the array updates its value
CheckMoves Function
CheckMoves Function => checks if game is over after every move
public static boolean checkmoves (int[] board){
if(((board[1] == 0) && (board[2] == 0) && (board[3] == 0) && (board[4] == 0) && (board[5] == 0) && (board[6] == 0))
&& ((board[8] == 0) && (board[9] == 0) && (board[10] == 0) && (board[11] == 0) &&
(board[12] == 0) && (board[13] == 0)))
{
return false;
}
else
{
return true;
}
}
// checks to see if there are any more available moves for either Player 1 or Player2
If there aren't boolean count becomes false and the while loop that runs the other functions ends and the game is over
Main Program --> getting needed values and calling all needed functions
while (count == true)
// while loop that ends game when there are no more moves
{
if (StdDraw.mousePressed())
{
int c = (int)(StdDraw.mouseX());
// gets int value of x cordinate that pressed on the StdDraw game board
int r = (int)(StdDraw.mouseY());
// gets int value of y cordinate that was pressed on the StdDraw game board
if(r == 0)
{
if (board [c] !=0)
{
//change board
int start = 0;
int col = 0;
int beans = 0;
start = c;
start --;
beans = board[c];
// gets move value based on the int value in the array at the point that was clicked on the board
board[c] = 0;
board = updatearray(board, c, beans);
playerId = playerGoAgain(board, c, beans, playerId);
playerId = 3 - playerId;
if (playerId == 1)
{
System.out.println("Player 1's turn");
}
else if(playerId == 2)
{
System.out.println("Player 2's turn");
}
}
//upadate StdDraw
updatepicture(c, board, image_name0, image_name1, image_name2, image_name3, image_name4, image_name5,
image_name6, image_name7, image_name8, image_name9, image_name10, image_name11, image_name12,
image_name13, image_name14, image_name15, image_name16, image_name17, image_name18, image_name19,
image_name20, image_empty, image_name21, image_name22, image_name23, image_name24,
image_name25, image_name26, image_name27, image_name28, image_name29, image_name30,
image_name31, image_name32, image_name33, image_name34, image_name35, image_name36, image_name37,
image_name38, image_name39, image_name40);
count = checkmoves(board); // calls the checkmoves function which checks if game has ended or not => when it ends it makes Boolean count = False
}
Possible Changes and Improvements
- Use StdDraw.Text rather than StdDraw.picture to update the StdDraw game board --> this would have reduced the workload of creating all the .gif picture files but would make the game board less colorful etc...
- Implement a function that allows Player 1 to only be able to click on the bottom half of the board and that Player 2 can only click on the top half of the board --> even though these are the rules and not doing so would be breaking the rules implementing a function would make sure players dont make mistakes on where they can click
- Use graphics instead of StdDraw to implement the board and add some type of animation to show the movement of the beans on the board