First off. Hello,

I'm having issues with my little program I've got a few classes down to a tee and am having issues with the War class.

With the following code I iterate a new game of War and I have stats that hopefully will help some figure out whats wrong.

Anyways when I run the simulation the results(For the Total Battles) are always the same. 52 battles per iteration and in my test of 2000 iterations I get a total of 52000 battles. Now I've done my research and I know for a fact that this is not how it is. Among a number of things I'm trying to create new Shuffling methods.

Shuffling Methods:
1. This is the one that I already have
2. Perfectly interlacing each and every card
3. Using both methods 1 and 2 to create this third method
Note:I know that using the first method and using an over load to
iterate through it(do it several times) is suffecient I just want to
have more of a variety is all.

Before reading the code I would like to give thanks to you for reading and possibly helping me with this subject. All critcisim and advice is greatly accepted!

Thanks again,
- Poab

Time for code:::

667 lines of code from top to bottom(including the brackets)

``````using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
public enum Suit
{
Diamonds, Spades, Clubs, Hearts
}

public enum FaceValue
{
Two = 2,
Three = 3,
Four = 4,
Five = 5,
Six = 6,
Seven = 7,
Eight = 8,
Nine = 9,
Ten = 10,
Jack = 11,
Queen = 12,
King = 13,
Ace = 14
}

public struct Stats
{
public int Player1Wins;
public int Player2Wins;
public int Player1Loss;
public int Player2Loss;
public int TotalBattles;
public int TotalWars;
}

public class Card
{
// Members/Fields
private readonly Suit suit;
private readonly FaceValue faceVal;

// Properties
public Suit Suit { get { return suit; } }
public FaceValue FaceVal { get { return faceVal; } }

// Constructor
public Card(Suit suit, FaceValue faceVal)
{
this.suit = suit;
this.faceVal = faceVal;
}

// Override
public override string ToString()
{
return "The " + faceVal.ToString() + " of " + suit.ToString();
}

}

public class Deck
{
// Members/Fields
private string sDeck;
protected List<Card> cards = new List<Card>();
private Random random;

// Properties
public Card this[int position] { get { return (Card)cards[position]; } }
public int Cards { get { return cards.Count; } }

// Constructor
public Deck()
{
foreach (Suit s in Enum.GetValues(typeof(Suit)))
{
foreach (FaceValue f in Enum.GetValues(typeof(FaceValue)))
{
cards.Add(new Card(s, f));
}
}
random = new Random();
}

// Public Methods
public Card Draw()
{
Card card = cards[0];
cards.RemoveAt(0);

return card;
}

public void Shuffle()
{
for (int n = 0; n != 1; n++)
{
for (int i = 0; i < cards.Count; i++)
{
int index1 = i;
int index2 = random.Next(cards.Count);

SwapCard(index1, index2);
}
}
}
public void Shuffle(int SuffleAmount)
{
if (SuffleAmount == 0)
{
throw new ArgumentException("You must shuffle at least once");
}
else
{
for (int n = 0; n != SuffleAmount; n++)
{
for (int i = 0; i != cards.Count; i++)
{
int index1 = i;
int index2 = random.Next(cards.Count);

SwapCard(index1, index2);
}
}
}
}

// Private Method
private void SwapCard(int index1, int index2)
{
Card card = cards[index1];
cards[index1] = cards[index2];
cards[index2] = card;
}

// Override
public override string ToString()
{
foreach (Card c in cards)
{
sDeck += string.Format("{0}\n", c);
}

return sDeck;
}
}

public class Hand
{
// Members/Fields
protected List<Card> cards = new List<Card>();
// Properties
public int NumCards { get { return cards.Count; } }
public List<Card> Cards { get { return cards; } }

// Public Method
public bool ContainsCard(FaceValue item)
{
foreach (Card c in cards)
{
if (c.FaceVal == item)
{
return true;
}
}
return false;
}
}

