Question
Basic logic of the tile game. Java (ArrayList) Number tiles are squares with a number from 1 to 9 on each side. A board for
Basic logic of the tile game. Java (ArrayList)
Number tiles are squares with a number from 1 to 9 on each side. A board for the game is a sequence of number tiles t0..tk-1 satisfying this property:
The right side number of ti = the left side number of ti+1 for i = 0..k-2
There are 2 players in the game. Each player starts with a "hand" of 5 number tiles. A move is made by a player removing a number tile from the players hand and placing it on the board (if possible) making sure the above property is satisfied. This means a tile can be placed on the board if
the right side of the tile = the left side of the first tile t0
the left side of the tile = the right side of the last tile tk-1
the right side of ti = the left side of the tile and the left side of ti+1 = the right side of the tile for i = 0..k-2
A number tile in the players hand can be rotated but once placed on the board its position is fixed. If a move cannot be made then one tile is added to the player's hand and the other player gets to move. A turn 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.
My problem: It seems i have a problem populating the board. Iv'e managed to print both hands then my program crashes. Any help would be great!
Current Code:
import java.util.ArrayList ; import java.util.Random ;
public class NumberTile { private ArrayList
import java.util.ArrayList ;
public class TileGame { NumberTile tile = new NumberTile(); public ArrayList
// Accessor for the board public ArrayList
if(firstTile == currentTile.getRight()) { return 0; } else if (lastTile == currentTile.getLeft()) { return board.size() - 1; } else { 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) { //call get index for fit int canInsert = getIndexForFit(currentTile); boolean canTileInsert = false; //if true, modify index if(canInsert == -1) { //rotate for (int r = 0; r < 3; r++) { currentTile.rotate(); int didRotationWork = getIndexForFit(currentTile);
if (didRotationWork == -1) { continue; }
else if (didRotationWork != -1) { canTileInsert = true; }
}
return false; }
else if(canInsert != -1) { return true; } return canTileInsert; } // 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 void makeMove(ArrayList
for (int i = 0; i < hand.size(); i++) { //call caninterserttile doesFit = canInsertTile(hand.get(i));
if(doesFit) { int index = getIndexForFit(hand.get(i)); board.add(index, hand.get(i)); hand.remove(i); break; } if (!doesFit) { hand.add(hand.size() -1, new NumberTile()); }
} }
/** * Get the board as a String * @return the board as a multi-line String */ public String toString() { // Do not modify this method return board.toString() ; // ArrayList as a String } } // end of TileGame class
public class GameTester // WHERE THE GAME RUNS { public static void main(String args[]) { TileGame game = new TileGame(); boolean winner = false;
//get two hands ArrayList
//create an empty board System.out.println(hand1); System.out.println(hand2); do { //make moves game.makeMove(hand1); game.makeMove(hand2); } while(!hand1.isEmpty() && !hand2.isEmpty());
if (hand1.isEmpty() && hand2.isEmpty()) { System.out.println("It is a tie!"); } else if (hand1.isEmpty()) { System.out.println("Player 1 won!"); } else if (hand2.isEmpty()) { System.out.println("Player 2 won!"); } System.out.println(game.toString()); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started