Cóż chciałem tego uniknąć, ze względu na dość dużą zawiłość mojego kodu (najpierw chcę się uporać z timerem, a później będę zajmował się optymalizacją kodu).
Więc jeżeli coś będzie niezrozumiałe to proszę o wiadomość- wyjaśnię ;)
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Net;
using System.Windows.Media.Imaging;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Windows.Threading;
namespace Hangman
{
public partial class Plays : PhoneApplicationPage
{
string folderName = "Hangman";
string fileNameR = "Hangman-random.txt";
string fileNameT = "Hangman-tip.txt";
string fileNameL = "Hangman-lvl.txt";
int liczba;
int licznik = 0;
int k = 1;
int o = 0;
int[] tips = new int[10];
int ea = 0;
int md = 0;
int h = 0;
int hc = 0;
private string LoadTip()
{
string result = null;
using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
{
string filePath = System.IO.Path.Combine(folderName, fileNameT);
if (isf.FileExists(filePath))
{
try
{
using (IsolatedStorageFileStream rawStream = isf.OpenFile(filePath,
System.IO.FileMode.Open))
{
StreamReader reader = new StreamReader(rawStream);
result = reader.ReadLine();
reader.Close();
}
}
catch
{
}
}
}
return result;
}
private string LoadLvl()
{
string result = null;
using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
{
string filePath = System.IO.Path.Combine(folderName, fileNameL);
if (isf.FileExists(filePath))
{
try
{
using (IsolatedStorageFileStream rawStream = isf.OpenFile(filePath,
System.IO.FileMode.Open))
{
StreamReader reader = new StreamReader(rawStream);
result = reader.ReadLine();
reader.Close();
}
}
catch
{
}
}
}
return result;
}
private string LoadRandom()
{
string result = null;
using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
{
string filePath = System.IO.Path.Combine(folderName, fileNameR);
if (isf.FileExists(filePath))
{
try
{
using (IsolatedStorageFileStream rawStream = isf.OpenFile(filePath,
System.IO.FileMode.Open))
{
StreamReader reader = new StreamReader(rawStream);
result = reader.ReadLine();
reader.Close();
}
}
catch
{
}
}
}
return result;
}
private void Timer()
{
System.Windows.Threading.DispatcherTimer dt = new System.Windows.Threading.DispatcherTimer();
dt.Interval = new TimeSpan(0, 0, 0, 0, 1000);
dt.Tick += new EventHandler(dt_Tick);
dt.Start();
}
public Plays()
{
InitializeComponent();
Losowanie((int.Parse(tip.Text) + 4).ToString(), (int.Parse(tip.Text) + 3).ToString(), (int.Parse(tip.Text) + 2).ToString(), (int.Parse(tip.Text) + 1).ToString());
LvlTXT(poziom);
Timer();
}
private void Losowanie(string easy, string medium, string hard, string hardcore)
{
try
{
string lvl = LoadLvl();
int random = int.Parse(LoadRandom());
int tipp = int.Parse(LoadTip());
if (lvl.Equals("easy"))
{
if (poziom == 1)
liczba = new Random().Next(1, random);
else
liczba = new Random().Next(1, ea);
if (poziom == 1)
PodpowiedzTXT(LoadTip());
else
{
if (int.Parse(tip.Text) + 4 <= 10)
PodpowiedzTXT(easy);
else
PodpowiedzTXT("10");
}
PoziomTXT(lvl.ToUpper());
if (poziom == 1)
LiczbaTXT(random.ToString());
else
LiczbaTXT((20 + ea).ToString());
for (; licznik < tipp; licznik++)
{
int tipss = new Random().Next(1, (tipp + 1));
if (tips[0] == tipss || tips[1] == tipss || tips[2] == tipss || tips[3] == tipss || tips[4] == tipss || tips[5] == tipss || tips[6] == tipss || tips[7] == tipss || tips[8] == tipss || tips[9] == tipss) //losowanie liczby bez powtórzeń, zdaję sobie sprawę, że nie jest to profesjonalne rozwiązanie, ale nie miałem pomysłu na nic innego
{
licznik--;
}
else
{
tips[licznik] = tipss;
}
}
}
else if (lvl.Equals("medium"))
{
if (poziom == 1)
liczba = new Random().Next(1, random);
else
liczba = new Random().Next(1, md);
if (poziom == 1)
PodpowiedzTXT(LoadTip());
else
{
if (int.Parse(tip.Text) + 3 <= 10)
PodpowiedzTXT(medium);
else
PodpowiedzTXT("10");
}
PoziomTXT(lvl.ToUpper());
if (poziom == 1)
LiczbaTXT(random.ToString());
else
LiczbaTXT((30 + md).ToString());
for (; licznik < tipp; licznik++)
{
int tipss = new Random().Next(1, (tipp + 1));
if (tips[0] == tipss || tips[1] == tipss || tips[2] == tipss || tips[3] == tipss || tips[4] == tipss)
{
licznik--;
}
else
{
tips[licznik] = tipss;
}
}
}
else if (lvl.Equals("hard"))
{
if (poziom == 1)
liczba = new Random().Next(1, random);
else
liczba = new Random().Next(1, h);
if (poziom == 1)
PodpowiedzTXT(LoadTip());
else
{
if (int.Parse(tip.Text) + 2 <= 10)
PodpowiedzTXT(hard);
else
PodpowiedzTXT("10");
}
PoziomTXT(lvl.ToUpper());
if (poziom == 1)
LiczbaTXT(random.ToString());
else
LiczbaTXT((40 + h).ToString());
for (; licznik < tipp; licznik++)
{
int tipss = new Random().Next(1, (tipp + 1));
if (tips[0] == tipss || tips[1] == tipss || tips[2] == tipss)
{
licznik--;
}
else
{
tips[licznik] = tipss;
}
}
}
else if (lvl.Equals("hardcore"))
{
if (poziom == 1)
liczba = new Random().Next(1, random);
else
liczba = new Random().Next(1, hc);
if (poziom == 1)
PodpowiedzTXT(LoadTip());
else
{
if (int.Parse(tip.Text) + 1 <= 10)
PodpowiedzTXT(hardcore);
else
PodpowiedzTXT("10");
}
PoziomTXT(lvl.ToUpper());
if (poziom == 1)
LiczbaTXT(random.ToString());
else
LiczbaTXT((50 + hc).ToString());
for (; licznik < tipp; licznik++)
{
int tipss = new Random().Next(1, (tipp + 1));
if (tips[0] == tipss)
{
licznik--;
}
else
{
tips[licznik] = tipss;
}
}
}
}
catch (Exception)
{
liczba = new Random().Next(1, 20);
PodpowiedzTXT("10");
PoziomTXT("EASY");
LiczbaTXT("20");
for (; licznik < 10; licznik++)
{
int tipss = new Random().Next(1, 11);
if (tips[0] == tipss || tips[1] == tipss || tips[2] == tipss || tips[3] == tipss || tips[4] == tipss || tips[5] == tipss || tips[6] == tipss || tips[7] == tipss || tips[8] == tipss || tips[9] == tipss)
{
licznik--;
}
else
{
tips[licznik] = tipss;
}
}
}
}
private void PodpowiedzTXT(string tips)
{
tip.Text = tips;
}
private void PoziomTXT(string poziom)
{
st.Text = poziom;
}
private void LiczbaTXT(string liczba)
{
number.Text = string.Format("1-{0}", liczba);
}
private void LvlTXT(int c)
{
lvl.Text = c.ToString();
}
private void Szubienica(int w)
{
if (w == 1)
{
BitmapImage myImage1 = new BitmapImage
(new Uri("Image/2.png", UriKind.Relative));
img.Source = myImage1;
}
else if (w == 2)
{
BitmapImage myImage1 = new BitmapImage
(new Uri("Image/3.png", UriKind.Relative));
img.Source = myImage1;
}
else if (w == 3)
{
BitmapImage myImage1 = new BitmapImage
(new Uri("Image/4.png", UriKind.Relative));
img.Source = myImage1;
}
else if (w == 4)
{
BitmapImage myImage1 = new BitmapImage
(new Uri("Image/5.png", UriKind.Relative));
img.Source = myImage1;
}
else if (w == 5)
{
BitmapImage myImage1 = new BitmapImage
(new Uri("Image/6.png", UriKind.Relative));
img.Source = myImage1;
}
else if (w == 6)
{
BitmapImage myImage1 = new BitmapImage
(new Uri("Image/7.png", UriKind.Relative));
img.Source = myImage1;
}
else if (w == 7)
{
BitmapImage myImage1 = new BitmapImage
(new Uri("Image/8.png", UriKind.Relative));
img.Source = myImage1;
}
else if (w == 8)
{
BitmapImage myImage1 = new BitmapImage
(new Uri("Image/9.png", UriKind.Relative));
img.Source = myImage1;
}
else if (w == 9)
{
BitmapImage myImage1 = new BitmapImage
(new Uri("Image/10.png", UriKind.Relative));
img.Source = myImage1;
}
else if (w == 10)
{
BitmapImage myImage1 = new BitmapImage
(new Uri("Image/11.png", UriKind.Relative));
img.Source = myImage1;
}
else if (w == 11)
{
BitmapImage myImage1 = new BitmapImage
(new Uri("Image/gameover.png", UriKind.Relative));
img.Source = myImage1;
}
else if (w == 12)
{
MessageBox.Show("Koniec gry!", "Przegrałeś", MessageBoxButton.OK);
NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
}
}
int w = 1;
int poziom = 1;
int easy = 180;
int medium = 120;
int hard = 90;
int hardcore = 60;
void dt_Tick(object sender, EventArgs e)
{
string lvl = LoadLvl();
if (lvl.Equals("easy"))
{
timer.Text = string.Format("{0}s", easy);
if (easy < 11)
timer.Foreground = new SolidColorBrush(Colors.Red);
if (easy < 0)
TimeOut();
easy--;
}
else if (lvl.Equals("medium"))
{
timer.Text = string.Format("{0}s", medium);
medium--;
if (medium < 11)
timer.Foreground = new SolidColorBrush(Colors.Red);
if (medium < 0)
TimeOut();
}
else if (lvl.Equals("hard"))
{
timer.Text = string.Format("{0}s", hard);
hard--;
if (hard < 11)
timer.Foreground = new SolidColorBrush(Colors.Red);
if (hard < 0)
TimeOut();
}
else if (lvl.Equals("hardcore"))
{
timer.Text = string.Format("{0}s", hardcore);
hardcore--;
if (hardcore < 11)
timer.Foreground = new SolidColorBrush(Colors.Red);
if (hardcore < 0)
TimeOut();
}
}
private void TimeOut()
{
MessageBox.Show("Koniec czasu...", "Przegrałeś!", MessageBoxButton.OK);
NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
}
private void check_btn1_Copy1_Click(object sender, RoutedEventArgs e)
{
for (; w < 13; w++)
{
try
{
if (tbx1.Text.Equals(liczba.ToString()))
{
MessageBox.Show("Wygrałeś!", "Gratulacje", MessageBoxButton.OK);
w = 1;
o = 0;
BitmapImage myImage1 = new BitmapImage
(new Uri("", UriKind.Relative));
img.Source = myImage1;
tbx1.Text = "";
BitmapImage myImage = new BitmapImage
(new Uri("Image/null.png", UriKind.Relative));
m.Source = myImage;
for (; ; )
{
LvlTXT(poziom + 1);
break;
}
poziom++;
ea = ea + 3;
md = md + 6;
h = h + 9;
hc = hc + 12;
Losowanie((int.Parse(tip.Text) + 4).ToString(), (int.Parse(tip.Text) + 3).ToString(), (int.Parse(tip.Text) + 2).ToString(), (int.Parse(tip.Text) + 1).ToString());
easy = 180;
medium = 120;
hard = 90;
hardcore = 60;
Timer();
}
else if (int.Parse(tbx1.Text) > liczba)
{
BitmapImage myImage = new BitmapImage
(new Uri("Image/mniej.png", UriKind.Relative));
m.Source = myImage;
Szubienica(w);
}
else if (int.Parse(tbx1.Text) < liczba)
{
BitmapImage myImage = new BitmapImage
(new Uri("Image/więcej.png", UriKind.Relative));
m.Source = myImage;
Szubienica(w);
}
}
catch (Exception blad)
{
MessageBox.Show(blad.Message, "Błąd!", MessageBoxButton.OK);
w--;
}
break;
}
w++;
}
private void check_btn1_Copy_Click(object sender, RoutedEventArgs e)
{
int LT = int.Parse(tip.Text);
try
{
for (; k <= LT; )
{
PodpowiedzTXT((LT - k).ToString());
break;
}
if ((LT - k) >= 0)
{
for (; o < 11; o++)
{
if (tips[o] == 1)
{
MessageBox.Show("1", "Podpowiedź", MessageBoxButton.OK);
break;
}
else if (tips[o] == 2)
{
MessageBox.Show("2", "Podpowiedź", MessageBoxButton.OK);
break;
}
else if (tips[o] == 3)
{
MessageBox.Show("3", "Podpowiedź", MessageBoxButton.OK);
break;
}
else if (tips[o] == 4)
{
MessageBox.Show("4", "Podpowiedź", MessageBoxButton.OK);
break;
}
else if (tips[o] == 5)
{
MessageBox.Show("5", "Podpowiedź", MessageBoxButton.OK);
break;
}
else if (tips[o] == 6)
{
MessageBox.Show("6", "Podpowiedź", MessageBoxButton.OK);
break;
}
else if (tips[o] == 7)
{
MessageBox.Show("7", "Podpowiedź", MessageBoxButton.OK);
break;
}
else if (tips[o] == 8)
{
MessageBox.Show("8", "Podpowiedź", MessageBoxButton.OK);
break;
}
else if (tips[o] == 9)
{
MessageBox.Show("9", "Podpowiedź", MessageBoxButton.OK);
break;
}
else if (tips[o] == 10)
{
MessageBox.Show("10", "Podpowiedź", MessageBoxButton.OK);
break;
}
}
o++;
}
else
MessageBox.Show("Wykorzystałeś wszystkie podpowiedzi!", "Błąd", MessageBoxButton.OK);
}
catch (Exception)
{
LT = 10;
for (; k <= LT; )
{
PodpowiedzTXT((LT - k).ToString());
break;
}
if ((LT - k) >= 0)
{
for (; o < LT; o++)
{
if (tips[o] == 1)
{
MessageBox.Show("1", "Podpowiedź", MessageBoxButton.OK);
break;
}
else if (tips[o] == 2)
{
MessageBox.Show("2", "Podpowiedź", MessageBoxButton.OK);
break;
}
else if (tips[o] == 3)
{
MessageBox.Show("3", "Podpowiedź", MessageBoxButton.OK);
break;
}
else if (tips[o] == 4)
{
MessageBox.Show("4", "Podpowiedź", MessageBoxButton.OK);
break;
}
else if (tips[o] == 5)
{
MessageBox.Show("5", "Podpowiedź", MessageBoxButton.OK);
break;
}
else if (tips[o] == 6)
{
MessageBox.Show("6", "Podpowiedź", MessageBoxButton.OK);
break;
}
else if (tips[o] == 7)
{
MessageBox.Show("7", "Podpowiedź", MessageBoxButton.OK);
break;
}
else if (tips[o] == 8)
{
MessageBox.Show("8", "Podpowiedź", MessageBoxButton.OK);
break;
}
else if (tips[o] == 9)
{
MessageBox.Show("9", "Podpowiedź", MessageBoxButton.OK);
break;
}
else if (tips[o] == 10)
{
MessageBox.Show("10", "Podpowiedź", MessageBoxButton.OK);
break;
}
}
o++;
}
else
MessageBox.Show("Wykorzystałeś wszystkie podpowiedzi!", "Błąd", MessageBoxButton.OK);
}
}
}
}
Pozdrawiam