Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

How can I fix my code so it's not going directly to exception anymore? I have a dice game where you can pick the number

How can I fix my code so it's not going directly to exception anymore?

I have a dice game where you can pick the number of players and dices, and after you start the game, no more players can be added. But somehow, when I enter the number of players and dices it goes directly to the exception saying the game has already started. If I comment this exception, it goes directly to another one saying you need at least 2 players to start the game.

Only if I comment all exceptions it goes to the actual game, but it doesn't run...it gives me an argument out of range exception, but I can't see why it's giving this exception.

Here is the code I have:

using System; using System.Collections.Generic; using System.Text;

namespace DiceGame { public class Program { public static int numberOfDice { get; set; } public static int numberOfPlayers { get; set; }

static void Main(string[] args) { Console.WriteLine("Welcome to the Dice Game! "); Player player; DiceGame game;

do { Console.WriteLine(" How many dices do you want to play with?"); var input = Console.ReadLine(); numberOfDice = int.Parse(input); game = new DiceGame(Program.numberOfDice); } while (Program.numberOfDice < 2);

do { Console.WriteLine(" How many players will be playing?"); var input2 = Console.ReadLine(); numberOfPlayers = int.Parse(input2); for (int i = 0; i < Program.numberOfPlayers; i++) { player = new Player("Player#" + i); game.AddPlayer(player); } } while (Program.numberOfPlayers < 2);

game.Start(); game.SetNextPlayer();

for (int i = 0; i < 6; i++) { game.PlayTurn(); if (RollResult.Jackpot.Equals(2)) { game.IsGameOver = true; } else { Console.WriteLine($" {game.ActivePlayer.Name} "); game.PlayTurn(); Console.WriteLine("Your dice numbers are: "); for (int j = 0; j < Program.numberOfDice-1; j ++) { Console.WriteLine(game.dice[j]); } Console.WriteLine($"Your score is: {game.ActivePlayer.Score} "); Console.WriteLine(" Press any key to continue"); Console.ReadKey();

} } game.IsGameOver = true;

Console.WriteLine($" The game is over! The winner is {game.TheWinner()}! Winner History: {game.ActivePlayer.History}"); } } }

using System; using System.Collections.Generic; using System.Text;

namespace DiceGame { public class Player { public string Name { get; set; }

public int Score { get; set; } public List History { get; set; }

public Player(RollResult History) { this.History = new List(); }

public Player(string Name) { Name = this.Name; } } }

using System; using System.Collections.Generic; using System.Linq; using System.Text;

namespace DiceGame { public class DiceGame { public List dice { get; set; } public List Players { get; set; } public Player ActivePlayer { get; private set; }

private bool isGameOver;

public bool IsGameOver { get { return isGameOver; } set { isGameOver = true; } }

public DiceGame(int numberOfDice) { dice = new List(numberOfDice); }

public void AddPlayer(Player player) { // if (IsGameOver) { Players = new List(); Players.Add(player); // } else // { // throw new Exception(message: "Game has already started."); // }

}

public void Start() { // if (Players.Count > 1) // { IsGameOver = false; ActivePlayer = Players[0]; // } else // { // throw new Exception(message: "You need at least 2 players to start the game."); // } }

public void PlayTurn() { while (!IsGameOver) { RollDice(); UpdatePlayerStat(); ComputeTurnResult(); SetNextPlayer(); } }

public void RollDice() {

for (int i = 0; i < dice.Count; i++) dice[i].Roll(); }

public void ComputeTurnResult() { for (int i = 0; i < dice.Count; i++) { for (int j = 0; j < j+1; j++) { if (dice[i] == dice[i + 1] && dice[i].Equals(dice[i].Max)) { ActivePlayer.Score = dice.Count * dice[i].Max * 10; } else if (dice[i] == dice[i + 1] && !(dice[i].Equals(dice[i].Max))) { ActivePlayer.Score = dice.Count * dice[i].Face * 5; } else { ActivePlayer.Score = 0; } } } }

public void UpdatePlayerStat() { for (int i = 0; i < dice.Count; i++) { if (dice[i] == dice[i + 1] && dice[i].Equals(dice[i].Max)) { ActivePlayer.History.Add(RollResult.Jackpot); } else if (dice[i] == dice[i + 1] && !(dice[i].Equals(dice[i].Max))) { ActivePlayer.History.Add(RollResult.Win); } else { ActivePlayer.History.Add(RollResult.Lose); } } }

public void SetNextPlayer() { for (int i = 0; i < Players.Count; i++) { ActivePlayer = Players[i]; } }

public Player TheWinner() { for(int i =0; i < Players.Count; i++) { if (Players[i].Score > Players[i + 1].Score) { return Players[i]; } else { return null; } } return null; }

} }

using System; using System.Collections.Generic; using System.Text;

namespace DiceGame { public class Dice {

public int Max { get; set; } public int Face { get; private set; }

public Dice(int max) { this.Max = 6; } public void Roll() { Random diceNumberGenerator = new Random(); Face = diceNumberGenerator.Next(1, Max+1); } } }

using System; using System.Collections.Generic; using System.Text;

namespace DiceGame { public enum RollResult { Jackpot, Win, Lose } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2014 Nancy France September 15 19 2014 Proceedings Part 2 Lnai 8725

Authors: Toon Calders ,Floriana Esposito ,Eyke Hullermeier ,Rosa Meo

2014th Edition

3662448505, 978-3662448502

More Books

Students also viewed these Databases questions

Question

1. What are your creative strengths?

Answered: 1 week ago

Question

What metaphors might describe how we work together?

Answered: 1 week ago