Advertisement
Guest User

Untitled

a guest
Apr 19th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.01 KB | None | 0 0
  1. package zut;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.InputStreamReader;
  5.  
  6. public class Szachownica {
  7.     private static final String nextLineChar = "\n";
  8.    
  9.     public static void main(String[] args) {
  10.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  11.         try {
  12.         System.out.print("Podaj szerokosc kafelka: ");
  13.         int tileWidth = Integer.parseInt(br.readLine());
  14.         System.out.print("Podaj wysokosc kafelka: ");
  15.         int tileHeight = Integer.parseInt(br.readLine());
  16.         System.out.print("Podaj ilosc kafelkow w poziomie: ");
  17.         int width = Integer.parseInt(br.readLine());
  18.         System.out.print("Podaj ilosc kafelkow w pionie: ");
  19.         int height = Integer.parseInt(br.readLine());
  20.         System.out.print("Podaj znak bialego kafelka: ");
  21.         String whiteTile = br.readLine();
  22.         System.out.print("Podaj znak czarnego kafelka: ");
  23.         String blackTile = br.readLine();
  24.         if(validateInput(tileWidth, tileHeight, width, height, whiteTile, blackTile)){
  25.             printBoard(tileWidth, tileHeight, width, height, whiteTile, blackTile);
  26.         }
  27.         } catch(Exception ex){          
  28.         }
  29.         finally{
  30.          System.exit(0);
  31.         }
  32.     }
  33.    
  34.     private static String switchTile(String current, String white, String black)
  35.     {
  36.         if(current.equals(white)){
  37.             current = black;
  38.         }
  39.         else{
  40.             current = white;
  41.         }
  42.         return current;
  43.     }
  44.    
  45.     private static void printBoard(int tileWidth, int tileHeight,int width, int height, String whiteTile,String blackTile)
  46.     {
  47.         StringBuilder boxBuilder = new StringBuilder();
  48.         String currentTile = whiteTile;
  49.         for (int i = 0; i < height; i++) {
  50.             for (int j = 0; j < tileHeight; j++) {
  51.                 for (int x = 0; x < width; x++) {                
  52.                 for (int k = 0; k < tileWidth; k++) {
  53.                     boxBuilder.append(currentTile);
  54.                 }
  55.                 currentTile = switchTile(currentTile, whiteTile, blackTile);
  56.                 }
  57.                 boxBuilder.append(nextLineChar);
  58.             }
  59.             currentTile = switchTile(currentTile, whiteTile, blackTile);
  60.         }
  61.         System.out.print(boxBuilder.toString());
  62.     }
  63.    
  64.     private static Boolean validateInput(int tileWidth, int tileHeight,int width, int height, String whiteTile,String blackTile)
  65.     {
  66.         Boolean isValid = true;
  67.         if(tileWidth < 1 || tileWidth > 15){
  68.             isValid = false;
  69.         }
  70.         if(tileHeight < 1 || tileHeight >15){
  71.             isValid = false;
  72.         }
  73.         if(width < 1 || width > 15){
  74.             isValid = false;
  75.         }
  76.         if(height < 1 || height > 15){
  77.             isValid = false;
  78.         }
  79.         if(whiteTile.length() != 1){
  80.             isValid = false;
  81.         }
  82.         if(blackTile.length() != 1){
  83.             isValid = false;
  84.         }
  85.         return isValid;
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement