meilleur respect des conventions

This commit is contained in:
bwbl
2024-10-23 23:01:43 +02:00
parent 57d169531c
commit 51a28c132c

View File

@ -13,17 +13,17 @@
public readonly ConsoleColor SECONDPLAYERCOLOR = ConsoleColor.DarkMagenta;
const ConsoleColor backgroundColor = ConsoleColor.DarkYellow;
const ConsoleColor forgroundColor = ConsoleColor.Black;*/
const ConsoleColor foregroundColor = ConsoleColor.Black;*/
public readonly ConsoleColor FIRSTPLAYERCOLOR = ConsoleColor.Red;
public readonly ConsoleColor SECONDPLAYERCOLOR = ConsoleColor.Yellow;
const ConsoleColor backgroundColor = ConsoleColor.Black;
const ConsoleColor forgroundColor = ConsoleColor.White;
const ConsoleColor BACKGROUND_COLOR = ConsoleColor.Black;
const ConsoleColor FOREGROUND_COLOR = ConsoleColor.White;
public void SetColor(ConsoleColor backgroundColor = backgroundColor, ConsoleColor forgroundColor = forgroundColor)
public void SetColor(ConsoleColor backgroundColor = BACKGROUND_COLOR, ConsoleColor foregroundColor = FOREGROUND_COLOR)
{
Console.ForegroundColor = forgroundColor;
Console.ForegroundColor = foregroundColor;
Console.BackgroundColor = backgroundColor;
}
@ -36,16 +36,16 @@
int[] boardSize = new Board().BoardSize();
Token[,] board = new Token[boardSize[0], boardSize[1]];
new Game().Start(board);
new Game().replay();
new Game().Replay();
}
}
public class Game
{
private Draw d = new Draw();
private Program p = new Program();
private Draw _draw = new Draw();
private Program _program = new Program();
public void replay()
public void Replay()
{
Console.WriteLine("Voulez-vous rejouer ? (o/n)");
switch (Console.ReadKey().Key)
@ -57,35 +57,36 @@
int[] boardSize = new Board().BoardSize();
Token[,] board = new Token[boardSize[0], boardSize[1]];
new Game().Start(board);
new Game().replay();
new Game().Replay();
break;
case ConsoleKey.N:
return;
default:
Console.WriteLine("Entrée invalide");
replay();
Replay();
break;
}
}
public void Start(Token[,] board)
{
int pos = 0;
int currentPosition = 0;
Token currentPlayer = Token.FirstPlayer;
bool placed = false;
Console.Clear();
d.DrawTitle(d.leftSpacing);
d.DrawPlayGroundBorder(board);
d.DrawBorder(board);
d.DrawHelp(board);
Console.CursorVisible = false;
_draw.DrawTitle(_draw.leftSpacing);
_draw.DrawPlayGroundBorder(board);
_draw.DrawBorder(board);
_draw.DrawHelp(board);
while (!CheckWin(board))
{
if (CheckDraw(board))
break;
d.DrawBoard(board);
d.DrawPlayGround(board, currentPlayer, pos);
_draw.DrawBoard(board);
_draw.DrawPlayGround(board, currentPlayer, currentPosition);
ConsoleKey key = Console.ReadKey(true).Key;
if (key == ConsoleKey.Escape)
@ -96,18 +97,22 @@
switch (key)
{
case ConsoleKey.RightArrow:
if (pos < board.GetLength(0) - 1)
pos++;
if (currentPosition < board.GetLength(0) - 1)
currentPosition++;
else
currentPosition = 0;
break;
case ConsoleKey.LeftArrow:
if (pos > 0)
pos--;
if (currentPosition > 0)
currentPosition--;
else
currentPosition = board.GetLength(0) - 1;
break;
case ConsoleKey.Enter:
case ConsoleKey.Spacebar:
placed = PlaceToken(board, currentPlayer, pos);
placed = PlaceToken(board, currentPlayer, currentPosition);
if (placed && currentPlayer == Token.FirstPlayer)
currentPlayer = Token.SecondPlayer;
else if (placed && currentPlayer == Token.SecondPlayer)
@ -116,29 +121,29 @@
break;
}
}
d.DrawBoard(board);
d.DrawPlayGround(board, currentPlayer, pos);
p.SetColor();
_draw.DrawBoard(board);
_draw.DrawPlayGround(board, currentPlayer, currentPosition);
_program.SetColor();
//Console.Clear();
Console.SetCursorPosition(0, board.GetLength(1) * 2 + d.topSpacing + 4);
Console.SetCursorPosition(0, board.GetLength(1) * 2 + _draw.topSpacing + 4);
// currentplayer est inversé
if (CheckDraw(board))
Console.WriteLine("il y a eu une égalité");
else if (currentPlayer == Token.FirstPlayer)
{
Console.Write("Le ");
p.SetColor(forgroundColor: p.SECONDPLAYERCOLOR);
_program.SetColor(foregroundColor: _program.SECONDPLAYERCOLOR);
Console.Write("joueur 2");
p.SetColor();
_program.SetColor();
Console.WriteLine(" a gagné");
}
else if (currentPlayer == Token.SecondPlayer)
{
Console.Write("Le ");
p.SetColor(forgroundColor: p.FIRSTPLAYERCOLOR);
_program.SetColor(foregroundColor: _program.FIRSTPLAYERCOLOR);
Console.Write("joueur 1");
p.SetColor();
_program.SetColor();
Console.WriteLine(" a gagné");
} else if (currentPlayer == Token.None)
{
@ -249,12 +254,12 @@
return false;
}
private bool PlaceToken(Token[,] board, Token currentPlayer, int pos)
private bool PlaceToken(Token[,] board, Token currentPlayer, int currentPosition)
{
for (int i = board.GetLength(1) - 1; i >= 0; i--)
{
if (board[pos, i] != Token.None) continue;
board[pos, i] = currentPlayer;
if (board[currentPosition, i] != Token.None) continue;
board[currentPosition, i] = currentPlayer;
return true;
}
return false;
@ -262,20 +267,20 @@
}
public class Board
{
private Program p = new Program();
private Program _program = new Program();
public int[] BoardSize()
{
int[] boardSize = { 0, 0 }; // initialisation de la taille
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(forgroundColor: ConsoleColor.Red);
_program.SetColor(foregroundColor: ConsoleColor.Red);
Console.Write("5x6");
p.SetColor();
_program.SetColor();
Console.Write(" et ");
p.SetColor(forgroundColor: ConsoleColor.Red);
_program.SetColor(foregroundColor: ConsoleColor.Red);
Console.Write("13x16\n");
p.SetColor();
_program.SetColor();
Console.WriteLine("exemple: 6x7 (6 lignes et 7 colonnes)");
Console.Write("taille: ");
@ -310,7 +315,7 @@
public class Draw
{
private Program p = new Program();
private Program _program = new Program();
public int topSpacing = 4;
public int leftSpacing = 10;
public void DrawPlayGroundBorder(Token[,] board)
@ -342,13 +347,13 @@
Console.Write("╝");
}
public void DrawPlayGround(Token[,] board, Token currentPlayer, int pos)
public void DrawPlayGround(Token[,] board, Token currentPlayer, int currentPosition)
{
for (int x = 0; x < board.GetLength(0); x++)
{
Console.SetCursorPosition(leftSpacing + x * 4+2, topSpacing + 1);
if (pos == x) Console.Write(DrawToken(currentPlayer));
if (currentPosition == x) Console.Write(DrawToken(currentPlayer));
else Console.Write(DrawToken(Token.None));
}
}
@ -411,13 +416,13 @@
switch (token)
{
case Token.FirstPlayer:
p.SetColor(forgroundColor: p.FIRSTPLAYERCOLOR);
_program.SetColor(foregroundColor: _program.FIRSTPLAYERCOLOR);
return "■";
case Token.SecondPlayer:
p.SetColor(forgroundColor: p.SECONDPLAYERCOLOR);
_program.SetColor(foregroundColor: _program.SECONDPLAYERCOLOR);
return "■";
case Token.None:
p.SetColor(forgroundColor: ConsoleColor.DarkGray);
_program.SetColor(foregroundColor: ConsoleColor.DarkGray);
return "≡";
default:
return default;
@ -446,13 +451,13 @@
WriteHelp(board, 18, 3, "Spacebar ou Enter");
WriteHelp(board, 4, 4, "Quitter");
WriteHelp(board, 18, 4, "Escape");
p.SetColor(forgroundColor: p.FIRSTPLAYERCOLOR);
_program.SetColor(foregroundColor: _program.FIRSTPLAYERCOLOR);
WriteHelp(board, 4, 6, "Joueur 1: ");
p.SetColor(p.FIRSTPLAYERCOLOR);
_program.SetColor(_program.FIRSTPLAYERCOLOR);
WriteHelp(board, 14, 6, " ");
p.SetColor(forgroundColor: p.SECONDPLAYERCOLOR);
_program.SetColor(foregroundColor: _program.SECONDPLAYERCOLOR);
WriteHelp(board, 18, 6, "Joueur 2: ");
p.SetColor(p.SECONDPLAYERCOLOR);
_program.SetColor(_program.SECONDPLAYERCOLOR);
WriteHelp(board, 28, 6, " ");
}