public class Player
{
// Members/Fields
private string sDeck;
private Deck curDeck;
private List<Card> cards = new List<Card>();
private int Win = 0, Loss = 0;

// Properties
public Deck CurrentDeck { get { return curDeck; } set { curDeck = value; } }
public int CardCount { get { return cards.Count; } }
public int GetWins{get{ return Win;} set{Win = value;}}
public int GetLosses{get{return Loss;}set{Loss = value;} }

// Public Methods
public void AddCard(Card c)
{
cards.Add(c);
}
public Card RemCard()
{
Card card = cards[0];
cards.RemoveAt(0);
return card;
}

// Override
public override string ToString()
{
foreach (Card c in cards)
{
sDeck += string.Format("{0}\n", c);
}
return sDeck;
}
}

public class Deal
{
// Members/Fields
private Deck deck = new Deck();

// Properties
public Deck GetDeck { get { return deck; } }

// Public Methods
public void DealCards(Player p1, Player p2)
{
deck.Shuffle(10);
for (int i = 0; i != 26; i++)
{
p1.AddCard(deck.Draw());
p2.AddCard(deck.Draw());
}
}
}

public class War
{
// Members/Fields
private Player player1 = new Player();
private Player player2 = new Player();
private Deal dealCards = new Deal();
private int BattleCount = 0;
private int wars = 0;
private string sStatus = "";
private Random r = new Random(Environment.TickCount);
private int rn;

public int Player1Wins { get { return player1.GetWins; } }
public int Player1Loss { get { return player1.GetLosses; } }
public int Player2Wins { get { return player2.GetWins; } }
public int Player2Loss { get { return player2.GetLosses; } }
public int Battels { get { return BattleCount; } }
public int Wars { get { return wars; } }

// Constructors
public War()
{
dealCards.DealCards(player1, player2); // Start The Game
}

// Public Methods
public void PlayGame()
{
while (true)
{
//Console.WriteLine(player1.CardCount + "\n" + player2.CardCount+"\n\n"); // Debug Information

Battle(player1, player2); // Battle

BattleCount++; // Keep count of the number of battles

if (sStatus == "!!! War !!!")
{
sStatus = WarBattle(player1, player2);
wars++;
}

if (player1.CardCount == 0 || player2.CardCount == 52)
{
//Console.WriteLine("Player 2 Wins"); // Comented out to compute results faster
break;
}
if (player1.CardCount == 52 || player2.CardCount == 0)
{
//Console.WriteLine("Player 1 Wins"); // Comented out to compute results faster
break;
}
}
}

public void SingleBattle() // This method is used for debuging
{
Console.Write(Battle(player1, player2));
Console.WriteLine("\n--- Game Stats ---\nBattle: {0}\nPlayer1 Card Count: {1}|{3}\nPlayer2 Card Count: {2}|{4}", BattleCount + 1, player1.CardCount, player2.CardCount, player1.CardCount - 26, player2.CardCount - 26);
BattleCount++;
if (sStatus == "!!! War !!!")
sStatus = WarBattle(player1, player2);
}

// Private Methods
private string Battle(Player p1, Player p2)
{
Card p1Card = p1.RemCard(); // Remove 1 Card from Player1's Deck
Card p2Card = p2.RemCard(); // Remove 1 Card from Player2's Deck

//Console.WriteLine(string.Format("\nPlayer 1 Draws : {0}", p1Card)); // Comented out to provide a faster "end-game" Calculation
//Console.WriteLine(string.Format("Player 2 Draws : {0}\n", p2Card)); // Comented out to provide a faster "end-game" Calculation

if (p1Card.FaceVal > p2Card.FaceVal)
{
rn = r.Next(1, 2);
if (rn == 1)
{
p1.AddCard(p1Card);
p1.AddCard(p2Card);
}
if (rn == 2)
{
p2.AddCard(p2Card);
p2.AddCard(p1Card);
}
player1.GetWins++;
player2.GetLosses++;
return "Player 1 has won!";
}
else if (p2Card.FaceVal > p1Card.FaceVal)
{
rn = r.Next(1, 2);
if (rn == 1)
{
p1.AddCard(p1Card);
p1.AddCard(p2Card);
}
if (rn == 2)
{
p2.AddCard(p2Card);
p2.AddCard(p1Card);
}
player1.GetLosses++;
player2.GetWins++;
return "Player 2 has won!";
}
else if (p1Card.FaceVal == p2Card.FaceVal)
{
return "!!! War !!!";
}
else
{
return "eRR";
}
}
private string WarBattle(Player p1, Player p2)
{
// Burn both player's cards
Card p1Burn1 = p1.RemCard(); // Burn/Put cards "face-down"
Card p1Burn2 = p1.RemCard(); // Burn/Put cards "face-down"

Card p2Burn1 = p2.RemCard(); // Burn/Put cards "face-down"
Card p2Burn2 = p2.RemCard(); // Burn/Put cards "face-down"

// Draw the 'battle card'
Card p1Card = p1.RemCard(); // Draw of the actuall "battle" card
Card p2Card = p2.RemCard(); // Draw of the actuall "battle" card

// Display whats going on
//Console.WriteLine(string.Format("\nPlayer 1 Burns 3 Cards and Draws : {0}", p1Card)); // Comented out to provide faster end-game calculations
//Console.WriteLine(string.Format("Player 2 Burns 3 Cards and Draws : {0}", p2Card)); // Comented out to provide faster end-game calculations

/* ------------------------------------------------
The following IF_ELSE-IF Statement and Switch blocks
in the IF_ELSE are to provide a "random" order of
cards placed back into the winner's deck

NOTE: This feature is not complete as there are
a total of 36 different combinations of each way
the cards can be placed
------------------------------------------------ */
if (p1Card.FaceVal > p2Card.FaceVal)
{
rn = r.Next(1, 16);

switch (rn)
{
case 1:
p1.AddCard(p1Card);
p1.AddCard(p1Burn1);
p1.AddCard(p1Burn2);
p1.AddCard(p2Burn1);
p1.AddCard(p2Burn2);
p1.AddCard(p2Card);
break;
case 2:
p1.AddCard(p1Card);
p1.AddCard(p1Burn2);
p1.AddCard(p2Burn1);
p1.AddCard(p2Burn2);
p1.AddCard(p2Card);
p1.AddCard(p1Burn1);
break;
case 3:
p1.AddCard(p1Card);
p1.AddCard(p2Burn1);
p1.AddCard(p2Burn2);
p1.AddCard(p2Card);
p1.AddCard(p1Burn1);
p1.AddCard(p1Burn2);
break;
case 4:
p1.AddCard(p1Card);
p1.AddCard(p2Burn2);
p1.AddCard(p2Card);
p1.AddCard(p1Burn1);
p1.AddCard(p1Burn2);
p1.AddCard(p2Burn1);
break;
case 5:
p1.AddCard(p2Card);
p1.AddCard(p1Burn1);
p1.AddCard(p1Burn2);
p1.AddCard(p2Burn1);
p1.AddCard(p2Burn2);
p1.AddCard(p1Card);
break;
case 6:
p1.AddCard(p2Card);
p1.AddCard(p1Burn2);
p1.AddCard(p2Burn1);
p1.AddCard(p2Burn2);
p1.AddCard(p1Card);
p1.AddCard(p1Burn1);
break;
case 7:
p1.AddCard(p2Card);
p1.AddCard(p2Burn1);
p1.AddCard(p2Burn2);
p1.AddCard(p1Card);
p1.AddCard(p1Burn1);
p1.AddCard(p1Burn2);
break;
case 8:
p1.AddCard(p2Card);
p1.AddCard(p2Burn2);
p1.AddCard(p1Card);
p1.AddCard(p1Burn1);
p1.AddCard(p1Burn2);
p1.AddCard(p2Burn1);
break;
case 9:
p1.AddCard(p2Card);
p1.AddCard(p1Card);
p1.AddCard(p1Burn1);
p1.AddCard(p1Burn2);
p1.AddCard(p2Burn1);
p1.AddCard(p2Burn2);
break;
case 10:
p1.AddCard(p1Burn1);
p1.AddCard(p1Burn2);
p1.AddCard(p2Burn1);
p1.AddCard(p2Burn2);
p1.AddCard(p2Card);
p1.AddCard(p1Card);
break;
case 11:
p1.AddCard(p1Burn1);
p1.AddCard(p2Burn1);
p1.AddCard(p2Burn2);
p1.AddCard(p2Card);
p1.AddCard(p1Card);
p1.AddCard(p1Burn2);
break;
case 12:
p1.AddCard(p1Burn1);
p1.AddCard(p2Burn2);
p1.AddCard(p2Card);
p1.AddCard(p1Card);
p1.AddCard(p1Burn2);
p1.AddCard(p2Burn1);
break;
case 13:
p1.AddCard(p1Burn1);
p1.AddCard(p2Card);
p1.AddCard(p1Card);
p1.AddCard(p1Burn2);
p1.AddCard(p2Burn1);
p1.AddCard(p2Burn2);
break;
case 14:
p1.AddCard(p1Burn1);
p1.AddCard(p1Card);
p1.AddCard(p1Burn2);
p1.AddCard(p2Burn1);
p1.AddCard(p2Burn2);
p1.AddCard(p2Card);
break;

}
return "Player 1 has won!";
}
else if (p2Card.FaceVal > p1Card.FaceVal)
{
rn = r.Next(1, 16);
switch (rn)
{
case 1:
p2.AddCard(p1Card);
p2.AddCard(p1Burn1);
p2.AddCard(p1Burn2);
p2.AddCard(p2Burn1);
p2.AddCard(p2Burn2);
p2.AddCard(p2Card);
break;
case 2:
p2.AddCard(p1Card);
p2.AddCard(p1Burn2);
p2.AddCard(p2Burn1);
p2.AddCard(p2Burn2);
p2.AddCard(p2Card);
p2.AddCard(p1Burn1);
break;
case 3:
p2.AddCard(p1Card);
p2.AddCard(p2Burn1);
p2.AddCard(p2Burn2);
p2.AddCard(p2Card);
p2.AddCard(p1Burn1);
p2.AddCard(p1Burn2);
break;
case 4:
p2.AddCard(p1Card);
p2.AddCard(p2Burn2);
p2.AddCard(p2Card);
p2.AddCard(p1Burn1);
p2.AddCard(p1Burn2);
p2.AddCard(p2Burn1);
break;
case 5:
p2.AddCard(p2Card);
p2.AddCard(p1Burn1);
p2.AddCard(p1Burn2);
p2.AddCard(p2Burn1);
p2.AddCard(p2Burn2);
p2.AddCard(p1Card);
break;
case 6:
p2.AddCard(p2Card);
p2.AddCard(p1Burn2);
p2.AddCard(p2Burn1);
p2.AddCard(p2Burn2);
p2.AddCard(p1Card);
p2.AddCard(p1Burn1);
break;
case 7:
p2.AddCard(p2Card);
p2.AddCard(p2Burn1);
p2.AddCard(p2Burn2);
p2.AddCard(p1Card);
p2.AddCard(p1Burn1);
p2.AddCard(p1Burn2);
break;
case 8:
p2.AddCard(p2Card);
p2.AddCard(p2Burn2);
p2.AddCard(p1Card);
p2.AddCard(p1Burn1);
p2.AddCard(p1Burn2);
p2.AddCard(p2Burn1);
break;
case 9:
p2.AddCard(p2Card);
p2.AddCard(p1Card);
p2.AddCard(p1Burn1);
p2.AddCard(p1Burn2);
p2.AddCard(p2Burn1);
p2.AddCard(p2Burn2);
break;
case 10:
p2.AddCard(p1Burn1);
p2.AddCard(p1Burn2);
p2.AddCard(p2Burn1);
p2.AddCard(p2Burn2);
p2.AddCard(p2Card);
p2.AddCard(p1Card);
break;
case 11:
p2.AddCard(p1Burn1);
p2.AddCard(p2Burn1);
p2.AddCard(p2Burn2);
p2.AddCard(p2Card);
p2.AddCard(p1Card);
p2.AddCard(p1Burn2);
break;
case 12:
p2.AddCard(p1Burn1);
p2.AddCard(p2Burn2);
p2.AddCard(p2Card);
p2.AddCard(p1Card);
p2.AddCard(p1Burn2);
p2.AddCard(p2Burn1);
break;
case 13:
p2.AddCard(p1Burn1);
p2.AddCard(p2Card);
p2.AddCard(p1Card);
p2.AddCard(p1Burn2);
p2.AddCard(p2Burn1);
p2.AddCard(p2Burn2);
break;
case 14:
p2.AddCard(p1Burn1);
p2.AddCard(p1Card);
p2.AddCard(p1Burn2);
p2.AddCard(p2Burn1);
p2.AddCard(p2Burn2);
p2.AddCard(p2Card);
break;

}
return "Player 2 has won!";
}
else if (p1Card.FaceVal == p2Card.FaceVal)
{
return "!!! War !!!";
}
else
{
return "eRR";
}
}
}

