Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

User/nareshh74/refactor 1 #49

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ CSharp/basic/Trivia/bin/
CSharp/basic/Trivia/obj/
/CSharp/netcore2/.vs
/CSharp/basic/.vs
/.vs
214 changes: 65 additions & 149 deletions CSharp/netcore2/Game.cs
Original file line number Diff line number Diff line change
@@ -1,213 +1,129 @@
using System;
using System.Collections.Generic;
using System.Linq;
using trivia;

namespace Trivia
{
public class Game
{
private readonly List<Player> _players;
private readonly QuestionsDeck _questionsDeck;

private int _currentPlayerIndex = 0;

List<string> players = new List<string>();

int[] places = new int[6];
int[] purses = new int[6];

bool[] inPenaltyBox = new bool[6];

LinkedList<string> popQuestions = new LinkedList<string>();
LinkedList<string> scienceQuestions = new LinkedList<string>();
LinkedList<string> sportsQuestions = new LinkedList<string>();
LinkedList<string> rockQuestions = new LinkedList<string>();
public Game(List<string> players)
{
if (players.Count < 2)
{
throw new ArgumentException("You need at least 2 players to play the game");
}
if (players.Count > 6)
{
throw new ArgumentException("You need at most 6 players to play the game");
}

int currentPlayer = 0;
bool isGettingOutOfPenaltyBox;
this._players = new List<Player>();

public Game()
{
for (int i = 0; i < 50; i++)
foreach (var player in players)
{
popQuestions.AddLast("Pop Question " + i);
scienceQuestions.AddLast(("Science Question " + i));
sportsQuestions.AddLast(("Sports Question " + i));
rockQuestions.AddLast(CreateRockQuestion(i));
this.Add(player);
}
}

public String CreateRockQuestion(int index)
{
return "Rock Question " + index;
this._questionsDeck = QuestionsDeck.GetQuestionsDeck();
}

public bool IsPlayable()
{
return (HowManyPlayers() >= 2);
return (this._players.Count >= 2);
}

public bool Add(String playerName)
private bool Add(String playerName)
{


players.Add(playerName);
places[HowManyPlayers()] = 0;
purses[HowManyPlayers()] = 0;
inPenaltyBox[HowManyPlayers()] = false;
this._players.Add(new Player(playerName));

Console.WriteLine(playerName + " was Added");
Console.WriteLine("They are player number " + players.Count);
Console.WriteLine("They are player number " + this._players.Count);
return true;
}

public int HowManyPlayers()
{
return players.Count;
}

public void Roll(int roll)
{
Console.WriteLine(players[currentPlayer] + " is the current player");
var currentPlayer = this._players[_currentPlayerIndex];
Console.WriteLine(currentPlayer + " is the current player");
Console.WriteLine("They have rolled a " + roll);

if (inPenaltyBox[currentPlayer])
if (currentPlayer.IsInPenaltyBox() && roll % 2 == 0)
{
if (roll % 2 != 0)
{
isGettingOutOfPenaltyBox = true;

Console.WriteLine(players[currentPlayer] + " is getting out of the penalty box");
places[currentPlayer] = places[currentPlayer] + roll;
if (places[currentPlayer] > 11) places[currentPlayer] = places[currentPlayer] - 12;

Console.WriteLine(players[currentPlayer]
+ "'s new location is "
+ places[currentPlayer]);
Console.WriteLine("The category is " + CurrentCategory());
AskQuestion();
}
else
{
Console.WriteLine(players[currentPlayer] + " is not getting out of the penalty box");
isGettingOutOfPenaltyBox = false;
}

Console.WriteLine(currentPlayer + " is not getting out of the penalty box");
currentPlayer.CantGetOutOfPenaltyBox();
return;
}
else
{

places[currentPlayer] = places[currentPlayer] + roll;
if (places[currentPlayer] > 11) places[currentPlayer] = places[currentPlayer] - 12;

Console.WriteLine(players[currentPlayer]
+ "'s new location is "
+ places[currentPlayer]);
Console.WriteLine("The category is " + CurrentCategory());
AskQuestion();
if (currentPlayer.IsInPenaltyBox())
{
currentPlayer.MightGetOutOfPenaltyBox();
Console.WriteLine(currentPlayer + " is getting out of the penalty box");
}

}
currentPlayer.Move(roll);

Console.WriteLine("The category is " + this.CurrentCategory());
this._questionsDeck.AskQuestionForPlayer(currentPlayer);

private void AskQuestion()
{
if (CurrentCategory() == "Pop")
{
Console.WriteLine(popQuestions.First());
popQuestions.RemoveFirst();
}
if (CurrentCategory() == "Science")
{
Console.WriteLine(scienceQuestions.First());
scienceQuestions.RemoveFirst();
}
if (CurrentCategory() == "Sports")
{
Console.WriteLine(sportsQuestions.First());
sportsQuestions.RemoveFirst();
}
if (CurrentCategory() == "Rock")
{
Console.WriteLine(rockQuestions.First());
rockQuestions.RemoveFirst();
}
}


private String CurrentCategory()
{
if (places[currentPlayer] == 0) return "Pop";
if (places[currentPlayer] == 4) return "Pop";
if (places[currentPlayer] == 8) return "Pop";
if (places[currentPlayer] == 1) return "Science";
if (places[currentPlayer] == 5) return "Science";
if (places[currentPlayer] == 9) return "Science";
if (places[currentPlayer] == 2) return "Sports";
if (places[currentPlayer] == 6) return "Sports";
if (places[currentPlayer] == 10) return "Sports";
int currentPlayerPlace = this._players[_currentPlayerIndex].GetPlace();
if (currentPlayerPlace == 0) return "Pop";
if (currentPlayerPlace == 4) return "Pop";
if (currentPlayerPlace == 8) return "Pop";
if (currentPlayerPlace == 1) return "Science";
if (currentPlayerPlace == 5) return "Science";
if (currentPlayerPlace == 9) return "Science";
if (currentPlayerPlace == 2) return "Sports";
if (currentPlayerPlace == 6) return "Sports";
if (currentPlayerPlace == 10) return "Sports";
return "Rock";
}

public bool WasCorrectlyAnswered()
{
if (inPenaltyBox[currentPlayer])
var currentPlayer = this._players[_currentPlayerIndex];
if (currentPlayer.IsInPenaltyBox() && !currentPlayer.CanGetOutOfPenaltyBox())
{
if (isGettingOutOfPenaltyBox)
{
Console.WriteLine("Answer was correct!!!!");
currentPlayer++;
if (currentPlayer == players.Count) currentPlayer = 0;
purses[currentPlayer]++;
Console.WriteLine(players[currentPlayer]
+ " now has "
+ purses[currentPlayer]
+ " Gold Coins.");

bool winner = DidPlayerWin();

return winner;
}
else
{
currentPlayer++;
if (currentPlayer == players.Count) currentPlayer = 0;
return true;
}

this.GiveTurnToNextPlayer();
return true;
}

currentPlayer.TakeOutOfPenaltyBox();

}
else
{
Console.WriteLine("Answer was correct!!!!");
currentPlayer.AddPurse();

Console.WriteLine("Answer was corrent!!!!");
purses[currentPlayer]++;
Console.WriteLine(players[currentPlayer]
+ " now has "
+ purses[currentPlayer]
+ " Gold Coins.");
this.GiveTurnToNextPlayer();

bool winner = DidPlayerWin();
currentPlayer++;
if (currentPlayer == players.Count) currentPlayer = 0;
return currentPlayer.YetToWin();
}

return winner;
}
private void GiveTurnToNextPlayer()
{
_currentPlayerIndex++;
if (_currentPlayerIndex == this._players.Count) _currentPlayerIndex = 0;
}

public bool WrongAnswer()
{
var currentPlayer = this._players[_currentPlayerIndex];
Console.WriteLine("Question was incorrectly answered");
Console.WriteLine(players[currentPlayer] + " was sent to the penalty box");
inPenaltyBox[currentPlayer] = true;
Console.WriteLine(currentPlayer + " was sent to the penalty box");
currentPlayer.PutInPenaltyBox();

currentPlayer++;
if (currentPlayer == players.Count) currentPlayer = 0;
this.GiveTurnToNextPlayer();
return true;
}


private bool DidPlayerWin()
{
return !(purses[currentPlayer] == 6);
}
}

}
8 changes: 3 additions & 5 deletions CSharp/netcore2/GameRunner.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;

namespace Trivia
{
Expand All @@ -9,11 +10,8 @@ public class GameRunner

public static void Main(String[] args)
{
Game aGame = new Game();

aGame.Add("Chet");
aGame.Add("Pat");
aGame.Add("Sue");
var players = new List<string> { "Chet", "Pat", "Sue" };
Game aGame = new Game(players);

Random rand = new Random();

Expand Down
82 changes: 82 additions & 0 deletions CSharp/netcore2/Player.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
using System;

namespace trivia
{
internal class Player
{
private readonly string _name;
private int _place;
private int _purse;
private bool _inPenaltyBox;
private bool _isGettingOutOfPenaltyBox;

public Player(string name)
{
this._name = name;
this._place = 0;
this._purse = 0;
this._inPenaltyBox = false;
}

public override string ToString() => this._name;

public bool IsInPenaltyBox()
{
return this._inPenaltyBox;
}

public void PutInPenaltyBox()
{
this._inPenaltyBox = true;
}

public void TakeOutOfPenaltyBox()
{
this._inPenaltyBox = false;
}

public void MightGetOutOfPenaltyBox()
{
this._isGettingOutOfPenaltyBox = true;
}

public void CantGetOutOfPenaltyBox()
{
this._isGettingOutOfPenaltyBox = false;
}

public bool CanGetOutOfPenaltyBox() => this._isGettingOutOfPenaltyBox;

public void Move(int roll)
{
this._place += roll;
if (this._place > 11)
{
this._place -= 12;
}

Console.WriteLine(this._name
+ "'s new location is "
+ this._place);
}

public int GetPlace()
{
return this._place;
}

public void AddPurse()
{
this._purse++;
Console.WriteLine(this._name
+ " now has "
+ this._purse
+ " Gold Coins.");
}

public bool YetToWin()
{
return this._purse != 6;
}
}
}
Loading