Advertisement
Guest User

Untitled

a guest
Apr 6th, 2017
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.93 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.IO;
  11.  
  12. namespace parkoviskoEvidencia
  13. {
  14.     public partial class Form1 : Form
  15.     {
  16.         // ZBYTOCNE VECI CO MI NEJDU Z NEJAKEHO DOVODU VYMAZAT, DEBUG PROSIM
  17.         //private void parkoviskoPictureBox_LoadCompleted(object sender, AsyncCompletedEventArgs e)
  18.         //{
  19.  
  20.         //}
  21.         private void Form1_Load(object sender, EventArgs e)
  22.         {
  23.  
  24.         }
  25.         /// <summary>
  26.         /// KONIEC DIVNEHO BUGU
  27.         /// </summary>
  28.  
  29.         public Form1()
  30.         {
  31.             InitializeComponent();
  32.         }
  33.  
  34.         private Parkovisko parkovisko = new Parkovisko();
  35.  
  36.         private void parkoviskoPictureBox_Paint(object sender, PaintEventArgs e)
  37.         {
  38.             parkovisko.Vykresli(e.Graphics);
  39.         }
  40.  
  41.         private void parkoviskoPictureBox_MouseClick(object sender, MouseEventArgs e)
  42.         {
  43.             parkovisko.Klik(e.X, e.Y);
  44.             parkoviskoPictureBox.Refresh();
  45.         }
  46.  
  47.         private void ulozitButton_Click(object sender, EventArgs e)
  48.         {
  49.             DialogResult vysledok = parkoviskoSaveFileDialog.ShowDialog();
  50.             if (vysledok == DialogResult.OK)
  51.             {
  52.                 try
  53.                 {
  54.                     parkovisko.Uloz(parkoviskoSaveFileDialog.FileName);
  55.                 }
  56.                 catch (Exception ex)
  57.                 {
  58.                     MessageBox.Show("Súbor sa nepodarilo uložiť.", "Chyba", MessageBoxButtons.OK, MessageBoxIcon.Error);
  59.                 }
  60.             }
  61.         }
  62.  
  63.         private void nacitatButton_Click(object sender, EventArgs e)
  64.         {
  65.             DialogResult vysledok = parkoviskoOpenFileDialog.ShowDialog();
  66.             if (vysledok == DialogResult.OK)
  67.             {
  68.                 try
  69.                 {
  70.                     parkovisko.Nacitaj(parkoviskoOpenFileDialog.FileName);
  71.                 }
  72.                 catch (Exception ex)
  73.                 {
  74.                     MessageBox.Show("Súbor sa nepodarilo načítať.", "Chyba", MessageBoxButtons.OK, MessageBoxIcon.Error);
  75.                 }
  76.             }    
  77.         }
  78.     }
  79.  
  80.     class Parkovisko
  81.     {
  82.         private bool[,] miesta = new bool[30, 15];
  83.         private const int velkost = 16;
  84.         private const int medzera = 2;
  85.  
  86.         public void Vykresli(Graphics g)
  87.         {
  88.             Brush brush;
  89.             for (int j = 0; j < miesta.GetLength(1); j++)
  90.             {
  91.                 for (int i = 0; i < miesta.GetLength(0); i++)
  92.                 {
  93.                     if (miesta[i, j])
  94.                         brush = Brushes.Red;
  95.                     else
  96.                         brush = Brushes.Green;
  97.                     g.FillRectangle(brush, i * (velkost + medzera), j * (velkost + medzera), velkost, velkost);
  98.                 }
  99.             }
  100.         }
  101.  
  102.         public void PrepniStav(int x, int y)
  103.         {
  104.             miesta[x, y] = !miesta[x, y];
  105.         }
  106.  
  107.         public void Klik(int x, int y)
  108.         {
  109.             int px = x / (velkost + medzera);
  110.             int py = y / (velkost + medzera);
  111.             if (px < miesta.GetLength(0) && py < miesta.GetLength(1))
  112.                 PrepniStav(px, py);
  113.         }
  114.  
  115.         public void Uloz(string cesta)
  116.         {
  117.             using (StreamWriter sw = new StreamWriter(cesta))
  118.             {
  119.                 int obsadene = 0;
  120.                 for (int j = 0; j < miesta.GetLength(1); j++)
  121.                 {
  122.                     string riadok = "";
  123.                     for (int i = 0; i < miesta.GetLength(0); i++)
  124.                     {
  125.                         if (miesta[i, j])
  126.                         {
  127.                             riadok += "1";
  128.                             obsadene++;
  129.                         }
  130.                         else
  131.                             riadok += "0";
  132.                     }
  133.                     sw.Write(riadok + Environment.NewLine);
  134.                     sw.Flush();
  135.                 }
  136.                 //sw.Write("Volných: {0}" + Environment.NewLine, miesta.Length - obsadene);
  137.                 //sw.Write("Obsadených: {0}" + Environment.NewLine, obsadene);
  138.             }
  139.         }
  140.  
  141.         public void Nacitaj(string cesta)
  142.         {
  143.             using (StreamReader sr = new StreamReader(cesta))
  144.             {
  145.                 for (int j = 0; j < miesta.GetLength(0); j++)
  146.                 {
  147.                     string riadok = sr.ReadLine();
  148.                     for (int i = 0; i < miesta.GetLength(1); i++)
  149.                     {
  150.                         if (riadok[i] == '1')
  151.                             miesta[i, j] = true;
  152.                         else
  153.                             miesta[i, j] = false;
  154.                     }
  155.                 }
  156.             }
  157.         }
  158.     }
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement