-
Notifications
You must be signed in to change notification settings - Fork 1
/
Game.cs
139 lines (117 loc) · 3.88 KB
/
Game.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
using System;
using System.Collections.Generic;
using System.Linq;
using Fluxx.Loaders;
using Fluxx.Models.Cards;
namespace Fluxx
{
public class Game
{
public Game()
{
InitializeBoard();
Players = new List<Player>();
}
public string Id { get; set; }
public bool InProgress { get; set; }
public Player CurrentPlayer { get; set; }
public List<Player> Players { get; private set; }
private int MaxPlayers { get; } = 5;
public List<Card> DrawPile { get; set; }
public List<Card> DiscardPile { get; set; }
public List<RuleCard> ActiveRules { get; set; }
public List<GoalCard> ActiveGoals { get; set; }
public void AddPlayer(string connectionId, string username)
{
if (Players.Count < MaxPlayers)
{
Player newPlayer = new Player()
{
ConnectionId = connectionId,
Name = username,
Color = GeneratePlayerColor(),
Hand = new List<Card>(),
PlayedKeepers = new List<Card>()
};
Player prevPlayer = Players.LastOrDefault();
if(prevPlayer != null) {
prevPlayer._nextPlayer = newPlayer;
}
newPlayer._previousPlayer = prevPlayer ?? null;
Players.Add(newPlayer);
}
else
{
throw new Exception("Game has reached max group size");
}
}
public Player GetPlayer(string connectionId)
{
var player = Players.FirstOrDefault(x => x.ConnectionId == connectionId);
if(player != null)
{
return player;
}
return null;
}
public void NextPlayer()
{
CurrentPlayer = CurrentPlayer._nextPlayer;
}
public void DealCards()
{
foreach(Player player in Players)
{
IEnumerable<Card> playerHand = DrawPile.Take(3);
DrawPile.RemoveRange(0, 3);
player.Hand = playerHand;
}
}
private void InitializeBoard()
{
CardLoader cardLoader = new CardLoader();
var _cardData = cardLoader.LoadCards();
var shuffledCards = new List<Card>();
shuffledCards.AddRange(_cardData.GoalCards);
shuffledCards.AddRange(_cardData.KeeperCards);
shuffledCards.AddRange(_cardData.RuleCards);
DrawPile = shuffledCards;
DiscardPile = new List<Card>();
ActiveRules = new List<RuleCard>();
ActiveGoals = new List<GoalCard>();
}
public bool CheckVictory(int row, int column)
{
//var playerColor = Board[row][column];
//if (CheckHorizontally(row, column, playerColor))
//{
// return true;
//}
//if (CheckVertically(row, column, playerColor))
//{
// return true;
//}
//if (CheckDiagonally(row, column, playerColor))
//{
// return true;
//}
return false;
}
private string GeneratePlayerColor()
{
var random = new Random();
var color = string.Format("#{0:X6}", random.Next(0x1000000));
return color;
}
}
public class Player
{
public string Name { get; set; }
public string ConnectionId { get; set; }
public string Color { get; set; }
public IEnumerable<Card> Hand { get; set; }
public IEnumerable<Card> PlayedKeepers { get; set; }
public Player _nextPlayer { get; set; }
public Player _previousPlayer { get; set; }
}
}