This repository has been archived by the owner on Feb 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
Konzolos kvíz alkalmazás #15
Open
Qdrony
wants to merge
2
commits into
CsharptutorialHungary:main
Choose a base branch
from
Qdrony:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,211 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reflection.Metadata.Ecma335; | ||
using System.Runtime.InteropServices.ComTypes; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using static System.Runtime.InteropServices.JavaScript.JSType; | ||
|
||
namespace ConsoleApp_ZD6VEU | ||
{ | ||
internal class Jatek | ||
{ | ||
|
||
CancellationTokenSource _cts = new(); | ||
|
||
public JatekBeallitas jatekBeallitas; | ||
|
||
public int jelenlegiIdo { get; set; } | ||
|
||
public int adottValasz { get; set; } | ||
|
||
public int helyesValasz = 0; | ||
|
||
public int helytelenValasz = 0; | ||
|
||
public int jelenlegiKerdes = 0; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Field-et nem szoktunk kipublikálni 👎 |
||
|
||
public bool pluszIdo = true; | ||
|
||
public bool felezes = true; | ||
|
||
public void inicializalas() | ||
{ | ||
helyesValasz = 0; | ||
|
||
helytelenValasz = 0; | ||
|
||
jelenlegiKerdes = 0; | ||
|
||
pluszIdo = true; | ||
|
||
felezes = true; | ||
} | ||
public void Inditas() | ||
{ | ||
inicializalas(); | ||
Tajekoztat(); | ||
Console.WriteLine("Indulhat? (Nyomjon meg egy bármilyen gombot)"); | ||
Console.ReadKey(); | ||
Console.Clear(); | ||
|
||
while (jatekBeallitas.jatekKerdesek.Count != jelenlegiKerdes) | ||
{ | ||
jelenlegiIdo = jatekBeallitas.ido; | ||
Kiiratas(); | ||
Kor(); | ||
if (adottValasz == jatekBeallitas.jatekKerdesek[jelenlegiKerdes].helyes) | ||
{ | ||
Console.Clear(); | ||
Console.WriteLine("Megfelelő válasz!"); | ||
helyesValasz++; | ||
} | ||
else if (adottValasz == 0) | ||
{ | ||
Console.Clear(); | ||
Console.WriteLine("Az idő letelt!"); | ||
helytelenValasz++; | ||
} | ||
else { | ||
Console.Clear(); | ||
Console.WriteLine("Sajnos nem megfelelő válasz!"); | ||
helytelenValasz++; | ||
} | ||
Console.WriteLine("Tovább? (Nyomjon meg egy gombot)"); | ||
Console.ReadKey(); | ||
Console.Clear(); | ||
jelenlegiKerdes++; | ||
} | ||
Console.WriteLine("A játékos időnek vége!"); | ||
Console.WriteLine(jatekBeallitas.jatekKerdesek.Count() + " darab kérdésből " + helyesValasz + " sikerült eltalálni."); | ||
Console.ReadKey(); | ||
} | ||
|
||
public void Tajekoztat() | ||
{ | ||
Console.WriteLine("Minden választási lehetőség elött fel van sorolva egy szám.\n" + | ||
"A lehetőség kiválasztásához az adott számot kell leütni a billentyűzeten.\n" + | ||
"Összesen két segítsége van, amiket egy játék során egyszer használhat."); | ||
Console.WriteLine(); | ||
} | ||
|
||
public void Kiiratas() | ||
{ | ||
Kerdes kerdes = jatekBeallitas.jatekKerdesek[jelenlegiKerdes]; | ||
|
||
Console.Clear(); | ||
Console.WriteLine("Kérdes: " + kerdes.kerdes); | ||
Console.WriteLine(); | ||
Console.WriteLine("1: " + kerdes.valaszA); | ||
Console.WriteLine(); | ||
Console.WriteLine("2: " + kerdes.valaszB); | ||
Console.WriteLine(); | ||
Console.WriteLine("3: " + kerdes.valaszC); | ||
Console.WriteLine(); | ||
Console.WriteLine("4: " + kerdes.valaszD); | ||
Console.WriteLine(); | ||
Console.WriteLine(); | ||
if (pluszIdo) | ||
{ | ||
Console.WriteLine("5: Plusz idő kérése"); | ||
} | ||
if (felezes) | ||
{ | ||
Console.WriteLine("6: Válasz lehetőségek felezése"); | ||
} | ||
Console.WriteLine(); | ||
Console.WriteLine("Hátra lévő idő: " + jelenlegiIdo); | ||
} | ||
|
||
public void Kor() | ||
{ | ||
_cts = new CancellationTokenSource(); | ||
|
||
Thread egy = new Thread(Ido); | ||
Thread ketto = new Thread(Valasz); | ||
|
||
egy.Start(); | ||
ketto.Start(); | ||
|
||
egy.Join(); | ||
ketto.Join(); | ||
} | ||
|
||
public void Ido() | ||
{ | ||
while (jelenlegiIdo > 0 && !_cts.Token.IsCancellationRequested) | ||
{ | ||
Kiiratas(); | ||
Thread.Sleep(1000); | ||
jelenlegiIdo--; | ||
} | ||
_cts.Cancel(); | ||
} | ||
|
||
public void Valasz() | ||
{ | ||
int input = 0; | ||
while (!_cts.Token.IsCancellationRequested) | ||
{ | ||
if (Console.KeyAvailable) | ||
{ | ||
ConsoleKeyInfo keyInfo = Console.ReadKey(); | ||
if (int.TryParse(keyInfo.KeyChar.ToString(), out input)) | ||
{ | ||
if (input >= 1 && input <= 4) | ||
{ | ||
_cts.Cancel(); | ||
break; | ||
} | ||
else if (input == 5 && pluszIdo) | ||
{ | ||
pluszIdoSegitseg(); | ||
} | ||
else if (input == 6 && felezes) | ||
{ | ||
felezoSegitseg(); | ||
} | ||
} | ||
else | ||
|
||
{ | ||
Console.WriteLine("Nem jó input"); | ||
} | ||
} | ||
} | ||
adottValasz = input; | ||
} | ||
|
||
public void felezoSegitseg() | ||
{ | ||
if (jatekBeallitas.jatekKerdesek[jelenlegiKerdes].helyes == 1) | ||
{ | ||
jatekBeallitas.jatekKerdesek[jelenlegiKerdes].valaszC = ""; | ||
jatekBeallitas.jatekKerdesek[jelenlegiKerdes].valaszD = ""; | ||
} | ||
else if (jatekBeallitas.jatekKerdesek[jelenlegiKerdes].helyes == 2) | ||
{ | ||
jatekBeallitas.jatekKerdesek[jelenlegiKerdes].valaszA = ""; | ||
jatekBeallitas.jatekKerdesek[jelenlegiKerdes].valaszC = ""; | ||
} | ||
else if (jatekBeallitas.jatekKerdesek[jelenlegiKerdes].helyes == 3) | ||
{ | ||
jatekBeallitas.jatekKerdesek[jelenlegiKerdes].valaszB = ""; | ||
jatekBeallitas.jatekKerdesek[jelenlegiKerdes].valaszD = ""; | ||
} | ||
else if (jatekBeallitas.jatekKerdesek[jelenlegiKerdes].helyes == 4) | ||
{ | ||
jatekBeallitas.jatekKerdesek[jelenlegiKerdes].valaszC = ""; | ||
jatekBeallitas.jatekKerdesek[jelenlegiKerdes].valaszA = ""; | ||
} | ||
felezes = false; | ||
} | ||
|
||
public void pluszIdoSegitseg() | ||
{ | ||
jelenlegiIdo += 10; | ||
pluszIdo = false; | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
using System.Threading.Tasks; | ||
|
||
namespace ConsoleApp_ZD6VEU | ||
{ | ||
internal class JatekBeallitas | ||
{ | ||
public int kerdesMenyiseg = 10; | ||
public int ido = 10; | ||
public List<Kerdes> kerdesek { get; set; } | ||
public List<Kerdes> jatekKerdesek { get; set; } | ||
|
||
public string appPath = AppContext.BaseDirectory; | ||
|
||
public string fileName = "kerdesek.txt"; | ||
|
||
public string valasztotTema = "alap"; | ||
|
||
public void temakKivalasztasa() | ||
{ | ||
temakBeallitasa(); | ||
int input = 0; | ||
while (input != -1) | ||
{ | ||
Console.Clear(); | ||
Console.WriteLine("Téma kiválasztása! "); | ||
Console.WriteLine("Választott téma: " + valasztotTema); | ||
Console.WriteLine(); | ||
Console.WriteLine("Lehetséges témák: "); | ||
Console.WriteLine(); | ||
|
||
for (int i = 0; i < Kerdes.temak.Count; i++) | ||
{ | ||
Console.WriteLine((i+1) + ": " + Kerdes.temak[i]); | ||
} | ||
Console.WriteLine(); | ||
Console.WriteLine("9: alap (10 random kérdés)"); | ||
Console.WriteLine(); | ||
Console.WriteLine("0: Kilépés"); | ||
|
||
input = 0; | ||
ConsoleKeyInfo keyInfo = Console.ReadKey(); | ||
try | ||
{ | ||
int.TryParse(keyInfo.KeyChar.ToString(), out input); | ||
} | ||
catch (IOException e) | ||
{ | ||
Console.WriteLine("Nem jó input!"); | ||
} | ||
|
||
input--; | ||
if (input >= 0 && input <= Kerdes.temak.Count()) | ||
{ | ||
jatekKerdesek = (List<Kerdes>)kerdesek.Where(k => k.tema == Kerdes.temak[input]) | ||
.OrderBy(k => k.kerdes) | ||
.ToList(); | ||
valasztotTema = Kerdes.temak[input]; | ||
} | ||
if(input == 8) | ||
{ | ||
veletlenKerdesek(); | ||
valasztotTema = "alap"; | ||
} | ||
} | ||
} | ||
|
||
public void temakBeallitasa() | ||
{ | ||
kerdesek.ForEach(k => { k.temakFeltoltese(); }); | ||
} | ||
|
||
public void veletlenKerdesek() | ||
{ | ||
Random random = new Random(); | ||
List<Kerdes> kivalasztottKerdesek = new List<Kerdes>(); | ||
|
||
if (kerdesMenyiseg <= kerdesek.Count()) | ||
{ | ||
List<Kerdes> kerdesekMasolat = new List<Kerdes>(kerdesek); | ||
|
||
for (int i = 0; i < kerdesMenyiseg; i++) | ||
{ | ||
int index = random.Next(kerdesekMasolat.Count); | ||
kivalasztottKerdesek.Add(kerdesekMasolat[index]); | ||
kerdesekMasolat.RemoveAt(index); | ||
} | ||
} | ||
|
||
jatekKerdesek = kivalasztottKerdesek; | ||
} | ||
|
||
|
||
public async Task<bool> kerdesekKimentese() | ||
{ | ||
try | ||
{ | ||
string jsonString = JsonSerializer.Serialize(kerdesek, new JsonSerializerOptions() | ||
{ | ||
WriteIndented = true, | ||
Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping | ||
}); | ||
|
||
var path = Path.Combine(appPath, fileName); | ||
using (var textFile = File.CreateText(path)) | ||
{ | ||
textFile.WriteLine(jsonString); | ||
} | ||
} catch(IOException e) | ||
{ | ||
Console.WriteLine(e.Message); | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
public async Task<bool> kerdesekBeolvasasa() | ||
{ | ||
try | ||
{ | ||
string? jsonString = null; | ||
var path = Path.Combine(appPath, fileName); | ||
using (var reader = File.OpenText(path)) | ||
{ | ||
string? line = null; | ||
do | ||
{ | ||
line = reader.ReadLine(); | ||
jsonString += line; | ||
} | ||
while (line != null); | ||
} | ||
if(jsonString == null) | ||
{ | ||
return false; | ||
} | ||
kerdesek = JsonSerializer.Deserialize<List<Kerdes>>(jsonString, new JsonSerializerOptions() | ||
{ | ||
WriteIndented = true, | ||
Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping | ||
})!; | ||
} | ||
catch(IOException e) | ||
{ | ||
Console.WriteLine(e.Message); | ||
return false; | ||
} | ||
return true; | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Javascript biztos kell??