Cześć piszę mini grę w przesuwanie klocków aby ułożyć je w kolejności.
Gra działa ale zastanawiam się jak bym mógł usprawnić kod? Bo o ile dla planszy 3x3 kod zajmuje 300 linijek to gdybym chciał zrobić kod dla planszy 4x4 to długość kodu to już byłaby masakra.
Przypisanie liczb do przycisków mógłbym wrzucić w tablicę i pętlę foreach no i parę linijek kodu zaoszczędzę ale jak ogarnąć te wszystkie warunki dla każdego kliknięcia przycisku?Da się to zrobić jakoś krócej?
Tak żeby wrzucić to do jednej metody która działa na kliknięcie jakiegokolwiek przycisku?
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;
namespace ukladanka
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
NewGame();
}
private void NewGame()
{
int index;
int i = 0;
Random rand = new Random();
List<int> numbers = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8 };//lista liczb na przyciskach
string[] randomNumbers = new string[8];//tu będą wymieszane przyciski
foreach (string b in randomNumbers) //pętla mieszająca tablicę liczb
{
index = rand.Next(0, numbers.Count);
randomNumbers[i] = numbers[index].ToString();
numbers.RemoveAt(index);
i++;
}
//Przypisanie cyfr do przycisków
button1.Text = randomNumbers[0];
button2.Text = randomNumbers[1];
button3.Text = randomNumbers[2];
button4.Text = randomNumbers[3];
button5.Text = randomNumbers[4];
button6.Text = randomNumbers[5];
button7.Text = randomNumbers[6];
button8.Text = randomNumbers[7];
}
private void button1_Click(object sender, EventArgs e)
{
if (!button2.Visible)//Jeśli przycisk obok jest niewidoczny
{
button2.Visible = true;//to zmieniamy go na widoczny
button2.Text = button1.Text;//przypisujemy mu tekst z wciśniętego przyciski
button1.Text = "";//wciśnięty przycisk dostaje pusty tekst
button1.Visible= false;//i staje się niewidoczny
CheckWin();
}
if (!button4.Visible)
{
button4.Visible = true;
button4.Text = button1.Text;
button1.Text = "";
button1.Visible= false;
CheckWin();
}
}
private void button2_Click(object sender, EventArgs e)
{
if (!button1.Visible)
{
button1.Text = button2.Text;
button1.Visible = true;
button2.Text = "";
button2.Visible = false;
CheckWin();
}
if (!button5.Visible)
{
button5.Visible = true;
button5.Text = button2.Text;
button2.Text = "";
button2.Visible = false;
CheckWin();
}
if (!button3.Visible)
{
button3.Visible = true;
button3.Text = button2.Text;
button2.Text = "";
button2.Visible = false;
CheckWin();
}
}
private void button3_Click(object sender, EventArgs e)
{
if (!button2.Visible)
{
button2.Text = button3.Text;
button2.Visible = true;
button3.Text = "";
button3.Visible = false;
CheckWin();
}
if (!button6.Visible)
{
button6.Visible = true;
button6.Text = button3.Text;
button3.Text = "";
button3.Visible = false;
CheckWin();
}
}
private void button4_Click(object sender, EventArgs e)
{
if (!button1.Visible)
{
button1.Text = button4.Text;
button1.Visible = true;
button4.Text = "";
button4.Visible = false;
CheckWin();
}
if (!button5.Visible)
{
button5.Visible = true;
button5.Text = button4.Text;
button4.Text = "";
button4.Visible = false;
CheckWin();
}
if (!button7.Visible)
{
button7.Visible = true;
button7.Text = button4.Text;
button4.Text = "";
button4.Visible = false;
CheckWin();
}
}
private void button5_Click(object sender, EventArgs e)
{
if (!button2.Visible)
{
button2.Text = button5.Text;
button2.Visible = true;
button5.Text = "";
button5.Visible = false;
CheckWin();
}
if (!button4.Visible)
{
button4.Visible = true;
button4.Text = button5.Text;
button5.Text = "";
button5.Visible = false;
CheckWin();
}
if (!button6.Visible)
{
button6.Visible = true;
button6.Text = button5.Text;
button5.Text = "";
button5.Visible = false;
CheckWin();
}
if (!button8.Visible)
{
button8.Visible = true;
button8.Text = button5.Text;
button5.Text = "";
button5.Visible = false;
CheckWin();
}
}
private void button6_Click(object sender, EventArgs e)
{
if (!button3.Visible)
{
button3.Text = button6.Text;
button3.Visible = true;
button6.Text = "";
button6.Visible = false;
CheckWin();
}
if (!button5.Visible)
{
button5.Visible = true;
button5.Text = button6.Text;
button6.Text = "";
button6.Visible = false;
CheckWin();
}
if (!button9.Visible)
{
button9.Visible = true;
button9.Text = button6.Text;
button6.Text = "";
button6.Visible = false;
CheckWin();
}
}
private void button7_Click(object sender, EventArgs e)
{
if (!button4.Visible)
{
button4.Text = button7.Text;
button4.Visible = true;
button7.Text = "";
button7.Visible = false;
CheckWin();
}
if (!button8.Visible)
{
button8.Visible = true;
button8.Text = button7.Text;
button7.Text = "";
button7.Visible = false;
CheckWin();
}
}
private void button8_Click(object sender, EventArgs e)
{
if (!button5.Visible)
{
button5.Text = button8.Text;
button5.Visible = true;
button8.Text = "";
button8.Visible = false;
CheckWin();
}
if (!button7.Visible)
{
button7.Visible = true;
button7.Text = button8.Text;
button8.Text = "";
button8.Visible = false;
CheckWin();
}
if (!button9.Visible)
{
button9.Visible = true;
button9.Text = button8.Text;
button8.Text = "";
button8.Visible = false;
CheckWin();
}
}
private void button9_Click(object sender, EventArgs e)
{
if (!button8.Visible)
{
button8.Text = button9.Text;
button8.Visible = true;
button9.Text = "";
button9.Visible = false;
CheckWin();
}
if (!button6.Visible)
{
button6.Visible = true;
button6.Text = button9.Text;
button9.Text = "";
button9.Visible = false;
CheckWin();
}
}
private void CheckWin()
{
//Jeśli przyciski są ułożone po kolei to wygrywasz
if (button1.Text == "1" & button2.Text == "2" & button3.Text == "3" &
button4.Text == "4" & button5.Text == "5" & button6.Text == "6" &
button7.Text == "7" & button8.Text == "8")
label1.Text = "Wygrana";
}
private void button10_Click(object sender, EventArgs e)
{
NewGame();
}
}
}