How to pick up the 4th card after a tie? [duplicate] - c#

This question already has answers here:
What is an IndexOutOfRangeException / ArgumentOutOfRangeException and how do I fix it?
(5 answers)
Closed 1 year ago.
I have this card war game; the game starts with each player flipping over 1 card and whomever has the highest card wins. If there is a tie, the next three cards in the deck are played face down and the 4th card dealt face up, whomever has the highest card wins. The game is played until 1 player has all of the cards; however, I haven’t been able to finish it correctly, it crashes every time that I hit enter after the statement "lets flipping cards," and I haven’t been able to make the players to get the 4th card because when using a third variable r3, it crashes likewise.
using System;
using System.Collections.Generic;
namespace PTCB12WARGAME2
{
class Card
{
public string Name;
public int Value;
public string Suit;
public override string ToString() { return string.Format("{0} of {1}", Name, Suit); }
}
class DeckOfCards
{
static void Main(string[] args)
{
List<Card> DeckOfCards = new List<Card>();
var names = new[] { "Ace", "Two", "Three", "Four", "Five", "Six",
"Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King" };
var suits = new[] { "Hearts", "Clubs", "Diamonds", "Spades" };
var cards = new List<Card>();
// Populate our deck of cards with two nested loops
foreach (var suit in suits)
{
// We use a 'for' loop here instead of 'foreach' so we can use the index to
// populate the Value (which is always one more than the index of the name)
for (int i = 0; i < names.Length; i++)
{
cards.Add(new Card { Name = names[i], Value = i + 1, Suit = suit });
}
}
//Display deck of 52 cards
Console.WriteLine("Each player is flipping over 1 card and whomever has the highest card wins");
Console.ReadLine();
for (int i = 0; i < DeckOfCards.Count; i++)
{
Console.WriteLine(DeckOfCards[i]);
}
Console.WriteLine();
// create just ONCE the random object
Random random = new Random();
List<Card> deckOfPlayer1 = new List<Card>();
List<Card> deckOfPlayer2 = new List<Card>();
do
{
Console.WriteLine("Let's flip cards");
string userAnswer = Console.ReadLine();
if (userAnswer == "quit")
break;
// pick a random card for player 1
int r1;
r1 = random.Next(0, DeckOfCards.Count);
Card c1 = DeckOfCards[r1];
deckOfPlayer1.Add(c1);
// pick a random card for player 2
int r2;
r2 = random.Next(0, DeckOfCards.Count);
Card c2 = DeckOfCards[r2];
deckOfPlayer2.Add(c2);
Console.WriteLine("Player1 has: {0}", c1);
Console.WriteLine("Player2 has: {0}", c2);
// now compare the cards
if (c1.Value == c2.Value )
{
Console.WriteLine("Its a tie! Next three cards in deck are played face down, " +
"4th card face up, whoever has the highest card wins");
int r3;
r3 = random.Next(0, DeckOfCards.Count);
Card c3 = DeckOfCards[3];
deckOfPlayer1.Add(c3);
Console.WriteLine(deckOfPlayer1[3]);
Console.WriteLine(deckOfPlayer2[3]);
if (c1.Value > c2.Value && c2.Value != 1)
{
deckOfPlayer1.Add(c2);
deckOfPlayer2.Remove(c2);
Console.WriteLine("Player1 wins");
}
else
{
Console.WriteLine("Player2 wins");
deckOfPlayer2.Add(c1);
deckOfPlayer1.Remove(c1);
}
}
else if (c1.Value == 1 || (c1.Value > c2.Value && c2.Value != 1))
{
deckOfPlayer1.Add(c2);
deckOfPlayer2.Remove(c2);
Console.WriteLine("Player1 wins");
}
else
{
Console.WriteLine("Player2 wins");
deckOfPlayer2.Add(c1);
deckOfPlayer1.Remove(c1);
}
} while (deckOfPlayer1.Count < 52 || (deckOfPlayer2.Count < 52));
}
}
}

