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