471 lines
18 KiB
C#
471 lines
18 KiB
C#
namespace P4
|
|
{
|
|
public enum Token
|
|
{
|
|
None,
|
|
FirstPlayer,
|
|
SecondPlayer,
|
|
}
|
|
|
|
public class Program
|
|
{
|
|
/*public readonly ConsoleColor FIRSTPLAYERCOLOR = ConsoleColor.Magenta;
|
|
public readonly ConsoleColor SECONDPLAYERCOLOR = ConsoleColor.DarkMagenta;
|
|
|
|
const ConsoleColor backgroundColor = ConsoleColor.DarkYellow;
|
|
const ConsoleColor foregroundColor = ConsoleColor.Black;*/
|
|
|
|
public readonly ConsoleColor FIRSTPLAYERCOLOR = ConsoleColor.Red;
|
|
public readonly ConsoleColor SECONDPLAYERCOLOR = ConsoleColor.Yellow;
|
|
|
|
const ConsoleColor BACKGROUND_COLOR = ConsoleColor.Black;
|
|
const ConsoleColor FOREGROUND_COLOR = ConsoleColor.White;
|
|
|
|
public void SetColor(ConsoleColor backgroundColor = BACKGROUND_COLOR, ConsoleColor foregroundColor = FOREGROUND_COLOR)
|
|
{
|
|
Console.ForegroundColor = foregroundColor;
|
|
Console.BackgroundColor = backgroundColor;
|
|
}
|
|
|
|
static void Main(string[] args)
|
|
{
|
|
new Program().SetColor();
|
|
Console.Clear();
|
|
new Draw().DrawTitle();
|
|
Console.CursorVisible = false;
|
|
int[] boardSize = new Board().BoardSize();
|
|
Token[,] board = new Token[boardSize[0], boardSize[1]];
|
|
new Game().Start(board);
|
|
new Game().Replay();
|
|
}
|
|
}
|
|
|
|
public class Game
|
|
{
|
|
private Draw _draw = new Draw();
|
|
private Program _program = new Program();
|
|
|
|
public void Replay()
|
|
{
|
|
Console.WriteLine("Voulez-vous rejouer ? (o/n)");
|
|
switch (Console.ReadKey().Key)
|
|
{
|
|
case ConsoleKey.O:
|
|
new Program().SetColor();
|
|
Console.Clear();
|
|
new Draw().DrawTitle();
|
|
int[] boardSize = new Board().BoardSize();
|
|
Token[,] board = new Token[boardSize[0], boardSize[1]];
|
|
new Game().Start(board);
|
|
new Game().Replay();
|
|
break;
|
|
case ConsoleKey.N:
|
|
return;
|
|
default:
|
|
Console.WriteLine("Entrée invalide");
|
|
Replay();
|
|
break;
|
|
|
|
}
|
|
}
|
|
public void Start(Token[,] board)
|
|
{
|
|
int currentPosition = 0;
|
|
Token currentPlayer = Token.FirstPlayer;
|
|
bool placed = false;
|
|
|
|
Console.Clear();
|
|
Console.CursorVisible = false;
|
|
_draw.DrawTitle(_draw.leftSpacing);
|
|
_draw.DrawPlayGroundBorder(board);
|
|
_draw.DrawBorder(board);
|
|
_draw.DrawHelp(board);
|
|
|
|
while (!CheckWin(board))
|
|
{
|
|
if (CheckDraw(board))
|
|
break;
|
|
_draw.DrawBoard(board);
|
|
_draw.DrawPlayGround(board, currentPlayer, currentPosition);
|
|
|
|
ConsoleKey key = Console.ReadKey(true).Key;
|
|
if (key == ConsoleKey.Escape)
|
|
{
|
|
currentPlayer = Token.None;
|
|
break;
|
|
}
|
|
switch (key)
|
|
{
|
|
case ConsoleKey.RightArrow:
|
|
if (currentPosition < board.GetLength(0) - 1)
|
|
currentPosition++;
|
|
else
|
|
currentPosition = 0;
|
|
break;
|
|
|
|
case ConsoleKey.LeftArrow:
|
|
if (currentPosition > 0)
|
|
currentPosition--;
|
|
else
|
|
currentPosition = board.GetLength(0) - 1;
|
|
break;
|
|
|
|
case ConsoleKey.Enter:
|
|
case ConsoleKey.Spacebar:
|
|
placed = PlaceToken(board, currentPlayer, currentPosition);
|
|
if (placed && currentPlayer == Token.FirstPlayer)
|
|
currentPlayer = Token.SecondPlayer;
|
|
else if (placed && currentPlayer == Token.SecondPlayer)
|
|
currentPlayer = Token.FirstPlayer;
|
|
|
|
break;
|
|
}
|
|
}
|
|
_draw.DrawBoard(board);
|
|
_draw.DrawPlayGround(board, currentPlayer, currentPosition);
|
|
_program.SetColor();
|
|
//Console.Clear();
|
|
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 ");
|
|
_program.SetColor(foregroundColor: _program.SECONDPLAYERCOLOR);
|
|
Console.Write("joueur 2");
|
|
_program.SetColor();
|
|
Console.WriteLine(" a gagné");
|
|
|
|
}
|
|
else if (currentPlayer == Token.SecondPlayer)
|
|
{
|
|
Console.Write("Le ");
|
|
_program.SetColor(foregroundColor: _program.FIRSTPLAYERCOLOR);
|
|
Console.Write("joueur 1");
|
|
_program.SetColor();
|
|
Console.WriteLine(" a gagné");
|
|
} else if (currentPlayer == Token.None)
|
|
{
|
|
Console.WriteLine("La partie à été arrêtée");
|
|
}
|
|
Console.WriteLine("La partie à duré " + Turn(board) + " tours");
|
|
}
|
|
|
|
static int Turn(Token[,] board)
|
|
{
|
|
int count = 0; // compteur
|
|
for (int y = 0; y < board.GetLength(1); y++)
|
|
{
|
|
for (int x = 0; x < board.GetLength(0); x++)
|
|
{
|
|
if (board[x, y] != 0)
|
|
{
|
|
count++;
|
|
}
|
|
}
|
|
}
|
|
return count;
|
|
}
|
|
private bool CheckDraw(Token[,] board)
|
|
{
|
|
for (int y = 0; y < board.GetLength(1); y++)
|
|
{
|
|
for (int x = 0; x < board.GetLength(0); x++)
|
|
{
|
|
if (board[x, y] == 0)
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
private bool CheckWin(Token[,] board)
|
|
{
|
|
Token[] players = {Token.FirstPlayer, Token.SecondPlayer};
|
|
// boucle qui alterne entre les joueurs 1 et 2
|
|
foreach (var j in players)
|
|
{
|
|
// boucle qui fait toutes les colonnes
|
|
for (int y = 0; y < board.GetLength(1); y++)
|
|
{
|
|
// boucle qui fait toute la valeur des colonnes
|
|
for (int x = 0; x < board.GetLength(0); x++)
|
|
{
|
|
// on check les bordures horizontales
|
|
if (x + 3 < board.GetLength(0))
|
|
{
|
|
// on check les 4 prochains jetons dans le sens horizontal
|
|
if (board[x, y] == j &&
|
|
board[x + 1, y] == j &&
|
|
board[x + 2, y] == j &&
|
|
board[x + 3, y] == j)
|
|
{
|
|
// on retourne vrai si la condition est respectée
|
|
return true;
|
|
}
|
|
}
|
|
// on check les bordure verticales
|
|
if (y + 3 < board.GetLength(1))
|
|
{
|
|
// on check les 4 prochains jetons dans le sens vertical
|
|
if (board[x, y] == j &&
|
|
board[x, y + 1] == j &&
|
|
board[x, y + 2] == j &&
|
|
board[x, y + 3] == j)
|
|
{
|
|
// on retourne vrai si la condition est respectée
|
|
return true;
|
|
}
|
|
}
|
|
// on check les bordures horizontales et verticales
|
|
if (y + 3 < board.GetLength(1) && x + 3 < board.GetLength(0))
|
|
{
|
|
// on check les 4 prochains jetons dans le sens horizontal et vertical
|
|
if (board[x, y] == j &&
|
|
board[x + 1, y + 1] == j &&
|
|
board[x + 2, y + 2] == j &&
|
|
board[x + 3, y + 3] == j)
|
|
{
|
|
// on retourne vrai si la condition est respectée
|
|
return true;
|
|
}
|
|
}
|
|
// on check les bordures horizontales et verticales
|
|
if (y - 3 > 0 && x + 3 < board.GetLength(0))
|
|
{
|
|
// on check les 4 prochains jetons dans le sens horizontal et vertical
|
|
if (board[x, y] == j &&
|
|
board[x + 1, y - 1] == j &&
|
|
board[x + 2, y - 2] == j &&
|
|
board[x + 3, y - 3] == j)
|
|
{
|
|
// on retourne vrai si la condition est respectée
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// si rien n'a été trouvé, on retourne false
|
|
return false;
|
|
}
|
|
|
|
private bool PlaceToken(Token[,] board, Token currentPlayer, int currentPosition)
|
|
{
|
|
for (int i = board.GetLength(1) - 1; i >= 0; i--)
|
|
{
|
|
if (board[currentPosition, i] != Token.None) continue;
|
|
board[currentPosition, i] = currentPlayer;
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
}
|
|
public class Board
|
|
{
|
|
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 ");
|
|
_program.SetColor(foregroundColor: ConsoleColor.Red);
|
|
Console.Write("5x6");
|
|
_program.SetColor();
|
|
Console.Write(" et ");
|
|
_program.SetColor(foregroundColor: ConsoleColor.Red);
|
|
Console.Write("13x16\n");
|
|
_program.SetColor();
|
|
Console.WriteLine("exemple: 6x7 (6 lignes et 7 colonnes)");
|
|
Console.Write("taille: ");
|
|
|
|
// récupération de l'entrée utilisateur dans une liste grâce à un split
|
|
input = Console.ReadLine().Split('x');
|
|
|
|
// on essaye de Parse en int les entrées, sinon on fait une récursion
|
|
try
|
|
{
|
|
boardSize[0] = int.Parse(input[1]);
|
|
boardSize[1] = int.Parse(input[0]);
|
|
}
|
|
catch
|
|
{
|
|
Console.WriteLine("La taille est invalide. réessayer");
|
|
return BoardSize();
|
|
}
|
|
|
|
// on check si les entrées sont incorrectes si oui on fait une récursion
|
|
if (boardSize[1] <= 5 || boardSize[1] >= 13 || boardSize[0] <= 6 || boardSize[0] >= 16)
|
|
{
|
|
Console.WriteLine("La taille est invalide. réessayer");
|
|
return BoardSize();
|
|
}
|
|
|
|
// et si tous les checks sont passés, on retourne la taille
|
|
Console.WriteLine("taille valide");
|
|
return boardSize;
|
|
}
|
|
}
|
|
|
|
|
|
public class Draw
|
|
{
|
|
private Program _program = new Program();
|
|
public int topSpacing = 4;
|
|
public int leftSpacing = 10;
|
|
public void DrawPlayGroundBorder(Token[,] board)
|
|
{
|
|
Console.SetCursorPosition(leftSpacing, 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(leftSpacing + x * 4, y * 2 + topSpacing + 1);
|
|
Console.Write("║");
|
|
}
|
|
Console.Write(" ║");
|
|
}
|
|
|
|
Console.SetCursorPosition(leftSpacing, 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 currentPosition)
|
|
{
|
|
for (int x = 0; x < board.GetLength(0); x++)
|
|
{
|
|
Console.SetCursorPosition(leftSpacing + x * 4+2, topSpacing + 1);
|
|
|
|
if (currentPosition == x) Console.Write(DrawToken(currentPlayer));
|
|
else Console.Write(DrawToken(Token.None));
|
|
}
|
|
}
|
|
|
|
public void DrawBorder(Token[,] board)
|
|
{
|
|
int playgroundSpacing = topSpacing + 3;
|
|
Console.SetCursorPosition(leftSpacing, 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(leftSpacing + x * 4, y * 2 + playgroundSpacing + 1);
|
|
Console.Write("║");
|
|
}
|
|
Console.Write(" ║");
|
|
}
|
|
|
|
for (int y = 0; y < board.GetLength(1) - 1; y++)
|
|
{
|
|
Console.SetCursorPosition(leftSpacing, y * 2 + playgroundSpacing + 2);
|
|
Console.Write("╠═══");
|
|
for (int x = 1; x < board.GetLength(0); x++)
|
|
{
|
|
Console.SetCursorPosition(leftSpacing + x * 4, y * 2 + playgroundSpacing + 2);
|
|
Console.Write("╬═══");
|
|
}
|
|
Console.Write("╣");
|
|
}
|
|
|
|
Console.SetCursorPosition(leftSpacing, 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)
|
|
{
|
|
int playgroundSpacing = topSpacing + 3;
|
|
for (int y = 0; y < board.GetLength(1); y++)
|
|
for (int x = 0; x < board.GetLength(0); x++)
|
|
{
|
|
Console.SetCursorPosition(leftSpacing + x * 4+2, y*2 + playgroundSpacing + 1);
|
|
Console.Write("{0}", DrawToken(board[x, y]));
|
|
}
|
|
}
|
|
|
|
public string DrawToken(Token token)
|
|
{
|
|
switch (token)
|
|
{
|
|
case Token.FirstPlayer:
|
|
_program.SetColor(foregroundColor: _program.FIRSTPLAYERCOLOR);
|
|
return "■";
|
|
case Token.SecondPlayer:
|
|
_program.SetColor(foregroundColor: _program.SECONDPLAYERCOLOR);
|
|
return "■";
|
|
case Token.None:
|
|
_program.SetColor(foregroundColor: ConsoleColor.DarkGray);
|
|
return "≡";
|
|
default:
|
|
return default;
|
|
}
|
|
}
|
|
|
|
public void DrawTitle(int spacing = 0)
|
|
{
|
|
Console.SetCursorPosition(spacing, 0);
|
|
Console.WriteLine("╔═══════════════════════════════════╗");
|
|
Console.SetCursorPosition(spacing, 1);
|
|
Console.WriteLine("║ Bienvenue dans le jeu Puissance 4 ║");
|
|
Console.SetCursorPosition(spacing, 2);
|
|
Console.WriteLine("║ Réalisé par Théophile Borboën ║");
|
|
Console.SetCursorPosition(spacing, 3);
|
|
Console.WriteLine("╚═══════════════════════════════════╝");
|
|
|
|
}
|
|
public void DrawHelp(Token[,] board)
|
|
{
|
|
WriteHelp(board, 0, 0, "Mode d'utilisation");
|
|
WriteHelp(board, 0, 1, "-------------------");
|
|
WriteHelp(board, 4, 2, "Déplacement");
|
|
WriteHelp(board, 18, 2, "Touches directionnelles");
|
|
WriteHelp(board, 4, 3, "Tir");
|
|
WriteHelp(board, 18, 3, "Spacebar ou Enter");
|
|
WriteHelp(board, 4, 4, "Quitter");
|
|
WriteHelp(board, 18, 4, "Escape");
|
|
_program.SetColor(foregroundColor: _program.FIRSTPLAYERCOLOR);
|
|
WriteHelp(board, 4, 6, "Joueur 1: ");
|
|
_program.SetColor(_program.FIRSTPLAYERCOLOR);
|
|
WriteHelp(board, 14, 6, " ");
|
|
_program.SetColor(foregroundColor: _program.SECONDPLAYERCOLOR);
|
|
WriteHelp(board, 18, 6, "Joueur 2: ");
|
|
_program.SetColor(_program.SECONDPLAYERCOLOR);
|
|
WriteHelp(board, 28, 6, " ");
|
|
}
|
|
|
|
private void WriteHelp(Token[,] board, int x, int y, string text)
|
|
{
|
|
Console.SetCursorPosition(leftSpacing + board.GetLength(0) * 4 + 8 + x, topSpacing + y);
|
|
Console.Write(text);
|
|
}
|
|
}
|
|
}
|