class Program
{
static void Main(string[] args)
{
// Fields
War w;
Stats s = new Stats();
int NumberOfWarIterations = 2000;

/* ----------------------------------------------------------------
For() Loop to iterate through 2000 new games of wars and to also
keep track of a number of statistics that will hopefully narrow
down where the bug is coming from.
---------------------------------------------------------------- */

for (int i = 0; i != NumberOfWarIterations; i++)
{
w = new War();
w.PlayGame();
s.Player1Loss  += w.Player1Loss;
s.Player1Wins  += w.Player1Wins;
s.Player2Loss  += w.Player2Loss;
s.Player2Wins  += w.Player2Wins;
s.TotalBattles += w.Battels; // depending on the amount of "War Games" to iterate trough this value seems to always be 52*Number of iterations
s.TotalWars    += w.Wars;
}

// Display the statistical data
Console.WriteLine("---   Game Stats   ---\n");
Console.WriteLine("Player 1 Wins: {0}", s.Player1Wins);
Console.WriteLine("Player 1 Loss: {0}\n", s.Player1Loss);
Console.WriteLine("Player 2 Wins: {0}", s.Player2Wins);
Console.WriteLine("Player 2 Loss: {0}\n------------------", s.Player2Loss);
Console.WriteLine("Total Battles: {0}", s.TotalBattles);
Console.WriteLine("   Total Wars: {0}", s.TotalWars);
Console.WriteLine("--- End Game Stats ---\n\n");

// Total battles are 52 * NumberOfWarIterations
// in this case the total would be : 52,000 battles for 2000 iterations
}
}
}``````

