Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using System.IO;
- namespace parkoviskoEvidencia
- {
- public partial class Form1 : Form
- {
- // ZBYTOCNE VECI CO MI NEJDU Z NEJAKEHO DOVODU VYMAZAT, DEBUG PROSIM
- //private void parkoviskoPictureBox_LoadCompleted(object sender, AsyncCompletedEventArgs e)
- //{
- //}
- private void Form1_Load(object sender, EventArgs e)
- {
- }
- /// <summary>
- /// KONIEC DIVNEHO BUGU
- /// </summary>
- public Form1()
- {
- InitializeComponent();
- }
- private Parkovisko parkovisko = new Parkovisko();
- private void parkoviskoPictureBox_Paint(object sender, PaintEventArgs e)
- {
- parkovisko.Vykresli(e.Graphics);
- }
- private void parkoviskoPictureBox_MouseClick(object sender, MouseEventArgs e)
- {
- parkovisko.Klik(e.X, e.Y);
- parkoviskoPictureBox.Refresh();
- }
- private void ulozitButton_Click(object sender, EventArgs e)
- {
- DialogResult vysledok = parkoviskoSaveFileDialog.ShowDialog();
- if (vysledok == DialogResult.OK)
- {
- try
- {
- parkovisko.Uloz(parkoviskoSaveFileDialog.FileName);
- }
- catch (Exception ex)
- {
- MessageBox.Show("Súbor sa nepodarilo uložiť.", "Chyba", MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- }
- }
- private void nacitatButton_Click(object sender, EventArgs e)
- {
- DialogResult vysledok = parkoviskoOpenFileDialog.ShowDialog();
- if (vysledok == DialogResult.OK)
- {
- try
- {
- parkovisko.Nacitaj(parkoviskoOpenFileDialog.FileName);
- }
- catch (Exception ex)
- {
- MessageBox.Show("Súbor sa nepodarilo načítať.", "Chyba", MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- }
- }
- }
- class Parkovisko
- {
- private bool[,] miesta = new bool[30, 15];
- private const int velkost = 16;
- private const int medzera = 2;
- public void Vykresli(Graphics g)
- {
- Brush brush;
- for (int j = 0; j < miesta.GetLength(1); j++)
- {
- for (int i = 0; i < miesta.GetLength(0); i++)
- {
- if (miesta[i, j])
- brush = Brushes.Red;
- else
- brush = Brushes.Green;
- g.FillRectangle(brush, i * (velkost + medzera), j * (velkost + medzera), velkost, velkost);
- }
- }
- }
- public void PrepniStav(int x, int y)
- {
- miesta[x, y] = !miesta[x, y];
- }
- public void Klik(int x, int y)
- {
- int px = x / (velkost + medzera);
- int py = y / (velkost + medzera);
- if (px < miesta.GetLength(0) && py < miesta.GetLength(1))
- PrepniStav(px, py);
- }
- public void Uloz(string cesta)
- {
- using (StreamWriter sw = new StreamWriter(cesta))
- {
- int obsadene = 0;
- for (int j = 0; j < miesta.GetLength(1); j++)
- {
- string riadok = "";
- for (int i = 0; i < miesta.GetLength(0); i++)
- {
- if (miesta[i, j])
- {
- riadok += "1";
- obsadene++;
- }
- else
- riadok += "0";
- }
- sw.Write(riadok + Environment.NewLine);
- sw.Flush();
- }
- //sw.Write("Volných: {0}" + Environment.NewLine, miesta.Length - obsadene);
- //sw.Write("Obsadených: {0}" + Environment.NewLine, obsadene);
- }
- }
- public void Nacitaj(string cesta)
- {
- using (StreamReader sr = new StreamReader(cesta))
- {
- for (int j = 0; j < miesta.GetLength(0); j++)
- {
- string riadok = sr.ReadLine();
- for (int i = 0; i < miesta.GetLength(1); i++)
- {
- if (riadok[i] == '1')
- miesta[i, j] = true;
- else
- miesta[i, j] = false;
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement