Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
46 changes: 46 additions & 0 deletions exercise.main/CardHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace exercise.main
{
public class CardHandler
{
public Dictionary<string, int> CardValues { get; set; } = new Dictionary<string, int>
{
{"1",1 },
{"2",2},
{"3",3},
{"4",4},
{"5",5},
{"6",6},
{"7",7 },
{"8",8},
{"9",9},
{"10",10},
{"J", 11},
{"Q", 12},
{"K", 13 },
{"A",14},
};
public CardHandler() { }

public int GetValueOfCard(string card)
{
return CardValues[card];
}

public bool isPair(Tuple<string, string> pair)
{
return pair.Item1 == pair.Item2;
}

public bool isTriplet(Tuple<string, string, string> triplet) {
return triplet.Item1 == triplet.Item2 && triplet.Item2 == triplet.Item3;
}


}
}
26 changes: 22 additions & 4 deletions exercise.main/Core.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,41 @@ namespace exercise.main
{
public class Core
{


public Core() { }
// Note:
// IEnumerable is a class that the List inherits (ie List has all the functionality of IEnumerable)
// IEnumerable allows you to use a foreach loop on the variable hands (which is a List of tuples)

//TODO: complete the winningPair method, without chaning the method signature
CardHandler handler = new CardHandler();

public bool winningPair(IEnumerable<Tuple<string, string>> hands, out Tuple<string, string> result)
{
result = new Tuple<string,string>(string.Empty, string.Empty);

// your code here...
// your code here...

var winningValue = 0;
var currentIndex = 0;
foreach (var item in hands)
{
if(handler.isPair(item) && handler.GetValueOfCard(item.Item1) > winningValue)
{
result = item;
winningValue = handler.GetValueOfCard(item.Item2);
}
currentIndex++;
}



return result.Item1!=string.Empty ? true : false;
}

public int GetValueOfCard(string card)
{
return 0;
return handler.GetValueOfCard(card);
}
}
}
20 changes: 18 additions & 2 deletions exercise.main/Extension.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Metadata;
using System.Text;
using System.Threading.Tasks;

Expand All @@ -9,12 +10,27 @@ namespace exercise.main
public class Extension
{
//TODO: complete the following method, keeping the signature the same
public bool winningThree(IEnumerable<Tuple<string, string, string>> hand, out Tuple<string, string, string> result)
public bool winningThree(IEnumerable<Tuple<string, string, string>> hands, out Tuple<string, string, string> result)
{
result = new Tuple<string, string, string>(string.Empty, string.Empty, string.Empty);

return false;

CardHandler handler = new CardHandler();
var winningValue = 0;
var currentIndex = 0;
foreach (var item in hands)
{
if (handler.isTriplet(item) && handler.GetValueOfCard(item.Item1) > winningValue)
{
result = item;
winningValue = handler.GetValueOfCard(item.Item2);
}
currentIndex++;
}
return result.Item1 != string.Empty ? true : false;
}


}

}
17 changes: 17 additions & 0 deletions exercise.main/Program.cs
Original file line number Diff line number Diff line change
@@ -1,2 +1,19 @@
// See https://aka.ms/new-console-template for more information
using exercise.main.UngradedExtension;

Console.WriteLine("Hello, World!");


PokerGame game = new PokerGame("Nigel", "AJ");

game.Start();
game.PrintGameStatus();
game.playTurn();

game.playTurn();

Console.WriteLine("Game over");

game.PrintGameStatus();


30 changes: 30 additions & 0 deletions exercise.main/UngradedExtension/Card.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace exercise.main.UngradedExtension
{
public class Card

{
public Suit Suit { get; set; }
public string Value { get; set; }

public Card(string value, Suit suit) {
Suit = suit;
Value = value;
}

public void PrintCard()
{
Console.WriteLine(this.ToString());
}
public override string ToString()
{
return this.Value + " of " + this.Suit;
}
}

}
51 changes: 51 additions & 0 deletions exercise.main/UngradedExtension/Deck.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace exercise.main.UngradedExtension
{
public class Deck
{
List<Card> cards;

public Deck() {
InitializeDeck();
}

public void InitializeDeck()
{
CardHandler cardHandler = new CardHandler();
cards = new List<Card>();
foreach (var suit in Enum.GetValues<Suit>()) {
foreach (var value in cardHandler.CardValues)
{
Card newCard = new Card(value.Key, suit);
cards.Add(newCard);
}
}
Shuffle();
}
public void Shuffle()
{
Random rng = new Random();
int n = cards.Count;
while (n > 1)
{
n--;
int k = rng.Next(n + 1);
Card card = cards[k];
cards[k] = cards[n];
cards[n] = card;
}
}

public Card Deal()
{
Card dealtCard = cards.Last();
cards.Remove(dealtCard);
return dealtCard;
}
}
}
41 changes: 41 additions & 0 deletions exercise.main/UngradedExtension/Player.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace exercise.main.UngradedExtension
{
public class Player
{

public string Name { get; set; }
public List<Card> hand { get; set; }

public Player(string name)
{
Name = name;
}

public void ClearHand()
{
hand = new List<Card>();
}

public void AddCardToHand(Card card)
{
hand.Add(card);
}

public List<Card> GetHand()
{
return hand;
}

public void PrintHand()
{
Console.WriteLine(this.Name + " has:");
hand.ForEach(x => x.PrintCard());
}
}
}
67 changes: 67 additions & 0 deletions exercise.main/UngradedExtension/PokerGame.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace exercise.main.UngradedExtension
{
public class PokerGame
{
Player player1;
Player player2;
Table table;
Deck deck;
public PokerGame(string p1, string p2) {
player1 = new Player(p1);
player2 = new Player(p2);
table = new Table();
deck = new Deck();
}


public void Start()
{
deck.InitializeDeck();
player1.ClearHand();
player2.ClearHand();
table.clearTable();

player1.AddCardToHand(deck.Deal());
player2.AddCardToHand(deck.Deal());
player1.AddCardToHand(deck.Deal());
player2.AddCardToHand(deck.Deal());

table.AddCard(deck.Deal());
table.AddCard(deck.Deal());
table.AddCard(deck.Deal());

}

public void playTurn()
{
table.AddCard(deck.Deal());
}

public void simulateGame()
{
Start();
PrintGameStatus();
playTurn();

playTurn();

Console.WriteLine("Game over");

PrintGameStatus();
}

public void PrintGameStatus()
{
Console.WriteLine("The game now looks like: ");
this.table.PrintTable();
this.player1.PrintHand();
this.player2.PrintHand();
}
}
}
16 changes: 16 additions & 0 deletions exercise.main/UngradedExtension/Suit.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace exercise.main.UngradedExtension
{
public enum Suit
{
Hearts,
Spades,
Diamonds,
Clubs
}
}
34 changes: 34 additions & 0 deletions exercise.main/UngradedExtension/Table.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace exercise.main.UngradedExtension
{
public class Table
{
List<Card> cards;
public Table()
{
cards = new List<Card>();
}

public void AddCard(Card card)
{
cards.Add(card);
Console.WriteLine("Added " + card.ToString() + " to the table");
}

public void clearTable()
{
cards.Clear();
}

public void PrintTable()
{
Console.WriteLine("Table cards:");
cards.ForEach(x => x.PrintCard());
}
}
}
Loading