Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 tile ; // Constructs a NumberTile object using 4 random integers in the // range 1 to 9 public NumberTile() { tile = new ArrayList(); Random numbers = new Random(); int topNum = numbers.nextInt(9) + 1; int bottomNum = numbers.nextInt(9) + 1; int leftNum = numbers.nextInt(9) + 1; int rightNum = numbers.nextInt(9) + 1; tile.add(0, leftNum); tile.add(1, topNum); tile.add(2, rightNum); tile.add(3, bottomNum); } // Rotate the tile 90 degrees public void rotate() { // storing leftNum in temp variable so it doesn't get lost. int temp = tile.get(0); tile.set(0, tile.get(1)); tile.set(1, tile.get(2)); tile.set(2, tile.get(3)); tile.set(3, temp); } public int getLeft() { // Do not modify this method return tile.get(0) ; } public int getRight() { // Do not modify this method return tile.get(2) ; } // returns the tile as a string in the form // 4 // 5 7 // 1 public String toString() { String out = " " + " " + tile.get(1) + " " + " " + tile.get(0) + " " + tile.get(2) + " " + " " + tile.get(3) + " " + " "; return out; } } // end of NumberTile class

import java.util.ArrayList ;

public class TileGame { NumberTile tile = new NumberTile(); public ArrayList board ; // Creates an empty board public TileGame() { board = new ArrayList<>(); // board.add(0, tile); }

// Accessor for the board public ArrayList getBoard() { // Do not modify this method //Instead of returning array, return whats inside. return board ; } // Creates and returns a hand of 5 random number tiles public ArrayList getHand() { ArrayList hand = new ArrayList() ; NumberTile tileOne = new NumberTile(); NumberTile tileTwo = new NumberTile(); NumberTile tileThree = new NumberTile(); NumberTile tileFour = new NumberTile(); NumberTile tileFive = new NumberTile(); hand.add(0, tileOne); hand.add(1, tileTwo); hand.add(2, tileThree); hand.add(3, tileFour); hand.add(4, tileFive); 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) { System.out.println(board.get(0)); int firstTile = board.get(0).getLeft(); int lastTile = board.get(board.size() - 1).getRight();

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 hand) { boolean doesFit = true;

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 hand1 = game.getHand(); ArrayList hand2 = game.getHand();

//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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases On The Web Designing And Programming For Network Access

Authors: Patricia Ju

1st Edition

1558515100, 978-1558515109

More Books

Students also viewed these Databases questions