How to read random line from text file that startswith * and lines below him that are related to him?
I have created to get random line with question using commands below, but after that it doesn't read lines below him that are related to him,instead of that he read lines from beggining!!!
var questions =
File.ReadLines(filePath)
.Where(line => line.StartsWith("*")).ToList();
var rng = new Random();
var myRandomQuestion = questions[rng.Next(questions.Count)].Substring(1);
label1.Text = myRandomQuestion;
My text file has 15000 lines.Lines with * character are questions and lines below them are terms relating to that question.
This is one part of file:
*Planete i njihovi sateliti
Zemlja=Mesec
Mars=Fobos
Jupiter=Io
Saturn=Titan
Uran=Titania
Neptun=Triton
Pluton=Haron
Merkur=Nema satelit
*Čuveni parovi iz umetnosti
Hamlet=Ofelija
Ruslan=Ljudmila
Zevs=Hera
Otelo=Dezdemona
Paris=Helena
Abelard=Eloiza
Paolo=Frančeska
Lanselot=Ginevra
*Latinski pojmovi
Kvalifikacija=Osposobljenost
Karantin=Izolacija
Radijacija=Zračenje
Ratifikacija=Potpisivanje
Racionalan=Razuman
Reakcija=Otpor
Realizacija=Ostvarenje
Rekapitulacija=Ponavljanje
*Poveži aktuelne predsednike sa državama u kojima vladaju.
Trajan Basesku=Rumunija
Karolos Papuljas=Grčka
Đorđo Napolitano=Italija
Tomas Hendrik Ilves=Estonija
Danilo Tirk=Slovenija
Dimitris Hristofias=Kipar
Tarja Halonen=Finska
Meri Mekelis=Irska
*Francuski gradovi
Bordeaux=Bordo
Auxerre=Okser
Toulouse=Tuluz
Nantes=Nant
Marseille=Marselj
Dijon=Dižon
Limoges=Limož
Chateauroux=Šatero
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Collections.Specialized;
using System.IO;
namespace Slagalica
{
public partial class Spojnice : MetroFramework.Forms.MetroForm
{
public Spojnice()
{
InitializeComponent();
}
string igra;
int j = 0;
Button[] button;
private void Spojnice_Load(object sender, EventArgs e)
{
Random r = new Random();
int indeks;
igra = "spojnice";
StreamReader sr = new StreamReader(igra + ".txt");
string[] niz1 = new string[8];
string[] niz2 = new string[8];
var questions = File.ReadLines(igra + ".txt").Where(line => line.StartsWith("*")).ToList();
var myRandomQuestion = questions[r.Next(questions.Count)].Substring(1);
label1.Text = myRandomQuestion;
button = new Button[] { button1, button2, button3, button4, button5, button6, button7, button8
,button9, button10, button11, button12, button13, button14, button15, button16 };
for (int i = 0; i < 8; i++)
{
string s = sr.ReadLine();
niz1[i] = s.Substring(0, s.IndexOf('='));
indeks = r.Next(1, 9);
bool ind = true;
while (ind)
{
ind = false;
switch (indeks)
{
case 1:
if (button9.Text == "")
button9.Text = s.Substring(s.IndexOf('=') + 1);
else
{
indeks = r.Next(1, 9);
ind = true;
}
break;
case 2:
if (button10.Text == "")
button10.Text = s.Substring(s.IndexOf('=') + 1);
else
{
indeks = r.Next(1, 9);
ind = true;
}
break;
case 3:
if (button11.Text == "")
button11.Text = s.Substring(s.IndexOf('=') + 1);
else
{
indeks = r.Next(1, 9);
ind = true;
}
break;
case 4:
if (button12.Text == "")
button12.Text = s.Substring(s.IndexOf('=') + 1);
else
{
indeks = r.Next(1, 9);
ind = true;
}
break;
case 5:
if (button13.Text == "")
button13.Text = s.Substring(s.IndexOf('=') + 1);
else
{
indeks = r.Next(1, 9);
ind = true;
}
break;
case 6:
if (button14.Text == "")
button14.Text = s.Substring(s.IndexOf('=') + 1);
else
{
indeks = r.Next(1, 9);
ind = true;
}
break;
case 7:
if (button15.Text == "")
button15.Text = s.Substring(s.IndexOf('=') + 1);
else
{
indeks = r.Next(1, 9);
ind = true;
}
break;
case 8:
if (button16.Text == "")
button16.Text = s.Substring(s.IndexOf('=') + 1);
else
{
indeks = r.Next(1, 9);
ind = true;
}
break;
}
}
}
for (j = 0; j < 8; j++)
button[j].Text = niz1[j];
for (j = 0; j < 16; j++)
button[j].BackColor = Color.DodgerBlue;
label1.BackColor = Color.DodgerBlue;
timer1.Enabled = true;
}
int ukupno = 0, pojam = 0;
private void RacunajZaDugme(Button dugme1, Button dugme2, string igr)
{
string s1 = dugme1.Text + "=" + dugme2.Text;
StreamReader sr = new StreamReader(igr + ".txt");
string s2 = sr.ReadLine();
while (!sr.EndOfStream)
{
s2 = sr.ReadLine();
if (s1 == s2)
{
ukupno += 4;
dugme1.BackColor = Color.Green;
dugme2.BackColor = Color.Green;
break;
}
if (s1 != s2)
{
dugme1.BackColor = Color.Red;
}
}
pojam++;
if (pojam == 8)
{
KrajIgre();
pojam = 0;
ukupno = 0;
}
}
private void KrajIgre()
{
timer1.Enabled = false;
button = new Button[] { button1, button2, button3, button4, button5, button6, button7, button8
,button9, button10, button11, button12, button13, button14, button15, button16 };
for (j = 0; j < 16; j++)
button[j].BackColor = Color.White;
for (j = 0; j < 16; j++)
button[j].Text = "";
MessageBox.Show("Osvojili ste " + ukupno + " poena ! ! !", "Kraj igre");
label1.Text = "";
label1.BackColor = Color.DarkBlue;
}
int i;
private void button2_Click(object sender, EventArgs e)
{
i = 2;
}
private void button3_Click(object sender, EventArgs e)
{
i = 3;
}
private void button4_Click(object sender, EventArgs e)
{
i = 4;
}
private void button5_Click(object sender, EventArgs e)
{
i = 5;
}
private void button6_Click(object sender, EventArgs e)
{
i = 6;
}
private void button7_Click(object sender, EventArgs e)
{
i = 7;
}
private void button8_Click(object sender, EventArgs e)
{
i = 8;
}
private void button9_Click(object sender, EventArgs e)
{
switch (i)
{
case 1:
RacunajZaDugme(button1, button9, igra);
break;
case 2:
RacunajZaDugme(button2, button9, igra);
break;
case 3:
RacunajZaDugme(button3, button9, igra);
break;
case 4:
RacunajZaDugme(button4, button9, igra);
break;
case 5:
RacunajZaDugme(button5, button9, igra);
break;
case 6:
RacunajZaDugme(button6, button9, igra);
break;
case 7:
RacunajZaDugme(button7, button9, igra);
break;
case 8:
RacunajZaDugme(button8, button9, igra);
break;
}
}
private void button10_Click(object sender, EventArgs e)
{
switch (i)
{
case 1:
RacunajZaDugme(button1, button10, igra);
break;
case 2:
RacunajZaDugme(button2, button10, igra);
break;
case 3:
RacunajZaDugme(button3, button10, igra);
break;
case 4:
RacunajZaDugme(button4, button10, igra);
break;
case 5:
RacunajZaDugme(button5, button10, igra);
break;
case 6:
RacunajZaDugme(button6, button10, igra);
break;
case 7:
RacunajZaDugme(button7, button10, igra);
break;
case 8:
RacunajZaDugme(button8, button10, igra);
break;
}
}
private void button11_Click(object sender, EventArgs e)
{
switch (i)
{
case 1:
RacunajZaDugme(button1, button11, igra);
break;
case 2:
RacunajZaDugme(button2, button11, igra);
break;
case 3:
RacunajZaDugme(button3, button11, igra);
break;
case 4:
RacunajZaDugme(button4, button11, igra);
break;
case 5:
RacunajZaDugme(button5, button11, igra);
break;
case 6:
RacunajZaDugme(button6, button11, igra);
break;
case 7:
RacunajZaDugme(button7, button11, igra);
break;
case 8:
RacunajZaDugme(button8, button11, igra);
break;
}
}
private void button12_Click(object sender, EventArgs e)
{
switch (i)
{
case 1:
RacunajZaDugme(button1, button12, igra);
break;
case 2:
RacunajZaDugme(button2, button12, igra);
break;
case 3:
RacunajZaDugme(button3, button12, igra);
break;
case 4:
RacunajZaDugme(button4, button12, igra);
break;
case 5:
RacunajZaDugme(button5, button12, igra);
break;
case 6:
RacunajZaDugme(button6, button12, igra);
break;
case 7:
RacunajZaDugme(button7, button12, igra);
break;
case 8:
RacunajZaDugme(button8, button12, igra);
break;
}
}
private void button13_Click(object sender, EventArgs e)
{
switch (i)
{
case 1:
RacunajZaDugme(button1, button13, igra);
break;
case 2:
RacunajZaDugme(button2, button13, igra);
break;
case 3:
RacunajZaDugme(button3, button13, igra);
break;
case 4:
RacunajZaDugme(button4, button13, igra);
break;
case 5:
RacunajZaDugme(button5, button13, igra);
break;
case 6:
RacunajZaDugme(button6, button13, igra);
break;
case 7:
RacunajZaDugme(button7, button13, igra);
break;
case 8:
RacunajZaDugme(button8, button13, igra);
break;
}
}
private void button14_Click(object sender, EventArgs e)
{
switch (i)
{
case 1:
RacunajZaDugme(button1, button14, igra);
break;
case 2:
RacunajZaDugme(button2, button14, igra);
break;
case 3:
RacunajZaDugme(button3, button14, igra);
break;
case 4:
RacunajZaDugme(button4, button14, igra);
break;
case 5:
RacunajZaDugme(button5, button14, igra);
break;
case 6:
RacunajZaDugme(button6, button14, igra);
break;
case 7:
RacunajZaDugme(button7, button14, igra);
break;
case 8:
RacunajZaDugme(button8, button14, igra);
break;
}
}
private void button15_Click(object sender, EventArgs e)
{
switch (i)
{
case 1:
RacunajZaDugme(button1, button15, igra);
break;
case 2:
RacunajZaDugme(button2, button15, igra);
break;
case 3:
RacunajZaDugme(button3, button15, igra);
break;
case 4:
RacunajZaDugme(button4, button15, igra);
break;
case 5:
RacunajZaDugme(button5, button15, igra);
break;
case 6:
RacunajZaDugme(button6, button15, igra);
break;
case 7:
RacunajZaDugme(button7, button15, igra);
break;
case 8:
RacunajZaDugme(button8, button15, igra);
break;
}
}
private void button16_Click(object sender, EventArgs e)
{
switch (i)
{
case 1:
RacunajZaDugme(button1, button16, igra);
break;
case 2:
RacunajZaDugme(button2, button16, igra);
break;
case 3:
RacunajZaDugme(button3, button16, igra);
break;
case 4:
RacunajZaDugme(button4, button16, igra);
break;
case 5:
RacunajZaDugme(button5, button16, igra);
break;
case 6:
RacunajZaDugme(button6, button16, igra);
break;
case 7:
RacunajZaDugme(button7, button16, igra);
break;
case 8:
RacunajZaDugme(button8, button16, igra);
break;
}
}
private void button1_Click(object sender, EventArgs e)
{
i = 1;
}
}
}
|