Created
April 29, 2020 07:18
-
-
Save renatomoor/3eb81299c98ae1ffdffeffe1ba72773b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
namespace Pendu | |
{ | |
class Program | |
{ | |
private static string _player1, _player2, _word = ""; | |
private static List<char> _letters = new List<char>(); | |
private static int _tentatives = 1; | |
private static bool _won = false; | |
static void Main(string[] args) | |
{ | |
Welcome(); | |
StartGame(); | |
} | |
static void Welcome() | |
{ | |
Console.WriteLine("Bienvenue sur le jeu de pendu"); | |
Console.WriteLine("Voulez vous commencer a jouer? o/n"); | |
} | |
static void StartGame() | |
{ | |
var userAnswer = Console.ReadKey(); | |
if (userAnswer.Key == ConsoleKey.O) | |
{ | |
Game(); | |
} | |
else if (userAnswer.Key == ConsoleKey.N) | |
{ | |
Exit(); | |
} | |
else | |
{ | |
Console.WriteLine("Mouvaise reponse"); | |
Console.WriteLine("La lettre n'est pas valide o / n"); | |
StartGame(); | |
} | |
} | |
static void Exit() | |
{ | |
Environment.Exit(0); | |
} | |
static void Game() | |
{ | |
CreatePseudos(); | |
SelectWord(); | |
while (!_won) | |
{ | |
WriteWordStatus(); | |
TentativeLetter(); | |
CheckGame(); | |
} | |
} | |
static void SelectWord() | |
{ | |
Console.WriteLine(_player1 + " Ecrivez le mot a decouvrir"); | |
_word = Console.ReadLine(); | |
} | |
static void TentativeLetter() | |
{ | |
if (_tentatives <= 12) | |
{ | |
Console.WriteLine("Tentative numero: " + _tentatives); | |
Console.WriteLine("Il vous reste : " + (13 - _tentatives) + " essais"); | |
Console.WriteLine("Le mot a chercher a " + _word.Length + " lettres"); | |
Console.WriteLine("Ecrivez une lettre pour tenter: "); | |
_letters.Add(Convert.ToChar(Console.Read())); | |
_tentatives++; | |
} | |
} | |
static void CheckGame() | |
{ | |
foreach (var letter in _word) | |
{ | |
if (!_letters.Contains(letter)) | |
{ | |
_won = false; | |
return; | |
} | |
} | |
_won = true; | |
} | |
static void WriteWordStatus() | |
{ | |
foreach (var letter in _word) | |
{ | |
if (_letters.Contains(letter)) | |
{ | |
Console.Write(letter); | |
} | |
else | |
{ | |
Console.Write("_"); | |
} | |
} | |
Console.WriteLine(""); | |
} | |
static void CreatePseudos() | |
{ | |
Console.WriteLine("Rentrer le pseudo du joueur 1: "); | |
_player1 = Console.ReadLine(); | |
Console.WriteLine("Rentrer le pseudo du joueur 2: "); | |
_player2 = Console.ReadLine(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment