Find the bugs in this Java TileGame and make it run. Use Arrays. DO NOT USE ARRA
ID: 3563176 • Letter: F
Question
Find the bugs in this Java TileGame and make it run. Use Arrays. DO NOT USE ARRAYLIST. I have already work in the tree classes. it prints hand 1, hand 2 and board. but it is not playing.
Main Class
--------------------------------------------------------------------------------------------------------------------------------------------------
public class TileTester {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
//Create game
TileGame game = new TileGame();
//Creates board.
NumberTile board = new NumberTile();
//Create two hands
NumberTile [] hand1 = game.getHand();
NumberTile [] hand2 = game.getHand();
// Creates String for playGame called yes.
String playGame = "yes";
while (playGame.equals("yes")) {
System.out.println("Game Starts------------------ ");
//print first hand
System.out.println("Initial hand1");
_print_hand(hand1);
//print second hand
System.out.println("Initial hand2");
_print_hand(hand2);
// Creates initial tile for the board.
System.out.println("Initial board tile");
System.out.println(board);
//play hands
while (true) {
//play turn
game.makeMove(hand1);
game.makeMove(hand2);
if (hand1 != null && hand1.equals(0)) {
break;
}
if (hand2 != null && hand1.equals(0)) {
break;
}
}
//game loop
//print final board
System.out.println("Final board");
System.out.println(game.toString());
//print first hand
System.out.println("Final hand1");
_print_hand(hand1);
//print second hand
System.out.println("Final hand2");
_print_hand(hand2);
if (hand1 != null && hand1.equals(0) && hand2 != null && hand1.equals(0)) {
System.out.println("GAME WAS A TIE************ ");
} else if (hand1 != null && hand1.equals(0)) {
System.out.println("hand1 WINS********* ");
} else {
System.out.println("hand2 WINS********** ");
}
System.out.println("Game Ends------------------ ");
//get user input zero for stop, 1 for play again
System.out.println("Want to play again? yes/no");
Scanner input = new Scanner(System.in);
playGame = input.next();
}
//user input loop
}
public static void _print_hand(NumberTile[] hand) {
if (hand != null && hand.equals(0)) {
System.out.println(" No tiles on this hand ");
return;
}
for (int i = 0; i < hand.length; i++) {
System.out.println(hand[i].toString());
}
}
}
//End of game.
TileGame class:
-------------------------------------------------------------------------------------------------------------------------------------------------------
public class TileGame {
private NumberTile [] board;
// Creates an empty board
public TileGame() {
board = new NumberTile [0];
}
// Accessor for the board
public NumberTile [] getBoard() {
// Do not modify this method
return board;
}
// Creates and returns a hand of 5 random number tiles
public NumberTile [] getHand() {
NumberTile [] hand = new NumberTile [5];
for (int i = 0; i < 5; i++) {
hand[i]=(new NumberTile());
}
return hand;
}
// If the current tile fits in the board (without rotating) then
// return the index i of a tile in the board so that the current tile
// fits before ti for i = 0..k-1, or return k if the current tile fits
// after the last tile. If the tile does not fit, return -1
public int getIndexForFit(NumberTile currentTile) {
//board is empty this is the first tile
if (board.length == 0) {
return 0;
}
//board now is not empty tile fits before 0
if (currentTile.getRight() == board [0].getLeft()) {
return 0;
}
//board now is not empty tile fits after last
if (currentTile.getLeft() == board[(board.length-1)].getRight()) {
return board.length;
}
//board now has at least two tiles. as two previous conditions will
//fit a tile after or before the firts (or not at all)
//lets try to fit the tile in the middle
for (int i = 0; i < board.length; i++) {
if (currentTile.getLeft() == board [i].getRight()
&& currentTile.getRight() == board[(i + 1)].getLeft()) {
return i+1;
}
}
return -1;
}
// Call the method getIndexForFit to see whether a tile can be inserted
// into the board. In this method the tile can be rotated. If the tile
// can be inserted, return true. If the tile does not fit after
// rotating (at most 3 times), return false.
public boolean canInsertTile(NumberTile currentTile) {
for (int i = 0; i < 4; i++) {
//rotate tile
if (getIndexForFit(currentTile) != -1) {
return true;
}
currentTile.rotate();
}
return false;
}
// Make a move. I.e. if a tile in the hand fits on the board
// then remove it from the hand and place it in the board. If no tile
// from the hand fits, then add another tile to the hand
public NumberTile[] makeMove(NumberTile[] hand) {
// hand cannot be empty playing game
for (int i = 0; i < hand.length; i++) {
if (canInsertTile(hand[i])) {
NumberTile[] newHand = new NumberTile[hand.length - 1];
for (int j = 0; j < i; j++) {
newHand[j] = hand[j];
}// end for
for (int j = i + 1; j < hand.length; j++) {
newHand[j - 1] = hand[j];
}// end for
return newHand;
}// end if
}// end for
NumberTile[] newHand = new NumberTile[hand.length + 1];
for (int i = 0; i < hand.length; i++) {
newHand[i] = hand[i];
}// end for
newHand[hand.length] = new NumberTile();
return newHand;
}
/**
* Get the board as a String
*
* @return the board as a multi-line String
*/
@Override
public String toString() {
return board.toString(); // note: calls inherited toString method
}
} // end of TileGame class
NumberTile Class:
------------------------------------------------------------------------------------------------------------------------------------------------
import java.util.Random;
public class NumberTile {
//private final ArrayList<Integer> tile;
private final int[] tile;
// Constructs a NumberTile object using 4 random integers in the
// range 1 to 9
public NumberTile() {
Random rand = new Random(System.nanoTime());
//tile = new ArrayList<Integer>(4);
tile = new int[4];
for (int i = 0; i < 4; i++) {
// tile.add(rand.nextInt(8) + 1);
tile[i]= rand.nextInt(8) + 1;
//we need to delay here to allow time to pass
//otherwise no random numbers
for(int delay=0; delay<1000; delay++);
}
}
// Rotate the tile 90 degrees
public void rotate() {
//Integer tmp = tile.get(0);
int tmp = tile[0];
tile[0]= tile[1];
tile[1] = tile[2];
tile[2]= tile[3];
tile[3]= tmp;
}
public int getLeft() {
// Do not modify this method
return tile[0];//tile.get(0);
}
public int getRight() {
// Do not modify this method
return tile[2]; //tile.get(2);
}
// returns the tile as a string in the form
// 4
// 5 7
// 1
@Override
public String toString() {
String out = " " + tile[3] + " "
+ tile[0] + " " + tile[2] + " "
+ " " + + tile[1] + " ";
return out;
}
} // end of NumberTile class
Explanation / Answer
import java.awt.Canvas; import java.awt.Dimension; import java.awt.Graphics; import java.awt.image.BufferStrategy; import java.awt.image.BufferedImage; public class Game extends Canvas implements Runnable{ private void renderTiles(Graphics g) { // the overlay variable determines if I need to render anything off screen // (True if player is moving) int overlay = 0; if (moving) { overlay = 1; } // the four variables below determine the starting/finishing x/y variables // to begin rendering int startX = -((xOffset/(Tile.TILESIZE*SCALE))+overlay); int finishX = -(((xOffset-(WIDTH*SCALE))/(Tile.TILESIZE*SCALE))-overlay); int startY = -((yOffset/(Tile.TILESIZE*SCALE))+overlay); int finishY = -(((yOffset-(HEIGHT*SCALE))/(Tile.TILESIZE*SCALE))-overlay); // to avoid crashes, the variables can't render what isn't there in the list if (startX < 0) { startX = 0; } if (finishX > map.getMapWidth()) { finishX = map.getMapWidth(); } if (startY < 0) { startY = 0; } if (finishY > map.getMapHeight()) { finishY = map.getMapHeight(); } // where the tiles are render. tiles are stored in an ArrayList so that // switching between maps is easier for (int y = startY; yRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.