Question: Hi, I need help with this assignment: ANSWER NEEDS TO USE THE SHELL PROGRAMS BELOW: THERE NEEDS TO BE 5 PROGRAMS, THE 4 SHELLS COMPLETED

Hi, I need help with this assignment:

ANSWER NEEDS TO USE THE SHELL PROGRAMS BELOW:

THERE NEEDS TO BE 5 PROGRAMS, THE 4 SHELLS COMPLETED PLUS THE TESTER PROGRAM

WE ARE IN BASIC JAVA SO WE NEED TO FOLLOW THE INSTRUCTIONS AND NOT ADVANCED TECHNIQUES

I ATTEMPTED TO SOLVE BUT WAS HAVING ISSUE SPECIALLY FOR the TileGame class

I include my programs at the very bottom, thanks

write a Java program to simulate playing a number tile game

Number tiles are squares with a number from 1 to 9 at each side. A board for the game is a sequence of number tiles satisfying this property:

The right side of each tile (except the last) = the left side of the next tile on the board

There are 2 players in the game, both of which are the computer. Each player starts with a "hand" of 5 number tiles. A move is made by a player removing tile from the hand and placing it on the board, if possible. A tile may be placed on the board in any of 3 ways:

As the new first tile

Between two adjacent tiles

As the new last tile

A number tile in the players hand can be rotated 90 degrees but once placed on the board its position is fixed

If none of the tiles in the hand can be placed on the board, then one random tile is added to the player's hand

A round is when player1 makes a move and then player2 makes a move (to make the game fair)

The game ends when all tiles are removed from one (or both) player's hand(s). If one player's hand is empty and the other player's hand still contains tiles, then the player with the empty hand is the winner. If both players hands are empty, then the game ends in a tie

III. Specifications

Your program will consist of 5 classes

The NumberTile class models a number tile

The Hand class models a players hand of number tiles

The Board class models the board where tiles are inserted from the hands

The TileGame class conducts the game

Test class with main method Use the skeletons of the NumberTile, Hand, Board, and TileGame classes, available online. Do not add or remove any methods, and do not modify any of the method declarations. Simply code the bodies of the method stubs where necessaryYour main method will do this and only this: Create a TileGame object, call the play() method, and then print the results returned by thetoString()method. After each game, ask whether the user wants to see another. To get credit, you must use the declared ArrayList in each class as your principal data structure. To get credit, all instance variables in every class must be declared private.

These are the classes:

NumberTile.java

