Mi a hiba a kódomban?
Akasztófa játék. Teljesen jól működik(most az összes szóból választ), de nekem az kéne, hogy tudjak kategóriát választani, és az alapján válasszon egy random szót. Van egy globális változó, a choice, aminek az értéke attól függ, hogy melyik kategóriának a gombját nyomta meg a játékos. Nem tudom, hogy miért nem működik a kódom.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace Hangman
{
public partial class Form2 : Form
{
private Bitmap[] hangImages = { Hangman.Properties.Resources._1, Hangman.Properties.Resources._2, Hangman.Properties.Resources._3,
Hangman.Properties.Resources._4, Hangman.Properties.Resources._5, Hangman.Properties.Resources._6,
Hangman.Properties.Resources._7};
public int wrongGuesses = 0;
private string current = "";
private string copyCurrent = "";
static List<fileReading> read = new List<fileReading>();
static void reading()
{
StreamReader sr = new StreamReader("forras.csv");
string sor;
string[] temp;
sr.ReadLine();
while ((sor = sr.ReadLine()) != null)
{
temp = sor.Split(';');
fileReading file = new fileReading();
file.category = Convert.ToInt32(temp[0]);
file.number = Convert.ToInt32(temp[1]);
file.word = Convert.ToString(temp[2]);
read.Add(file);
}
sr.Close();
}
private string[] words;
public Form2()
{
InitializeComponent();
}
private void loadwords()
{
char[] delimiterChars = { ';' };
string[] readText = File.ReadAllLines("forras.csv");
words = new string[readText.Length];
int index = 0;
foreach (string s in readText)
{
string[] line = s.Split(delimiterChars);
if (Global.choice.Equals(1))
{
words[index++] = line[2];
}
if (Global.choice.Equals(2))
{
words[index++] = line[2];
}
if (Global.choice.Equals(3))
{
words[index++] = line[2];
}
if (Global.choice.Equals(4))
{
words[index++] = line[2];
}
}
}
private void setupWordChoice()
{
wrongGuesses = 0;
hangImage.Image = hangImages[wrongGuesses];
int guessIndex = (new Random()).Next(words.Length);
current = words[guessIndex];
copyCurrent = "";
for (int index=0; index<current.Length;index++ )
{
copyCurrent += "_";
}
displayCopy();
}
private void displayCopy()
{
lblShowWord.Text = "";
for (int index = 0; index < copyCurrent.Length; index++)
{
lblShowWord.Text += copyCurrent.Substring(index, 1);
lblShowWord.Text += " ";
}
}
private void updateCopy(char guess)
{
}
private void Form2_Load(object sender, EventArgs e)
{
loadwords();
setupWordChoice();
}
private void guessClick(object sender, EventArgs e)
{
Button choice = sender as Button;
choice.Enabled = false;
if (current.Contains(choice.Text))
{
char[] temp = copyCurrent.ToCharArray();
char[] find = current.ToCharArray();
char guessChar = choice.Text.ElementAt(0);
for (int index = 0; index < find.Length; index++)
{
if (find[index] == guessChar)
{
temp[index] = guessChar;
}
}
copyCurrent = new string(temp);
displayCopy();
}
else
{
wrongGuesses++;
}
if (wrongGuesses < 7)
{
hangImage.Image = hangImages[wrongGuesses];
}
else
{
lblResult.Text = "You Lose!";
}
if (copyCurrent.Equals(current))
{
lblResult.Text = "You Win!";
}
}
private void Menu_Click(object sender, EventArgs e)
{
lblResult.Text = "";
this.Hide();
Form1 menu = new Form1();
lblResult.Text = "";
menu.ShowDialog();
this.Dispose(false);
}
}
class fileReading
{
public int category;
public int number;
public string word;
}
}
![*](http://static.gyakorikerdesek.hu/p/vsz2.png)
![*](http://static.gyakorikerdesek.hu/p/vsz2.png)
![*](http://static.gyakorikerdesek.hu/p/vsz2.png)
![*](http://static.gyakorikerdesek.hu/p/vsz2.png)
![*](http://static.gyakorikerdesek.hu/p/vsz1.png)
![*](http://static.gyakorikerdesek.hu/p/vsz2.png)
![*](http://static.gyakorikerdesek.hu/p/vsz2.png)
![*](http://static.gyakorikerdesek.hu/p/vsz1.png)
![*](http://static.gyakorikerdesek.hu/p/vsz0.png)
![*](http://static.gyakorikerdesek.hu/p/vsz0.png)
![*](http://static.gyakorikerdesek.hu/p/vsz2.png)
![*](http://static.gyakorikerdesek.hu/p/vsz2.png)
![*](http://static.gyakorikerdesek.hu/p/vsz2.png)
![*](http://static.gyakorikerdesek.hu/p/vsz2.png)
![*](http://static.gyakorikerdesek.hu/p/vsz1.png)
Szerintem nem sok teteje volt kimoderálni az udvariatlanul a kérdésbe másolt, ráadásul fontos kellékeket nélkülöző kódodat illető kritikámat, de a moderátor ítélete ellen nincs mit ágálni. Kevesebb indulattal és szószaporítással továbbra is azt javaslom, hogy zippeld össze a szükséges dolgokat, és tedd föl valahová, hogy letöltve rögtön ki is lehessen próbálni a programodat.
Igen, egy programozónak a fentiekből is értenie kéne, csakhogy én nem vagyok programozó, mindössze egy mamlasz, aki örömét leli az efféle fejtörőkben. Hadd ne kelljen már újrarajzolnom a hiányzó képi elemeket, összeállítanom egy szókészletet, amikor tudván tudom, hogy ezek valahol már megvannak készen -- meg aztán semmi közük a voltaképpeni problémádhoz.
Fogalmam sincs, hogy bármiféle értelmes módon bele tudom-e ártani magamat a kérdésedbe, de hátha pont az én elbaltázott kódom provokálja majd ki valakiből, hogy normálisabb megoldást javasoljon helyette. Máris nagyobb haszon, mint néhány általános megjegyzést begyűjteni.
Kapcsolódó kérdések:
Minden jog fenntartva © 2025, www.gyakorikerdesek.hu
GYIK | Szabályzat | Jogi nyilatkozat | Adatvédelem | Cookie beállítások | WebMinute Kft. | Facebook | Kapcsolat: info(kukac)gyakorikerdesek.hu
Ha kifogással szeretne élni valamely tartalommal kapcsolatban, kérjük jelezze e-mailes elérhetőségünkön!