Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

java: Mancala game a guide to the Mancala game rule, please read: http://ultraboardgames.com/mancala/game-rules.php The following code missed some logic, please convert the MancalaBoard class to

java: Mancala game

a guide to the Mancala game rule, please read: http://ultraboardgames.com/mancala/game-rules.php

The following code missed some logic, please convert the MancalaBoard class to "Player" class, and create two players object to make the game plays properly, and make the class more object-oriented.

here is the code (including PlayMancala class (main)) and MancalaBoard class:

import java.util.Scanner; /** * * Main program for a mancala game. The MancalaBoard class contains * all the game logic. * * * */ public class PlayMancala { /** * * Main method to start game. * * @param args unused * */ public static void main(String[] args) { MancalaBoard board = new MancalaBoard(); board.initialize(); Scanner kbd = new Scanner(System.in); String input; System.out.println("Welcome to Mancala!"); do { board.play(); System.out.print("Play again (Y for yes)? "); input = kbd.nextLine().toUpperCase(); } while (input.charAt(0) == 'Y'); System.out.println("Thanks for playing."); } } 

//MancalaBoard class:

import java.util.Arrays; import java.util.Scanner; /** * * Game logic for mancala. * * * */ public class MancalaBoard { // if one player's pits are empty, the game is over public static final int GAMEOVER[] = {0,0,0,0,0,0}; // number of pits for each player public static final int BOARDSIZE = 6; // these values represent the number of 'stones' in the pits and mancalas of players 1 and 2 // we will treat player 1 as "south" with right mancala, player 2 as "north" with left mancala int mancala1, mancala2; int[] pits1 = new int[BOARDSIZE]; int[] pits2 = new int[BOARDSIZE]; /** * * Sets all board values to beginning-of-game values. * */ void initialize() { mancala1 = mancala2 = 0; Arrays.fill(pits1, 4); Arrays.fill(pits2, 4); } /** * * Coordinates the play of one game. * */ void play() { Scanner kbd = new Scanner(System.in); int pit; System.out.println("Here's the board; your pits are on the bottom, and your mancala on the right:"); do { do { // this loop handles "extra" turns for landing in an empty pit display(); do { // this loop makes sure the selected pit is valid for play System.out.print("Which pit will you play from? "); pit = kbd.nextInt(); } while (!valid(pit)); } while (playMove(pit) == 0); } while (getWinner() < 0); } /** * * Verifies validity of selected pit--checking both that it's in range and that * the selected pit has stones to play. * * @param pit The number of the selected pit * @return true if selected pit is viable to play from * */ boolean valid(int pit) { if (pit < 1 || pit > BOARDSIZE) { System.out.println("That is not a valid pit number. Try again."); return false; } else if (pits1[pit-1] == 0) { System.out.println("You can't play from an empty pit. Try again."); return false; } return true; } /** * * Displays the current state of the game. * */ void display() { System.out.println(" 1 2 3 4 5 6 "); System.out.println("--------------------------"); displayPitsNorth(pits2); System.out.println(); System.out.format("%4d", mancala2); System.out.print(" "); System.out.format("%4d", mancala1); System.out.println(); System.out.println(); displayPitsSouth(pits1); System.out.println("--------------------------"); System.out.println(); } /** * * Displays the number of stones in the pits of the "south" player. * */ void displayPitsSouth(int[] pits) { for (int i=0; i=0; i--) { System.out.format("%4d", pits[i]); } System.out.println(); } /** * * Processes one play of player 1 * * @param pit number to play from * @return 0 if another turn is warranted; -1 if problems arose; positive value otherwise * */ int playMove(int pit) { int stones = pits1[pit-1]; // number of stones to drop pits1[pit-1] = 0; // clear starting pit int last = 0; // number of stones in last pit played (0 means play again) /* in the unlikely (impossible?) case that the pit contains enough stones * to go around more than once, distribute the "full laps" first */ int numPits = 2*BOARDSIZE + 2; // 2 players' pits and mancalas if (stones > numPits) { incrementAll(stones / numPits); stones = stones % numPits; // what's left } /* play the remaining stones: * in the maximum case, there will be enough stones remaining to deposit on player 1's side, * player 1's mancala, player 2's side, player 2's mancala, and then back to player 1 */ /* play stones on player 1's side */ while (stones > 0 & pit < BOARDSIZE) { last = pits1[pit]; pits1[pit]++; pit++; stones--; } /* play stone in player 1's mancala, if any */ if (stones != 0) { last = mancala1; mancala1++; stones--; } /* play any remaining stones on player 2's side */ /* note that array indices follow direction of play */ pit = 0; while (stones > 0 & pit < BOARDSIZE) { last = pits2[pit]; pits2[pit]++; pit++; stones--; } if (stones != 0) { last = mancala2; mancala2++; stones--; } /* possibly, there are enough stones to get back around to player 1 */ pit = 0; while (stones > 0 & pit < BOARDSIZE) { last = pits1[pit]; pits1[pit]++; pit++; stones--; } /* when out of stones, check to see status of last pit played */ if (stones == 0) { if (last == 0) { System.out.println("Take another turn!"); return 0; // take another turn } else return pit; } else { System.out.println("I didn't expect to have stones left. Programmer fail."); return -1; } } /** * * Add n stones to each pit and mancala. * * @param n The number of stones to add. * **/ void incrementAll(int n) { for (int i=0; i mancala2) return 1; else if (mancala2 > mancala1) return 2; else return 0; } else return -1; } /** * * At endgame, player with remaining stones 'captures' them into her mancala. * **/ 
 void sweep() { for (int i=0; i                        

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Visual Basic Net Database Programming

Authors: Rod Stephens

1st Edition

0789726815, 978-0789726810

More Books

Students also viewed these Databases questions

Question

4 Discuss the provision of respondent incentives.

Answered: 1 week ago