Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Below is the code that I need to convert in JAVAFx import java.util.Random; public class Tiles { private static final int TILE = 28; private

Below is the code that I need to convert in JAVAFx

import java.util.Random;

public class Tiles { private static final int TILE = 28; private static String[ ] tiles = new String[TILE];

//function to set 28 different tiles public static void setTiles(){ tiles[0] = "yw"; tiles[1] = "sb"; tiles[2] = "ob"; tiles[3] = "yo"; tiles[4] = "sr"; tiles[5] = "bg"; tiles[6] = "ww"; tiles[7] = "gg"; tiles[8] = "wg"; tiles[9] = "yy"; tiles[10] = "rb"; tiles[11] = "yg"; tiles[12] = "sg"; tiles[13] = "ss"; tiles[14] = "yr"; tiles[15] = "ys"; tiles[16] = "or"; tiles[17] = "bb"; tiles[18] = "os"; tiles[19] = "ow"; tiles[20] = "rg"; tiles[21] = "og"; tiles[22] = "wb"; tiles[23] = "yb"; tiles[24] = "sw"; tiles[25] = "rr"; tiles[26] = "rw"; tiles[27] = "oo"; }

//function that randonly splits tiles and return an array of 14 tiles public static String[ ] splitTiles() { String[ ] tile = new String[tiles.length / 2]; for (int i = 0; i < tile.length; i++) { tile[i] = ""; } int filled = 0; Random rand = new Random(); while (tile[tile.length - 1].equals("")) { int k = rand.nextInt(tiles.length); if (!tiles[k].equals("")) { tile[filled] = tiles[k]; tiles[k] = ""; filled++; } } return tile; } }

import java.util.Scanner;

public class Players { private String[ ] hand; private boolean canPlay;

//constructor public Players() { canPlay = true; }

//funciton that handles players move public void move(Board board, Scanner sc) { boolean isValid = false; while (!isValid) { System.out.println("Which tile would you like to play? Enter stop if you cannot place a tile"); String a = sc.nextLine(); if (a.equalsIgnoreCase("stop")) { this.canPlay = false; isValid = true; } else { int b = Integer.parseInt(a) - 1; String tileFromHand = this.hand[b]; System.out.println("Where would you like to place the tile?"); String location = sc.nextLine();

String row; int col; if (Character.isDigit(location.charAt(0))) { col = Integer.parseInt(location.substring(0, 1)); row = location.substring(1, location.length()); } else { col = Integer.parseInt(location.substring(1, location.length())); row = location.substring(0, 1); }

isValid = board.placeTile(tileFromHand, row, col); if (isValid) { this.hand[b] = " "; } } } }

//prints a list of tiles for each players public void showHand() { for (int i = 0; i < this.hand.length; i++) { System.out.print(i + 1 + " "); } System.out.println(); for (int i = 0; i < this.hand.length; i++) { System.out.print(this.hand[i] + " "); } System.out.println(" "); }

//get half of the tiles for a player public void getHand() { this.hand = Tiles.splitTiles(); }

public boolean getStatus() { return this.canPlay; }

}

//Board class public class Board { private final int ROW = 11; //Number of row needed to make the board and to keep track of the move

private final int COLUMN = 11; //Number of coloumn needed to make the board and to keep track of the move private final String[ ][ ] theBoard; // Matrix to create a Board private final String[ ]side; //Keep track of the position array private final int[ ]top; //Keep track of the position array

//constructor that inialize matrix and arrays to keep track of the positions of the move public Board () { this.top = new int[COLUMN]; this.side = new String[ROW]; this.theBoard= new String[ROW][COLUMN];

//array to keep track of number of rows side[0] = "a"; side[1] = "b"; side[2] = "c"; side[3] = "d"; side[4] = "e"; side[5] = "f"; side[6] = "g"; side[7] = "h"; side[8] = "i"; side[9] = "j"; side[10] = "k";

//array to keep track of number of columns top[0] = 1; top[1] = 2; top[2] = 3; top[3] = 4; top[4] = 5; top[5] = 6; top[6] = 7; top[7] = 8; top[8] = 9; top[9] = 10; top[10] = 11;

