2
\$\begingroup\$

I am pretty new to C# and so i would like to ask you, how i could improve my code. I am currently coding a Visual Studio C# Console Game where you have to stack blocks. Currently you can only Stack 3 blocks because my code is getting really long and i am really losing my orientation.

The voids "Reihe1()", "Reihe2()" and "Reihe3()" are for the different rows. Is there any way i can improve my code? To completely understand my game pls. just try it in Visual Studio.

I hope that you can help me. And please keep in mind, that i am a beginner!

Concept:

Ok so first you have "3 blocks". You can place them were you want. Then after that you have another "3 blocks" but now the blocks are 1 "layer" higher and you have to "stack" them exactly on the other 3. If you can "stack" them exactly over the "3 blocks" which are in the layer down, you can continue with another "3 blocks" one layer higher. Otherwise you'll have 2 "blocks" or 1 "block" to continue. "layer" = row

If you completely missed all 3 "blocks" you'll loose. Btw. you can copy the code into vs to try the game. And sorry for my bad English

Stacker Game: https://en.wikipedia.org/wiki/Stacker_(arcade_game)

Here is the code:

//(c) DomeGamer14

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using static System.Console;
using System.IO;

namespace Test1
{
    class Program
    {
        public static string[] field = new string[70];
        public static int counter = 0;

        static void Main(string[] args)
        {
            for (int i = 0; i < field.Length; i++)
            {
                field[i] = "O";
                Write(field[i]);
                if (i == 6 || i == 13 || i == 20 || i == 27 || i == 34 || i == 41 || i == 48 || i == 55 || i == 62 || i == 69)
                {
                    WriteLine();
                }
            }
            ReadLine();

            Reihe1();
            ReadKey();
            Clear();
            Reihe2();
            ReadKey();
            ReadKey();
            Clear();
            Reihe3();
            ReadKey();
            ReadKey();
        }

        static void Show()
        {
            for (int i = 0; i < field.Length; i++)
            {
                Write(field[i]);
                if (i == 6 || i == 13 || i == 20 || i == 27 || i == 34 || i == 41 || i == 48 || i == 55 || i == 62 || i == 69)
                {
                    WriteLine();
                }
            }
        }

        static void Reihe1()
        {
            do
            {
                Clear();
                field[63] = "X";
                field[64] = "X";
                field[65] = "X";
                Show();
                if (Console.KeyAvailable == true)
                    break;
                Thread.Sleep(200);
                Clear();
                field[63] = "O";
                field[66] = "X";
                Show();
                if (Console.KeyAvailable == true)
                    break;
                Thread.Sleep(200);
                Clear();
                field[64] = "O";
                field[67] = "X";
                Show();
                if (Console.KeyAvailable == true)
                    break;
                Thread.Sleep(200);
                Clear();
                field[65] = "O";
                field[68] = "X";
                Show();
                if (Console.KeyAvailable == true)
                    break;
                Thread.Sleep(200);
                Clear();
                field[66] = "O";
                field[69] = "X";
                Show();
                if (Console.KeyAvailable == true)
                    break;
                Thread.Sleep(200);
                Clear();
                field[69] = "O";
                field[66] = "X";
                Show();
                if (Console.KeyAvailable == true)
                    break;
                Thread.Sleep(200);
                Clear();
                field[68] = "O";
                field[65] = "X";
                Show();
                if (Console.KeyAvailable == true)
                    break;
                Thread.Sleep(200);
                Clear();
                field[67] = "O";
                field[64] = "X";
                Show();
                if (Console.KeyAvailable == true)
                    break;
                Thread.Sleep(200);
                Clear();
                field[66] = "O";
                field[63] = "X";
                Show();
                if (Console.KeyAvailable == true)
                    break;
            } while (true);
        }

        static void Reihe2()
        {

            do
            {
                Clear();
                field[56] = "X";
                field[57] = "X";
                field[58] = "X";
                Show();
                if (Console.KeyAvailable == true)
                    break;
                Thread.Sleep(200);
                Clear();
                field[56] = "O";
                field[59] = "X";
                Show();
                if (Console.KeyAvailable == true)
                    break;
                Thread.Sleep(200);
                Clear();
                field[57] = "O";
                field[60] = "X";
                Show();
                if (Console.KeyAvailable == true)
                    break;
                Thread.Sleep(200);
                Clear();
                field[58] = "O";
                field[61] = "X";
                Show();
                if (Console.KeyAvailable == true)
                    break;
                Thread.Sleep(200);
                Clear();
                field[59] = "O";
                field[62] = "X";
                Show();
                if (Console.KeyAvailable == true)
                    break;
                Thread.Sleep(200);
                Clear();
                field[62] = "O";
                field[59] = "X";
                Show();
                if (Console.KeyAvailable == true)
                    break;
                Thread.Sleep(200);
                Clear();
                field[61] = "O";
                field[58] = "X";
                Show();
                if (Console.KeyAvailable == true)
                    break;
                Thread.Sleep(200);
                Clear();
                field[60] = "O";
                field[57] = "X";
                Show();
                if (Console.KeyAvailable == true)
                    break;
                Thread.Sleep(200);
                Clear();
                field[59] = "O";
                field[56] = "X";
                Show();
                if (Console.KeyAvailable == true)
                    break;
            } while (true);


            for (int i = 0; i < 63; i++)
            {
                if (field[i] == "X" && field[i + 7] != "X")
                {
                    Thread.Sleep(2000);
                    field[i] = "O";
                    Clear();
                    Show();
                    counter += 1;
                    if (counter == 3)
                    {
                        Clear();
                        WriteLine("Game Over!");
                        WriteLine("Press a Key to end the program!");
                        ReadKey();
                        ReadKey();
                        Environment.Exit(0);
                    }
                }
            }
        }

        static void Reihe3()
        {
            if (counter == 2)
            {
                do
                {
                    Clear();
                    field[49] = "X";
                    Show();
                    if (Console.KeyAvailable == true)
                        break;
                    Thread.Sleep(200);
                    Clear();
                    field[49] = "O";
                    field[50] = "X";
                    Show();
                    if (Console.KeyAvailable == true)
                        break;
                    Thread.Sleep(200);
                    Clear();
                    field[50] = "O";
                    field[51] = "X";
                    Show();
                    if (Console.KeyAvailable == true)
                        break;
                    Thread.Sleep(200);
                    Clear();
                    field[51] = "O";
                    field[52] = "X";
                    Show();
                    if (Console.KeyAvailable == true)
                        break;
                    Thread.Sleep(200);
                    Clear();
                    field[52] = "O";
                    field[53] = "X";
                    Show();
                    if (Console.KeyAvailable == true)
                        break;
                    Thread.Sleep(200);
                    Clear();
                    field[53] = "O";
                    field[54] = "X";
                    Show();
                    if (Console.KeyAvailable == true)
                        break;
                    Thread.Sleep(200);
                    Clear();
                    field[54] = "O";
                    field[55] = "X";
                    Show();
                    if (Console.KeyAvailable == true)
                        break;
                    Thread.Sleep(200);
                    Clear();
                    field[55] = "O";
                    field[54] = "X";
                    Show();
                    if (Console.KeyAvailable == true)
                        break;
                    Thread.Sleep(200);
                    Clear();
                    field[54] = "O";
                    field[53] = "X";
                    Show();
                    if (Console.KeyAvailable == true)
                        break;
                    Thread.Sleep(200);
                    Clear();
                    field[53] = "O";
                    field[52] = "X";
                    Show();
                    if (Console.KeyAvailable == true)
                        break;
                    Thread.Sleep(200);
                    Clear();
                    field[52] = "O";
                    field[51] = "X";
                    Show();
                    if (Console.KeyAvailable == true)
                        break;
                    Thread.Sleep(200);
                    Clear();
                    field[51] = "O";
                    field[50] = "X";
                    Show();
                    if (Console.KeyAvailable == true)
                        break;
                    Thread.Sleep(200);
                    Clear();
                    field[50] = "O";
                    field[49] = "X";
                    Show();
                    if (Console.KeyAvailable == true)
                        break;
                } while (true);
                counter = 0;

                for (int i = 0; i < 63; i++)
                {
                    if (field[i] == "X" && field[i + 7] != "X")
                    {
                        Thread.Sleep(2000);
                        field[i] = "O";
                        Clear();
                        Show();
                        counter += 1;
                        if (counter == 3)
                        {
                            Clear();
                            WriteLine("Game Over!");
                            WriteLine("Press a Key to end the program!");
                            ReadKey();
                            ReadKey();
                            Environment.Exit(0);
                        }
                    }
                }
            }
            else if (counter == 1)
            {
                do
                {
                    Clear();
                    field[49] = "X";
                    field[50] = "X";
                    Show();
                    if (Console.KeyAvailable == true)
                        break;
                    Thread.Sleep(200);
                    Clear();
                    field[49] = "O";
                    field[51] = "X";
                    Show();
                    if (Console.KeyAvailable == true)
                        break;
                    Thread.Sleep(200);
                    Clear();
                    field[50] = "O";
                    field[52] = "X";
                    Show();
                    if (Console.KeyAvailable == true)
                        break;
                    Thread.Sleep(200);
                    Clear();
                    field[51] = "O";
                    field[53] = "X";
                    Show();
                    if (Console.KeyAvailable == true)
                        break;
                    Thread.Sleep(200);
                    Clear();
                    field[52] = "O";
                    field[54] = "X";
                    Show();
                    if (Console.KeyAvailable == true)
                        break;
                    Thread.Sleep(200);
                    Clear();
                    field[53] = "O";
                    field[55] = "X";
                    Show();
                    if (Console.KeyAvailable == true)
                        break;
                    Thread.Sleep(200);
                    Clear();
                    field[55] = "O";
                    field[53] = "X";
                    Show();
                    if (Console.KeyAvailable == true)
                        break;
                    Thread.Sleep(200);
                    Clear();
                    field[54] = "O";
                    field[52] = "X";
                    Show();
                    if (Console.KeyAvailable == true)
                        break;
                    Thread.Sleep(200);
                    Clear();
                    field[53] = "O";
                    field[51] = "X";
                    Show();
                    if (Console.KeyAvailable == true)
                        break;
                    Thread.Sleep(200);
                    Clear();
                    field[52] = "O";
                    field[50] = "X";
                    Show();
                    if (Console.KeyAvailable == true)
                        break;
                    Thread.Sleep(200);
                    Clear();
                    field[51] = "O";
                    field[49] = "X";
                    Show();
                    if (Console.KeyAvailable == true)
                        break;

                } while (true);

                counter = 0;

                for (int i = 0; i < 63; i++)
                {
                    if (field[i] == "X" && field[i + 7] != "X")
                    {
                        Thread.Sleep(2000);
                        field[i] = "O";
                        Clear();
                        Show();
                        counter += 1;
                        if (counter == 3)
                        {
                            Clear();
                            WriteLine("Game Over!");
                            WriteLine("Press a Key to end the program!");
                            ReadKey();
                            ReadKey();
                            Environment.Exit(0);
                        }
                    }
                }
            }
            else
            {
                do
                {
                    Clear();
                    field[49] = "X";
                    field[50] = "X";
                    field[51] = "X";
                    Show();
                    if (Console.KeyAvailable == true)
                        break;
                    Thread.Sleep(200);
                    Clear();
                    field[49] = "O";
                    field[52] = "X";
                    Show();
                    if (Console.KeyAvailable == true)
                        break;
                    Thread.Sleep(200);
                    Clear();
                    field[50] = "O";
                    field[53] = "X";
                    Show();
                    if (Console.KeyAvailable == true)
                        break;
                    Thread.Sleep(200);
                    Clear();
                    field[51] = "O";
                    field[54] = "X";
                    Show();
                    if (Console.KeyAvailable == true)
                        break;
                    Thread.Sleep(200);
                    Clear();
                    field[52] = "O";
                    field[55] = "X";
                    Show();
                    if (Console.KeyAvailable == true)
                        break;
                    Thread.Sleep(200);
                    Clear();
                    field[55] = "O";
                    field[52] = "X";
                    Show();
                    if (Console.KeyAvailable == true)
                        break;
                    Thread.Sleep(200);
                    Clear();
                    field[54] = "O";
                    field[51] = "X";
                    Show();
                    if (Console.KeyAvailable == true)
                        break;
                    Thread.Sleep(200);
                    Clear();
                    field[53] = "O";
                    field[50] = "X";
                    Show();
                    if (Console.KeyAvailable == true)
                        break;
                    Thread.Sleep(200);
                    Clear();
                    field[52] = "O";
                    field[49] = "X";
                    Show();
                    if (Console.KeyAvailable == true)
                        break;


                } while (true);

                counter = 0;

                for (int i = 0; i < 63; i++)
                {
                    if (field[i] == "X" && field[i + 7] != "X")
                    {
                        Thread.Sleep(2000);
                        field[i] = "O";
                        Clear();
                        Show();
                        counter += 1;
                        if (counter == 3)
                        {
                            Clear();
                            WriteLine("Game Over!");
                            WriteLine("Press a Key to end the program!");
                            ReadKey();
                            ReadKey();
                            Environment.Exit(0);
                        }
                    }
                }
            }
        }
    }
}
```
\$\endgroup\$
  • 1
    \$\begingroup\$ Could you describe your solution in more details? That's a lot of code and very few words about how it actually works. \$\endgroup\$ – t3chb0t May 24 '19 at 20:26
  • \$\begingroup\$ Ok so first you have "3 blocks". You can place them were you want. Then after that you have another "3 blocks" but now the blocks are 1 "layer" higher and you have to "stack" them exactly on the other 3. If you can "stack" them exactly over the "3 blocks" which are in the layer down, you can continue with another "3 blocks" one layer higher. Otherwise you'll have 2 "blocks" or 1 "block" to continue. "layer" = row \$\endgroup\$ – DomeGamer 14 May 24 '19 at 20:39
  • \$\begingroup\$ If you completly missed all 3 "blocks" you'll loose. Btw. you can copy the code into vs to try the game. And sorry for my bad english. \$\endgroup\$ – DomeGamer 14 May 24 '19 at 20:40
  • \$\begingroup\$ If somebody has time, we could also talk on Discord. \$\endgroup\$ – DomeGamer 14 May 24 '19 at 20:51
  • 1
    \$\begingroup\$ Welcome to CR! Feel free to edit these details into the post itself, and add more if you want. The more context reviewers have, the better! \$\endgroup\$ – Mathieu Guindon May 24 '19 at 20:58
3
\$\begingroup\$

There are a lot of improvements that had to be made, the most important for you to understand are arrays, especially 2D arrays; for loops; and ternary operators.

using System.Threading;
using static System.Console;

namespace Test1
{
    class Program
    {
        const int boardWidth = 7;
        const int boardHeight = 10;
        static bool[,] lines = new bool[boardWidth, boardHeight];
        static int nextLine = boardHeight - 1;
        static int nextLinePos = 0;
        static int nextLineMoving = 1;
        static int currentCupNumber = 3;
        static WinState winState = WinState.Undetermined;
        static void Main(string[] args)
        {
            while (winState == WinState.Undetermined)
            {
                UpdateGameState();
                ShowGameState();
            }
            Clear();
            if (winState == WinState.Won)
            {
                WriteLine("You have won the game!");
            }
            else
            {
                WriteLine("You have lost the game :(");
            }
            ReadKey();
        }
        static void ShowGameState()
        {
            Clear();
            for (int y = 0; y < boardHeight; y++)
            {
                for (int x = 0; x < boardWidth; x++)
                {
                    Write(lines[x, y] ? 'X' : 'O');
                }
                WriteLine();
            }
            SetCursorPosition(nextLinePos, nextLine);
            for (int i = 0; i < currentCupNumber; i++)
            {
                Write('X');
            }
        }
        static void UpdateGameState()
        {
            if (KeyAvailable)
            {
                ReadKey();
                bool accept = false;
                if (nextLine == boardHeight - 1)
                {
                    accept = true;
                    for (int i = 0; i < currentCupNumber; i++)
                    {
                        lines[i + nextLinePos, nextLine] = true;
                    }
                }
                else
                {
                    int align = 0;
                    for (int i = 0; i < currentCupNumber; i++)
                    {
                        if (lines[i + nextLinePos, nextLine + 1])
                        {
                            align++;
                            accept = true;
                            lines[nextLinePos + i, nextLine] = true;
                        }
                    }
                    currentCupNumber = align;
                }
                nextLine--;
                if (nextLine == 0)
                {
                    winState = WinState.Won;
                }
                else if (accept == false)
                {
                    winState = WinState.Lost;
                }
            }
            else
            {
                nextLinePos += nextLineMoving;
                nextLineMoving = nextLinePos >= boardWidth - currentCupNumber || nextLinePos == 0 ? nextLineMoving * -1 : nextLineMoving;
            }
            Thread.Sleep(200);
        }
    }
    enum WinState { Undetermined, Won, Lost }
}
| improve this answer | |
\$\endgroup\$
  • 2
    \$\begingroup\$ Welcome to Code Review! In order to be also useful for others who may find this question, it would be best to explain the code here instead of in private mails. \$\endgroup\$ – AlexV May 24 '19 at 21:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.