Question
Hello. What is happening in this part of the code? :) ----------------------------------------------------------------------------------------------------------- public void AnnounceWinner() { if (!gameIsStarted) { System.Console.WriteLine(The game is not started yet.);
Hello. What is happening in this part of the code? :)
-----------------------------------------------------------------------------------------------------------
public void AnnounceWinner() { if (!gameIsStarted) { System.Console.WriteLine("The game is not started yet."); } else { Player winner = players[0]; foreach (var player in players) { if (player.TotalHandValue() > winner.TotalHandValue()) { winner = player; } } System.Console.WriteLine("Winner is: " + winner.Name); } }
------------------------------------------------------------------------------------------------------------------------
Also I was wondering why our teacher made a list for Player. If it is because it makes everything easier?
------------------------------------------------------------------------------------------------------------------------
CONTEXT:
using System.Collections.Generic; namespace CardGame { public class Game { private List players = new List(); private bool gameIsStarted = false; private Deck deck = null; private int NUMBER_OF_CARDS_TO_DEAL = 5; public Game(Deck deck) { this.deck = deck; //Shuffle og tager deck } public void AcceptPlayer(Player player) { if (!gameIsStarted) { players.Add(player); } else { // Maybe report on the console that the game has started? // Or throw an exception? // The requirements does not say anything about this scenario // so maybe we should go back to the product owner and ask. } } public void Start() { gameIsStarted = true; DealCards(); } private void DealCards() { foreach (var player in players) //For hver spiller giver den 5 kort { deck.DealTo(player, NUMBER_OF_CARDS_TO_DEAL); } } public void AnnounceWinner() { if (!gameIsStarted) { System.Console.WriteLine("The game is not started yet."); } else { Player winner = players[0]; //Startvrdi - Tnk Lasses rundkreds foreach (var player in players) //Kigger alle spillere igennem { if (player.TotalHandValue() > winner.TotalHandValue()) //Sammenligning { winner = player; } } System.Console.WriteLine("Winner is: " + winner.Name); } } } }
--------------------------------------------------------------
Thank you!
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started