Add some colors

This commit is contained in:
bwbl 2024-10-10 16:09:16 +02:00
parent 0e3ea6b53e
commit 37b2d36ab7

View File

@ -1,5 +1,4 @@

namespace P4
namespace P4
{
public enum Token
{
@ -10,10 +9,22 @@ namespace P4
public class Program
{
public readonly ConsoleColor FIRSTPLAYERCOLOR = ConsoleColor.Green;
public readonly ConsoleColor SECONDPLAYERCOLOR = ConsoleColor.Red;
public readonly ConsoleColor FIRSTPLAYERCOLOR = ConsoleColor.Cyan;
public readonly ConsoleColor SECONDPLAYERCOLOR = ConsoleColor.Yellow;
/*
public readonly ConsoleColor FIRSTPLAYERCOLOR = ConsoleColor.Cyan;
public readonly ConsoleColor SECONDPLAYERCOLOR = ConsoleColor.Yellow;
public void SetColor(ConsoleColor backgroundColor = ConsoleColor.Black, ConsoleColor forgroundColor = ConsoleColor.White)
public readonly ConsoleColor FIRSTPLAYERCOLOR = ConsoleColor.DarkGreen;
public readonly ConsoleColor SECONDPLAYERCOLOR = ConsoleColor.DarkYellow;
*/
const ConsoleColor backgroundColor = ConsoleColor.DarkCyan;
const ConsoleColor forgroundColor = ConsoleColor.Green;
public void SetColor(ConsoleColor backgroundColor = backgroundColor, ConsoleColor forgroundColor = forgroundColor)
{
Console.ForegroundColor = forgroundColor;
Console.BackgroundColor = backgroundColor;
@ -22,6 +33,8 @@ namespace P4
static void Main(string[] args)
{
new Program().SetColor();
Console.Clear();
Console.CursorVisible = false;
int[] boardSize = new Board().BoardSize();
Token[,] board = new Token[boardSize[0], boardSize[1]];
new Game().Start(board);
@ -38,6 +51,10 @@ namespace P4
Token currentPlayer = Token.FirstPlayer;
bool placed = false;
Console.Clear();
d.DrawPlayGroundBorder(board);
d.DrawBorder(board);
while (!CheckWin(board))
{
if (CheckDraw(board))
@ -45,7 +62,7 @@ namespace P4
d.DrawBoard(board);
d.DrawPlayGround(board, currentPlayer, pos);
ConsoleKey key = Console.ReadKey().Key;
ConsoleKey key = Console.ReadKey(true).Key;
switch (key)
{
case ConsoleKey.RightArrow:
@ -79,7 +96,7 @@ namespace P4
else if (currentPlayer == Token.FirstPlayer)
{
Console.Write("Le ");
p.SetColor(default, p.SECONDPLAYERCOLOR);
p.SetColor(forgroundColor: p.SECONDPLAYERCOLOR);
Console.Write("joueur 2");
p.SetColor();
Console.WriteLine(" a gagné");
@ -88,7 +105,7 @@ namespace P4
else if (currentPlayer == Token.SecondPlayer)
{
Console.Write("Le ");
p.SetColor(default, p.FIRSTPLAYERCOLOR);
p.SetColor(forgroundColor: p.FIRSTPLAYERCOLOR);
Console.Write("joueur 1");
p.SetColor();
Console.WriteLine(" a gagné");
@ -219,11 +236,11 @@ namespace P4
string[] input = { "", "" }; // initialisation de l'entrée utilisateur
// écriture des instructions en couleur grâce à la méthode WriteColor
Console.Write("La taille doit être comprise entre ");
p.SetColor(default, ConsoleColor.Red);
p.SetColor(forgroundColor: ConsoleColor.Red);
Console.Write("5x6");
p.SetColor();
Console.Write(" et ");
p.SetColor(default, ConsoleColor.Red);
p.SetColor(forgroundColor: ConsoleColor.Red);
Console.Write("13x16\n");
p.SetColor();
Console.WriteLine("exemple: 6x7 (6 lignes et 7 colonnes)");
@ -261,26 +278,97 @@ namespace P4
public class Draw
{
private Program p = new Program();
private int topSpacing = 2;
public void DrawPlayGroundBorder(Token[,] board)
{
Console.SetCursorPosition(0, topSpacing);
Console.Write("╔═══");
for (int x = 0; x < board.GetLength(0) - 1; x++)
{
Console.Write("╦═══");
}
Console.Write("╗");
for (int y = 0; y < board.GetLength(1); y++)
{
for (int x = 0; x < board.GetLength(0); x++)
{
Console.SetCursorPosition(x * 4, y * 2 + topSpacing + 1);
Console.Write("║");
}
Console.Write(" ║");
}
Console.SetCursorPosition(0, topSpacing + 2);
Console.Write("╚═══");
for (int x = 0; x < board.GetLength(0) - 1; x++)
{
Console.Write("╩═══");
}
Console.Write("╝");
}
public void DrawPlayGround(Token[,] board, Token currentPlayer, int pos)
{
for (int x = 0; x < board.GetLength(0); x++)
{
Console.SetCursorPosition(x, 0);
Console.SetCursorPosition(x*4+2, topSpacing + 1);
if (pos == x) Console.Write(DrawToken(currentPlayer));
else Console.Write(DrawToken(Token.None));
}
}
public void DrawBorder(Token[,] board)
{
int playgroundSpacing = topSpacing + 3;
Console.SetCursorPosition(0, playgroundSpacing);
Console.Write("╔═══");
for (int x = 0; x < board.GetLength(0) - 1; x++)
{
Console.Write("╦═══");
}
Console.Write("╗");
for (int y = 0; y < board.GetLength(1); y++)
{
for (int x = 0; x < board.GetLength(0); x++)
{
Console.SetCursorPosition(x * 4, y * 2 + playgroundSpacing + 1);
Console.Write("║");
}
Console.Write(" ║");
}
for (int y = 0; y < board.GetLength(1) - 1; y++)
{
Console.SetCursorPosition(0, y * 2 + playgroundSpacing + 2);
Console.Write("╠═══");
for (int x = 1; x < board.GetLength(0); x++)
{
Console.SetCursorPosition(x * 4, y * 2 + playgroundSpacing + 2);
Console.Write("╬═══");
}
Console.Write("╣");
}
Console.SetCursorPosition(0, board.GetLength(1) * 2 + playgroundSpacing);
Console.Write("╚═══");
for (int x = 0; x < board.GetLength(0) - 1; x++)
{
Console.Write("╩═══");
}
Console.Write("╝");
}
public void DrawBoard(Token[,] board)
{
Console.Clear();
int playgroundSpacing = topSpacing + 3;
for (int y = 0; y < board.GetLength(1); y++)
for (int x = 0; x < board.GetLength(0); x++)
{
Console.SetCursorPosition(x, y + 2);
Console.Write(DrawToken(board[x, y]));
Console.SetCursorPosition(x*4+2, y*2 + playgroundSpacing + 1);
Console.Write("{0}", DrawToken(board[x, y]));
}
}
@ -289,14 +377,14 @@ namespace P4
switch (token)
{
case Token.FirstPlayer:
p.SetColor(default, p.FIRSTPLAYERCOLOR);
p.SetColor(forgroundColor: p.FIRSTPLAYERCOLOR);
return "■";
case Token.SecondPlayer:
p.SetColor(default, p.SECONDPLAYERCOLOR);
return "o";
p.SetColor(forgroundColor: p.SECONDPLAYERCOLOR);
return "";
case Token.None:
p.SetColor(default, ConsoleColor.DarkGray);
return "#";
p.SetColor(forgroundColor: ConsoleColor.DarkGray);
return "";
default:
return default;
}