1. using System.Threading;
using System;
using System.Collections.Generic;
using System.Linq;
namespace Bingo
{
public delegate void DrawNumberEventHandler(object sender, NumberDrawnEventArgs args);
public static class Utils
{
public static Stack<int> GetRandomList(int listcount, int min, int max)
{
Stack<int> list = new Stack<int>();
List<int> ints = new List<int>();
Thread.Sleep(1);
Random rd = new Random(DateTime.Now.Millisecond);
for (int i = min; i < max + 1; i++)
{
ints.Add(i);
}
for (int i = 1; i <= listcount; i++)
{
int pick = rd.Next(1, max - min + 3 - i);
list.Push(ints.ElementAt(pick - 1));
ints.RemoveAt(pick - 1);
}
list.Reverse();
return list;
}
}
public class BingoCell
{
private int row;
private int column;
private int value;
private bool ismarked;
public BingoCell(int row, int column, int value)
{
this.row = row;
this.column = column;
this.value = value;
this.ismarked = false;
}
/*public int this[int row, int col]
{
}*/
public void Mark()
2. {
ismarked = true;
}
public bool IsMarked
{
get { return ismarked; }
}
public int Row
{
get { return row; }
}
public int Column
{
get { return column; }
}
public int Value
{
get { return value; }
}
}
public class Card
{
private BingoCell[,] cells = new BingoCell[5, 5];
private int[] rowcount = new int[5];
private int[] colcount = new int[5];
private Game game;
public Card(Game game)
{
Initialize(game);
}
public void ShowCard()
{
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
if (j == 0) Console.WriteLine();
Console.Write(cells[j, i].Value.ToString() + 't');
}
}
Console.WriteLine();
}
public void Initialize(Game _game)
{
this.game = _game;
game.NumberDrawnEvent += NumberDrawn;
3. Stack<int>[] stack = new Stack<int>[5];
for (int i = 1; i <= 5; i++)
{
stack[i - 1] = Utils.GetRandomList(5, i * 15 - 14, i * 15);
}
for (int col = 1; col <= 5; col++)
{
for (int row = 1; row <= 5; row++)
{
if (row == 3 && col == 3)
{
stack[row - 1].Pop();
cells[row - 1, col - 1] = new BingoCell(row, col, 0);
}
else
{
cells[row - 1, col - 1] = new BingoCell(row, col, stack[row - 1].Pop());
}
}
}
}
public byte MarkedCount
{
private set;
get;
}
public event EventHandler BingoComplete;
private void NumberDrawn(object sender, NumberDrawnEventArgs args)
{
int col = ((args.NumberDrawn - 1) / 15);
for (int row = 0; row < 5; row++)
{
if (cells[col, row].Value == args.NumberDrawn)
{
BingoCell square = cells[col, row];
square.Mark();
MarkedCount++;
if (MarkedCount == 24 && BingoComplete != null)
BingoComplete(this, new EventArgs());
rowcount[row]++;
colcount[col]++;
break;
}
}
}
}
public class Player
4. {
private List<Card> cards;
private Game game;
public Player(Game game)
{
this.game = game;
}
public string Name { get; set; }
public void BuyCards(int numberofcards)
{
List<Card> cards = new List<Card>();
Card card;
for (int i = 1; i <= numberofcards; i++)
{
card = new Card(game);
card.BingoComplete += Bingo;
cards.Add(card);
}
this.cards = cards;
}
public event EventHandler BingoComplete;
private void Bingo(object sender, EventArgs e)
{
if (BingoComplete != null)
BingoComplete(this, e);
}
public void ShowCards()
{
foreach (Card card in Cards)
{
card.ShowCard();
}
Console.Read();
}
public List<Card> Cards
{
get { return cards; }
}
}
public class NumberDrawnEventArgs : EventArgs
{
public int NumberDrawn { get; private set; }
public NumberDrawnEventArgs(int numberdrawn)
{
this.NumberDrawn = numberdrawn;
5. }
}
public class Game
{
private Stack<int> numbers;
Stack<int> originalnumbers;
private List<Player> players = new List<Player>();
public event DrawNumberEventHandler NumberDrawnEvent;
public Game()
{
numbers = Utils.GetRandomList(75, 1, 75);
originalnumbers = new Stack<int>(numbers);
}
public Stack<int> Numbers
{
get { return numbers; }
}
public Player AddPlayer(string name, int numberofcards)
{
Player player = new Player(this);
player.Name = name;
player.BuyCards(numberofcards);
players.Add(player);
return player;
}
public int Draw()
{
int numberdrawn = numbers.Pop();
if (NumberDrawnEvent != null)
NumberDrawnEvent(this, new NumberDrawnEventArgs(numberdrawn));
return numberdrawn;
}
}
class Program
{
static bool BingoCalled = false;
static void Main(string[] args)
{
Game gm = new Game();
Player player = gm.AddPlayer("Henry", 100);
player.BingoComplete += BingoComplete;
while (!BingoCalled)
{
Console.Write(gm.Draw().ToString("D" + 2) + ",");