Posiedziałem trochę nad tym i wyszło trochę g**no kodu jako implementacja chorego pomysłu który nawet mnie przeraża
aż sam siebie zapytałem "to działa xD?".
Dwie tablice 2d. Jedna trzyma znaczki, a druga odległości od góry i lewej strony formsa i cała logika się na tym opiera.
Niech mi ktoś teraz powie, jak to zrobić normalnie i szybko bez takiego bajzlu.
PS: Random użyty, bo nazwy labeli kolidowały, a tak było najszybciej :P
using System;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
static char[,] tablica;
static Point[,] coords;
static int wysokosc;
static int szerokosc;
static int score = 0;
static int removedCounter = 0;
enum directions
{
North,
South,
West,
East
}
public Form1()
{
InitializeComponent();
Initialize();
Rysuj();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void Initialize()
{
string path = @"C:\test\input.txt";
string[] readText = File.ReadAllLines(path);
wysokosc = readText.Length;
szerokosc = readText[0].Length;
tablica = new char[wysokosc,szerokosc];
coords = new Point[wysokosc,szerokosc];
for (int i = 0; i < wysokosc; i++)
{
for (int j = 0; j < szerokosc; j++)
{
tablica[i,j] = readText[i][j];
}
}
int left = 0;
int top = 150;
for (int i =0; i<wysokosc; i++)
{
for (int j=0; j<szerokosc; j++)
{
left = 50 + (j + 1) * 75;
coords[i,j] = new Point(top, left);
}
top +=55;
}
}
private void Rysuj()
{
for (int i=0; i<wysokosc; i++)
{
for (int j = 0; j < szerokosc; j++)
{
Random rnd = new Random();
string current = tablica[i,j].ToString();
Label label = new Label();
label.Font = new Font("Arial", 16, FontStyle.Bold);
if (current=="#")
{
label.Text = current;
label.Name = "label" + j + i + rnd.Next(100);
label.ForeColor = Color.LawnGreen;
label.Size=new Size(20,20);
}
else if (current==".")
{
label.Text = "O";
label.Name = "label" + j + i+rnd.Next(100);
label.ForeColor = Color.Blue;
label.Size = new Size(30, 30);
}
else
{
label.Text = "X";
label.Name = "Player";
label.ForeColor = Color.Red;
}
label.Top = coords[i,j].X;
label.Left = coords[i,j].Y;
this.Controls.Add(label);
}
}
}
private int[] GetIndex_OfPlayersPosition_InCoords2DArray(int currentX, int currentY)
{
for (int i = 0; i < wysokosc; i++)
{
for (int j = 0; j < szerokosc; j++)
{
if (coords[i,j].X == currentX && coords[i,j].Y == currentY)
{
return new int[] { i, j };
}
}
}
return new int[] { 0, 0 };
}
private string GetNameOfTextLabel_OnCoords(int currentTop, int currentLeft)
{
foreach (Label x in this.Controls.OfType<Label>())
{
if (x.Top == currentTop && x.Left ==currentLeft && x.Name != "DisplayingScore")
{
return x.Name;
}
}
return "";
}
private void MovementHelper(string name, Control player)
{
Control newlabel = this.Controls[name];
if (newlabel.Text == "#")
{
MessageBox.Show("You cannot go there");
}
else
{
if (newlabel.Text.ToLower() == "o")
{
score++;
DisplayingScore.Text = $"Score: {score.ToString()}";
}
Control currentlabel = this.Controls[name];
currentlabel.Text = "";
newlabel.Text = "X";
newlabel.Font = new Font("Arial", 16, FontStyle.Bold);
newlabel.ForeColor = Color.Red;
newlabel.Name = "Player";
player.Text = "";
player.Name = "removed" + removedCounter;
removedCounter++;
}
}
private void Movement(directions direction)
{
Control player = this.Controls["Player"];
int[] pos = GetIndex_OfPlayersPosition_InCoords2DArray(player.Top, player.Left);
string name="";
switch (direction)
{
case directions.North:
name = GetNameOfTextLabel_OnCoords(coords[pos[0] - 1, pos[1]].X, coords[pos[0]-1, pos[1]].Y);
break;
case directions.South:
name = GetNameOfTextLabel_OnCoords(coords[pos[0] + 1, pos[1]].X, coords[pos[0]+1, pos[1]].Y);
break;
case directions.East:
name = GetNameOfTextLabel_OnCoords(coords[pos[0], pos[1]+1].X, coords[pos[0], pos[1]+1].Y);
break;
case directions.West:
name = GetNameOfTextLabel_OnCoords(coords[pos[0], pos[1]-1].X, coords[pos[0], pos[1]-1].Y);
break;
default:
MessageBox.Show("shit happens");
break;
}
MovementHelper(name, player);
}
private void North_Click(object sender, EventArgs e)
{
Movement(directions.North);
}
private void East_Click(object sender, EventArgs e)
{
Movement(directions.East);
}
private void South_Click(object sender, EventArgs e)
{
Movement(directions.South);
}
private void West_Click(object sender, EventArgs e)
{
Movement(directions.West);
}
}
}