First, please learn how to use interactive debugging. If all you know is "it crashed", then you're going to struggle to find and eliminate bugs in your code. Here's one tutorial you can start with.
In this instance, the problem is likely due to your use of two lists:
List<Card> DeckOfCards = new List<Card>(); // One here
var names = new[] { "Ace", "Two", "Three", "Four", "Five", "Six",
"Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King" };
var suits = new[] { "Hearts", "Clubs", "Diamonds", "Spades" };
var cards = new List<Card>(); // And one here
You are populating the second one with cards.Add, but you are trying to access the first one everywhere else, for example with these lines:
r1 = random.Next(0, DeckOfCards.Count);
Card c1 = DeckOfCards[r1];
Remove one of the lists, replace it with the other instance, and your code will have a better chance of running (disclaimer: I haven't compiled it to check for other issues)

Related

Display Cards in UI in Unity

I am trying to Display Cards in Canvas programmatically from Script onClick. I have Successfully shuffled my cards and stored in player1 and player2 Array (26 random card to each player). I have create a basic players zone for player1 and player2. My approach is when i click the button the cards get distributed in their respective zones with images, stored in cards
I am new to unity and have no idea to render images to Canvas, I tried creating gameObject Canvas with help of google and stackoverflow question but i didn't solve my problem. I have store my full project on dropbox LINK: https://www.dropbox.com/sh/ofx9m6kaljpiecz/AAC7q7ufgAK1jlPHOfsGJWkPa?dl=0.
Canvas Image: https://www.dropbox.com/s/y118yboj9hx0i52/Screenshot%20%2833%29.png?dl=0
Here my main HelloWord.cs file
public class HelloWorld : MonoBehaviour
{
Sprite sprite;
List<string> player1 = new List<string>();
List<string> player2 = new List<string>();
public static string[] suits = new string[] { "spades", "hearts", "clubs", "diamonds" };
public static string[] values = new string[] { "ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "jack", "queen", "king" };
public List<string> deck;
Texture2D myTexture;
public void PlayCards()
{
deck = GenerateDeck();
Shuffle(deck);
}
public void distributeCard()
{
for (var x = 1; x <= 26; x++)
{
player1.Add(deck[x]);
foreach (var item in player1)
{
print("Player 1 Cards:" + item.ToString());
}
}
for (var y = 26; y <= 52; y++)
{
player2.Add(deck[y]);
foreach (var item in player2)
{
print("Player 2 Cards:" + item.ToString());
}
}
}
public void onClick()
{
PlayCards();
distributeCard();
}
public static List<string> GenerateDeck()
{
List<string> newDeck = new List<string>();
foreach(string s in suits)
{
foreach(string v in values)
{
newDeck.Add(v +"_of_" + s);
}
}
return newDeck;
}
void Shuffle<T>(List<T> list)
{
System.Random random = new System.Random();
int n = list.Count;
while (n > 1)
{
int k = random.Next(n);
n--;
T temp = list[k];
list[k] = list[n];
list[n] = temp;
}
}
}
There is a UI Element called "RawImage". That is just a single Texture to be shown in your UI layer on your canvas. It can be added from the editor in UI -> Raw Image .
You can assign a material or a texture.
The element UnityEngine.UI.RawImage has a property 'material' that you can assign anytime. You can use that to assign materials you have prepared, one for each card.
When using a material, be sure to set the shader "Unlit/Color" or "Unlit/Transparent", because normal shaders using light will show a black image on the UI layer.

Shuffling Algorithm for Stroop Effect

I'm working on a small C# Winforms application that replicates several visual experiments concerning cognitive psychology. Instead of using premade images, I was trying to create them programmatically to train my skills.
Now I'm working on the Stroop Effect and I'm attempting to reproduce the following image:
I prepared two distinct arrays, one containing the colors and one containing the name of the colors:
Color[] colors =
{
Color.Blue,
Color.Red,
Color.Black,
Color.Green
};
String[] names =
{
"BLUE",
"RED",
"BLACK",
"GREEN"
};
The first step is easy. All I have to do is to draw the strings using their respective ForegroundColor, such that names[0] will be printed using colors[0], names[1] will be printed using colors[1] and so on...
And here comes the hard part of the task. Once the string array has been shuffled:
Random r = new Random();
String[] namesRandom = names.OrderBy(x => r.Next()).ToArray();
I have to shuffle the colors array too. But this must be achieved making sure that no color is being matched with its correct name. So, for example, this will be correct:
"BLACK" Color.Green
"GREEN" Color.Red
"RED" Color.Blue
"BLUE" Color.Black
and this will be wrong:
"BLACK" Color.Black -> Matching Color/Name
"GREEN" Color.Red
"RED" Color.Blue
"BLUE" Color.Green
Any idea about how to accomplish this efficiently? I would like to avoid the shuffle until condition is met approach.
P.S. = drawing strings on a Bitmap is not a problem and I don't need help on this development task.
[EDIT]
For 10k-ers that downvote for not being able to associate the concept of shuffle until condition is met with a few lines of code or because they think I'm trying to get my work done while I play Tetris, here is a quick implementation of my current algorithm:
public class Program
{
private static Random s_Random = new Random();
public static void Main(String[] args)
{
Color[] colors =
{
Color.Blue,
Color.Red,
Color.Black,
Color.Green
};
String[] names =
{
"BLUE",
"RED",
"BLACK",
"GREEN"
};
while (MatchingPairs(colors, names))
{
colors = Shuffle(colors);
names = Shuffle(names);
}
for (Int32 i = 0; i < colors.Length; ++i)
Console.WriteLine(colors[i].Name.ToUpperInvariant() + " | " + names[i]);
}
private static Boolean MatchingPairs(Color[] colors, String[] names)
{
for (Int32 i = 0; i < colors.Length; ++i)
{
if (colors[i].Name.ToUpperInvariant() == names[i])
return true;
}
return false;
}
private static T[] Shuffle<T>(T[] array)
{
return array.OrderBy(x => s_Random.Next()).ToArray();
}
}
So, as you can see, I can use my fingers to type code. I'm just wondering if there is a better approach to obtain the same result.
This is maybe not the best approach, but it should work fine. One way to solve this would be to assign random names until you have only two left (where index i == Count - 2). At that point, if the name of the item at index i + 1 is one of the remaining choices, use that for the item at index i.
Here's some sample code that uses a console application (and ConsoleColor), but the concept is the same. I've used a class that combines a ConsoleColor and a string, so if you want to use arrays it would need some modification.
First the class:
class NamedColor
{
public ConsoleColor Color { get; set; }
public string Name { get; set; }
public NamedColor(ConsoleColor color, string name)
{
Color = color;
Name = name;
}
}
And a method that prints out a list of these objects to the Console:
static void PrintColors(IEnumerable<NamedColor> colors)
{
Console.BackgroundColor = ConsoleColor.Gray;
Console.Write(new string(' ', 80));
foreach (var color in colors)
{
Console.ForegroundColor = color.Color;
Console.Write(color.Name + " ");
}
Console.Write(new string(' ', 139)); // Ugly, hard-coded length for simplicity
}
Then we can create a method that will take a list of these objects, shuffle all the names between them, and return that new list:
static NamedColor[] ShuffleNames(NamedColor[] colors)
{
var names = colors.Select(c => c.Name).ToList();
var shuffled = new NamedColor[colors.Length];
for (int i = 0; i < colors.Length; i++)
{
string name;
// If there are only two items left, and our list of names contains
// the *next* item's name, then we must take that name for this item
if (i == colors.Length - 2 && names.Contains(colors[i + 1].Name))
{
name = colors[i + 1].Name;
}
else
{
// Choose a random name from all names except this item's name
var candidateNames = names.Where(n => !n.Equals(colors[i].Name)).ToList();
name = candidateNames[rnd.Next(candidateNames.Count)];
}
shuffled[i] = new NamedColor(colors[i].Color, name);
names.Remove(name);
}
return shuffled;
}
Then we can shuffle our list, then shuffle the names, and then print out the list like so:
private static Random rnd = new Random();
static void Main(string[] args)
{
var correctColors = new NamedColor[]
{
new NamedColor(ConsoleColor.Blue, "BLUE"),
new NamedColor(ConsoleColor.Black, "BLACK"),
new NamedColor(ConsoleColor.Red, "RED"),
new NamedColor(ConsoleColor.Green, "GREEN"),
};
PrintColors(correctColors);
for (int count = 0; count < 10; count++)
{
// Shuffle the items, then shuffle the names
var shuffledColors = correctColors.OrderBy(c => rnd.NextDouble()).ToArray();
shuffledColors = ShuffleNames(shuffledColors);
PrintColors(shuffledColors);
}
Console.ResetColor();
Console.WriteLine("\nDone!\nPress any key to exit...");
Console.ReadKey();
}
Output
You could do the following:
Implement a rule of valid colors for each name:
Func<string, IEnumerable<Color>> validColorsRule = s =>
{
switch (s)
{
case "BLUE": return colors.Except(new[] { Color.Blue });
case "RED": return colors.Except(new[] { Color.Red });
case "BLACK": return colors.Except(new[] { Color.Black });
case "GREEN": return colors.Except(new[] { Color.Green });
default: throw new NotSupportedException();
}
};
Build the shuffled Color array with Enumerable.Aggregate:
Color[] colorRandom =
namesRandom.Aggregate(Enumerable.Empty<Color>(),
(acc, n) =>
acc.Concat(new[] {
validColorsRule(n).Except(acc)
.OrderBy(x => r.Next())
.FirstOrDefault() }))
.ToArray();
You could reformulate as a pseudo-random problem.
Generate a random integer between 1 and n-1(say i) and shift the color array(use a copy of it) by that i
Now generate a random sequence between 0 and n-1 without replacement and print the name and color
static void Main(string[] args)
{
ConsoleColor[] colors =
{
ConsoleColor.Blue,
ConsoleColor.Red,
ConsoleColor.Black,
ConsoleColor.Green
};
String[] names =
{
"BLUE",
"RED",
"BLACK",
"GREEN"
};
Random r = new Random();
int i = 0;
while(i==0)
i=r.Next() % (names.Length-1);
List<int> rndList = Enumerable.Range(0,names.Length).OrderBy(x => r.Next()).ToList();
Console.BackgroundColor = ConsoleColor.White;
foreach(int j in rndList)
{
int k = (j+i) % (colors.Length);
Console.ForegroundColor = colors[k];
Console.Write(names[j] + " ");
}
}
I think that you are trying to create a random derangement. A web search for this finds a paper about the efficient generation of such things at http://epubs.siam.org/doi/pdf/10.1137/1.9781611972986.7 - "Generating Random Derangements" by Martinez, Panholzer, and Prodinger. However the paper also shows that, even as n gets possibly very large, the chance of a randomly chosen permutation being a derangement remains very close to 1/e, so the simple solution of just generating a random permutation and checking to see if it is a derangement, repeating until this is true, may in practice be efficient enough for most purposes.

Creating a simple deck of cards C#

I'm trying to create a deck of 52 cards with the 4 suits: spades, hearts, clubs, and diamonds. I tried to create this for loop in my Deck class but seem to be running into some problems regarding actually getting the program to do what I want. I'm thinking maybe I could do 4 for loops as the assignment hinted at, but is it possible to use if/else-ifs to create 4 suits within the deck?
class Deck
{
private Card[] cards;
public Deck()
{
cards = new Card[52];
int check = 0;
for (int suitVal = 1; suitVal < 4; suitVal++)
{
for (int rankVal = 1; rankVal < 14; rankVal++)
{
if(suitVal == 1)
{
cards[check] = new Card(rankVal, "Spades");
}
else if (suitVal == 2)
{
cards[check] = new Card(rankVal, "Hearts");
}
else if (suitVal == 3)
{
cards[check] = new Card(rankVal, "Clubs");
}
else if (suitVal == 4)
{
cards[check] = new Card(rankVal, "Diamonds");
}
}
}
}
Yes, it is possible.
There are 13 cards and 4 suits. The idea is that for each suit, you create 13 cards with of said suit. The pseudo code is pretty much what you have already got:
for each of the four suits
loop 13 times for said suit
Here are the problems with your code:
1- Your check variable is never incremented, so you always overwrite the card on the position 0; It should increment after every card inserted (inner loop)
2- Your outter loop only runs 3 times (i = 1, i = 2, i = 3), and there are 4 suits.
Let me know if you need more help.
You could do it these ways:
class Deck
{
private Card[] cards;
public Deck()
{
cards =
new [] { "Spades", "Hearts", "Clubs", "Diamonds", }
.SelectMany(
suit => Enumerable.Range(1, 13),
(suit, rank) => new Card(rank, suit))
.ToArray();
}
}
Or:
class Deck
{
private Card[] cards;
public Deck()
{
var query =
from suit in new [] { "Spades", "Hearts", "Clubs", "Diamonds", }
from rank in Enumerable.Range(1, 13)
select new Card(rank, suit);
cards = query.ToArray();
}
}
Or:
class Deck
{
private Card[] cards;
public Deck()
{
cards = new Card[52];
var index = 0;
foreach (var suit in new [] { "Spades", "Hearts", "Clubs", "Diamonds", })
{
for (var rank = 1; rank <= 13; rank++)
{
cards[index++] = new Card(rank, suit);
}
}
}
}

C# assigning value to array

I'm trying to write a program that would shuffle 13 playing cards from ace to king. Deal 2 cards out and add up the value that's assigned to each card. ace = 11, king = 10, jack = 10, queen = 10, ten = 10, nine = 9, eight = 8 and so on... sort of like blackjack.
so far I'm only able to shuffle the cards and print out two cards randomly but i don't know how to assign a value to each card, add them up and print it out. example, if my two random cards is King and Eight then i would want the program to print out..
King
Eight
18
here's what i got...
static void Main(string[] args)
{
string[] Cards = new string[] {"Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "jack", "Queen", "King", "Ace"};
for (int x = 0; x < 100; x++) // looping the shuffle 100 times to maximize the randomness
{
for (int i = Cards.Length; i > 0; i--) //shuffle
{
string temp;
Random random = new Random();
int r = random.Next(i);
temp = Cards[r];
Cards[r] = Cards[i-1];
Cards[i-1] = temp;
}
}
Console.WriteLine(Cards[0]); //first random card
Console.WriteLine(Cards[1]); //second random card
Console.ReadKey();
}
For a great example of programming a card game, do a google search for KarliCards from the book Beginning Visual C# 2012 (you can easily find a full PDF, but I won't post a link because I'm not sure if it's legal). It has lots of stuff like how to use regular operators (+) on things like classes or structs.
Anyway, what you're looking for is an enum. It's very similar to the Dictionary(string)(int) suggested by Rob (I'm not sure how to write triangle brackets). Here's an example of how it works:
enum CardValue
{
One = 1,
Two = 2,
Three = 3,
Four = 4
}
static void Main(string[] args)
{
int myInt = (int)CardValue.One;
Console.WriteLine("output should be 1: " + myInt);
int mySum = (int)CardValue.One + (int)CardValue.Three;
Console.WriteLine("output should be 4: " + mySum);
Console.ReadKey();
}
My first language was Perl, so I tend to see everything as a struct instead of a class. There's always more than one way to do it....
public enum CardSuits
{
Clubs,
Spades,
Hearts,
Diamonds
}
public enum CardValues
{
Ace = 1,
Two = 2,
Three = 3,
Four = 4
}
public struct Card
{
public CardValues Value; // Card.Value = CardValues.Ace
public CardSuits Suit; // Card.Suit = CardSuits.Spades
public override string ToString()
{
// Card.ToString() == "Ace of Spades"
return String.Format(Value + " of " + Suit);
}
public string ToStringAsInteger()
{
// Card.ToStringAsInteger() == "1 of Spades"
return String.Format((int)Value + " of " + Suit);
}
}
static void Main(string[] args)
{
Card AceOfSpades = new Card();
AceOfSpades.Value = CardValues.Ace;
AceOfSpades.Suit = CardSuits.Spades;
Card TwoOfClubs = new Card();
TwoOfClubs.Value = CardValues.Two;
TwoOfClubs.Suit = CardSuits.Clubs;
int mySum = (int)AceOfSpades.Value + (int)TwoOfClubs.Value;
Console.WriteLine("Sum of Ace (1) and Two (2) is: " + mySum); // should be 3
Console.WriteLine("output of AceOfSpades.ToString() is: " + AceOfSpades.ToString());
Console.WriteLine("output of AceOfSpades.ToStringAsInteger is: " + AceOfSpades.ToStringAsInteger());
Console.ReadKey();
}
Here's how I would do this:
var cards = new Dictionary<string, int>()
{
{ "Two", 2 }, { "Three", 3 }, { "Four", 4 }, { "Five", 5 }, { "Six", 6 },
{ "Seven", 7 }, { "Eight", 8 }, { "Nine", 9 }, { "Ten", 10 }, { "Jack", 10 },
{ "Queen", 10 }, { "King", 10 }, { "Ace", 11 },
};
var random = new Random();
var selected = cards.OrderBy(c => random.Next()).Take(2).ToArray();
foreach (var card in selected)
{
Console.WriteLine(card.Key);
}
Console.WriteLine(selected.Sum(c => c.Value));
Running this, I get (for example):
Two
Ten
12
Now, just for a bit more info on your existing code.
Calling Random random = new Random(); within a loop will result in many (if not all) of the random numbers being the same. You should use a single instance of Random only.
There is no need for the for (int x = 0; x < 100; x++) loop as a single pass of the for (int i = Cards.Length; i > 0; i--) loop is sufficient to randomize the cards.

Unity 4 C# - IndexOutOfRangeException: Array index is out of range

Apologies if this is answered somewhere else but I believe my problem is very specific and I haven't got the slightest clue, based on past experience in python, why this code returns the error:
IndexOutOfRangeException: Array index is out of range.
(wrapper stelemref) object:stelemref (object,intptr,object)
MissionGen.Start () (at Assets/Scripts/MissionGen.cs:59)
I am currently in the process of learning C# in the Unity engine, and have created a small class experimentation script designed to generate missions using OOP, selecting target names from an array - but obviously, I have no idea why this won't work. As I believe the equivalent error in python only occurs when I have tried to refrence an array element that doesn't exist. And though this is probably also the case here, due to the way the code is written, I don't understand why. Anyway, here is the full script!:
// Simple mission generation script, using arrays and object orientated programming. Designed to be
// upgraded at a later date.
using UnityEngine;
using System.Collections;
// Mission generation template
public class MissionGen : MonoBehaviour {
public string[] t_list_t = new string[] {"Dave", "Johnson", "Hadaki", "Tim", "Timothy", "Chris Roberts"};
public string[] t_list_c_col = new string[] {"Blue", "Yellow", "Green", "Black", "Orange", "Purple"};
public string[] t_list_h_col = new string[] {"Black", "Green", "Orange", "Blue", "Red", "Brown"};
public class MissionTemplate {
// Mission properties
public int id;
public string t_name;
public string t_coat;
public string t_hat;
public string type;
public int reward1;
public string reward2;
// A method, for displaying the attributes of an individual mission.
public void ShowMission () {
print ("MISSION ID: " + id);
print ("TARGET NAME: " + t_name);
print ("TARGET COAT COLOUR: " + t_coat);
print ("TARGET HAT COLOUR: " + t_hat);
print ("MISSION TYPE: " + type);
print ("REWARD 1 (Money): " + reward1);
print ("REWARD 2 (Item): " + reward2);
}
}
// Mission array generation. Change the array range to generate more missions for use in the game.
void Start() {
// Change this variable to decide how many missions to generate:
int gen = 50;
// Change the above variable to designate the number of missions to generate.
MissionTemplate[] MISSION = new MissionTemplate[gen];
for (int i = 1; i <= gen; i++)
{
int t_pick = Random.Range (0,5);
int t_coat_col = Random.Range (0,5);
int t_hat_col = Random.Range (0,5);
MISSION[i] = new MissionTemplate();
MISSION[i].id = i;
MISSION[i].t_name = t_list_t[t_pick];
MISSION[i].t_coat = t_list_c_col[t_coat_col];
MISSION[i].t_hat = t_list_h_col[t_hat_col];
MISSION[i].type = "Assassination";
MISSION[i].reward1 = 0;
MISSION[i].reward2 = "";
}
for (int i = 1; i <= gen; i++)
{
MISSION[i].ShowMission();
}
}
}
As a breakdown, I believe the problems arise between the following code:
for (int i = 1; i <= gen; i++)
{
int t_pick = Random.Range (0,5);
int t_coat_col = Random.Range (0,5);
int t_hat_col = Random.Range (0,5);
MISSION[i] = new MissionTemplate();
MISSION[i].id = i;
MISSION[i].t_name = t_list_t[t_pick];
MISSION[i].t_coat = t_list_c_col[t_coat_col];
MISSION[i].t_hat = t_list_h_col[t_hat_col];
MISSION[i].type = "Assassination";
MISSION[i].reward1 = 0;
MISSION[i].reward2 = "";
}
for (int i = 1; i <= gen; i++)
{
MISSION[i].ShowMission();
}
}
}
And:
public class MissionGen : MonoBehaviour {
public string[] t_list_t = new string[] {"Dave", "Johnson", "Hadaki", "Tim", "Timothy", "Chris Roberts"};
public string[] t_list_c_col = new string[] {"Blue", "Yellow", "Green", "Black", "Orange", "Purple"};
public string[] t_list_h_col = new string[] {"Black", "Green", "Orange", "Blue", "Red", "Brown"};
Any help would be much appreciated!
Thanks,
This is the problem:
MissionTemplate[] MISSION = new MissionTemplate[gen];
for (int i = 1; i <= gen; i++)
{
...
MISSION[i] = new MissionTemplate();
Aside from violating normal naming conventions, arrays in C# are 0-based - for example, the valid indexes for an array of length 5 are 0, 1, 2, 3 and 4.
So your for loop should be:
for (int i = 0; i < gen; i++)
Or more "obviously correctly" (IMO):
for (int i = 0; i < MISSION.Length; i++)
That makes it obvious to anyone reading the code that you're staying within the bounds of the MISSION array, without having read the array creation statement.

Categories

Resources