//game board initialization theBoard[0][0] = " "; theBoard[0][1] = " "; theBoard[0][2] = " "; theBoard[0][3] = " "; theBoard[0][4] = " "; theBoard[0][5] = "r"; theBoard[0][6] = " "; theBoard[0][7] = " "; theBoard[0][8] = " "; theBoard[0][10] = " ";

theBoard[1][0] = " "; theBoard[1][1] = " "; theBoard[1][2] = " "; theBoard[1][3] = " "; theBoard[1][4] = "o"; theBoard[1][5] = "b"; theBoard[1][6] = "y"; theBoard[1][7] = " "; theBoard[1][8] = " "; theBoard[1][9] = " "; theBoard[1][10] = " ";

theBoard[2][0] = " "; theBoard[2][1] = " "; theBoard[2][2] = " "; theBoard[2][3] = "r"; theBoard[2][4] = "w"; theBoard[2][5] = "b"; theBoard[2][6] = "b"; theBoard[2][7] = "w"; theBoard[2][8] = " "; theBoard[2][9] = " "; theBoard[2][10] = " ";

theBoard[3][0] = " "; theBoard[3][1] = " "; theBoard[3][2] = "g"; theBoard[3][3] = "w"; theBoard[3][4] = "w"; theBoard[3][5] = "r"; theBoard[3][6] = "b"; theBoard[3][7] = "g"; theBoard[3][8] = "o"; theBoard[3][9] = " "; theBoard[3][10] = " ";

theBoard[4][0] = " "; theBoard[4][1] = "w"; theBoard[4][2] = "y"; theBoard[4][3] = "w"; theBoard[4][4] = "s"; theBoard[4][5] = " "; theBoard[4][6] = "s"; theBoard[4][7] = "g"; theBoard[4][8] = "g"; theBoard[4][9] = "g"; theBoard[4][10] = " ";

theBoard[5][0] = "o"; theBoard[5][1] = "y"; theBoard[5][2] = "y"; theBoard[5][3] = "b"; theBoard[5][4] = " "; theBoard[5][5] = " "; theBoard[5][6] = " "; theBoard[5][7] = "o"; theBoard[5][8] = "r"; theBoard[5][9] = "r"; theBoard[5][10] = "y";

theBoard[6][0] = " "; theBoard[6][1] = "g"; theBoard[6][2] = "y"; theBoard[6][3] = "o"; theBoard[6][4] = "w"; theBoard[6][5] = " "; theBoard[6][6] = "y"; theBoard[6][7] = "r"; theBoard[6][8] = "r"; theBoard[6][9] = "s"; theBoard[6][10] = " ";

theBoard[7][0] = " "; theBoard[7][1] = " "; theBoard[7][2] = "r"; theBoard[7][3] = "o"; theBoard[7][4] = "o"; theBoard[7][5] = "y"; theBoard[7][6] = "s"; theBoard[7][7] = "s"; theBoard[7][8] = "g"; theBoard[7][9] = " "; theBoard[7][10] = " ";

theBoard[8][0] = " "; theBoard[8][1] = " "; theBoard[8][2] = " "; theBoard[8][3] = "s"; theBoard[8][4] = "o"; theBoard[8][5] = "s"; theBoard[8][6] = "s"; theBoard[8][7] = "b"; theBoard[8][8] = " "; theBoard[8][9] = " "; theBoard[8][10] = " ";

theBoard[9][0] = " "; theBoard[9][1] = " "; theBoard[9][2] = " "; theBoard[9][3] = " "; theBoard[9][4] = "b"; theBoard[9][5] = "g"; theBoard[9][6] = "w"; theBoard[9][7] = " "; theBoard[9][8] = " "; theBoard[9][9] = " "; theBoard[9][10] = " ";

theBoard[10][0] = " "; theBoard[10][1] = " "; theBoard[10][2] = " "; theBoard[10][3] = " "; theBoard[10][4] = " "; theBoard[10][5] = "b"; theBoard[10][6] = " "; theBoard[10][7] = " "; theBoard[10][8] = " "; theBoard[10][9] = " "; theBoard[10][10] = " ";

}

