Click here to Skip to main content
15,904,153 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

- after moving numbers of 1 down

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

- or also after rotating clockwise numbers of 1

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 a my code:

C#
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;
     }

 }
Posted
Comments
BillWoodruff 8-Oct-14 9:43am    
I think doing this with text in a RichTextBox is going to be very difficult. I suggest you consider another approach: how about using Labels in a TableLayoutPanel ?

1 solution

Hi Andrej,

While I can imagine what you try to do here, I would suggest you consider a complete alternative approach.

1. Don't mess arround with strings (in your case characters stored in a string Array - even worse).. So create an "abstraction" of your "board" (game-field whatever you call it) e.g as 2 dimensional Array of booleans (you only have 2 states, don't you? - filled (1) or not (0)).

2. Don't try to do everything in one step. Split your code into different functions/methods. E.g. Create a method for each logical operation you want to support - to "move down", "rotate", ... - then just move arround (or overwrite) the booleans in the "board"-array. In these methods you can consider edge-cases (like rotation isn't possible cause your Piece is "to big" etc.).

3. After you have the "new" board-array (all operations applied) try to "render it" in one step - e.g if you really want to use the RichTextBox (consider BillWoodruff's idea) just rewrite according to your new board.

So you see, if you later decide to make a "real" tetris out of it - with graphics - you will have all the logic in place - just the rendering to change.

Examples (just quick ones to give the idea):
Represent your board as 2d Array
bool[,] board = new bool[20, 13];
- or even better create a board class that handles the "array-logic" internal.
Represent a pice as list of "coordinates" on your board;

C#
struct Coordinate
{
    public int X { get; set; }
    public int Y { get; set; }
}


A Piece is than a "collection" of coordinates
C#
class Piece
{
    public Coordinate[] Coordinates { get; set; }
}


So your Manipulation methods could have signatures like this:
public void MoveDown(Piece piece, bool[,] board) {  /* change board */ }


And Rendering could be done like this - just go through all "places" on your board and fill up your RichTextBox, Grid, Labels or whatever - here i used just the console.

C#
static void Render(bool[,] board)
{
    for (int iColumn = 0; iColumn < board.GetLength(1); iColumn++)
    {
        for (int iRow = 0; iRow < board.GetLength(0); iRow++)
        {
            Console.Write(board[iRow, iColumn] ? "1" : "0");
        }
        Console.WriteLine();
    }
}


So you see how everything sorts itself out to real "objects" in a Tetris game, - the board, the pieces, the positions - this is OOP - and I assume you are learning how to program object orientated with such a toy-project (or homework)...

Happy coding and
Kind regards

Johannes
 
Share this answer
 
Comments
Maciej Los 8-Oct-14 13:16pm    
Well explained, +5!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900