2020-12-13T14:22:37+01:00
2020-12-13T14:31:40+01:00
2022-08-11T23:35:31+02:00
Welfel
Sziasztok!
SOS-ban kellene a válasz. Írtam C#-ban egy játékot ami működik is. A baj az, hogy jó pár parancs nem fogadható el és nem tudom, hogyan kéne javítani.
Amit ki kell szedni a kódból:
• ne legyen this[pont p] indexelő -> cseréld ki egy függvényre
• ne legyen => operátor -> cseréld ki rendes get set blokkokra
• ne legyen List, vagy egy MyList saját lista implementációt csinálsz, vagy tömbre cseréld ki -> a saját lista osztály egy privát tömböt tartalmaz abból a típusból amiből helyettesíted a listát, van publikus Add, Get és Remove függvénye, a tömb mérete dinamikusan változzon, azaz Add esetén egy n+1 elemű új tömbbe átmásolod az eddigi elemeket és az utolsó helyre beszúrod az addolt elemet, remove esetén egy n-1 elemű tömb jön létre amibe azokat másolod csak át a régi tömbből ami nem a törlendő elem, a get pedig csak megkeres egy elemet és ha van akkor visszaadja, ha nincs akkor null vagy -1 vagy valami.
A kód:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace Feleves_feladat_javitas
{
class Program
{
const string fajlnev = "leaderboard.txt";
static void Main(string[] args)
{
Console.CursorSize = 100;
Console.SetWindowSize(80, 30);
Console.SetBufferSize(80, 30);
dicsosegtabla e = new dicsosegtabla();
Console.WriteLine("A kurzor mozgatása a WASD-ven illetve nyilakkal.");
Console.WriteLine("Kérlek add meg a nevedet: ");
e.Nev = Console.ReadLine();
Console.WriteLine("Mennyi ideig szeretnél játszani? (percben): ");
if (int.TryParse(Console.ReadLine(), out int perc))
{
e.Ido = perc;
}
else
{
e.Ido = 0;
}
Jatek game = new Jatek(79, 24, e.Ido * 60, 1.5);
game.jatekMenet();
Console.Clear();
e.Eredmeny = game.Eredmeny;
e.Mentes(fajlnev);
dicsosegtabla[] bemenet = dicsosegtabla.Load(fajlnev);
Console.WriteLine("Dicsőségtábla");
for (int i = 0; i < bemenet.Length; i++)
{
Console.WriteLine($"{bemenet.Nev,-30}{bemenet.Ido,3} perc {bemenet.Eredmeny,5} pont");
}
Console.ReadLine();
}
}
public class szin
{
private const int max = 8;
private static ConsoleColor[] colors = new ConsoleColor[]
{
ConsoleColor.Cyan,
ConsoleColor.Green,
ConsoleColor.Yellow,
ConsoleColor.Red,
ConsoleColor.Magenta,
ConsoleColor.DarkCyan,
ConsoleColor.DarkGreen,
ConsoleColor.Blue
};
private static char[] szimbolum = new char[]
{
'A',
'B',
'C',
'D',
'E',
'F',
'G',
'H',
};
private int ertek;
public ConsoleColor Color => colors[ertek];
public char Szimbolum => szimbolum[ertek];
public bool megjelol { get; set; }
public szin(int ertek)
{
if (ertek >= max) throw new ArgumentException("Ismeretlen.");
this.ertek = ertek;
}
public static szin CreateRandom(Random randomGenerator)
{
return new szin(randomGenerator.Next(max));
}
public bool Equals(szin masik)
{
if (masik == null)
{
return false;
}
return this.ertek == masik.ertek;
}
public static szin Kulonbozo(szin jewel)
{
return new szin((jewel.ertek + 1) % max);
}
public void visszad(pont p)
{
Console.SetCursorPosition(p.X, p.Y);
Console.ForegroundColor = this.Color;
Console.Write(this.Szimbolum);
}
}
public class palya
{
private readonly szin[,] jewels;
public int szeles => jewels.GetLength(0);
public int magas => jewels.GetLength(1);
public palya(int szeles, int magas)
{
jewels = new szin[szeles, magas];
}
public szin this[pont p]
{
get
{
if (IsInBounds(p))
{
return jewels[p.X, p.Y];
}
else
{
return null;
}
}
set
{
if (IsInBounds(p))
{
jewels[p.X, p.Y] = value;
}
}
}
public bool IsInBounds(pont p)
{
return p.X >= 0 && p.X < szeles && p.Y >= 0 && p.Y < magas;
}
}
class Allapot
{
public bool Racs => ChangedCells.Count > 0;
public bool VisszaIdo => DateTime.Now - utolsoIdo > TimeSpan.FromSeconds(1);
public bool Szukseges => Racs || VisszaIdo;
public List<pont
> ChangedCells
{ get; }
private DateTime utolsoIdo;
public Allapot()
{
utolsoIdo = DateTime.MinValue;
ChangedCells = new List<pont>();
}
public void Idozito()
{
utolsoIdo = DateTime.Now;
}
}
public class Jatek
{
private readonly Random random;
private pont kuzorpozicio;
// Esély egy ékszer létrehozására, amely garantáltan különbözik minden szomszédjától, hogy ellenőrizze a nehézségeket.
private double Esely;
private DateTime JatekVege;
private int JatekHossz;
private Allapot Allapot;
private palya grid;
public int Eredmeny { get; private set; }
public Jatek(int szeles, int magas, int gameLengthSeconds, double difficulty)
{
grid = new palya(szeles, magas);
random = new Random();
kuzorpozicio = new pont(szeles / 2, magas / 2);
Esely = 1 - Math.Exp(-difficulty);
this.JatekHossz = gameLengthSeconds;
Allapot = new Allapot();
}
public void jatekMenet()
{
Init();
Teljes();
bool fut = true;
while (fut)
{
bool meccsvan = meccs();
bool ujertek = false;
if (meccsvan)
{
Oszlopok();
}
else
{
ujertek = OszlopToltes();
}
// A felhasználótol bekért információ
if (!meccsvan && !ujertek && Console.KeyAvailable)
{
fut = Felhasznalo(fut);
}
else
{
// discard all user input while it's not the user's turn
while (Console.KeyAvailable)
{
Console.ReadKey(true);
}
}
//A játék végeredménye
if (DateTime.Now > JatekVege)
{
fut = false;
}
// Képernyő frissítése
if (Allapot.Szukseges)
{
visszaFrissit();
}
System.Threading.Thread.Sleep(30);
// Az összes új ékszer/érték megállítása egy pillanatra
if (meccsvan)
{
System.Threading.Thread.Sleep(100);
}
if (ujertek)
{
System.Threading.Thread.Sleep(200);
}
}
GameOver();
}
private void GameOver()
{
string uzenet = $"G A M E O V E R - {
SOS-ban kellene a válasz. Írtam C#-ban egy játékot ami működik is. A baj az, hogy jó pár parancs nem fogadható el és nem tudom, hogyan kéne javítani.
Amit ki kell szedni a kódból:
• ne legyen this[pont p] indexelő -> cseréld ki egy függvényre
• ne legyen => operátor -> cseréld ki rendes get set blokkokra
• ne legyen List, vagy egy MyList saját lista implementációt csinálsz, vagy tömbre cseréld ki -> a saját lista osztály egy privát tömböt tartalmaz abból a típusból amiből helyettesíted a listát, van publikus Add, Get és Remove függvénye, a tömb mérete dinamikusan változzon, azaz Add esetén egy n+1 elemű új tömbbe átmásolod az eddigi elemeket és az utolsó helyre beszúrod az addolt elemet, remove esetén egy n-1 elemű tömb jön létre amibe azokat másolod csak át a régi tömbből ami nem a törlendő elem, a get pedig csak megkeres egy elemet és ha van akkor visszaadja, ha nincs akkor null vagy -1 vagy valami.
A kód:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace Feleves_feladat_javitas
{
class Program
{
const string fajlnev = "leaderboard.txt";
static void Main(string[] args)
{
Console.CursorSize = 100;
Console.SetWindowSize(80, 30);
Console.SetBufferSize(80, 30);
dicsosegtabla e = new dicsosegtabla();
Console.WriteLine("A kurzor mozgatása a WASD-ven illetve nyilakkal.");
Console.WriteLine("Kérlek add meg a nevedet: ");
e.Nev = Console.ReadLine();
Console.WriteLine("Mennyi ideig szeretnél játszani? (percben): ");
if (int.TryParse(Console.ReadLine(), out int perc))
{
e.Ido = perc;
}
else
{
e.Ido = 0;
}
Jatek game = new Jatek(79, 24, e.Ido * 60, 1.5);
game.jatekMenet();
Console.Clear();
e.Eredmeny = game.Eredmeny;
e.Mentes(fajlnev);
dicsosegtabla[] bemenet = dicsosegtabla.Load(fajlnev);
Console.WriteLine("Dicsőségtábla");
for (int i = 0; i < bemenet.Length; i++)
{
Console.WriteLine($"{bemenet.Nev,-30}{bemenet.Ido,3} perc {bemenet.Eredmeny,5} pont");
}
Console.ReadLine();
}
}
public class szin
{
private const int max = 8;
private static ConsoleColor[] colors = new ConsoleColor[]
{
ConsoleColor.Cyan,
ConsoleColor.Green,
ConsoleColor.Yellow,
ConsoleColor.Red,
ConsoleColor.Magenta,
ConsoleColor.DarkCyan,
ConsoleColor.DarkGreen,
ConsoleColor.Blue
};
private static char[] szimbolum = new char[]
{
'A',
'B',
'C',
'D',
'E',
'F',
'G',
'H',
};
private int ertek;
public ConsoleColor Color => colors[ertek];
public char Szimbolum => szimbolum[ertek];
public bool megjelol { get; set; }
public szin(int ertek)
{
if (ertek >= max) throw new ArgumentException("Ismeretlen.");
this.ertek = ertek;
}
public static szin CreateRandom(Random randomGenerator)
{
return new szin(randomGenerator.Next(max));
}
public bool Equals(szin masik)
{
if (masik == null)
{
return false;
}
return this.ertek == masik.ertek;
}
public static szin Kulonbozo(szin jewel)
{
return new szin((jewel.ertek + 1) % max);
}
public void visszad(pont p)
{
Console.SetCursorPosition(p.X, p.Y);
Console.ForegroundColor = this.Color;
Console.Write(this.Szimbolum);
}
}
public class palya
{
private readonly szin[,] jewels;
public int szeles => jewels.GetLength(0);
public int magas => jewels.GetLength(1);
public palya(int szeles, int magas)
{
jewels = new szin[szeles, magas];
}
public szin this[pont p]
{
get
{
if (IsInBounds(p))
{
return jewels[p.X, p.Y];
}
else
{
return null;
}
}
set
{
if (IsInBounds(p))
{
jewels[p.X, p.Y] = value;
}
}
}
public bool IsInBounds(pont p)
{
return p.X >= 0 && p.X < szeles && p.Y >= 0 && p.Y < magas;
}
}
class Allapot
{
public bool Racs => ChangedCells.Count > 0;
public bool VisszaIdo => DateTime.Now - utolsoIdo > TimeSpan.FromSeconds(1);
public bool Szukseges => Racs || VisszaIdo;
public List<pont
> ChangedCells
{ get; }
private DateTime utolsoIdo;
public Allapot()
{
utolsoIdo = DateTime.MinValue;
ChangedCells = new List<pont>();
}
public void Idozito()
{
utolsoIdo = DateTime.Now;
}
}
public class Jatek
{
private readonly Random random;
private pont kuzorpozicio;
// Esély egy ékszer létrehozására, amely garantáltan különbözik minden szomszédjától, hogy ellenőrizze a nehézségeket.
private double Esely;
private DateTime JatekVege;
private int JatekHossz;
private Allapot Allapot;
private palya grid;
public int Eredmeny { get; private set; }
public Jatek(int szeles, int magas, int gameLengthSeconds, double difficulty)
{
grid = new palya(szeles, magas);
random = new Random();
kuzorpozicio = new pont(szeles / 2, magas / 2);
Esely = 1 - Math.Exp(-difficulty);
this.JatekHossz = gameLengthSeconds;
Allapot = new Allapot();
}
public void jatekMenet()
{
Init();
Teljes();
bool fut = true;
while (fut)
{
bool meccsvan = meccs();
bool ujertek = false;
if (meccsvan)
{
Oszlopok();
}
else
{
ujertek = OszlopToltes();
}
// A felhasználótol bekért információ
if (!meccsvan && !ujertek && Console.KeyAvailable)
{
fut = Felhasznalo(fut);
}
else
{
// discard all user input while it's not the user's turn
while (Console.KeyAvailable)
{
Console.ReadKey(true);
}
}
//A játék végeredménye
if (DateTime.Now > JatekVege)
{
fut = false;
}
// Képernyő frissítése
if (Allapot.Szukseges)
{
visszaFrissit();
}
System.Threading.Thread.Sleep(30);
// Az összes új ékszer/érték megállítása egy pillanatra
if (meccsvan)
{
System.Threading.Thread.Sleep(100);
}
if (ujertek)
{
System.Threading.Thread.Sleep(200);
}
}
GameOver();
}
private void GameOver()
{
string uzenet = $"G A M E O V E R - {
Mutasd a teljes hozzászólást!
Csatolt állomány
- Feleves_feladat.sln1,12 KB
- ⊓≡⊺∧⊓ɢ⋿∟válasza Welfel (14:22) részére
- 2020.12.13. 14:31
- permalink
Hali!
1. Próbálj meg normális, a problémát jól körülíró címet adni a témádnak.
2. Használd a forráskód-gombot (a szerkesztő-mező felett, balról a harmadik: </>), ha forráskódot illesztesz be (és akkor nem fog hibásan megjelenni a kódod).
Mutasd a teljes hozzászólást!
Ez a téma lezárásra került a moderátor által. A lezárás oka: Lásd hozzászólásomban.