Modify the StonesGame to use a two-dimensional grid of stones, capturing stones
ID: 3680975 • Letter: M
Question
Modify the StonesGame to use a two-dimensional grid of stones, capturing stones in all directions: horizontal, vertical, and diagonal. The original StonesGame class is provided as a reference. A stub class for the Stones2DGame is provided as well, and you should complete the stub methods so that the main provided works correctly.
import java.util.Scanner;
class StonesGame
{
private char [] board;
private int maxStones;
public StonesGame()
{
this(10);
}
public StonesGame(int maxStones)
{
this.maxStones = maxStones;
board = new char [maxStones];
for (int curElem = 0; curElem < maxStones; curElem++)
board[curElem] = ' ';
}
public int getMaxStones()
{
return maxStones;
}
public char placeStone(int position, char player)
{
if (position < 0 || position >= maxStones)
{
System.out.println("That is not a valid move");
return player;
}
else if (board[position] == ' ')
{
board[position] = player;
char otherPlayer = 'X';
if (player == 'X')
otherPlayer = 'O';
for (int direction = -1; direction < 2; direction += 2)
{
int curElem = position + direction;
while (curElem < maxStones - 1 &&
curElem >= 1 &&
board[curElem] == otherPlayer)
curElem = curElem + direction;
if (curElem < maxStones &&
curElem >= 0 && board[curElem] == player)
{
int borderElem = curElem;
for (curElem = position + direction;
curElem != borderElem; curElem += direction)
board[curElem] = ' ';
}
}
return otherPlayer;
}
else
{
System.out.println("That spot is already occupied.");
return player;
}
}
public boolean gameFull()
{
int curElem = 0;
while (curElem < maxStones && board[curElem] != ' ')
curElem++;
return curElem == maxStones;
}
public int count(char player)
{
int count = 0;
for (int curElem = 0; curElem < maxStones; curElem++)
if (board[curElem] == player)
count++;
return count;
}
public void display()
{
for (int curElem = 0; curElem < maxStones; curElem++)
System.out.print(String.format("%2d ", curElem));
System.out.println();
for (int curElem = 0; curElem < maxStones; curElem++)
System.out.print(" " + board[curElem] + " ");
System.out.println();
}
public static void main(String [] args)
{
Scanner kbd = new Scanner(System.in);
StonesGame trialGame = new StonesGame();
char player = 'X';
while(!trialGame.gameFull())
{
System.out.print("Enter a position (from 0 to " +
(trialGame.getMaxStones() - 1) + ") for an " + player +
" stone: ");
int move = kbd.nextInt();
player = trialGame.placeStone(move, player);
trialGame.display();
}
if (trialGame.count('X') > trialGame.count('O'))
System.out.println("X wins!");
else if (trialGame.count('O') > trialGame.count('X'))
System.out.println("O wins!");
else
System.out.println("The game is a tie.");
}
}
import java.util.Scanner;
class Stones2DGame
{
private char [][] board;
private int maxStonesAcross;
private int maxStonesDown;
public StonesGame()
{
this(10, 10);
}
public StonesGame(int maxStonesAcross, int maxStonesDown)
{
// Complete this constructor
}
public int getMaxStonesAcross()
{
// Complete this method
}
public int getMaxStonesDown()
{
// Complete this method
}
public char placeStone(int row, int col, char player)
{
// Complete this method
}
public boolean gameFull()
{
// Complete this method
}
public int count(char player)
{
// Complete this method
}
public void display()
{
// Complete this method
}
public static void main(String [] args)
{
Scanner kbd = new Scanner(System.in);
StonesGame trialGame = new StonesGame();
char player = 'X';
trialGame.display();
while(!trialGame.gameFull())
{
System.out.print("Enter a row (from 0 to " +
(trialGame.getMaxStonesDown() - 1) + ") for an " + player +
" stone: ");
int moveRow = kbd.nextInt();
System.out.print("Enter a column (from 0 to " +
(trialGame.getMaxStonesAcross() - 1) + ") for an " + player +
" stone: ");
int moveCol = kbd.nextInt();
player = trialGame.placeStone(moveRow, MoveCol, player);
trialGame.display();
}
if (trialGame.count('X') > trialGame.count('O'))
System.out.println("X wins!");
else if (trialGame.count('O') > trialGame.count('X'))
System.out.println("O wins!");
else
System.out.println("The game is a tie.");
}
}
Explanation / Answer
Please try this..well working code..
import java.util.Scanner;
import java.util.Random;
public class Stones
{
static int HUMAN = 0;
static int COMPUTER = 1;
static int STONES =13;
static Scanner keyboard = new Scanner(System.in);
static int choice = 0;
static int left = 0;
static Random generator = new Random ();
public static void main (String[]args)
{
ShowInstructions();
String prompt = "Would you like to play 13 Stones (1 for yes, 2 for no)?";
while (Again(prompt))
{
int goesFirst = GetCoinToss ();
PlayGame (goesFirst);
SwitchPlayer (goesFirst);
if (goesFirst == HUMAN)
{
DoComputerTurn(left);
}
else
{
dhTurn(left);
}
}
}
public static void ShowInstructions ()
{
System.out.println ("Welcome to the 13 Stones game. I hope you are ready to play against me. There is 13 stones in front of you. The first player is to remove either 1, 2, or 3 stones. Then the next player will remove either 1, 2, or 3 stones. The person to pick up the last stone losses. Good luck!");
}
public static boolean Again (String prompt)
{
System.out.println (prompt);
choice = keyboard.nextInt();
return (choice == 1);
}
public static int GetCoinToss()
{
final int heads = 0;
final int tails = 1;
System.out.println ("Call the coin toss...");
String enter = new String (keyboard.nextLine());
String call = new String (keyboard.nextLine ());
int toss = generator.nextInt(2);
if (((call.equalsIgnoreCase("Heads")) && (toss == heads)) || ((call.equalsIgnoreCase("Tails")) && (toss == tails)))
{
System.out.println ("You got lucky. So I guess you go first. Choose the number of stones you want.");
return HUMAN;
}
else
{
System.out.println ("Ha! You lost the coin toss! I go first.");
return COMPUTER;
}
}
public static int PlayGame(int whoseTurn)
{
if (whoseTurn == COMPUTER)
{
int compTakes = generator.nextInt(3) +1 ;
int left = STONES - compTakes;
System.out.println ("I took " + compTakes + " stones. There is " + left + " stones left. Your turn.");
return COMPUTER;
}
else
{
int humanTakes = keyboard.nextInt();
while ((humanTakes> 3) || (humanTakes<0))
{
System.out.println ("Trying to cheat, eh? Well, you can't! Try again.");
humanTakes = keyboard.nextInt ();
}
left = STONES - humanTakes;
System.out.println ("You took " + humanTakes + " stones. There is " + left + " stones left. My turn!");
return HUMAN;
}
}
public static int SwitchPlayer (int whoseTurn)
{
if ( whoseTurn == COMPUTER)
{
whoseTurn = HUMAN;
}
else
{
whoseTurn = COMPUTER;
}
return whoseTurn;
}
public static int dhTurn (int STONES)
{
int stonesPicked = 0;
System.out.println ("There is " + STONES + " stones left. Please enter the amount of stones you would like to take.");
stonesPicked = keyboard.nextInt();
while ((stonesPicked < 1) || (stonesPicked > 0))
{
System.out.println ("So you think you can cheat, eh? Well you can't! Enter the number you want to take again.");
stonesPicked = keyboard.nextInt();
}
int stonesRemaining = stonesPicked;
return stonesRemaining;
}
public static int DoComputerTurn (int STONES)
{
int compTakes = generator.nextInt(3) +1 ;
int left = STONES - compTakes;
System.out.println ("I took " + compTakes + " stones. There is " + left + " stones left. Your turn.");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.