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);
}
}
}
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);