1) I have a doubt in your Battle method.

``````if (p1Card.FaceVal > p2Card.FaceVal)
{
rn = r.Next(1, 2);
if (rn == 1)
{
p1.AddCard(p1Card);
p1.AddCard(p2Card);
}
if (rn == 2)
{
p2.AddCard(p2Card);
p2.AddCard(p1Card);
}
player1.GetWins++;
player2.GetLosses++;
return "Player 1 has won!";
}``````

You are giving the cards to the winner not only based on the value of the card but also on the random number generated.
If you were trying to shuffle the won cards before putting them into the winners deck, you might have to do.

``````if (p1Card.FaceVal > p2Card.FaceVal)
{
rn = r.Next(1, 2);
if (rn == 1)
{
p1.AddCard(p1Card);
p1.AddCard(p2Card);
}
if (rn == 2)
{
p1.AddCard(p2Card);
p1.AddCard(p1Card);
}
player1.GetWins++;
player2.GetLosses++;
return "Player 1 has won!";
}``````

Or is there any other reason you are using the random number for?

2)The return value of Battle is not being used.
So, you might want to do

``sStatus = Battle(player1, player2); // Battle``

in playgame method.

3) In the Battle method if the facevalues of the cards are the same then the cards are not added to the deck. This successively decreased the total number of cards in the game.
You might want to add them to the respective decks.

Ahh yes I forgot to have the cards return to the deck in the final else if statement. Will retest with your suggestions. And to answer the question yes the random number is to "shuffle" the cards back into the winners deck, as the rules of this game are: "When the winner is decided on whom ever has the highest face value and are not equal then the cards are returned to the bottom of the winner's deck in no specific order" that is why I have the random number. However in the "WarBattle" method not all posibilities are there do to the fact that its a bit time consuming and I wanted to get the basics there to get help on the major bugs...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.