Poprawione: 22.05.2020 12:45
using System;
class MainClass {
static void pokaz_ktora_runda(int runda) {
Console.ForegroundColor = ConsoleColor.Blue;
Console.WriteLine("RUNDA: " + runda);
Console.WriteLine("--------");
Console.Beep();
}
static void pokaz_suma_liczb_gracz(string gracz, int suma, ConsoleColor kolor) {
Console.WriteLine();
Console.Write("Suma liczb ");
Console.ForegroundColor = kolor;
Console.Write(gracz);
Console.ResetColor();
Console.Write(" wynosi: " + suma);
}
static void pokaz_werdykt(string gracz, string werdykt, ConsoleColor kolor) {
Console.WriteLine();
switch (werdykt) {
case "wygrał":
Console.Write("Runde wygrał: ");
Console.ForegroundColor = kolor;
Console.Write(gracz);
Console.ResetColor();
break;
case "remis":
Console.Write("Runde zakończyła się ");
Console.ForegroundColor = kolor;
Console.Write("remisem");
Console.ResetColor();
break;
}
}
static void pokaz_punkty_gracz(string gracz, int total, ConsoleColor kolor) {
Console.WriteLine();
Console.Write("Zdobyte punkty ");
Console.ForegroundColor = kolor;
Console.Write(gracz);
Console.ResetColor();
Console.Write(" wynosi: " + total);
}
static int podaj_liczbe_gracz(string gracz, ConsoleColor kolor) {
int liczba;
Console.ForegroundColor = kolor;
Console.WriteLine(gracz);
Console.ResetColor();
Console.Write("Podaj liczbę: ");
try {
liczba = int.Parse(Console.ReadLine());
} catch {
liczba = 0;
}
return liczba;
}
public static void Main (string[] args) {
int licznik = 1;
int runda = 1;
string gracz1 = "Marek gracz 1";
string gracz2 = "Tomek gracz 2";
int liczba1 = 0;
int liczba2 = 0;
int suma1 = 0;
int suma2 = 0;
int total1 = 0;
int total2 = 0;
ConsoleColor gracz_kolor1 = ConsoleColor.Cyan;
ConsoleColor gracz_kolor2 = ConsoleColor.DarkGreen;
Console.Clear();
pokaz_ktora_runda(runda);
while (total1 <= 1000 || total2 <= 1000) {
liczba1 = podaj_liczbe_gracz(gracz1, gracz_kolor1);
suma1 += liczba1;
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("NASTĘPNY GRACZ");
Console.ResetColor();
liczba2 = podaj_liczbe_gracz(gracz2, gracz_kolor2);
suma2 += liczba2;
if(licznik % 2 == 0) {
pokaz_suma_liczb_gracz(gracz1, suma1, gracz_kolor1);
pokaz_suma_liczb_gracz(gracz2, suma2, gracz_kolor2);
if(suma1 > suma2) {
total1 += 50;
total2 -= 50;
pokaz_werdykt(gracz1, "wygrał", gracz_kolor1);
} else if(suma2 > suma1) {
total2 += 50;
total1 -= 50;
pokaz_werdykt(gracz2, "wygrał", gracz_kolor2);
} else if(suma1 == suma2) {
pokaz_werdykt(gracz2, "remis", ConsoleColor.Red);
}
suma1 = 0;
suma2 =0;
runda++;
Console.WriteLine();
pokaz_punkty_gracz(gracz1, total1, gracz_kolor1);
pokaz_punkty_gracz(gracz2, total2, gracz_kolor2);
Console.WriteLine();
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Naciśnij SPACJE, aby kontynuować");
Console.ResetColor();
Console.ReadKey();
Console.Clear();
pokaz_ktora_runda(runda);
} else {
Console.WriteLine();
}
licznik++;
}
}
}
Demo on-line.
(C# Exceptions - Try..Catch), (C# Switch Statements), (C# Tutorial),