[C#] Kółko i krzyżyk

DonArkanio
Użytkownik
Użytkownik
Posty: 44
Rejestracja: 13 lut 2012, o 11:07
Płeć: Mężczyzna
Lokalizacja: Bielsko
Podziękował: 9 razy

[C#] Kółko i krzyżyk

Post autor: DonArkanio »

Witam serdecznie. Na zajęciach robimy aktualnie C#. Mam na zadanie napisać grę w kółko krzyżyk z tym że utknąłem w momencie wybierania strzałkami miejsca do wpisania. Czy mógł by mi ktoś wytłumaczyć jak powinien wyglądać ten zapis, albo mnie poprawić? Jest zaznaczone od któego momentu. Z góry dziękuję za pomoc. Pozdrawiam

Kod: Zaznacz cały

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Gra
{
    class Program
    {
        static void RysujPlansze(char[,] board, char sing, int cursorTop, int cursorLeft)
        {
            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    if (cursorTop == i && cursorLeft == j)
                    {
                        if (board[i, j] != ' ')
                            Console.BackgroundColor = ConsoleColor.Red;
                            Console.Write(sing);
                            Console.ResetColor();
                    }
                    else
                        Console.Write(board[i, j]);
                    if (j != 2)
                        Console.Write("|");
                }
                Console.WriteLine();
                if (i != 2)
                    Console.WriteLine("-+-+-");
            }


        }

        static void InitBoard(out char[,] board)
        {
            board = new char[3, 3];
            for (int i = 0; i < board.GetLength(0); i++)
            {
                for (int j = 0; j < board.GetLength(1); j++)
                {
                    board[i, j] = ' ';
                }
            }
        }


        static void Main(string[] args)
        {
            char[,] board;
            InitBoard(out board);
            int cursorTop = 1;
            int cursorLeft = 1;
            board[0, 0] = 'O';
            board[2, 2] = 'X';

            RysujPlansze(board, 'T', cursorTop, cursorLeft);

           //Od tego momentu do końca.
            do
            {

                board = new char[cursorTop, cursorLeft];

                if (Console.ReadKey().Key == ConsoleKey.UpArrow)
                {
                    cursorLeft--;
                    Console.SetCursorPosition(cursorTop, cursorLeft);
                    Console.WriteLine('X');
                }

                if (Console.ReadKey().Key == ConsoleKey.DownArrow)
                {
                    cursorLeft++;
                    Console.SetCursorPosition(cursorTop, cursorLeft);
                    Console.WriteLine('X');
                }

                if (Console.ReadKey().Key == ConsoleKey.LeftArrow)
                {
                    cursorTop++;
                    Console.SetCursorPosition(cursorTop, cursorLeft);
                    Console.WriteLine('X');
                }

                if (Console.ReadKey().Key == ConsoleKey.RightArrow)
                {
                    cursorTop--;
                    Console.SetCursorPosition(cursorTop, cursorLeft);
                    Console.WriteLine('X');
                }
            } while (true);
        }
    }
}
-- 5 lis 2012, o 19:40 --

Już mam chodziło o taką pętle:

Kod: Zaznacz cały

 do
            {
                var Key = Console.ReadKey();
                Console.Clear();

                switch (Key.Key)
                {

                    case (ConsoleKey.UpArrow):
                        {

                            RysujPlansze(board, sing, --cursorTop, cursorLeft);
                            break;
                        }

                    case (ConsoleKey.DownArrow):
                        {
                            RysujPlansze(board, sing, ++cursorTop, cursorLeft);
                            break;
                        }

                    case (ConsoleKey.LeftArrow):
                        {
                            RysujPlansze(board, sing, cursorTop, --cursorLeft);
                            break;
                        }
                    case (ConsoleKey.RightArrow):
                        {
                            RysujPlansze(board, sing, cursorTop, ++cursorLeft);

                            break;
                        }

                };



            } while (true);
ODPOWIEDZ