1.Write a Java program that plays a mathematical game called “Taking Stones” whi
ID: 3805652 • Letter: 1
Question
1.Write a Java program that plays a mathematical game called “Taking Stones” which is based on the Chinese game of Tsyan-shiz. In this game, the person who runs your program will be the human player and your program will be a simple Artificial Intelligence (AI) that will serve as the other player, as well as provide the narrative for the game and keep score. Your program must keep score impartially while participating in the game, but this is not a difficult task for the computer.
2.The rules of the game are:
a.The human player and the computer will roll a (virtual) pair of dice at the beginning of each game to see who goes first.
b.The player who goes first provides the number of stones that will be in the pile. The number must be between 10 and 50.
c.Each player then removes some number (between 1 and 3) of stones from the pile until one player removes the final stone.
d.The player who goes first:
i.Provides the number of stones to be placed in the pile,
ii.Removes the first set of 1 to 3 stones
e.Other player removes a set of 1 to 3 stones
f.The players then take turns(iteration) until the final stone is removed.
g.The play who removes the final stone is the winner
3.The program must perform the following: (see example output appendix in this document for clarity)
a.Use the System I/O (print(), println(), or (better) printf()) to introduce and describe the game, introduce itself as the AI player, and prompt the human player (the person running your program) for his or her name.
b.The program must use a Scanner object to receive the inputted response.
c.The response must be assigned to a properly typed variable.
d.Uses printf() to display a greeting to the human player that incorporates the name provided in response to the prompt: Welcome <user’s name> to the game of Taking Stones.
e.The user’s name must be output in proper name case (i.e., mixed case starting with upper case for the first letter) regardless of the case the user types in.
f.The program will then “roll” a virtual pair of dice twice,
i.first for the human player
ii.secondly for itself as the AI player,
iii.whichever player’s roll returns the largest number goes first.
g.The player who wins the roll and goes first will choose the number of stones (10 – 50) to be placed in the pile,
h.Game play
i.Player chooses a number (1 – 3) which remove the first set of stones from the pile.
ii.The program must reject invalid entries for these numbers by
1. inform the human player that the number is invalid
2.remind the player of the constraints,
3.prompt the player to re-enter an appropriate number.
iii.AI Player
1.Uses randome number generation to select such a number.
2.Java has two ways to generate random numbers
a.Math.random method from the Java API Math
b.java.util.random Class and the appropriate method from that class, to generate this number for the AI player (as well as for the roll of the pair of dice).
i.When one of the players has won a game, the program must output a congratulatory message naming the winner.
j.After a game has ended the program must ask
One more game? (Y/N):
The user answers with an uppercase or lowercase y or n respectively. Any other input is rejected. If the answer is Y or y, the program will return to step d above and once again initiate the two roles of the dice to determine which player goes first. If the answer is N or n, the program terminates.
4.Include Javadoc class comment following Project Comment Template on the content page.
5.The project requires one files to be turned in - Nimatron.java (the source code of your “Taking Stones” Game program)
6.Be sure to follow the Programming Project Submission Instructions posted by your instructor on the elearning site. The submission requirements are part of the grading for this assignment.
Explanation / Answer
package com;
import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Main {
public static final String TITLE = "";
public static final int BORDER_SIZE = 25;
public static void main(String[] args) {
new Main().init();
}
private void init() {
JFrame f = new JFrame();
f.setTitle(TITLE);
JPanel container = new JPanel();
container.setBackground(Color.GRAY);
container.setLayout(new BorderLayout());
f.add(container);
container.setBorder(BorderFactory.createEmptyBorder(BORDER_SIZE, BORDER_SIZE, BORDER_SIZE, BORDER_SIZE));
GBoard board = new GBoard();
container.add(board);
f.pack();
f.setResizable(false);
f.setLocationByPlatform(true);
f.setVisible(true);
}}
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JPanel;
public class GBoard extends JPanel
{
private static final long serialVersionUID = -494530433694385328L;
public static final int SIZE = 9;
public static final int N_OF_TILES = SIZE - 1;
public static final int TILE_SIZE = 40;
public static final int BORDER_SIZE = TILE_SIZE;
public enum State {
BLACK, WHITE
}
private State current_player;
private Grid grid;
private Point lastMove;
public GBoard() {
this.setBackground(Color.ORANGE);
grid = new Grid(SIZE);
current_player = State.BLACK;
this.addMouseListener(new MouseAdapter() {
@Override
public void mouseReleased(MouseEvent e) {
int row = Math.round((float) (e.getY() - BORDER_SIZE)
/ TILE_SIZE);
int col = Math.round((float) (e.getX() - BORDER_SIZE)
/ TILE_SIZE);
if (row >= SIZE || col >= SIZE || row < 0 || col < 0) {
return;
}
if (grid.isOccupied(row, col)) {
return;
}
grid.addStone(row, col, current_player);
lastMove = new Point(col, row);
if (current_player == State.BLACK) {
current_player = State.WHITE;
} else {
current_player = State.BLACK;
}
repaint();
}
});
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setColor(Color.BLACK);
for (int i = 0; i < SIZE; i++) {
g2.drawLine(BORDER_SIZE, i * TILE_SIZE + BORDER_SIZE, TILE_SIZE
* N_OF_TILES + BORDER_SIZE, i * TILE_SIZE + BORDER_SIZE);
}
for (int i = 0; i < SIZE; i++) {
g2.drawLine(i * TILE_SIZE + BORDER_SIZE, BORDER_SIZE, i * TILE_SIZE
+ BORDER_SIZE, TILE_SIZE * N_OF_TILES + BORDER_SIZE);
}
for (int row = 0; row < SIZE; row++) {
for (int col = 0; col < SIZE; col++) {
State state = grid.getState(row, col);
if (state != null) {
if (state == State.BLACK) {
g2.setColor(Color.BLACK);
} else {
g2.setColor(Color.WHITE);
}
g2.fillOval(col * TILE_SIZE + BORDER_SIZE - TILE_SIZE / 2,
row * TILE_SIZE + BORDER_SIZE - TILE_SIZE / 2,
TILE_SIZE, TILE_SIZE);
}
}
}
if (lastMove != null) {
g2.setColor(Color.RED);
g2.drawOval(lastMove.x * TILE_SIZE + BORDER_SIZE - TILE_SIZE / 2,
lastMove.y * TILE_SIZE + BORDER_SIZE - TILE_SIZE / 2,
TILE_SIZE, TILE_SIZE);
}
}
@Override
public Dimension getPreferredSize() {
return new Dimension(N_OF_TILES * TILE_SIZE + BORDER_SIZE * 2,
N_OF_TILES * TILE_SIZE + BORDER_SIZE * 2);
}
}
import com.GBoard.State;
public class Grid {
private final int SIZE;
private Stone[][] stones;
public Grid(int size) {
SIZE = size;
stones = new Stone[SIZE][SIZE];
}
*/
public void addStone(int row, int col, State state) {
Stone newStone = new Stone(row, col, state);
stones[row][col] = newStone;
Stone[] neighbors = new Stone[4];
if (row > 0) {
neighbors[0] = stones[row - 1][col];
}
if (row < SIZE - 1) {
neighbors[1] = stones[row + 1][col];
}
if (col > 1) {
neighbors[2] = stones[row][col - 1];
}
if (col < SIZE - 1) {
neighbors[3] = stones[row][col + 1];
}
Chain finalChain = new Chain(newStone.state);
for (Stone neighbor : neighbors) {
if (neighbor == null) {
continue;
}
newStone.liberties--;
neighbor.liberties--;
if (neighbor.state != newStone.state) {
checkStone(neighbor);
continue;
}
if (neighbor.chain != null) {
finalChain.joinc(neighbor.chain);
}
}
finalChain.addStone(newStone);
}
public void checkStone(Stone stone) {
if (stone.chain.getLiberties() == 0) {
for (Stone s : stone.chain.stones) {
s.chain = null;
stones[s.row][s.col] = null;
}
}
}
public boolean isOccupied(int row, int col) {
return stones[row][col] != null;
}
public State getState(int row, int col) {
Stone stone = stones[row][col];
if (stone == null) {
return null;
} else {
return stone.state;
}
}
}
import com.GBoard.State;
import java.util.ArrayList;
public class Chain {
public ArrayList<Stone> stones;
public State state;
public Chain(State state) {
stones = new ArrayList<>();
}
public int getLiberties() {
int total = 0;
for (Stone stone : stones) {
total += stone.liberties;
}
return total;
}
public void addStone(Stone stone) {
stone.chain = this;
stones.add(stone);
}
public void joinc(Chain chain) {
for (Stone stone : chain.stones) {
addStone(stone);
}
}
}
package com;
import com.GBoard.State;
public class Stone {
public Chain chain;
public State state;
public int liberties;
public int row;
public int col;
public Stone(int row, int col, State state) {
chain = null;
this.state = state;
liberties = 4;
this.row = row;
this.col = col;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.