This my 4 times sending you I did not get help yet please help me this time Ques
ID: 3571739 • Letter: T
Question
This my 4 times sending you I did not get help yet please help me this time Question: Rules The game provides a Kalah board and a number... Bookmark Rules The game provides a Kalah board and a number of seeds or counters. The board has 12 small pits, called houses, on each side; and a big pit, called an end zone, at each end. The object of the game is to capture more seeds than one's opponent. At the beginning of the game, four seeds are placed in each house. This is the traditional method. Each player controls the six houses and their seeds on the player's side of the board. The player's score is the number of seeds in the store to their right. Players take turns sowing their seeds. On a turn, the player removes all seeds from one of the houses under their control. Moving counter-clockwise, the player drops one seed in each house in turn, including the player's own store but not their opponent's. If the last sown seed lands in an empty house owned by the player, and the opposite house contains seeds, both the last seed and the opposite seeds are captured and placed into the player's store. If the last sown seed lands in the player's store, the player gets an additional move. There is no limit on the number of moves a player can make in their turn. When one player no longer has any seeds in any of their houses, the game ends. The other player moves all remaining seeds to their store, and the player with the most seeds in their store wins. It is possible for the game to end in a draw. Requirements The project name shall be _HW5_Mancala Create a JSP-based application using MVC architecture that implements the Mancala game as described above The application must display: The game board current player indicator current number of seeds in each pit current scores (number of seeds in each house) game over indicator winner indicator a quit button // I have some Code here it is work half way please need help My big issue is Java Lojic Here is My CODE /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package model; import java.io.Serializable; /** * * @author Gemechu */ public class Game implements Serializable { private final static int NUM_COLS = 6; private final static int NUM_ROWS = 2; private int[][] board; private int store1; private int store2; private int turn = 0; private Players winner = null; public void play(int rownumber, int colnumber) { int seeds = board[rownumber][colnumber]; int nextRow = rownumber; int nextCol = colnumber + 1; board[rownumber][colnumber] = 0; while (seeds > 0) { // if (nextCol <= 5 && seeds > 0) { board[nextRow][nextCol]++; seeds--; } if (nextCol == 5 && seeds > 1) { //store1++; } if (nextCol >= 5 && seeds > 0) { store1++; seeds--; board[nextRow][nextCol]++; seeds--; } nextCol++; // if (nextCol > 5 && seeds > 0) { // board[nextRow][nextCol]++; // seeds--; //} //if (nextCol == 4 && seeds == 1) { // store2++; // } //if (nextCol == 0 && seeds > 0) { // store2++; //seeds--; //board[nextRow][nextCol]++; //seeds--; //} //nextCol--; } } private enum Players { X, Z; } public Game() { board = new int[NUM_ROWS][NUM_COLS]; reset(); } public int[][] getBoard() { return board; } public final void reset() { turn = 0; winner = null; for (int row = 0; row < NUM_ROWS; row++) { for (int col = 0; col < NUM_COLS; col++) { board[row][col] = 4; } } } public Players getCurrentPlayer() { return turn % 2 == 0 ? Players.X : Players.Z; } public int getStore1() { return store1; } public int getStore2() { return store2; } }
Explanation / Answer
KalahGame.java
/**
* Stateless Logic for the Game of Kalah is as follows:
* <OL>
* <LI>The game provides a Kalah board and a number of seeds or counters. The
* board has 12 small pits, called pits, on each side; and a big pit, called an
* end zone, at each end. The object of the game is to capture more seeds than
* one's opponent.
* <LI>At the beginning of the game, four seeds are placed in each house. This
* is the traditional method.
* <LI>Each player controls the six pits and their seeds on the player's side of
* the board. The player's score is the number of seeds in the store to their
* right.
* <LI>Players take turns sowing their seeds. On a turn, the player removes all
* seeds from one of the pits under their control. Moving counter-clockwise, the
* player drops one seed in each house in turn, including the player's own store
* but not their opponent's.
* <LI>If the last sown seed lands in an empty house owned by the player, and
* the opposite house contains seeds, both the last seed and the opposite seeds
* are captured and placed into the player's store.
* <LI>If the last sown seed lands in the player's store, the player gets an
* additional move. There is no limit on the number of moves a player can make
* in their turn.
* <LI>When one player no longer has any seeds in any of their pits, the game
* ends. The other player moves all remaining seeds to their store, and the
* player with the most seeds in their store wins.
* </OL>
* It is possible for the game to end in a draw.
*
*
*/
public class KalahGame {
private Player checkForWinner(KalahBoard board) {
if (board.getPlayer1().getStore().getStones() > board.getPlayer2().getStore().getStones()) {
board.addMessage("Player 1 wins!");
return board.getPlayer1();
} else if (board.getPlayer1().getStore().getStones() < board.getPlayer2().getStore().getStones()) {
board.addMessage("Player 2 wins!");
return board.getPlayer2();
} else {
// Draw
board.addMessage("Its a draw!");
return null;
}
}
public void go(KalahBoard board, int houseNumber) {
CircularCursor<Pit> cursor = board.getHouseCursor(houseNumber - 1);
/**
* Players take turns sowing their seeds. On a turn, the player removes
* all seeds from one of the houses under their control. Moving
* counter-clockwise, the player drops one seed in each house in turn, .
*/
Pit pit = board.getPits().get(houseNumber - 1);
if (pit.getPlayer() != board.getCurrentPlayer()) {
board.addMessage("Wrong player");
return;
}
board.clearMessages();
int stones = pit.takeStones();
for (int i = stones; i > 0; i--) {
pit = cursor.next();
if (pit instanceof Store && pit.getPlayer() != board.getCurrentPlayer()) {
/**
* including the player's own store but not their opponent's
*/
pit = cursor.next();
}
/**
* If the last sown seed lands in an empty house owned by the
* player, and the opposite house contains seeds,
*/
if (pit instanceof House && i == 1 && pit.isEmpty() && pit.getPlayer() == board.getCurrentPlayer()
&& !((House) pit).getOppositeHouse().isEmpty()) {
/**
* both the last seed and the opposite seeds are captured and
* placed into the player's store.
*/
board.addMessage(board.getCurrentPlayer() + " captures " + ((House) pit).getOppositeHouse().getStones()
+ " stones");
board.getCurrentPlayer().getStore().addStones(1 + ((House) pit).getOppositeHouse().takeStones());
} else {
pit.addStone();
}
}
int player1Stones = board.getPlayer1().countStones();
int player2Stones = board.getPlayer2().countStones();
if (player1Stones == 0) {
board.getPlayer2().getStore().addStones(player2Stones);
board.getPlayer2().getHouses().forEach((player2house) -> {
player2house.setStones(0);
});
checkForWinner(board);
}
if (player2Stones == 0) {
board.getPlayer1().getStore().addStones(player1Stones);
board.getPlayer2().getHouses().forEach((player1house) -> {
player1house.setStones(0);
});
checkForWinner(board);
}
if (cursor.getCurrentElement() == board.getCurrentPlayer().getStore()) {
board.addMessage(board.getCurrentPlayer() + " gets an additional move");
} else {
board.changePlayer();
}
}
}
KalahBoard.java
import java.util.ArrayList;
import java.util.List;
/**
* The model of the Kalah Game
*
* <PRE>
* <--- Player 1
------------------------
6 5 4 3 2 1
7 14
8 9 10 11 12 13
------------------------
Player 2 --->
* </PRE>
*
*
*/
public class KalahBoard {
public final int configHouses;
public final int configStones;
private Player currentPlayer;
private final List<String> messages = new ArrayList<String>();
private final List<Pit> pits = new ArrayList<Pit>();
private Player player1;
private Player player2;
public KalahBoard(int configStones, int configHouses) {
this.configStones = configStones;
this.configHouses = configHouses;
initKalahBoard();
}
public void addMessage(String message) {
messages.add(message);
}
public void changePlayer() {
if (currentPlayer == player1) {
currentPlayer = player2;
} else {
currentPlayer = player1;
}
}
public void clearMessages() {
messages.clear();
}
public int getConfigHouses() {
return configHouses;
}
public int getConfigStones() {
return configStones;
}
public Player getCurrentPlayer() {
return currentPlayer;
}
public CircularCursor<Pit> getHouseCursor(int index) {
return new CircularCursor<Pit>(pits, index);
}
public List<String> getMessages() {
return messages;
}
public List<Pit> getPits() {
return pits;
}
public Player getPlayer1() {
return player1;
}
public Player getPlayer2() {
return player2;
}
private void initKalahBoard() {
// Configure Blank Board
player1 = new Player("Player 1");
for (int i = 0; i < configHouses; i++) {
player1.getHouses().add(new House(player1, configStones));
}
player1.setStore(new Store(player1, 0));
pits.addAll(player1.getHouses());
pits.add(player1.getStore());
player2 = new Player("Player 2");
for (int i = 0; i < configHouses; i++) {
House newHouse = new House(player2, configStones);
House oppositeHouse = player1.getHouses().get(configHouses - i - 1);
newHouse.setOppositeHouse(oppositeHouse);
oppositeHouse.setOppositeHouse(newHouse);
player2.getHouses().add(newHouse);
}
player2.setStore(new Store(player2, 0));
pits.addAll(player2.getHouses());
pits.add(player2.getStore());
messages.add("New Board");
changePlayer();
}
}
Pit.java
public class Pit {
private final Player player;
private int stones;
public Pit(Player player, int configStones) {
this.player = player;
this.stones = configStones;
}
public int getStones() {
return stones;
}
public int takeStones() {
int currentStones = stones;
stones = 0;
return currentStones;
}
public void setStones(int stones) {
this.stones = stones;
}
public Player getPlayer() {
return player;
}
public void addStone() {
stones++;
}
public void addStones(int newStones) {
stones += newStones;
}
public boolean isEmpty() {
return stones == 0;
}
}
CircularCursor.java
import java.util.List;
import java.util.ListIterator;
/**
* Utility for looping through a list in a forwards direction
*
* @param <T>
*/
public class CircularCursor<T> {
private List<T> list;
private int currentIndex;
private ListIterator<T> listIterator;
private T currentElement;
public CircularCursor(List<T> list, int index) {
this.list = list;
this.listIterator = list.listIterator(index);
this.currentElement = next();
}
public int getCurrentIndex() {
return currentIndex;
}
public T getCurrentElement() {
return currentElement;
}
public T next() {
if (listIterator.hasNext()) {
currentIndex = listIterator.nextIndex();
currentElement = listIterator.next();
return currentElement;
}
listIterator = list.listIterator();
return next();
}
}
Player.java
import java.util.ArrayList;
import java.util.List;
public class Player {
private final List<House> houses = new ArrayList<House>();
private final String name;
private int stones;
private Store store;
public Player(String name) {
this.name = name;
}
/**
* Count of all stones in houses
*/
public int countStones() {
return houses.stream().mapToInt((h) -> h.getStones()).sum();
}
public List<House> getHouses() {
return houses;
}
public String getName() {
return name;
}
public int getStones() {
return stones;
}
public Store getStore() {
return store;
}
public void setStones(int stones) {
this.stones = stones;
}
public void setStore(Store store) {
this.store = store;
}
@Override
public String toString() {
return name;
}
}
Store.java
public class Store extends Pit {
public Store(Player player, int configStones) {
super(player, configStones);
}
}
House.java
public class House extends Pit {
private House oppositeHouse;
public House(Player player, int configStones) {
super(player, configStones);
}
public House getOppositeHouse() {
return oppositeHouse;
}
public void setOppositeHouse(House opposite) {
this.oppositeHouse = opposite;
}
}
kalah.jsp
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head><title>Kalah - Chris Morgan</title>
</head>
<style>
.store {
height: 256px;
width: 128px;
display: block;
}
.player1 {
border: 1px solid red;
}
.player2 {
border: 1px solid blue;
}
</style>
<body>
<h1>Kalah</h1>
<a href="<c:url value="/newGame" />">New Game</a>
<table border=1>
<tr>
<c:set var="imgNumber" value="${board.player1.store.stones}"
scope="page"></c:set>
<c:if test="${imgNumber>24}">
<c:set var="imgNumber" value="24" />
</c:if>
<td class="player1" rowspan=2>Player 1<img class="store"
src="<c:url value="/images/stones/${imgNumber}.png"/>" />${board.player1.store.stones}
</td>
<c:forEach begin="1" end="${board.configHouses}" var="i" step="1">
<c:set var="j" value="${board.configHouses-i+1}" scope="page"></c:set>
<c:set var="imgNumber" value="${board.pits[j-1].stones}"
scope="page"></c:set>
<c:if test="${imgNumber>24}">
<c:set var="imgNumber" value="24" />
</c:if>
<td class="player1"><a href="<c:url value="/kalah/go/${j}" />"><img
src="/images/stones/${imgNumber}.png" /></a>${board.pits[j-1].stones}</td>
</c:forEach>
<c:set var="imgNumber" value="${board.player2.store.stones}"
scope="page"></c:set>
<c:if test="${imgNumber>24}">
<c:set var="imgNumber" value="24" />
</c:if>
<td rowspan=2 class="player2">Player 2<img class="store"
src="<c:url value="/images/stones/${imgNumber}.png"/>" />${board.player2.store.stones}
</td>
</tr>
<tr>
<c:forEach begin="1" end="${board.configHouses}" var="i" step="1">
<c:set var="j" value="${board.configHouses+1+i}" scope="page"></c:set>
<c:set var="imgNumber" value="${board.pits[j-1].stones}"
scope="page"></c:set>
<c:if test="${imgNumber>24}">
<c:set var="imgNumber" value="24" />
</c:if>
<td class="player2"><a href="<c:url value="/kalah/go/${j}" />"><img
src="/images/stones/${imgNumber}.png" /></a>${board.pits[j-1].stones}</td>
</c:forEach>
</tr>
</table>
<h2>
Current player is ${board.currentPlayer}
</h2>
<div class="messages">
<c:forEach var="message" items="${board.messages}">
<p>${message}</p>
</c:forEach>
</div>
<h3>Rules:</h3>
<OL>
<LI>The game provides a Kalah board and a number of seeds or
counters. The board has 12 small pits, called houses, on each side;
and a big pit, called an end zone, at each end. The object of the
game is to capture more seeds than one's opponent.
<LI>At the beginning of the game, four seeds are placed in each
house. This is the traditional method.
<LI>Each player controls the six houses and their seeds on the
player's side of the board. The player's score is the number of seeds
in the store to their right.
<LI>Players take turns sowing their seeds. On a turn, the player
removes all seeds from one of the houses under their control. Moving
counter-clockwise, the player drops one seed in each house in turn,
including the player's own store but not their opponent's.
<LI>If the last sown seed lands in an empty house owned by the
player, and the opposite house contains seeds, both the last seed and
the opposite seeds are captured and placed into the player's store.
<LI>If the last sown seed lands in the player's store, the player
gets an additional move. There is no limit on the number of moves a
player can make in their turn.
<LI>When one player no longer has any seeds in any of their
houses, the game ends. The other player moves all remaining seeds to
their store, and the player with the most seeds in their store wins.
</OL>
It is possible for the game to end in a draw.
</body>
</html>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.