using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
namespace The_Set_Game
{
public partial class SetGame : Form
{
private List<Card> dealtCards; //The list containing the cards currently being displayed on the screen.
private Deck myDeck; //The deck containing all of the cards - discards and dealt cards.
private const int MAXNUMCARDS = 12; //We only ever want 12 cards.
private List<Button> btnList; //List of the 12 buttons containing the 12 cards.
private List<int> selectedIndexes; //List of ints representing the index each selected card in dealtCards.
private int score = 0; //integer to keep track of the score.
private int time = 0; //Integer to keep track of the time.
private string strReason; //This string contains all the reasons why the users selection was not a set.
public SetGame()
{
InitializeComponent();
myDeck = new Deck();
myDeck.Shuffle(); //Sfuffles Deck
dealtCards = new List<Card>();
myDeck.Deal(true,dealtCards,selectedIndexes); //Deals 12 Cards. True argument indicates we are starting a new game.
btnList = new List<Button>();
selectedIndexes = new List<int>();
AddButtonsToList(); //Adds the 12 buttons to the Array.
Display(); //Update the button images.
myTimer.Start(); //Start timer.
}
//This method simply adds every button to the btnList.
private void AddButtonsToList()
{
btnList.Add(btnCard1); btnList.Add(btnCard2);
btnList.Add(btnCard3); btnList.Add(btnCard4);
btnList.Add(btnCard5); btnList.Add(btnCard6);
btnList.Add(btnCard7); btnList.Add(btnCard8);
btnList.Add(btnCard9); btnList.Add(btnCard10);
btnList.Add(btnCard11); btnList.Add(btnCard12);
}
//This method takes an integer representing a characteristic of the card.
//If the characteristic is the same on each card or different on each card, it returns true.
//If not then it returns false.
private bool AllSameOrDifferent(int characteristic)
{
switch (characteristic) //We want to take different actions, depending on the characteristic to check.
{
case 0:
//Fill enum
if (dealtCards[selectedIndexes[0]].Fill == dealtCards[selectedIndexes[1]].Fill
&& dealtCards[selectedIndexes[1]].Fill == dealtCards[selectedIndexes[2]].Fill)
//Characteristic same on all cards.
{
Debug.WriteLine("All have same fill");
return true;
}
else if (dealtCards[selectedIndexes[0]].Fill != dealtCards[selectedIndexes[1]].Fill
&& dealtCards[selectedIndexes[1]].Fill != dealtCards[selectedIndexes[2]].Fill
&& dealtCards[selectedIndexes[0]].Fill != dealtCards[selectedIndexes[2]].Fill)
//characteristic different on all cards.
{
Debug.WriteLine("All have different fill");
return true;
}
else
{
//characteristic is same on only 2 cards. (VERY BAD)
strReason = ", Two fills are the Same";
Debug.WriteLine("Two fills are the same");
return false;
}
case 1:
// Shape enum
if (dealtCards[selectedIndexes[0]].Shape == dealtCards[selectedIndexes[1]].Shape
&& dealtCards[selectedIndexes[1]].Shape == dealtCards[selectedIndexes[2]].Shape)
{
Debug.WriteLine("All have same Shape");
return true;
}
else if (dealtCards[selectedIndexes[0]].Shape != dealtCards[selectedIndexes[1]].Shape
&& dealtCards[selectedIndexes[1]].Shape != dealtCards[selectedIndexes[2]].Shape
&& dealtCards[selectedIndexes[0]].Shape != dealtCards[selectedIndexes[2]].Shape)
{
Debug.WriteLine("All have different Shape");
return true;
}
else
{
strReason += ", Two shapes are the Same";
Debug.WriteLine("Two Shapes are the same");
return false;
}
case 2:
// Color enum
if (dealtCards[selectedIndexes[0]].Color == dealtCards[selectedIndexes[1]].Color
&& dealtCards[selectedIndexes[1]].Color == dealtCards[selectedIndexes[2]].Color)
{
Debug.WriteLine("All have same Color");
return true;
}
else if (dealtCards[selectedIndexes[0]].Color != dealtCards[selectedIndexes[1]].Color
&& dealtCards[selectedIndexes[1]].Color != dealtCards[selectedIndexes[2]].Color
&& dealtCards[selectedIndexes[0]].Color != dealtCards[selectedIndexes[2]].Color)
{
Debug.WriteLine("All have different Color");
return true;
}
else
{
strReason += ", Two colors are the Same";
Debug.WriteLine("Two Colors are the same, ");
return false;
}
case 3:
// Number enum
if (dealtCards[selectedIndexes[0]].Number == dealtCards[selectedIndexes[1]].Number
&& dealtCards[selectedIndexes[1]].Number == dealtCards[selectedIndexes[2]].Number)
{
Debug.WriteLine("All have same number");
return true;
}
else if (dealtCards[selectedIndexes[0]].Number != dealtCards[selectedIndexes[1]].Number
&& dealtCards[selectedIndexes[1]].Number != dealtCards[selectedIndexes[2]].Number
&& dealtCards[selectedIndexes[0]].Number != dealtCards[selectedIndexes[2]].Number)
{
Debug.WriteLine("All have different number");
return true;
}
else
{
strReason += " ,Two numbers are the Same";
Debug.WriteLine("Two numbers are the same");
return false;
}
default:
return false;
}
}
//This method passes each card characteristic to the AllSameOrDifferent method
//and will count the number of times true is returned. After going through each
//characteristic we judge if its a set or not and take the appropriate action.
private void IsSet()
{
int counter = 0;
for (int i = 0; i <= 3; i++) //Each value of i will represent a card characteristic.
{
if (AllSameOrDifferent(i))
{
counter++;
}
}
if (counter == 4) //We have a set.
{
//MessageBox.Show("ZOMG WE HAVE A SET!");
MessageBox.Show("You found a set! :D Good Stuff Bro!","Good Stuff Bro!");
score++;
Debug.WriteLine("Score went up to " + score);
if (score == 30) //Score high enough to win the entire game!
{
WinGame();
}
}
else
{
//MessageBox pops up telling us all the reasons why its not a set.
MessageBox.Show("That is not a set! " + "\r\n" + "Reason : " + strReason.TrimStart(' ', ',') + ".","Bad Choice Mate!");
score--;
Debug.WriteLine("score went down to " + score);
}
lblScore.Text = "Score : " + score; //Updates Score label.
ReplaceCards();
}
//Actions to perform when our score reaches 30!
private void WinGame()
{
foreach (Button b in btnList) //goes through each button hiding and disabling it.
{
b.Visible = false;
b.Enabled = false;
}
Debug.WriteLine("ZOMG we won the whole game!!!!");
MessageBox.Show("YOU ARE THE SUPREME WINNER! How Did you develop so much patience?!");
btnLeave.Visible = true; //Button so the user can escape this game.
myTimer.Stop(); //Stops timer.
}
//After a successful/unsuccessful set, we would like to replace the 3 selected cards.
private void ReplaceCards()
{
myDeck.Deal(false, dealtCards, selectedIndexes); //Will replace the 3 cards at the appropriate selected indexes.
selectedIndexes = new List<int>();
Display(); //Updates buttons so we can see the new cards.
}
//Goes through each card updating the associated image and if its selected
//draws a red border around the card.
private void Display()
{
for (int i = 0; i < MAXNUMCARDS; i++)
{
btnList[i].Image = dealtCards[i].CardPic;
if (dealtCards[i].Selected == true)
{
btnList[i].FlatAppearance.BorderSize = 2;
}
else
{
btnList[i].FlatAppearance.BorderSize = 0;
}
}
}
//Every time we select a card this method is called.
//If the card is not already selected and added to the
//selectedIndexes list, it will be a selected
//and vice versa.
private void SelectCard(int cardIndex)
{
if (dealtCards[cardIndex].Selected == false) //Card not already selected.
{
if (selectedIndexes.Count <= 2)
{
dealtCards[cardIndex].Selected = true;
selectedIndexes.Add(cardIndex);
}
}
else
{
dealtCards[cardIndex].Selected = false;
selectedIndexes.Remove(cardIndex);
}
Display(); //Update buttons to show selected card with red border.
if (selectedIndexes.Count == 3) //If we have three selected cards.
{
IsSet(); //Check if selected cards form a set or not.
}
}
//These buttons each call SelectedIndex, passing the index of the card
//that should be updated with a red border.
private void btnCard1_Click(object sender, EventArgs e)
{
SelectCard(0);
}
private void btnCard2_Click(object sender, EventArgs e)
{
SelectCard(1);
}
private void btnCard3_Click(object sender, EventArgs e)
{
SelectCard(2);
}
private void btnCard4_Click(object sender, EventArgs e)
{
SelectCard(3);
}
private void btnCard5_Click(object sender, EventArgs e)
{
SelectCard(4);
}
private void btnCard6_Click(object sender, EventArgs e)
{
SelectCard(5);
}
private void btnCard7_Click(object sender, EventArgs e)
{
SelectCard(6);
}
private void btnCard8_Click(object sender, EventArgs e)
{
SelectCard(7);
}
private void btnCard9_Click(object sender, EventArgs e)
{
SelectCard(8);
}
private void btnCard10_Click(object sender, EventArgs e)
{
SelectCard(9);
}
private void btnCard11_Click(object sender, EventArgs e)
{
SelectCard(10);
}
private void btnCard12_Click(object sender, EventArgs e)
{
SelectCard(11);
}
private void mnuExit_Click(object sender, EventArgs e)
{
this.Close();
}
private void btnLeave_Click(object sender, EventArgs e)
{
MessageBox.Show("Good idea! This game gives me a headache!");
this.Close();
}
//Each time the timer ticks, we want to update the time integer and also display
//it in a label.
private void myTimer_Tick(object sender, EventArgs e)
{
time++;
lblTime.Text = "Time : " + time;
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
namespace The_Set_Game
{
//This class contains the recipe for creating a card used in the game of set.
//It has variables and properties for each chracteristic of the card (Color, shape, fill, num).
//It also has contains a Bitmap which is usd when displaying the card.
class Card
{
//The objects on each card have three possible Colors. (Red, Green or Purple)
public enum CardColor
{
Red,
Green,
Purple
};
//The objects on each card have three possible Shapes. (Round, Diamond or Squiggly)
public enum CardShape
{
Round,
Diamond,
Squiggly
};
//The objects on each card have three possible fills. (Filled, Striped or Nofill)
public enum CardFill
{
Filled,
Striped,
NoFill
};
//There could be 1,2 or 3 objects on a card.
public enum CardNum
{
One,
Two,
Three
};
private Bitmap cardPic; //variable that contains the .gif that contains the card picture.
private CardColor color;
private CardShape shape;
private CardNum number;
private CardFill fill;
private bool selected; //variable that tells us whether this card is selected or not.
public Card()
{
selected = false;
}
public Boolean Selected
{
get
{
return selected;
}
set
{
selected = value;
}
}
public CardColor Color
{
get
{
return color;
}
set
{
color = value;
}
}
public CardShape Shape
{
get
{
return shape;
}
set
{
shape = value;
}
}
public CardNum Number
{
get
{
return number;
}
set
{
number = value;
}
}
public CardFill Fill
{
get
{
return fill;
}
set
{
fill = value;
}
}
public Bitmap CardPic
{
get
{
return cardPic;
}
set
{
cardPic = value;
}
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Diagnostics;
namespace The_Set_Game
{
//This is the Deck class which basically controls the functions a typical deck can make, shuffle, deal etc..
class Deck
{
private Card myCard; //Card we will use to help in the creation of the entire deck.
private List<Card> deck; //list containing all the cards - the discards and dealt.
private List<Card> discards; //list containing all the discards after successful/unsuccessful set attempts.
private const int MAXNUMCARDS = 12; //We only ever want to display 12 cards at once.
public Deck()
{
deck = new List<Card>();
discards = new List<Card>();
CreateDeck(); //Creates a deck of 81 cards.
}
//This function will make different actions depending on whether we are starting a new game or not.
//If we are starting a new game we want to deal 12 cards, otherwise just three.
public void Deal(bool newGame, List<Card> dealtCards, List<int> indexesToRemove)
{
if (newGame == true) //We are starting a new game.
{
//This loop adds 12 cards to the dealtCards list, and removes those dealt cards from the deck.
for (int i = 1; i <= MAXNUMCARDS; i++)
{
dealtCards.Add(deck[0]);
deck.RemoveAt(0);
}
}
else
{
//First we want to do a check to see if there are enough cards in the deck.
//If not then first we add all the discards back to the deck and shuffle them before
//dealing.
if (deck.Count == 0)
{
Debug.WriteLine("ZOMG WE HAVE USED ALL THE CARDS IN THE DECK! (REPLENISHING)");
for (int i = 0; i < discards.Count; i ++)
{
deck.Add(discards[i]);
Shuffle();
}
discards.Clear();
}
//Loops through the three selected card indexes, removing the appropriate
//indexes from dealtCards and adding those cards to the discards pile. Then we
//replenish the dealtCards by replacing the cards with new ones.
for (int i = 0; i < indexesToRemove.Count; i++)
{
dealtCards[indexesToRemove[i]].Selected = false;
discards.Add(dealtCards[indexesToRemove[i]]);
dealtCards.RemoveAt(indexesToRemove[i]);
dealtCards.Insert(indexesToRemove[i], deck[0]);
deck.RemoveAt(0);
}
}
}
//This method takes two integers and swaps the values at these indexes.
private void Swap(int index1, int index2)
{
Card tempCard = deck[index1];
deck[index1] = deck[index2];
deck[index2] = tempCard;
}
//This method goes through each card in the deck and swaps it with a random card in the deck.
//This whole process is repeated 10 times for extreme randomness :P
public void Shuffle()
{
Random myRand = new Random(); //The random object we are using to generate random indexes.
int randomIndex;
for (int i = 1; i <= 10; i++)
{
for (int j = 0; j < deck.Count; j++)
{
randomIndex = myRand.Next(0, deck.Count);
Swap(j, randomIndex);
}
}
}
//This method creates 81 different cards, and assigns the characteristics so NO
//TWO CARDS are the same. It also adds the appropriate card picture to each card.
private void CreateDeck()
{
int counter = 0;
string cardname; //string containing the filename of the card.
//I HATE big nested for loops but it seemed like a reasonable plan of attack.
//Might try a recursive function next time.
for (int a = 1; a <= 3; a++)
{
for (int b = 1; b <= 3; b++)
{
for (int c = 1; c <= 3; c++)
{
for (int d = 1; d <= 3; d++)
{
counter++;
myCard = new Card();
//For cards with associate number less than 10 we need to add "0" to the filename.
if (counter < 10)
{
cardname = "0" + counter + ".gif";
}
else
{
cardname = counter + ".gif";
}
myCard.CardPic = new Bitmap("Set Card Images\\" + cardname); //assigns appropriate picture to the cards picture property.
//a is used in this switch statement to decide the value of the CardFill enum.
switch (a)
{
case 1:
myCard.Fill = Card.CardFill.Filled;
break;
case 2:
myCard.Fill = Card.CardFill.Striped;
break;
case 3:
myCard.Fill = Card.CardFill.NoFill;
break;
}
//b is used in this switch statement to decide the value of the CardShape enum.
switch (b)
{
case 1:
myCard.Shape = Card.CardShape.Squiggly;
break;
case 2:
myCard.Shape = Card.CardShape.Diamond;
break;
case 3:
myCard.Shape = Card.CardShape.Round;
break;
}
//c is used in this switch statement to decide the value of the CardColor enum.
switch (c)
{
case 1:
myCard.Color = Card.CardColor.Red;
break;
case 2:
myCard.Color = Card.CardColor.Purple;
break;
case 3:
myCard.Color = Card.CardColor.Green;
break;
}
//d is used in this switch statement to decide the value of the CardNum enum.
switch (d)
{
case 1:
myCard.Number = Card.CardNum.One;
break;
case 2:
myCard.Number = Card.CardNum.Two;
break;
case 3:
myCard.Number = Card.CardNum.Three;
break;
}
deck.Add(myCard); //adds the card we have finished creating to the deck.
}
}
}
}
}
}
}