0

I need to solve behavior of four numbers, which will move down or rotate as the meaning of the game Tetris. This is that I want to do on richTextBox in C#, but my code is still not working good. I want to do as illustrated below. How can I do to that numbers are moving in right direction?

0 0 0 0 1 1 1 1 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0 0 0 0

after moving down four numbers of "1"

0 0 0 0 0 0 0 0 0 0 0 0 0

0 0 0 0 1 1 1 1 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0 0 0 0

or also after rotating clockwise four numbers of "1"

0 0 0 0 0 0 0 0 0 0 0 0 0

0 0 0 0 1 0 0 0 0 0 0 0 0

0 0 0 0 1 0 0 0 0 0 0 0 0

0 0 0 0 1 0 0 0 0 0 0 0 0

0 0 0 0 1 0 0 0 0 0 0 0 0

Here is my code.

string[] pole8x8 = new string[400];
string[] pole4x4 = new string[4*2];
List<string> numbers = new List<string>();
int len = 52;

public Form1()
{
    InitializeComponent();

    for (int i = 0; i < pole8x8.Length; i+=2)
    {
        pole8x8[i] = "0 ";
        richTextBox1.Text += pole8x8[i];
        richTextBox1.BackColor = Color.Black;
        richTextBox1.ForeColor = Color.White;
    }

    for (int i = 0; i < pole4x4.Length; i+=2)
    {
        pole4x4[i] = "1 ";
        richTextBox1.SelectionStart = 18;
        richTextBox1.SelectedText = pole4x4[i];
        numbers.Add(pole4x4[i]);
     }

 }

 private void button1_Click(object sender, EventArgs e)
 {
     richTextBox1.SelectionStart += len;

     foreach (string s in numbers)
     {
        richTextBox1.SelectedText = s;
     }

 }
  • 2
    please rename your buttons/etc to give them more meaningful names - it's a complete pet-hate of mine! –  Oct 08 '14 at 12:38
  • It's unclear what, exactly, you want help with. Causing your "pieces" to move down the text box? Also, your variables look to be a bit misnamed. You have "pole4x4" and "pole8x8" I think that pole4x4 is supposed to be your "line" piece and pole8x8 is supposed to be a row in your well. But it's unclear. (4x4 defines a square containing 16 points... 4 across and 4 down, not a "pole". Similarly, 8x8 would be a square with 64 points. If I read this correctly, you mean them to be 1x4 and 1x8. Is that correct?) – DeadZone Oct 08 '14 at 13:28
  • I'm sorry for unreasonable names of variables. pole4x4 is related to four numbers consisting of "1" and pole8x8 is for others numbers like "0", which is string in array by a size 400. You can it try in C# and easy it figure out. – Andrej Kozlovský Oct 08 '14 at 13:37
  • Besides changing the characters to show the board like you want it, there is another probelm with your code. (At least!) Do you have a spcial reason to use a `RichTextBox`??? It is a rather weird choice, over, say a `DataGridView`.. Please hae a look at the [rules I set up in this post](http://stackoverflow.com/questions/25957207/color-output-strings-in-richtextbox/25957583#25957583), or else your coloring will never work right.. – TaW Oct 08 '14 at 13:45

1 Answers1

0

Well, I think that your implementation needs some refinement, but here is some code that will start with a couple of shapes and move them down the well. They... 'crumble' at the bottom, unlike tetris where the shapes retain their rigidness. But I think this answers your question. the rest is up to you.

Edit: I forgot to mention. My RichTextBox is set to use Courier New font. (and non-true type font should work.)

public partial class Form1 : Form
{
    private int _tickCounter = 0;
    private int _tickLimit = 500;  // set to 10 or something for the game
    private string[,] _dataArray;

    public Form1()
    {
        InitializeComponent();
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        _tickCounter++;
        if (_tickCounter >= _tickLimit)
        {
            // add a piece
            AddNewPieceToWell(true);
            _tickCounter = 0;
        }
        else
        {
            // Move the current pieces downward.
            for (int rowCounter = _dataArray.GetUpperBound(0); rowCounter >= 1; rowCounter--)
            {
                for (int colCounter = 0; colCounter <= _dataArray.GetUpperBound(1); colCounter++)
                {
                    if (_dataArray[rowCounter, colCounter] == " " && _dataArray[rowCounter - 1, colCounter] == "0")
                    {
                        _dataArray[rowCounter, colCounter] = "0";
                        _dataArray[rowCounter - 1, colCounter] = " ";
                    }
                }
            }
        }

        DrawWell();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        InitializeWell();
        timer1.Start();
    }

    private void InitializeWell()
    {
        _dataArray = new string[,]{
            {" ", " ", "0", "0", "0", "0", " ", " "},
            {" ", " ", " ", " ", " ", " ", " ", " "},
            {" ", " ", " ", " ", " ", " ", " ", " "},
            {" ", " ", " ", " ", " ", " ", " ", " "},
            {"0", "0", "0", "0", " ", " ", " ", " "},
            {" ", " ", " ", " ", " ", " ", " ", " "},
            {" ", " ", " ", " ", " ", " ", " ", " "}
        };
        DrawWell();
    }

    private void DrawWell()
    {
        rtbWell.Text = string.Empty;
        for (int rowCounter = 0; rowCounter <= _dataArray.GetUpperBound(0); rowCounter++)
        {
            for (int colCounter = 0; colCounter <= _dataArray.GetUpperBound(1); colCounter++)
            {
                rtbWell.Text += _dataArray[rowCounter, colCounter];
            }
            rtbWell.Text += Environment.NewLine;
        }
    }

    private void AddNewPieceToWell(bool RandomPiece = true)
    {
        // ToDo
    }
}
DeadZone
  • 1,633
  • 1
  • 17
  • 32