Skip to content

Commit

Permalink
Добавил меню і відображаю чий ход
Browse files Browse the repository at this point in the history
  • Loading branch information
kimpors committed Mar 6, 2021
1 parent 5b0c735 commit a82503a
Showing 1 changed file with 48 additions and 12 deletions.
60 changes: 48 additions & 12 deletions TicTacToe/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,53 +23,88 @@ public Player(int score, char symbol , string name)

}




class Program
{


// Вивод поля
static void PrintField(char[] field)
static void PrintField(char[] field, Player player)
{
Console.WriteLine($"{field[0]} | {field[1]} | {field[2]}");
Console.WriteLine($"{field[0]} | {field[1]} | {field[2]} \t\t step {player.Name}");
Console.WriteLine($"{field[3]} | {field[4]} | {field[5]}");
Console.WriteLine($"{field[6]} | {field[7]} | {field[8]}");

}

static void Main(string[] args)
{
// Меню

bool isDone = false;
do
{
Console.WriteLine("1.Play\n2.Log\n3.Quit");
string choise = Console.ReadKey(true).KeyChar.ToString();

switch (choise)
{
case "1":
isDone = true;
break;

case "3":
return;

default:
Console.WriteLine("Hmmm Try again");
Console.ReadKey(true);
break;
}
} while (!isDone);

//Ігра

char[] field = { '1', '2', '3', '4', '5', '6', '7', '8', '9' };


Player P1 = new Player(0, 'X', "John");
Player P2 = new Player(0, 'O', "Nica");

Player playerStep = new Player(); // Показивает хто ходить

for (int i = 0; i < 9; i++)
{
Console.Clear();
PrintField(field);


playerStep = i % 2 == 0 ? P1 : P2;


PrintField(field, playerStep);


byte input;
byte.TryParse(Console.ReadKey(true).KeyChar.ToString(), out input);


//Ето для того чтоби не можна було замінити уже поставлений знак
if (input > 0)
{
if (field[input - 1] == Convert.ToChar(input.ToString()))
{
field[input - 1] = i % 2 == 0 ? P1.Symbol : P2.Symbol;
field[input - 1] = playerStep.Symbol;
}
else
{
i--;
}
}






// Логіка вийграша
//Да да я знаю громосткій кусок кода
if (field[0] == field[1] && field[1] == field[2] ||
field[3] == field[4] && field[4] == field[5] ||
field[6] == field[7] && field[7] == field[8] ||
Expand All @@ -80,9 +115,10 @@ static void Main(string[] args)
field[2] == field[5] && field[5] == field[8] )
{
Console.Clear();
PrintField(field);
PrintField(field,playerStep);
Console.WriteLine($"{playerStep.Name} win");


Console.WriteLine("You win");
Console.ReadKey(true);
return;
}
Expand Down

0 comments on commit a82503a

Please sign in to comment.