//A function to print the board. //First it's printing an array to keep track of number of columns //then it's printing a board along with the number of total rows.

public void printBoard() { System.out.print(" "); for (int i = 0; i < top.length; i++) { System.out.print(" " + top[i]); } System.out.println(" "); for (int i = 0; i < theBoard.length; i++) { System.out.print(side[i] + " "); for (int j = 0; j < theBoard[i].length; j++) { System.out.print(theBoard[i][j] + " "); } System.out.println(" "); } }

//function to check if the move is valid or not //arguments: tile, column number and row number public boolean placeTile(String tile, String row, int col) { boolean isValid = true; boolean validRow = false; boolean validCol = false; boolean validPlace = false; int c = col - 1; //getting a column number //checks if the row is valid for (int i = 0; i < side.length; i++) { if (side[i].equals(row)) { validRow = true; } } //checks if the column is valid for (int i = 0; i < top.length; i++) { if (this.top[i] == col) { validCol = true; } } //if the column and row is valid then this condition checks //if the tile can be place in any of the four directions //whcih is top, right, bottom, and left if (validRow && validCol) { int r = row.toLowerCase().charAt(0) - 97; if (tile.contains(this.theBoard[r][c])) { if (this.theBoard[r][c].equals(tile.substring(0, 1))) { if (r - 1 >= 0 && this.theBoard[r - 1][c].equals(tile.substring(1, tile.length()))) { validPlace = true; } else if (r + 1 < this.side.length && this.theBoard[r + 1][c].equals(tile.substring(1, tile.length()))) { validPlace = true; } else if (c - 1 >= 0 && this.theBoard[r][c - 1].equals(tile.substring(1, tile.length()))) { validPlace = true; } else if (c + 1 < this.top.length && this.theBoard[r][c + 1].equals(tile.substring(1, tile.length()))) { validPlace = true; } else { isValid = false; } } else { if (r - 1 >= 0 && this.theBoard[r - 1][c].equals(tile.substring(0, 1))) { validPlace = true; } else if (r + 1 < this.side.length && this.theBoard[r + 1][c].equals(tile.substring(0, 1))) { validPlace = true; } else if (c - 1 >= 0 && this.theBoard[r][c - 1].equals(tile.substring(0, 1))) { validPlace = true; } else if (c + 1 < this.top.length && this.theBoard[r][c + 1].equals(tile.substring(0, 1))) { validPlace = true; } else { isValid = false; } } } else { isValid = false; } } else { isValid = false; } if (!(validPlace && validRow && validCol)) { isValid = false; } return isValid; } }

import java.util.Scanner;

public class Game { //A function that prints everything public static void printEverything(Players p1, Players p2, Board board) { System.out.println(" "); System.out.println("Player 1: "); p1.showHand(); board.printBoard(); System.out.println("Player 2: "); p2.showHand(); }

public static void main(String[ ] args) { //Boolean to keep track of a winner boolean[ ] winner = { true, true }; boolean inPlay = true; Scanner in = new Scanner(System.in); System.out.println("It is a two player game. "); System.out.println("Please enter 'yes' if there are two players: "); String playing = in.nextLine(); while (playing.equalsIgnoreCase("yes")) { playing = "no"; inPlay = true; //setting up a game: // a board and two players with their set of 14 tiles Board board = new Board(); Tiles.setTiles(); Players p1 = new Players(); p1.getHand(); Players p2 = new Players(); p2.getHand();

printEverything(p1, p2, board);

if (winner[0] && winner[1]) { System.out.println("Who would like to go first? Enter 1 for player1 or 2 for player2"); String turn = in.nextLine(); if (turn.toLowerCase().contains("one") || turn.contains("1")) { while (inPlay) { System.out.println("Player 1:"); p1.move(board, in); printEverything(p1, p2, board); if (p1.getStatus()) { System.out.println("Player 2:"); p2.move(board, in); printEverything(p1, p2, board); if (!p2.getStatus()) { inPlay = false; winner[1] = false; winner[0] = true; } } else { inPlay = false; winner[0] = false; winner[1] = true; } }

} else { while (inPlay) { System.out.println("Player 2:"); p2.move(board, in); printEverything(p1, p2, board); if (p2.getStatus()) { System.out.println("Player 1:"); p1.move(board, in); printEverything(p1, p2, board); if (!p1.getStatus()) { inPlay = false; winner[0] = false; winner[1] = true; } } else { inPlay = false; winner[1] = false; winner[0] = true; } } } } else if (winner[0]) { while (inPlay) { System.out.println("Player 1:"); p1.move(board, in); printEverything(p1, p2, board); if (p1.getStatus()) { System.out.println("Player 2:"); p2.move(board, in); printEverything(p1, p2, board); if (!p2.getStatus()) { inPlay = false; winner[1] = false; winner[0] = true; } } else { inPlay = false; winner[0] = false; winner[1] = true; } } } else if (winner[1]) { while (inPlay) { System.out.println("Player 2:"); p2.move(board, in); printEverything(p1, p2, board); if (p2.getStatus()) { System.out.println("Player 1:"); p1.move(board, in); printEverything(p1, p2, board); if (!p1.getStatus()) { inPlay = false; winner[0] = false; winner[1] = true; } } else { inPlay = false; winner[1] = false; winner[0] = true; } } } if (winner[0]) { System.out.println("Player 1 is the winner!"); } else { System.out.println("Player 2 is the winner!"); } System.out.println("Would you like to play again?"); playing = in.nextLine(); } in.close(); }

}

