Juego de cartas "21" en consola con C#
Juego de cartas 21 en consola (Aprendiendo C#)
Una versión básica del clásico juego de cartas 21, desarrollado en consola con C#. El usuario compite contra la computadora, decide si tomar más cartas y prueba su suerte con monedas virtuales. Para aprender estructuras de control y lógica en C#.
1System.Random random = new System.Random();
2
3
4// Welcome
5Console.WriteLine("HELLO, WELCOME!!");
6Console.WriteLine("Don you want to play 21 or another videogame? => Yes/No");
7
8// Variables
9string confirmStar = Console.ReadLine();
10int numberCoin;
11int computerCart;
12int playerCart;
13string desicion;
14int totalPlayer;
15string seconDesicion;
16int num = 0;
17string endDesicion;
18
19// Confirmación de entrada - estado
20switch (confirmStar)
21{
22 case "Yes":
23 Console.WriteLine("How many coins do you have?");
24 numberCoin = Convert.ToInt32(Console.ReadLine());
25 Console.WriteLine($"Great, you have {numberCoin} opportunities");
26 for (int i = 0; i < numberCoin; i++) {
27 Console.WriteLine("Take a card => Yes/No");
28 desicion = Console.ReadLine();
29 switch (desicion) {
30 case "Yes":
31 computerCart = random.Next(12, 21);
32 playerCart = random.Next(1, 25);
33 Console.WriteLine($"Your card is {playerCart}");
34 Console.WriteLine("Do you want to take other card? => Yes/No");
35 seconDesicion = Console.ReadLine();
36
37 switch (seconDesicion) {
38 case "No":
39 if (playerCart > computerCart && playerCart <= 21) {
40 Console.WriteLine($"Your card is: {playerCart} and The computer is: {computerCart}");
41 Console.WriteLine("Congratulation, you win!!");
42 } else if (playerCart < computerCart || playerCart == computerCart) {
43 Console.WriteLine($"Your card is: {playerCart} and The computer is: {computerCart}");
44 Console.WriteLine("You lost, game over!! :(");
45 } else {
46 Console.WriteLine("Invalid Code");
47 }
48 break;
49
50 case "Yes":
51 totalPlayer = playerCart;
52 totalPlayer = totalPlayer + random.Next(1, 21);
53 Console.WriteLine($"Your card is: {totalPlayer}");
54 Console.WriteLine("Do you want other card? => Yes/No");
55 endDesicion = Console.ReadLine();
56 while (endDesicion == "Yes" && totalPlayer < 21) {
57 totalPlayer = totalPlayer + random.Next(1, 21);
58 Console.WriteLine($"Your card is: {totalPlayer}");
59 Console.WriteLine("Do you want other card? => Yes/No ");
60 endDesicion = Console.ReadLine();
61 }
62
63 if (totalPlayer > computerCart && totalPlayer <= 21) {
64 Console.WriteLine($"Your card is: {totalPlayer} and The computer is: {computerCart}");
65 Console.WriteLine("Congratulation, you win!!");
66 } else if (totalPlayer < computerCart || totalPlayer == computerCart) {
67 Console.WriteLine($"Your card is: {totalPlayer} and The computer is: {computerCart}");
68 Console.WriteLine("You lost, game over!! :(");
69 } else if (totalPlayer > 21) {
70 Console.WriteLine($"Sorry but your card is: {totalPlayer}, game over, you lost!! :(");
71 }
72 break;
73
74 default:
75 Console.WriteLine("Invalid Code");
76 break;
77 }
78
79 break;
80 default:
81 Console.WriteLine("Invalid Error");
82 break;
83 }
84 Console.WriteLine("----------- THIS ROUND HAS ENDED, LETS'GO WITH THE NEXT -----------");
85 }
86 break;
87 case "No":
88 Console.WriteLine("You're screwed because there is only 21");
89 break;
90 default:
91 Console.WriteLine("Invalid code");
92 break;
93}
Explora el Proyecto
Contenido Relacionado
Aprende Lenguaje C# de CERO a EXPERTO
Aprende a programar en C# sin ningún conocimiento previo
Curso Básico de Programación con C#
Desarrolla tus habilidades de programación en C# aprendiendo a crear un juego de casino tipo Blackjack. Implementa estructuras de control como IF y SWITCH, y usa ciclos WHILE y FOR para mejorar la lógica de tu juego. Ideal para principiantes.
Comentarios
Cargando comentarios...