kalinkata

Monopoly

Mar 26th, 2016
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.22 KB | None | 0 0
  1. package com.company;
  2.  
  3.  
  4. import java.util.Scanner;
  5.  
  6. public class Monopoly {
  7.     public static void main(String[] args) {
  8.         Scanner scanner = new Scanner(System.in);
  9.         String[] dimensions = scanner.nextLine().split("\\s+");
  10.  
  11.         int rows = Integer.parseInt(dimensions[0]);
  12.         int cols = Integer.parseInt(dimensions[1]);
  13.  
  14.         String[] lines = new String[rows];
  15.  
  16.         for (int i = 0; i < rows; i++) {
  17.             lines[i] = scanner.nextLine();
  18.         }
  19.  
  20.         char[][] matrix = fillTheMatrix(lines, rows, cols);
  21.  
  22.         int turns = 0;
  23.         int money = 50;
  24.         int hotels = 0;
  25.  
  26.         for (int row = 0; row < rows; row++) {
  27.             for (int col = 0; col < cols; col++) {
  28.                 if (row % 2 == 0) {
  29.                     char ch = matrix[row][col];
  30.                     switch (ch) {
  31.                         case 'H': {
  32.                             hotels++;
  33.                             System.out.printf("Bought a hotel for %s." +
  34.                                     " Total hotels: %d.\n", money, hotels);
  35.  
  36.                             money = 0;
  37.                             break;
  38.                         }
  39.                         case 'S': {
  40.                             int mToSpend = (row + 1) * (col + 1);
  41.  
  42.                             if (money < mToSpend) {
  43.                                 System.out.printf("Spent %d money at the shop.\n",
  44.                                         money);
  45.  
  46.                                 money = 0;
  47.                             } else {
  48.                                 System.out.printf("Spent %d money at the shop.\n",
  49.                                         mToSpend);
  50.  
  51.                                 money -= mToSpend;
  52.                             }
  53.  
  54.                             break;
  55.                         }
  56.                         case 'J': {
  57.                             System.out.printf("Gone to jail at turn %d.\n",
  58.                                     turns);
  59.  
  60.                             turns += 2;
  61.  
  62.                             money += (hotels * 10) * 2;
  63.  
  64.                             break;
  65.                         }
  66.                     }
  67.                 } else {
  68.                     char ch = matrix[row][cols - col - 1];
  69.  
  70.                     switch (ch) {
  71.                         case 'H': {
  72.                             hotels++;
  73.                             System.out.printf("Bought a hotel for %s." +
  74.                                     " Total hotels: %d.\n", money, hotels);
  75.  
  76.                             money = 0;
  77.  
  78.                             break;
  79.                         }
  80.                         case 'S': {
  81.                             int mToSpend = (row + 1) * ((cols - col - 1) + 1);
  82.  
  83.                             if (money < mToSpend) {
  84.                                 System.out.printf("Spent %d money at the shop.\n",
  85.                                         money);
  86.  
  87.                                 money = 0;
  88.                             } else {
  89.                                 System.out.printf("Spent %d money at the shop.\n",
  90.                                         mToSpend);
  91.  
  92.                                 money -= mToSpend;
  93.                             }
  94.  
  95.                             break;
  96.                         }
  97.                         case 'J': {
  98.                             System.out.printf("Gone to jail at turn %d.\n",
  99.                                     turns);
  100.  
  101.                             turns += 2;
  102.  
  103.                             money += (hotels * 10) * 2;
  104.  
  105.                             break;
  106.                         }
  107.                     }
  108.                 }
  109.  
  110.                 turns++;
  111.  
  112.                 money += (hotels * 10);
  113.             }
  114.         }
  115.  
  116.         System.out.printf("Turns %d\n", turns);
  117.         System.out.printf("Money %d", money);
  118.     }
  119.     public static char[][] fillTheMatrix(String[] lines, int rows, int cols) {
  120.         char[][] matrix = new char[rows][];
  121.         for (int row = 0; row < rows; row++) {
  122.             char[] innerArr = new char[cols];
  123.             for (int col = 0; col < cols; col++) {
  124.                 innerArr[col] = lines[row].charAt(col);
  125.             }
  126.             matrix[row] = innerArr;
  127.         }
  128.         return matrix;
  129.     }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment