控制台c# - tik tak toe - 保存现场?

时间:2016-09-29 14:00:14

标签: c#

我刚刚学习C#,我得到了通过控制台应用程序构建“Tik Tak Toe”游戏的任务;但是我在代码中看到错误时遇到了很大的问题:当我输入一行和一列时,程序将只打印第一个玩家的棋子和颜色。不知怎的,我的第二个问题是,当涉及到下一个玩家时,控制台将不会保存当前的游戏统计数据并将绘制一个新的领域。

我编码错误了什么?

namespace Tik_Tak_Toe_
{
    class Program
    {
        static int[,] field = new int[3, 3];
        static Player[] p;
        static int i;
        static bool gamerunning = true;
        static Random rnd = new Random();
        static int currentplayer = 0;
        static int column;
        static int line;
        static int playercolumn = 7;
        static int playerline = 7;

        static void Main(string[] args)
        {
            INITIALIZE();
            Console.Clear();
            DrawField();
            currentplayer = rnd.Next(1, 2);

            while (gamerunning==true)
            {
                UPDATE();
            }

            Console.ReadLine();
        }

        static void INITIALIZE()
        {
            playerconfiguration();
            DrawField();
        }

        static void playerconfiguration()
        {
            p = new Player[2];
            for (i = 0; i <= 1; i++)
            {
                Console.WriteLine("Player " + (i + 1) + ", enter your name!");
                p[i].name = Console.ReadLine();
                Console.WriteLine(p[i].name + ", choose a color: ");
                ColorConfiguration();
                Console.WriteLine("... and your symbol example: X or O: ");
                p[i].pawn = Console.ReadKey().KeyChar;
                Console.WriteLine();
            }            
        }

        static void ColorConfiguration()
        {
            Console.WriteLine("Type one of the following colors: blue, pink, yellow, white, red oder darkblue");
            bool whatcolorinput = true;

            while (whatcolorinput == true)
            {
                string whatcolor = Console.ReadLine();

                switch (whatcolor)
                {
                    case "blue":
                        p[i].color = ConsoleColor.Cyan;
                        whatcolorinput = false;
                        break;

                    case "pink":
                        p[i].color = ConsoleColor.Magenta;
                        whatcolorinput = false;
                        break;

                    case "yellow":
                        p[i].color = ConsoleColor.Yellow;
                        whatcolorinput = false;
                        break;

                    case "white":
                        p[i].color = ConsoleColor.White;
                        whatcolorinput = false;
                        break;

                    case "red":
                        p[i].color = ConsoleColor.Red;
                        whatcolorinput = false;
                        break;

                    case "darkblue":
                        p[i].color = ConsoleColor.DarkCyan;
                        whatcolorinput = false;
                        break;

                    default:
                        Console.WriteLine("Type one of the following colors: blue, pink, yellow, white, red oder darkblue");
                        break;
                }
            }
        }

        static void UPDATE()
        {
            DrawField();
            Console.WriteLine(p[currentplayer].name + ", it's your turn!");
            PlayerInput();
            UpdateField();
            currentplayer = (currentplayer + 1) % 2;
        }

        static void DrawField()
        {
            for ( column=0; column<field.GetLength(1); column++)
            {
                Console.ForegroundColor = ConsoleColor.DarkMagenta;
                Console.Write((column+1) + "|");
                Console.ResetColor();
                for ( line=0; line<field.GetLength(0); line++)
                {
                    if (field[column,line]==0 && (column != playercolumn || line != playerline))
                    {
                        Console.Write("   ");
                    }
                    else
                    {
                        Console.ForegroundColor = p[field[playercolumn, playerline]].color;
                        Console.Write(" " + p[field[playercolumn, playerline]].pawn + " ");
                        Console.ResetColor();
                    }
                }
                Console.WriteLine();
            }
            Console.ForegroundColor = ConsoleColor.DarkMagenta;
            Console.WriteLine("  ________");
            Console.WriteLine("   1  2  3");
            Console.ResetColor();
        }

        static void PlayerInput()
        {
            Console.WriteLine("First, choose a column: ");
            bool columninput = true;

            while (columninput == true)
            {
                try
                {
                    playercolumn = Convert.ToInt32(Console.ReadLine());

                    if (column < 1 || column > 3)
                    {
                        Console.WriteLine("Choose a column.");
                    }
                    else
                    {
                        columninput = false;
                    }
                }
                catch
                {
                    Console.WriteLine("Choose a column.");
                }
            }

            playercolumn -= 1;
            Console.WriteLine("... and now a line");
            bool lineinput = true;

            while (lineinput == true)
            {
                try
                {
                    playerline = Convert.ToInt32(Console.ReadLine());

                    if (line < 1 || line > 3)
                    {
                        Console.WriteLine("Choose a line.");
                    }
                    else
                    {
                        lineinput = false;
                    }
                }
                catch
                {
                    Console.WriteLine("Choose a line.");
                }
            }
            playerline -= 1;
        }

        static void UpdateField()
        {
        }

        static void FINISH()
        {
        }
    }
}

2 个答案:

答案 0 :(得分:0)

你只有全局变量来存储playercolumn和playerline。

每次执行PlayerInputy时,都会替换此变量所具有的值。

您需要一个3x3矩阵来存储播放器选项,然后将此矩阵打印为电路板。如果已经选择了列和行,则需要拒绝用户输入。

您希望将用户移动存储在字段全局变量中,但您并未在任何地方进行分配。

我修改了代码,在更新时,重复播放输入,直到它对更新有效:

do
{
    PlayerInput();
} while (!UpdateField());

在DrawField中,检查字段变量中的值,而不是播放器输入

static void DrawField()
{
    for (column = 0; column < field.GetLength(1); column++)
    {
        Console.ForegroundColor = ConsoleColor.DarkMagenta;
        Console.Write((column + 1) + "|");
        Console.ResetColor();
        for (line = 0; line < field.GetLength(0); line++)
        {
            if (field[column, line] == 0)
            {
                Console.Write("   ");
            }
            else
            {
                Console.ForegroundColor = p[field[column, line] - 1].color;
                Console.Write(" " + p[field[column, line] - 1].pawn + " ");
                Console.ResetColor();
            }
        }
        Console.WriteLine();
    }
    Console.ForegroundColor = ConsoleColor.DarkMagenta;
    Console.WriteLine("  ________");
    Console.WriteLine("   1  2  3");
    Console.ResetColor();
}

并实施了UpdateField:

static bool UpdateField()
{
    if (field[playercolumn, playerline] != 0)
    {
        Console.WriteLine("Column already chosen");
        return false;
    }
    field[playercolumn, playerline] = currentplayer + 1;
    return true;
 }

还需要检查比赛何时结束。

答案 1 :(得分:-1)

您的代码中存在很多问题。首先,您从未将播放器输入存储在field数组中,因此显然在重绘表时,只绘制了最后一个输入。您还将一些变量更换为lineplayerline。在解决了这些问题和一些小问题并添加了一个Player类(我希望它或多或少是这样的,因为你没有提供它),正确绘制板的代码或多或少是这样的:

class Program
{
    static int[,] field = new int[3, 3];
    static Player[] p;
    static int i;
    static bool gamerunning = true;
    static Random rnd = new Random();
    static int currentplayer = 0;
    static int playercolumn = 7;
    static int playerline = 7;

    static void Main(string[] args)
    {
        INITIALIZE();
        Console.Clear();
        DrawField();
        currentplayer = rnd.Next(1, 2);
        while (gamerunning == true)
        {
            UPDATE();
        }
        Console.ReadLine();
    }

    public class Player
    {
        public string name { get; set; }
        public char pawn { get; set; }
        public ConsoleColor color { get; set;}
    }

    static void INITIALIZE()
    {
        playerconfiguration();
        DrawField();
    }

    static void playerconfiguration()
    {
        p = new Player[2];
        for (i = 0; i <= 1; i++)
        {
            p[i] = new Player();
            Console.WriteLine("Spieler " + (i + 1) + ", gib deinen Namen ein!");
            p[i].name = Console.ReadLine();
            Console.WriteLine(p[i].name + ", wähle deine Farbe: ");
            ColorConfiguration();
            Console.WriteLine("... und nun dein Symbol z.B. X oder O: ");
            p[i].pawn = Console.ReadKey().KeyChar;
            Console.WriteLine();
        }
    }

    static void ColorConfiguration()
    {
        Console.WriteLine("Gib eine der folgenden Farben ein: blau, pink, gelb, weiss, rot oder dunkelblau");
        bool whatcolorinput = true;
        while (whatcolorinput == true)
        {
            string whatcolor = Console.ReadLine();
            switch (whatcolor)
            {
                case "blau":
                    p[i].color = ConsoleColor.Cyan;
                    whatcolorinput = false;
                    break;
                case "pink":
                    p[i].color = ConsoleColor.Magenta;
                    whatcolorinput = false;
                    break;
                case "gelb":
                    p[i].color = ConsoleColor.Yellow;
                    whatcolorinput = false;
                    break;
                case "weiss":
                    p[i].color = ConsoleColor.White;
                    whatcolorinput = false;
                    break;
                case "rot":
                    p[i].color = ConsoleColor.Red;
                    whatcolorinput = false;
                    break;
                case "dunkelblau":
                    p[i].color = ConsoleColor.DarkCyan;
                    whatcolorinput = false;
                    break;
                default:
                    Console.WriteLine("Gib eine der folgenden Farben ein: blau, pink, gelb, weiss, rot oder dunkelblau");
                    break;
            }
        }
    }

    static void UPDATE()
    {
        DrawField();
        Console.WriteLine(p[currentplayer].name + ", du bist dran!");
        PlayerInput();
        UpdateField();
        currentplayer = (currentplayer + 1) % 2;
    }

    static void DrawField()
    {
        for (int line = 0; line < field.GetLength(1); line++)
        {
            Console.ForegroundColor = ConsoleColor.DarkMagenta;
            Console.Write((line + 1) + "|");
            Console.ResetColor();
            for (int column = 0; column < field.GetLength(0); column++)
            {
                if (field[line, column] == 0)
                {
                    Console.Write("   ");
                }
                else
                {
                    Console.ForegroundColor = p[field[line, column]-1].color;
                    Console.Write(" " + p[field[line, column] -1].pawn + " ");
                    Console.ResetColor();
                }
            }
            Console.WriteLine();
        }
        Console.ForegroundColor = ConsoleColor.DarkMagenta;
        Console.WriteLine("  ________");
        Console.WriteLine("   1  2  3");
        Console.ResetColor();
    }

    static void PlayerInput()
    {
        Console.WriteLine("Wähle zuerst eine Spalte: ");

        bool lineinput = true;
        while (lineinput == true)
        {
            try
            {
                playerline = Convert.ToInt32(Console.ReadLine());
                if (playerline < 1 || playerline > 3)
                {
                    Console.WriteLine("Wähle eine Spalte.");
                }
                else
                {
                    lineinput = false;
                }
            }
            catch
            {
                Console.WriteLine("Wähle eine Spalte.");
            }
        }

        bool columninput = true;
        while (columninput == true)
        {
            try
            {
                playercolumn = Convert.ToInt32(Console.ReadLine());
                if (playercolumn < 1 || playercolumn > 3)
                {
                    Console.WriteLine("Wähle eine Zeile.");
                }
                else
                {
                    columninput = false;
                }
            }
            catch
            {
                Console.WriteLine("Wähle eine Zeile.");
            }
        }
        playercolumn -= 1;
        Console.WriteLine("... und nun eine Spalte");


        //field[line-1, column] = new int();

        playerline -= 1;
        field[playerline, playercolumn] = currentplayer+1;
    }

    static void UpdateField()
    {

    }

    static void FINISH()
    {

    }
}

研究此代码并与您的代码进行比较,看看您的错误是什么。当然,你仍然必须检查一个位置是否被采取,当一个玩家赢了,什么时候没有移动并且游戏的结果是平局。