Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package zut;
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
- public class Szachownica {
- private static final String nextLineChar = "\n";
- public static void main(String[] args) {
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- try {
- System.out.print("Podaj szerokosc kafelka: ");
- int tileWidth = Integer.parseInt(br.readLine());
- System.out.print("Podaj wysokosc kafelka: ");
- int tileHeight = Integer.parseInt(br.readLine());
- System.out.print("Podaj ilosc kafelkow w poziomie: ");
- int width = Integer.parseInt(br.readLine());
- System.out.print("Podaj ilosc kafelkow w pionie: ");
- int height = Integer.parseInt(br.readLine());
- System.out.print("Podaj znak bialego kafelka: ");
- String whiteTile = br.readLine();
- System.out.print("Podaj znak czarnego kafelka: ");
- String blackTile = br.readLine();
- if(validateInput(tileWidth, tileHeight, width, height, whiteTile, blackTile)){
- printBoard(tileWidth, tileHeight, width, height, whiteTile, blackTile);
- }
- } catch(Exception ex){
- }
- finally{
- System.exit(0);
- }
- }
- private static String switchTile(String current, String white, String black)
- {
- if(current.equals(white)){
- current = black;
- }
- else{
- current = white;
- }
- return current;
- }
- private static void printBoard(int tileWidth, int tileHeight,int width, int height, String whiteTile,String blackTile)
- {
- StringBuilder boxBuilder = new StringBuilder();
- String currentTile = whiteTile;
- for (int i = 0; i < height; i++) {
- for (int j = 0; j < tileHeight; j++) {
- for (int x = 0; x < width; x++) {
- for (int k = 0; k < tileWidth; k++) {
- boxBuilder.append(currentTile);
- }
- currentTile = switchTile(currentTile, whiteTile, blackTile);
- }
- boxBuilder.append(nextLineChar);
- }
- currentTile = switchTile(currentTile, whiteTile, blackTile);
- }
- System.out.print(boxBuilder.toString());
- }
- private static Boolean validateInput(int tileWidth, int tileHeight,int width, int height, String whiteTile,String blackTile)
- {
- Boolean isValid = true;
- if(tileWidth < 1 || tileWidth > 15){
- isValid = false;
- }
- if(tileHeight < 1 || tileHeight >15){
- isValid = false;
- }
- if(width < 1 || width > 15){
- isValid = false;
- }
- if(height < 1 || height > 15){
- isValid = false;
- }
- if(whiteTile.length() != 1){
- isValid = false;
- }
- if(blackTile.length() != 1){
- isValid = false;
- }
- return isValid;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement