add a lot of methode
This commit is contained in:
parent
6658911f6c
commit
a9bb0a8bfc
@ -1,15 +1,165 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Runtime.Remoting.Messaging;
|
||||||
using System.Linq;
|
using System.Security.Permissions;
|
||||||
using System.Text;
|
using static _319_POO.Game;
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace _319_POO
|
namespace _319_POO
|
||||||
{
|
{
|
||||||
internal class Program
|
public enum Token
|
||||||
{
|
{
|
||||||
|
None,
|
||||||
|
FirstPlayer,
|
||||||
|
SecondPlayer
|
||||||
|
}
|
||||||
|
|
||||||
|
public class Program
|
||||||
|
{
|
||||||
|
public void SetColor(ConsoleColor backgroundColor = ConsoleColor.Black, ConsoleColor forgroundColor = ConsoleColor.White)
|
||||||
|
{
|
||||||
|
Console.ForegroundColor = forgroundColor;
|
||||||
|
Console.BackgroundColor = backgroundColor;
|
||||||
|
}
|
||||||
static void Main(string[] args)
|
static void Main(string[] args)
|
||||||
{
|
{
|
||||||
|
int[] boardSize = new Board().BoardSize();
|
||||||
|
Token[,] board = new Token[boardSize[0],boardSize[1]];
|
||||||
|
new Game().Start(board);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
public class Game
|
||||||
|
{
|
||||||
|
private Draw d = new Draw();
|
||||||
|
public void Start(Token[,] board)
|
||||||
|
{
|
||||||
|
int pos = 0;
|
||||||
|
Token currentPlayer = Token.FirstPlayer;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
d.DrawBoard(board);
|
||||||
|
d.DrawPlayGround(board,currentPlayer,pos);
|
||||||
|
|
||||||
|
ConsoleKey key = Console.ReadKey().Key;
|
||||||
|
switch (key)
|
||||||
|
{
|
||||||
|
case ConsoleKey.RightArrow:
|
||||||
|
pos++;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case ConsoleKey.LeftArrow:
|
||||||
|
pos--;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case ConsoleKey.Enter:
|
||||||
|
if (PlaceToken(board, currentPlayer) && currentPlayer == Token.FirstPlayer)
|
||||||
|
currentPlayer = Token.SecondPlayer;
|
||||||
|
else if (PlaceToken(board, currentPlayer) && currentPlayer == Token.SecondPlayer)
|
||||||
|
currentPlayer = Token.FirstPlayer;
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private bool PlaceToken(Token[,] board, Token currentPlayer)
|
||||||
|
{
|
||||||
|
for (int i = board.GetLength(1) - 1; i >= 0; i--)
|
||||||
|
{
|
||||||
|
if (board[board.GetLength(1), i] == Token.None)
|
||||||
|
board[board.GetLength(1), i] = currentPlayer;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public class Board
|
||||||
|
{
|
||||||
|
private Program p = 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(default, ConsoleColor.Red);
|
||||||
|
Console.Write("5x6", ConsoleColor.Red);
|
||||||
|
p.SetColor();
|
||||||
|
Console.Write(" et ");
|
||||||
|
p.SetColor(default, ConsoleColor.Red);
|
||||||
|
Console.Write("13x16\n", ConsoleColor.Red);
|
||||||
|
p.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
|
||||||
|
{
|
||||||
|
public void DrawPlayGround(Token[,] board, Token currentPlayer, int pos)
|
||||||
|
{
|
||||||
|
for (int x = 0; x < board.GetLength(0); x++)
|
||||||
|
{
|
||||||
|
Console.SetCursorPosition(x, 0);
|
||||||
|
|
||||||
|
if (pos == x) Console.Write(DrawToken(currentPlayer));
|
||||||
|
else Console.Write(DrawToken(Token.None));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void DrawBoard(Token[,] board)
|
||||||
|
{
|
||||||
|
Console.Clear();
|
||||||
|
|
||||||
|
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]));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public char DrawToken(Token token)
|
||||||
|
{
|
||||||
|
switch (token)
|
||||||
|
{
|
||||||
|
case Token.FirstPlayer:
|
||||||
|
return 'X';
|
||||||
|
case Token.SecondPlayer:
|
||||||
|
return 'O';
|
||||||
|
case Token.None:
|
||||||
|
return '.';
|
||||||
|
default:
|
||||||
|
return ' ';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user