/*

package boardGame;

import java.util.Scanner;

public class game {

public static void printEverything(player p1, player p2, board board) { System.out.println(" "); System.out.println("Player 1: "); p1.showHand(); board.printBoard(); System.out.println("Player 2: "); p2.showHand();

}

public static void main(String[] args) { boolean[] winner = { true, true }; boolean inPlay = true; Scanner in = new Scanner(System.in); System.out.println("Do you have two players?"); String playing = in.nextLine(); while (playing.equalsIgnoreCase("yes")) { playing = "no"; inPlay = true; board board = new board(); tile.setTiles(); player p1 = new player(); p1.getHand(); player p2 = new player(); p2.getHand();

printEverything(p1, p2, board);

if (winner[0] && winner[1]) { System.out.println("Who would like to go first?"); String turn = in.nextLine(); if (turn.toLowerCase().contains("one") || turn.contains("1")) { while (inPlay) { System.out.println("Player 1:"); p1.move(board, in); printEverything(p1, p2, board); if (p1.getStatus()) { System.out.println("Player 2:"); p2.move(board, in); printEverything(p1, p2, board); if (!p2.getStatus()) { inPlay = false; winner[1] = false; winner[0] = true; } } else { inPlay = false; winner[0] = false; winner[1] = true; } }

} else { while (inPlay) { System.out.println("Player 2:"); p2.move(board, in); printEverything(p1, p2, board); if (p2.getStatus()) { System.out.println("Player 1:"); p1.move(board, in); printEverything(p1, p2, board); if (!p1.getStatus()) { inPlay = false; winner[0] = false; winner[1] = true; } } else { inPlay = false; winner[1] = false; winner[0] = true; } } } } else if (winner[0]) { while (inPlay) { System.out.println("Player 1:"); p1.move(board, in); printEverything(p1, p2, board); if (p1.getStatus()) { System.out.println("Player 2:"); p2.move(board, in); printEverything(p1, p2, board); if (!p2.getStatus()) { inPlay = false; winner[1] = false; winner[0] = true; } } else { inPlay = false; winner[0] = false; winner[1] = true; } } } else if (winner[1]) { while (inPlay) { System.out.println("Player 2:"); p2.move(board, in); printEverything(p1, p2, board); if (p2.getStatus()) { System.out.println("Player 1:"); p1.move(board, in); printEverything(p1, p2, board); if (!p1.getStatus()) { inPlay = false; winner[0] = false; winner[1] = true; } } else { inPlay = false; winner[1] = false; winner[0] = true; } } } if (winner[0]) { System.out.println("Player 1 is the winner!"); } else { System.out.println("Player 2 is the winner!"); } System.out.println("Would you like to play again?"); playing = in.nextLine(); } in.close(); }

} */

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