in VSC first i tried to return the variable uint PlayersDiceRoll but i kept getting a error about not being able to put Random.Next() in a static method or something like that, so i changed it and tried to call the method as a whole and am now getting a error saying the variable DiceRoll isnt in my code, so someone please help me.
using System;
namespace _Test_simplyVideoGame
{
//Change class name to video game title when it is known.
class Program
{
public void Main(string[] args)
{
PlayerDiceRoll(DiceRoll);
}
public uint PlayerDiceRoll(uint DiceRoll)
{
Random Dice = new Random();
uint PlayersDiceRoll = Convert.ToUInt32(Random.Next)(uint.MinValue,uint.MaxValue);
return DiceRoll;
}
}
}
Start by making your functions static
actually use the Random object
and if you only want positive random number I would just use int from 0 to max which is 2b values, if you need 4b possible options you could go from negative to possitive and then add the possitive, but you must make sure not to exceed the bounds of uint
static void Main(string[] args)
{
var playerRolled = PlayerDiceRoll();
Console.WriteLine("Player rolled: " + playerRolled);
Console.ReadLine();
}
public static int PlayerDiceRoll()
{
Random Dice = new Random();
var randomValue = Dice.Next(0, int.MaxValue);
return randomValue;
}
And just for a bit of fun:
You could make general dice if you need it:
static void Main(string[] args)
{
var playerRolled = PlayerDiceRoll();
Console.WriteLine("Player rolled: " + playerRolled);
Console.ReadLine();
Console.WriteLine("Player rolls default die: " + PlayerRoleSidedDie(6));
Console.ReadLine();
Console.WriteLine("Player rolls 64 value die die with 16 sides : " + PlayerRoleSidedDie(16,4));
Console.ReadLine();
Console.WriteLine("Player rolls two 6 sided dice");
var die1 = PlayerRoleSidedDie(6);
var die2 = PlayerRoleSidedDie(6);
Console.WriteLine("die 1 was: " + die1 + " die 2 was: " + die2 + " total roll was: " + (die1 + die2));
Console.ReadLine();
}
public static int PlayerDiceRoll()
{
Random Dice = new Random();
var randomValue = Dice.Next(0, int.MaxValue);
return randomValue;
}
public static int PlayerRoleSidedDie(int size, int multiplyer=1)
{
Random die = new Random();
var selectedSide = die.Next(1, size);
return selectedSide * multiplyer;
}
}
Related
i'm a beginner to c#,so i was trying to make a program that rolls a die for you and the enemy for 10 turns,each turns adding the number of your roll to an overall count and whoever got the largest in the end won,i didn't finish it all the way but this is what i have so far:
namespace dfgs
{
class dice
{
public static void Main(String[] args)
{
int plsc = 0;
int aisc = 0;
int turns = 0;
Random plrnd = new Random();
Random airnd = new Random();
while (turns < 11)
{
Console.WriteLine("Player Roll Is" + Convert.ToInt32(plrnd.Next(6)));
Console.WriteLine("AI Roll Is" + Convert.ToInt32(airnd.Next(6)));
plsc = plsc + plrnd;
aisc = aisc + airnd;
Console.WriteLine("Type A and hit enter to go again");
string nxt = Console.ReadLine();
if (nxt == "A"){
turns++;
}
Console.ReadLine();
}
}
}
}
and whenever i try to compile i get the error Operator +' cannot be applied to operands of type int' and System.Random',this error appears twice,i tried changing the type of the random numbers to int but then i got the error messege Type int' does not contain a definition for Next' and no extension method Next' of type int' could be found. Are you missing an assembly reference? i am kinda stuck here,any help would be appreciated.
EDIT:Thank you to everyone who answered, i have managed to make this work,here is the final code,it's not the cleanest but works as intended:
namespace dfgs
{
class die
{
public static void Main(String[] args)
{
int plsc = 0;
int aisc = 0;
int turns = 0;
Random plrnd = new Random();
Random airnd = new Random();
while (turns < 10)
{
Console.WriteLine("Player Roll Is " + Convert.ToInt32(plrnd.Next(6)));
Console.WriteLine("AI Roll Is " + Convert.ToInt32(airnd.Next(6)));
plsc = plsc + plrnd.Next(6);
aisc = aisc + airnd.Next(6);
Console.WriteLine("Type A and hit enter to go again");
string nxt = Console.ReadLine();
if (nxt == "A"){
turns++;
}
else{
break;
}
if (turns == 10){
if (plsc > aisc){
Console.WriteLine("The Player Has Won,Ai Score: " + aisc + " Player Score: " + plsc);
}
else if (aisc > plsc){
Console.WriteLine("The AI Has Won,Ai Score: " + aisc + " Player Score: " + plsc);
}
break;
}
}
Console.ReadLine();
}
}
}
You probably want to save the random int you get from the Random, rather than trying to calculate number, plus a random_number_generator_machine - which, in some real world terms would be like asking someone "what is your age, divided by a cheese sandwich?"..
var plroll = plrnd.Next(6);
var airoll = airnd.Next(6);
Console.WriteLine("Player Roll Is" + plroll);
Console.WriteLine("AI Roll Is" + airoll));
plsc = plsc + plroll;
aisc = aisc + airoll;
You don't need a Random for the player and a Random for the computer; one Random could adequately generate for both, btw:
int plsc = 0;
int aisc = 0;
int turns = 0;
Random r = new Random();
while (turns < 11)
{
var plroll = r.Next(6);
var airoll = r.Next(6);
Console.WriteLine("Player Roll Is" + plroll);
Console.WriteLine("AI Roll Is" + airoll));
plsc = plsc + plroll;
aisc = aisc + airoll;
Look at this line:
plsc = plsc + plrnd;
In this code, plrnd is not a number. Instead, it's a generator for producing numbers. You must tell the generator to give you the next number, as you did on the line above:
plrnd.Next(6)
Moreover, you likely need to change the code above to save the results of each call in variables so you can use that same value to both show to the user and increment the total count.
Since you want to use the result of the roll multiple times, start by assigning the results to a variable for later use:
while (turns < 11)
{
// roll dies, store results in local variable
var plRoll = plrnd.Next(6);
var aiRoll = airnd.Next(6);
// then reuse the variables
Console.WriteLine("Player Roll Is " + plRoll);
Console.WriteLine("AI Roll Is " + aiRoll);
plsc = plsc + plRoll;
aisc = aisc + aiRoll;
Console.WriteLine("Type A and hit enter to go again");
string nxt = Console.ReadLine();
if (nxt == "A"){
turns++;
}
else {
// remember to break out if the player doesn't want to continue
break;
}
}
int is a built in value data type in C#. Random is a class object. The + operator does not know how to add an int data type and a Random class object together. What you probably meant to do is to use the Next() method, which does return an int. Also, you only want to new up the Random class one time. Consider using this code instead:
namespace dfgs
{
class dice
{
public static void Main(String[] args)
{
Random rnd = new Random();
int plsc = 0;
int aisc = 0;
int turns = 0;
int plrnd = rnd.Next(6);
int airnd = rnd.Next(6);
while (turns < 11)
{
Console.WriteLine("Player Roll Is" + plrnd);
Console.WriteLine("AI Roll Is" + airnd);
plsc = plsc + plrnd;
aisc = aisc + airnd;
Console.WriteLine("Type A and hit enter to go again");
string nxt = Console.ReadLine();
if (nxt == "A"){
turns++;
}
Console.ReadLine();
}
}
}
}
My program won't move onto the next method after it executes the main method.
Right now, it just prints out "Hey! Welcome to Tina's Dice Game. Let's Start!", and then stops. How do I fix this? I've messed around with it for a while, but nothing worked. Thank you for your help.
using System;
namespace Major_Coding_Assignment_1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hey! Welcome to Tina's Dice Game.");
Console.WriteLine("Let's start!");
}
public static int numberofInvokes = 0;
public void EvenOrOdd()
{
numberofInvokes += 1;
Random rnd = new Random();
int x = rnd.Next(1, 7);
int y = rnd.Next(1, 7);
int added = x + y;
if (added % 2 == 0)
{
Console.WriteLine("I got" + " " + x + " " + "and" + " " + y);
Console.WriteLine("Evens are better than odds.");
}
else
{
Console.WriteLine("I got" + " " + x + " " + "and" + " " + y);
Console.WriteLine("Odds are still cool!");
}
}
public void playAgain()
{
Console.WriteLine("Do you want to play again?");
string val = Console.ReadLine();
if (val == "yes")
{
EvenOrOdd();
}
else
{
Console.WriteLine("The number of times the dice was thrown is:" + " " + numberofInvokes);
Console.WriteLine("Thanks for playing, come play again soon!");
}
}
}
}
You need to call the methods you need from the main function like this...
static void Main(string[] args)
{
Console.WriteLine("Hey! Welcome to Tina's Dice Game.");
Console.WriteLine("Let's start!");
EvenOrOdd();
playAgain();
}
The better way is to call playAgain inside EvenOrOdd method to make things look cleaner.
static void Main(string[] args)
{
Console.WriteLine("Hey! Welcome to Tina's Dice Game.");
Console.WriteLine("Let's start!");
EvenOrOdd();
}
public static int numberofInvokes = 0;
public void EvenOrOdd()
{
numberofInvokes += 1;
Random rnd = new Random();
int x = rnd.Next(1, 7);
int y = rnd.Next(1, 7);
int added = x + y;
if (added % 2 == 0)
{
Console.WriteLine("I got" + " " + x + " " + "and" + " " + y);
Console.WriteLine("Evens are better than odds.");
}
else
{
Console.WriteLine("I got" + " " + x + " " + "and" + " " + y);
Console.WriteLine("Odds are still cool!");
}
playAgain(); //Call playAgain from here.
}
public void playAgain()
{
Console.WriteLine("Do you want to play again?");
string val = Console.ReadLine();
if (val == "yes")
{
EvenOrOdd();
}
else
{
Console.WriteLine("The number of times the dice was thrown is:" + " " + numberofInvokes);
Console.WriteLine("Thanks for playing, come play again soon!");
}
}
its important that you call your methods from main. I like to think of it like a recipe main is the first instruction but you need to tell the reader where to look next. The same is true for C#, you need to tell the compiler where to go next by calling the next function in the main one.
static void Main(string[] args)
{
//your start code
nextFunction();
}
public void nextFunction(){
//your next function code
}
I made a game where you first say how many players there are and then every player throws 3 darts and it loops until one player gets 301 points and now I want to get the player who got more than 301 points from the list of players and I want to get their score so I can print out their score and say that they won at the end of the game but I can't figure out how to do that. Thanks
class Program
{
public static void Main(string[] args)
{
Game My_game = new Game();
My_game.PlayGame();
}
}
public class Game
{
private List<Player> player_list = new List<Player>();
private List<int> total_list = new List<int>();
public void PlayGame()
{
Random random_number = new Random();
int throw1;
int throw2;
int throw3;
string more_players = "yes";
while (more_players == "yes")
{
Console.WriteLine("What is the players name?: ");
player_list.Add(new Player(Console.ReadLine()));
Console.WriteLine("Are there more players?");
more_players = Console.ReadLine();
}
Console.WriteLine("\n\n Welcome to the dartgame! \n" +
"\n Game Rules: Each player throws 3 darts at a time." +
"\n Every throw can be worth 0-20 points." +
"\n Whoever gets 301 points first is the winner!");
Console.WriteLine("\nPlayers:");
foreach (var players in player_list)
{
Console.WriteLine(players);
}
int total_points = 0;
while (!player_list.Any(x => x.calculate_points() >= 301))
{
foreach (var players in player_list)
{
Console.WriteLine("\n\n\n\n");
Console.WriteLine("\n first throw for{0}!", players);
Console.WriteLine("Press space to throw a dart!");
Console.ReadKey();
throw1 = random_number.Next(1, 20);
Console.WriteLine("You got " + throw1 + " points!");
Console.WriteLine("\n second throw for{0}!", players);
Console.WriteLine("Press space to throw a dart!");
Console.ReadKey();
throw2 = random_number.Next(1, 20);
Console.WriteLine("You got " + throw2 + " points!");
Console.WriteLine("\n third throw for{0}!", players);
Console.WriteLine("Press space to throw a dart!");
Console.ReadKey();
throw3 = random_number.Next(1, 20);
Console.WriteLine("You got " + throw3 + " points!");
total_points = throw1 + throw2 + throw3;
Console.WriteLine("\nPoints for this round: " + total_points);
total_list.Add(total_points);
total_points = total_list.Sum(x => Convert.ToInt32(x));
players.Add_turn(throw1, throw2, throw3);
}
foreach (var players in player_list)
{
players.print_turns();
}
}
}
}
class Player
{
private string name;
private List<Turns> turn_list = new List<Turns>();
public Player(string _name)
{
name = _name;
}
public void Add_turn(int turn1, int turn2, int turn3)
{
turn_list.Add(new Turns(turn1, turn2, turn3));
}
public int calculate_points()
{
int total = 0;
foreach (var turns in turn_list)
{
total = total + turns.Get_Score();
}
return total;
}
public void print_turns()
{
Console.WriteLine("\n\n\n\n----------------------------------------");
Console.WriteLine("Points for {0}", name);
foreach (var turns in turn_list)
{
Console.WriteLine(turns);
}
Console.WriteLine("\n Total points: {0}", calculate_points());
}
public override string ToString()
{
return string.Format(" {0} ", name);
}
}
class Turns
{
private int turn1;
private int turn2;
private int turn3;
public Turns(int _turn1, int _turn2, int _turn3)
{
turn1 = _turn1;
turn2 = _turn2;
turn3 = _turn3;
}
public int Get_Score()
{
int totalt;
totalt = turn1 + turn2 + turn3;
return totalt;
}
public override string ToString()
{
return string.Format("\n throw 1: {0} \n throw 2: {1} \n throw 3: {2}", turn1, turn2, turn3);
}
}
Well, this is how you already check if any players match the criteria:
player_list.Any(x => x.calculate_points() >= 301)
So as soon as one does match the criteria, you can get that single player:
player_list.Single(x => x.calculate_points() >= 301)
Or all matching players, if there's more than one:
player_list.Where(x => x.calculate_points() >= 301)
Or just the first matching player, if there's more than one but you only want one:
player_list.First(x => x.calculate_points() >= 301)
Or perhaps the player with the highest score (not accounting for a tie score):
player_list.OrderByDescending(x => x.calculate_points()).First();
playerDice = new Dice();
int playerDiceNo = playerDice.getfaceofDie();
MessageBox.Show("Your roll" + playerDiceNo);
compDice = new Dice();
int compDiceNo = compDice.getfaceofDie();
MessageBox.Show("Computers roll:" + compDiceNo);
above is my method for when the roll button is clicked.
Below is my dice class:
class Dice
{
private int faceofDie;
public void rollDice()
{
Random rollDice = new Random();
faceofDie = rollDice.Next(1, 7);
}
public int getfaceofDie()
{
return faceofDie;
}
}
I have stated my variables for compDice and playerDice as :
Dice compDice;
Dice playerDice;
I can't seem to figure out why it's returning 0 for both rolls over & over. can anyone help?
I can't seem to figure out why it's returning 0 for both rolls over & over. can anyone help?
You never call rollDice(), so the faceofDie variable is never set, and has it's default value of 0.
playerDice = new Dice();
playerDice.rollDice(); // Add this
int playerDiceNo = playerDice.getfaceofDie();
MessageBox.Show("Your roll" + playerDiceNo);
A better approach would be to roll the dice the first time in the constructor, and to not keep creating new Random instances:
class Dice
{
private static Random diceRoller = new Random();
private int faceofDie;
public Dice()
{
this.RollDice(); // Roll once on construction
}
public void RollDice()
{
lock(diceRoller)
faceofDie = diceRoller.Next(1, 7);
}
public int FaceOfDie
{
get { return faceofDie; }
}
}
The static Random instance will prevent multiple dice implemented at the same time from getting the same seed (as they'll all share a single random), which will help keep your results more consistent. This also moves to standard C# conventions, and would be used like:
playerDice = new Dice();
int playerDiceNo = playerDice.FaceOfDie;
MessageBox.Show("Your roll" + playerDiceNo);
compDice = new Dice();
int compDiceNo = compDice.FaceOfDie;
MessageBox.Show("Computers roll:" + compDiceNo);
I have the below code and my issue is that when I reference the variables it gives me the error: an object reference is required for the non-static field, method, or property for each variable. I know it is something to do with my public int and public double being non static but I am not sure how to fix it. Could someone show me possibly?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace homework3
{
public class Program
{
public int number_of_scores;
public double score, total_score = 0, high_score, low_score, average;
}
class Class1{
static void Main(string[] args){
Console.Write("please enter the number of scores that you
wish to process? ");
Program.number_of_scores = int.Parse(Console.ReadLine());
Console.Write("Please enter score " + 1 + " ");
Program.score = double.Parse(Console.ReadLine());
Program.high_score = Program.score;
Program.low_score = Program.score;
Program.total_score = Program.total_score = Program.score;
for (int i = 2; i <= number_of_scores; i++);
}
}
class class2
{
static void Main(string[] args){
Console.Write("Please enter score " + i + " ");
Program.score = double.Parse(Console.ReadLine());
Program.total_score = Program.total_score + Program.score;
if(Program.score > Program.high_score)
Program.high_score = Program.score;
if(Program.score < Program.low_score)
Program.low_score = Program.score;
}
}
class Class3
{
static void Main(string[] args){
Program.average = Program.total_score / Program.number_of_scores;
Console.WriteLine("you entered " + Program.number_of_scores +
" scores");
Console.WriteLine("The high score is " + Program.high_score);
Console.WriteLine("The low score is " + Program.low_score);
Console.WriteLine("the average score is " + Program.average);
}
}
In the line:
Program.number_of_scores = int.Parse(Console.ReadLine());
you try to reference the instance variable number_of_scores from a static method.
The most trivial way to get that to work is to declare number_of_scores as static:
static public int number_of_scores;
Some of your other fields have the same issue.
You need to declare number_of_scores and score (and the other variables) as static.
public static int number_of_scores;
public static double score, //etc