import java.util.ArrayList ; /** * A NumberTile is a square tile with an int between 1 and 9, inclusive, on * each side */ public class NumberTile { private ArrayList tile ; /** * Create a NumberTile object using 4 random ints in the range 1..9 */ public NumberTile() { } /** * Rotate this tile 90 degrees clockwise */ public void rotate() { } /** * Get the number on the left side of this tile * @return the number on the left side of this tile */ public int getLeft() { // DO NOT MODIFY THIS METHOD! // ========================= return tile.get(0) ; } /** * Get the number on the right side of this tile * @return the number on the right side of this tile */ public int getRight() { // DO NOT MODIFY THIS METHOD! // ========================= return tile.get(2) ; } /** * Return a String representation of this tile in the form * * 4 * 5 7 * 1 * * @return the tile as a multi-line String */ public String toString() { // temporary return statement so skeleton will compile and run return null; } } // end of NumberTile class 

Hand.java

import java.util.ArrayList; /** * A Hand is a collection of NumberTiles. Tiles may be removed from * the Hand and new tiles added to it */ public class Hand { private ArrayList hand ; private static int HAND_SIZE = 5 ; // starting hand size /** * Create a new Hand of HAND_SIZE tiles */ public Hand() { } /** * Create a new Hand as an exact duplicate (i.e. a "deep copy") of another * @param toBeCopied the Hand to be copied */ public Hand(Hand toBeCopied) { } /** * Get the tile at a specified index in this Hand * @param index the index (position) of the tile to be returned * @return the NumberTile at the specified index */ public NumberTile get(int index) { // temporary return statement so skeleton will compile and run return null ; } /** * Get the size of this Hand * @return the number of tiles in the Hand */ public int getSize() { // temporary return statement so skeleton will compile and run return -999 ; } /** * Add a new tile to this Hand */ public void addTile() { } /** * Remove the tile at a specified index (position) from this Hand * @param index the index (position) of the tile to be removed * */ public void removeTile(int index) { } /** * Is this Hand empty? * @return true if this Hand is empty (contains no tiles); otherwise false * */ public boolean isEmpty() { // temporary return statement so skeleton will compile and run return true ; } /** * Get a String representation of this Hand * @return the NumberTiles in this Hand as a multi-line String */ public String toString() { // DO NOT MODIFY THIS METHOD! // ========================== return hand.toString() ; // call toString of ArrayList class } } 

Board.java

import java.util.ArrayList; /** * A Board is a collection of Number Tiles */ public class Board { private ArrayList board ; /** * Create a new Board with one NumberTile */ public Board() { } /** * Get the tile at a specified index on this Board * @param index the index (position) of the tile to be returned * @return the NumberTile at the specified index */ public NumberTile getTile (int index) { // temporary return statement so skeleton will compile and run return null; } /** * Get the size of this Board * @return the number of tiles on the Board */ public int getSize() { // temporary return statement so skeleton will compile and run return -999 ; } /** * Add a new tile to this Board at a specified index (position) * @param index the index (position) at which to insert the tile * @param tile the tile to be inserted */ public void addTile(int index, NumberTile tile) { } /** * Get a String representation of this Board * @return the NumberTiles on the Board as a multi-line String */ public String toString() { // DO NOT MODIFY THIS METHOD! // ========================== return board.toString() ; // call toString of ArrayList class } } 

TileGame.java

public class TileGame { // instance variable declarations go here // Create a Tilegame object public TileGame() { } // Play the game public void play() { } // If the current tile fits in the board, returns the index at which // it will be inserted. If the tile does not fit, returns -1 private int getIndexForFit(NumberTile tile) { // temporary return statement so skeleton will compile and run return -999; } // Make a move from a hand. If a tile in the hand fits on the board // then remove it from the hand and place it in the board. The tile may // be rotated up to 3 times. If no tile from the hand fits, then add // another tile to the hand private void makeMove(Hand hand) { } // Get the results of the game as a humongous multi-line String containing // both starting hands, the final board, both final hands, and a message // indicating the winner // HINT: call the toString methods of the Hand and Board classes public String toString() { // temporary return statement so skeleton will compile and run return "Hi Mom!" ; } } // end of TileGame class 

---------------------------------------------------------------------------------------------------------------

import java.util.ArrayList; import java.util.Random;

/** * A NumberTile is a square tile with an int between 1 and 9, inclusive, on each * side */ public class NumberTile {

private ArrayList tile;

/** * Create a NumberTile object using 4 random ints in the range 1..9 */ public NumberTile() { int tileCorners = 4; // number of corners our tile has tile = new ArrayList(); // creates a number tile object Random generator = new Random(); // creates a random object to generate // numbers // looping to fill up the tile(ArrayList) with random numbers from // 1 to 9 inclusive for (int i = 0; i < tileCorners; i++) { tile.add(generator.nextInt(9) + 1); } }

/** * Rotate this tile 90 degrees clockwise */ public void rotate() { // moves each tile value one index location clockwise int tempPos = tile.get(0); tile.set(0, tile.get(3)); tile.set(3, tile.get(2)); tile.set(2, tile.get(1)); tile.set(1, tempPos); }

/** * Get the number on the left side of this tile * * @return the number on the left side of this tile */ public int getLeft() { // DO NOT MODIFY THIS METHOD! // ========================= return tile.get(0); }

/** * Get the number on the right side of this tile * * @return the number on the right side of this tile */ public int getRight() { // DO NOT MODIFY THIS METHOD! // ========================= return tile.get(2); }

/** * Return a String representation of this tile in the form * * 4 * 5 7 * 1 * * @return the tile as a multi-line String */ public String toString() { return " " + tile.get(1) + " " + tile.get(0) + " " + tile.get(2) + " " + " " + tile.get(3) + " "; } } // end of NumberTile class

import java.util.ArrayList;

/** * A Hand is a collection of NumberTiles. Tiles may be removed from * the Hand and new tiles added to it */ public class Hand { private ArrayList hand ; private static int HAND_SIZE = 5 ; // starting hand size /** * Create a new Hand of HAND_SIZE tiles */ public Hand() { // creates a NumberTile hand object hand = new ArrayList();

// add 5 tiles to the hand for ( int i = 0; i < HAND_SIZE; i++) { hand.add(new NumberTile()); } } /** * Create a new Hand as an exact duplicate (i.e. a "deep copy") of another * @param toBeCopied the Hand to be copied */ public Hand(Hand toBeCopied) { this.hand = new ArrayList(toBeCopied.hand); } /** * Get the tile at a specified index in this Hand * @param index the index (position) of the tile to be returned * @return the NumberTile at the specified index */ public NumberTile get(int index) { return hand.get(index); } /** * Get the size of this Hand * @return the number of tiles in the Hand */ public int getSize() { return hand.size() ; } /** * Add a new tile to this Hand */ public void addTile() { hand.add(new NumberTile()); } /** * Remove the tile at a specified index (position) from this Hand * @param index the index (position) of the tile to be removed * */ public void removeTile(int index) { hand.remove(index); } /** * Is this Hand empty? * @return true if this Hand is empty (contains no tiles); otherwise false * */ public boolean isEmpty() { if (hand.isEmpty()) return true; else return false; } /** * Get a String representation of this Hand * @return the NumberTiles in this Hand as a multi-line String */ public String toString() { // DO NOT MODIFY THIS METHOD! // ========================== return hand.toString() ; // call toString of ArrayList class } }

import java.util.ArrayList;

/** * A Board is a collection of Number Tiles */ public class Board { private ArrayList board ; /** * Create a new Board with one NumberTile */ public Board() { board = new ArrayList(); } /** * Get the tile at a specified index on this Board * @param index the index (position) of the tile to be returned * @return the NumberTile at the specified index */ public NumberTile getTile (int index) { return board.get(index); } /** * Get the size of this Board * @return the number of tiles on the Board */ public int getSize() { return board.size(); } /** * Add a new tile to this Board at a specified index (position) * @param index the index (position) at which to insert the tile * @param tile the tile to be inserted */ public void addTile(int index, NumberTile tile) { board.add(index, tile); } /** * Get a String representation of this Board * @return the NumberTiles on the Board as a multi-line String */ public String toString() { // DO NOT MODIFY THIS METHOD! // ========================== return board.toString() ; // call toString of ArrayList class } }

import java.util.ArrayList;

public class TileGame {

String p1InitialHand; String p2InitialHand;

// instance variable declarations go here // to store both players hands private Hand player1; private Hand player2;

// to store the board private Board board;

// to store the turn for each round private int turn = 1;

// Create a Tilegame object public TileGame() {

player1 = new Hand();// creates a hand of 5 tiles for player1 p1InitialHand = "Player 1's Initial Hand: " + player1.toString(); player2 = new Hand();// creates a hand of 5 tiles for player2 p2InitialHand = "Player 2's Initial Hand: " + player2.toString(); board = new Board();// creates a new Board with one NumberTile }

// Play the game // The game ends when all tiles are removed from one // (or both) player's hand(s). public void play() { while (gameStatus() == -1) {

System.out.println(this + " ");

if (turn == 1) {

System.out.println("Turn: Player 1");

makeMove(player1);

} else {

System.out.println("Turn: Player 2");

makeMove(player2);

}

turn++;

if (turn == 3) {

turn = 1;

}

}

System.out.println(this + " ");

int status = gameStatus();

if (status == 0) {

System.out.println(" Its a Tie!!!");

} else if (status == 1) {

System.out.println(" Player1 won!!!");

} else if (status == 2) {

System.out.println(" Player2 won!!!");

}

System.out.println();

}

// If the current tile fits in the board, returns the index at which // it will be inserted. If the tile does not fit, returns -1 private int getIndexForFit(NumberTile tile) { int size = board.getSize();

for (int i = 0; i < size; i++) {

if (i == 0) {

if (tile.getRight() == board.getTile(0).getLeft()) {

return i;

}

}

if (i == size - 1) {

if (tile.getLeft() == board.getTile(size - 1).getRight()) {

return size;

}

} else if (tile.getLeft() == board.getTile(i).getRight() && tile.getRight() == board.getTile(i + 1).getLeft()) {

return i;

}

}

return -1; }

// Make a move from a hand. If a tile in the hand fits on the board // then remove it from the hand and place it in the board. The tile may // be rotated up to 3 times. If no tile from the hand fits, then add // another tile to the hand private void makeMove(Hand hand) {

for (int i = 0; i < hand.getSize(); i++) {

NumberTile tile = hand.get(i);

int index = getIndexForFit(tile);

int count = 0;

while (count < 3) {

if (index != -1) {

board.addTile(index, tile);

hand.removeTile(i);

return;

}

tile.rotate();

index = getIndexForFit(tile);

count++;

}

}

// add a tile if it doesn't fit hand.addTile();

}

/*

* Returns 0 on tie, 1 for player 1 s winner, 2 for player 2 as winner

* -1 for no results till now

*/ int gameStatus() {

if (player1.getSize() == 0 && player2.getSize() == 0) {

return 0; // tie

} else if (player1.getSize() == 0) {

return 1;

} else if (player2.getSize() == 0) {

return 2;

} else {

return -1;

}

}

// Get the results of the game as a humongous multi-line String containing // both starting hands, the final board, both final hands, and a message // indicating the winner // HINT: call the toString methods of the Hand and Board classes public String toString() {

return "Player 1's Final Hand: " + player1.toString() + " " + " Player 2's Final Hand: " + player2.toString() + " " + " Final Board: " + board.toString() + " " + "Player 1's Initial Hand: " + p1InitialHand + " " + "Player 2's Initial Hand: " + p2InitialHand;

} } // end of TileGame class

public class TestTileGame {

/** * @param args the command line arguments */ public static void main(String[] args) { TileGame tileGame = new TileGame();

tileGame.play(); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!