drewhosick
drewhosick
CC#
Created by drewhosick on 12/22/2023 in #help
Damned Arrays
I knew it couldn't be hard but I just couldn't figure out what I was doing wrong. I just moved the declaration outside the method and it works now. Thank you for the quick lesson on scope. I didn't know you couldn't create a public variable in the method that would be available outside by using the public modifier.
16 replies
CC#
Created by drewhosick on 12/22/2023 in #help
Damned Arrays
ah crap eh. Ok thanks
16 replies
CC#
Created by drewhosick on 12/17/2023 in #help
accessing an instance of a class in another class
So like this right? It seems to work so I assume it's right
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace RacingRiot
{
public class Race
{
public void DrawTrack(Car playerCar, Car aiCar)
{
Console.WriteLine("\nYou\n");
playerCar.DrawCar();
Console.WriteLine("\n\nopponent\n");
aiCar.DrawCar();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace RacingRiot
{
public class Race
{
public void DrawTrack(Car playerCar, Car aiCar)
{
Console.WriteLine("\nYou\n");
playerCar.DrawCar();
Console.WriteLine("\n\nopponent\n");
aiCar.DrawCar();
}
}
}
20 replies
CC#
Created by drewhosick on 12/17/2023 in #help
accessing an instance of a class in another class
ah ok. Thanks all
20 replies
CC#
Created by drewhosick on 12/17/2023 in #help
accessing an instance of a class in another class
Ok so kind of like a pointer?
20 replies
CC#
Created by drewhosick on 12/17/2023 in #help
accessing an instance of a class in another class
Sorry, I still find the class thing a little confusing so I want to get it right. and thanks for your help.
20 replies
CC#
Created by drewhosick on 12/17/2023 in #help
accessing an instance of a class in another class
as an argument? Would you then have to copy it into a new instance of the Car Class inside that Race Class or can you just access the instances directly from the arguments passed in?
20 replies
CC#
Created by drewhosick on 12/17/2023 in #help
accessing an instance of a class in another class
Finally, though it's not really important to the discussion Start.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RacingRiot
{
static class Start
{
static public void StartMessage()
{
Console.WriteLine("Welcome to Racing Riot. The Racing Game where you try to be the first to the Finish Line.\n\n");
Console.WriteLine("Try to beat the other racers on the screen by taking less guesses to guess the random numbers between 1-100. For every one you get wrong the other cars move ahead by 5. For every good guess you " +
"move ahead by 25");
Console.WriteLine("Are you ready? y/n");
char startPlay = Console.ReadKey().KeyChar;



if ((startPlay == 'y') || (startPlay == 'Y'))
{
Console.WriteLine("\nAlright Let's get started.");
Thread.Sleep(5000);
Console.Clear();
}
else if ((startPlay == 'n') || (startPlay == 'N'))
{
Console.WriteLine("\nAlright, maybe next time. Have a good day. Goodbye.");
Thread.Sleep(5000);
Console.Clear();
}
else
{
Console.WriteLine("\nYou chose an invalid response. Sorry. Goodbye.");
Thread.Sleep(5000);
Console.Clear();
}
}

}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RacingRiot
{
static class Start
{
static public void StartMessage()
{
Console.WriteLine("Welcome to Racing Riot. The Racing Game where you try to be the first to the Finish Line.\n\n");
Console.WriteLine("Try to beat the other racers on the screen by taking less guesses to guess the random numbers between 1-100. For every one you get wrong the other cars move ahead by 5. For every good guess you " +
"move ahead by 25");
Console.WriteLine("Are you ready? y/n");
char startPlay = Console.ReadKey().KeyChar;



if ((startPlay == 'y') || (startPlay == 'Y'))
{
Console.WriteLine("\nAlright Let's get started.");
Thread.Sleep(5000);
Console.Clear();
}
else if ((startPlay == 'n') || (startPlay == 'N'))
{
Console.WriteLine("\nAlright, maybe next time. Have a good day. Goodbye.");
Thread.Sleep(5000);
Console.Clear();
}
else
{
Console.WriteLine("\nYou chose an invalid response. Sorry. Goodbye.");
Thread.Sleep(5000);
Console.Clear();
}
}

}
}
20 replies
CC#
Created by drewhosick on 12/17/2023 in #help
accessing an instance of a class in another class
Race.cs Class where I'm having issues. And I know I can't say playerCar.DrawCar() cause it's an instance from Program.CS. Just trying to figure out the best way to pass it over.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace RacingRiot
{
public class Race
{
public void DrawTrack()
{
Console.WriteLine("\nYou\n");
playerCar.DrawCar();
Console.WriteLine("\n\nopponent\n");
aiCar.DrawCar();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace RacingRiot
{
public class Race
{
public void DrawTrack()
{
Console.WriteLine("\nYou\n");
playerCar.DrawCar();
Console.WriteLine("\n\nopponent\n");
aiCar.DrawCar();
}
}
}
20 replies
CC#
Created by drewhosick on 12/17/2023 in #help
accessing an instance of a class in another class
Car.cs Class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RacingRiot
{
public class Car
{
public int behindCar = 0;
public int frontCar = 100;
public string trackBehindCar = "";
public string trackFrontCar = "";
public string spaceBehindCar = "";
public string spaceFrontCar = "";
public void DrawCar()
{
trackBehindCar = "";
trackFrontCar = "";
string spaceBehindCar = "";

for (int i = 0; i < behindCar; i++)
{
trackBehindCar += "_";
spaceBehindCar += " ";
}
for (int i = 0; i < frontCar; i++)
{
trackFrontCar += "_";
}
Console.WriteLine($"{spaceBehindCar}.-'--`-._\n{trackBehindCar}'-O---O--'{trackFrontCar}\n");
}

public void advanceCar()
{
behindCar++;
frontCar--;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RacingRiot
{
public class Car
{
public int behindCar = 0;
public int frontCar = 100;
public string trackBehindCar = "";
public string trackFrontCar = "";
public string spaceBehindCar = "";
public string spaceFrontCar = "";
public void DrawCar()
{
trackBehindCar = "";
trackFrontCar = "";
string spaceBehindCar = "";

for (int i = 0; i < behindCar; i++)
{
trackBehindCar += "_";
spaceBehindCar += " ";
}
for (int i = 0; i < frontCar; i++)
{
trackFrontCar += "_";
}
Console.WriteLine($"{spaceBehindCar}.-'--`-._\n{trackBehindCar}'-O---O--'{trackFrontCar}\n");
}

public void advanceCar()
{
behindCar++;
frontCar--;
}
}
}
20 replies
CC#
Created by drewhosick on 12/17/2023 in #help
accessing an instance of a class in another class
This is the Program.cs
namespace RacingRiot
{
internal class Program
{
static void Main(string[] args)
{
Car playerCar = new Car();
Car aiCar = new Car();
Race race = new Race();

Start.StartMessage();
race.DrawTrack();
Thread.Sleep(5000);
}
}
}
namespace RacingRiot
{
internal class Program
{
static void Main(string[] args)
{
Car playerCar = new Car();
Car aiCar = new Car();
Race race = new Race();

Start.StartMessage();
race.DrawTrack();
Thread.Sleep(5000);
}
}
}
20 replies
CC#
Created by drewhosick on 12/13/2023 in #help
WPF and variables that I need access to in a Button_Click event
Yeah, I started reading about it but I am not very familiar with it yet. I will read more and continue to learn. Thanks for your time
23 replies
CC#
Created by drewhosick on 12/13/2023 in #help
WPF and variables that I need access to in a Button_Click event
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Guess_Number_Game
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private int lives = 10;
private int random = 0;

public MainWindow()
{
InitializeComponent();
Random rnd = new Random();
random = rnd.Next() % 100;
}

private void txbInput_KeyDown(object sender, KeyEventArgs e)
{
if(lives == 0)
{
lblFrom.Content = "You";
lblTo.Content = "lose";
return;
}
if(e.Key == Key.Enter)
{
lives--;
int userGuessed = Int32.Parse(txbInput.Text);
if ( userGuessed == random)
{
lblFrom.Content = "You";
lblTo.Content = "win";
return;
}
if (userGuessed < random)
{
lblFrom.Content = userGuessed;
lblStatus.Content = $"Remaining Lives {lives}";
}
else
{
lblTo.Content = userGuessed;
lblStatus.Content = $"Remaining Lives {lives}";
}
}

}
}
}
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Guess_Number_Game
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private int lives = 10;
private int random = 0;

public MainWindow()
{
InitializeComponent();
Random rnd = new Random();
random = rnd.Next() % 100;
}

private void txbInput_KeyDown(object sender, KeyEventArgs e)
{
if(lives == 0)
{
lblFrom.Content = "You";
lblTo.Content = "lose";
return;
}
if(e.Key == Key.Enter)
{
lives--;
int userGuessed = Int32.Parse(txbInput.Text);
if ( userGuessed == random)
{
lblFrom.Content = "You";
lblTo.Content = "win";
return;
}
if (userGuessed < random)
{
lblFrom.Content = userGuessed;
lblStatus.Content = $"Remaining Lives {lives}";
}
else
{
lblTo.Content = userGuessed;
lblStatus.Content = $"Remaining Lives {lives}";
}
}

}
}
}
23 replies
CC#
Created by drewhosick on 12/13/2023 in #help
WPF and variables that I need access to in a Button_Click event
Ok thank you. I will continue to work on it. I did end up using Int32.Prase and followed someone'se tutorial to try it out but I guess it's still nott he WPF way. I'll have to learn more about it. As of right now I have this working:
23 replies
CC#
Created by drewhosick on 12/13/2023 in #help
WPF and variables that I need access to in a Button_Click event
This is what I had...
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApp1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();

}
int guess;

private void Window_Load(object sender, RoutedEventArgs e)
{
Random rand = new Random();
guess = rand.Next(1, 100);
}


private void Button_Click(object sender, RoutedEventArgs e)
{
if (Convert.ToInt32(GuessInput.Text) < 1 || Convert.ToInt32(GuessInput.Text) > 100)
{
DisplayTextBox.Text = "Please choose a number between 1 and 100 and input it in the box below.";
}
else if (Convert.ToInt32(GuessInput.Text) == guess)
{
DisplayTextBox.Text = $"Congratulations! {GuessInput.Text} was the right number.";
}
else if (Convert.ToInt32(GuessInput.Text) < guess)
{
DisplayTextBox.Text = $"{GuessInput.Text} is too high. Try again.";
}
else
{
DisplayTextBox.Text = $"{GuessInput.Text} is too low. Try again.";

}
}
}
}
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApp1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();

}
int guess;

private void Window_Load(object sender, RoutedEventArgs e)
{
Random rand = new Random();
guess = rand.Next(1, 100);
}


private void Button_Click(object sender, RoutedEventArgs e)
{
if (Convert.ToInt32(GuessInput.Text) < 1 || Convert.ToInt32(GuessInput.Text) > 100)
{
DisplayTextBox.Text = "Please choose a number between 1 and 100 and input it in the box below.";
}
else if (Convert.ToInt32(GuessInput.Text) == guess)
{
DisplayTextBox.Text = $"Congratulations! {GuessInput.Text} was the right number.";
}
else if (Convert.ToInt32(GuessInput.Text) < guess)
{
DisplayTextBox.Text = $"{GuessInput.Text} is too high. Try again.";
}
else
{
DisplayTextBox.Text = $"{GuessInput.Text} is too low. Try again.";

}
}
}
}
23 replies
CC#
Created by drewhosick on 12/13/2023 in #help
WPF and variables that I need access to in a Button_Click event
No description
23 replies
CC#
Created by drewhosick on 12/13/2023 in #help
WPF and variables that I need access to in a Button_Click event
No description
23 replies
CC#
Created by drewhosick on 12/11/2023 in #help
Why Do you have to Create an instance of a Class in some cases for methods and not others?
thanks
16 replies
CC#
Created by drewhosick on 12/11/2023 in #help
Why Do you have to Create an instance of a Class in some cases for methods and not others?
Ok. Thank you
16 replies
CC#
Created by drewhosick on 12/11/2023 in #help
Why Do you have to Create an instance of a Class in some cases for methods and not others?
Makes sense but why can you use methods without creating objects in certain cases like on an array or variable? Is it because the object is already an instance of that type?
16 replies