-
June 11th, 2014, 10:06 PM
#1
Need help with assignment.
Can't work out where i should start with this assignment when it comes to taking information from the "ProcessCards", for instance how i could get the getTotalAttack summed up through runnning through the processcards.
trying to learn more than just get the answer, probably just knowing how a grab information from the cards and get it to count down the different.
Code:
using UnityEngine;
using System.Collections;
public class SubmissionCS : MonoBehaviour {
string studentName = "studentname";
//you will need more data than this, but this should get you started
public int cardCount;
public int totalAttack;
public int minHealth;
public int maxSurvive;
public int bestCardName;
public int specialCount;
//called for you before any calculations begin
public void PrepareCalc()
{
//set all calculation values to zero or null or "" etc.
}
//called once for every card
public void ProcessCard(string cardName,int hp, int attack, int armour, SpecialAttackTypeCS spcType)
{
//handle input data
}
//called after all cards have been passed to ProcessCard, after this the gets will be called by the testing application
public void FinishCalc()
{
//you have now processed all cards, do any final processing that you may need
}
//how many cards are in the deck you were asked to process
public int GetNumberOfCards()
{
return 7;
}
//what is the total damage of the deck
public int GetTotalAttack()
{
return 37;
}
//what is the lowest health of any card in your deck
public int GetLowestHealth()
{
return 2;
}
//what is the average attack of the deck, within 1% error
public float GetAverageAttack()
{
return 5;
}
//what is the largest single attack a card in your deck can survive
public int GetMaxSurvivableAttack()
{
return 7;
}
//what is the average single attack cards in your deck can survive, within 1% error
public float GetAverageSurvivableAttack()
{
return 01;
}
//This is a bit trickey, this is asking for the name of the card that is the highest damage and in the case of a tie the highest survivability (which is health and armour together)
public string GetNameOfBestCard()
{
return "Preditor";
}
//how many cards have special abilities
public int GetNumberOfSpecials()
{
return 4;
}
//what is the most common special ability your deck has, excluding None
public SpecialAttackTypeCS GetMostCommonSpecial()
{
return SpecialAttackTypeCS.Regen;
}
}
Code:
using UnityEngine;
using System.Collections;
public class TestCS : MonoBehaviour {
SubmissionCS sub;
void Start () {
sub = FindObjectOfType<SubmissionCS>();
Invoke("RunTests", 0.01f);
}
void RunTests()
{
sub.PrepareCalc();
sub.ProcessCard("Assassin", 5, 4, 0, SpecialAttackTypeCS.None);
sub.ProcessCard("Predator", 5, 5, 0, SpecialAttackTypeCS.Poison);
sub.ProcessCard("Techtician", 3, 7, 0, SpecialAttackTypeCS.Regen);
sub.ProcessCard("General", 2, 8, 0, SpecialAttackTypeCS.None);
sub.ProcessCard("Fiend", 8, 1, 0, SpecialAttackTypeCS.Regen);
sub.ProcessCard("Guardian", 2, 7, 1, SpecialAttackTypeCS.None);
sub.ProcessCard("Spectre", 3, 5, 2, SpecialAttackTypeCS.None);
sub.FinishCalc();
//test the results
print( "Number of cards is " + (sub.GetNumberOfCards() == 7f ? "Correct" : "Incorrect"));
print( "Total attack is " + (sub.GetTotalAttack() == 37f ? "Correct" : "Incorrect"));
print( "Lowest health is " + (sub.GetLowestHealth() == 2f ? "Correct" : "Incorrect"));
float returned = sub.GetAverageAttack();
float desired = 5.2857142857f;
float err = Mathf.Abs( (returned / desired) - 1f);
print( "Average attack is " + ( err <= 0.01f ? "Correct" : "Incorrect"));
print( "Max Survivable attack is " + (sub.GetMaxSurvivableAttack() == 7f ? "Correct" : "Incorrect"));
returned = sub.GetAverageSurvivableAttack();
desired = 4.4285714286f - 1f;
err = Mathf.Abs( (returned / desired) - 1f);
print( "Average survivability is " + ( err <= 0.01f ? "Correct" : "Incorrect"));
print( "Number of specials is " + (sub.GetNumberOfSpecials() == 3f ? "Correct" : "Incorrect"));
print( "Most common specials is " + (sub.GetMostCommonSpecial() == SpecialAttackTypeCS.Regen ? "Correct" : "Incorrect"));
}
}
-
June 11th, 2014, 10:08 PM
#2
Re: Need help with assignment.
all the return #, are set to 0 really. the numbers are there cause i just wanted to check.
-
June 12th, 2014, 09:03 PM
#3
Re: Need help with assignment.
though i don't see the actual card class
the simple answer is
you need a list or lists and some for, foreach or while loops to do these tasks
the long explanation or answer
for understanding practical purposes would be
you need to know how to structure and organize it
so you can tackle it piece by piece
for example if i were to analyze this problem
so you can see how i would approach it right of the bat
because this is a problem that has many parts
we need a very basic class hierarchy first to make this clear
first will focus on between were data is (variables) and were it is worked on (methods)
and were the work order is placed (program method execution ordering)
class holding data(feilds properties)
class working on groups of data(lists , methods)
class that organize work (sequential execution of group methods or helper class's that cross these class gaps)
structurally i would start with just those basic ideas in mind
then start busting up the problem with simple questions and answers (that are super basic) like so
its a card game i need each cards values maped out
so ill be setting up a card class, "what uses a deck of cards" , a player ,
"who controls a games order" the rules which is basically what a game is a "game" class
anything past that will require more thought so...
well just focus on the simplest parts first so we can see some other things well need clearly
at this point i would get the pen out or loosen up the knuckls
either draw on paper or write out the responsibility's of each class, as we start to make them up
i.e. what it should be responsible for
even more importantly
what it should Not be responsible for
one good and fast way to organize everything, is to write it out at the class level as you code
in comments those responsibilitys
add and delete class's by some clear responsibility
you don't want one class to be responsible for everything
so, its actually better to first say, what it is not responsible for clearly ... "is" ... "is not"
for example
i would organize it like so before i even started really coding
so that i know my game plan makes sense
Code:
public class Card{
// this class is primarily responsible for individual card stats
// descriptions special stats individual to the card drawings
// it maybe responsible for printing out its basic stats or drawing them ect...
// this class in Not responsible for dealing with groups of cards in any way
// feilds
private int attack =0;
ect...
// constructor
public Card(string name,int attack, int defense,.....){}
// field properties
public int Attack{get;set;}
ect...
// calculation methods and or properties
public int GetAttackDeffenseSum(){ return attack + defense;}
}
from that point i would make a player class and define its responsibilitys
in my estimation that would be responsible for maintaining a list of cards
and performing operations on groups of cards such as
public class Player{
// this class will not be responsible for major interactions between other instances of this class except...
// in the case of static PlayerMethods but these should be avoided preferably so the class doesn't get to cluttered
// we should write a seperate class for that and think of a name ...
// this class will be responsible for maintaining lists of cards and operating on groups of them primarily
// this class would be responsible for adding cards to a list and removing them
// it would also be responsible for averaging and holding fields that relate to players decks stats
List<Card> playercard_list = new List<Card>();
// there might be multiple fields that are similar such as all_cards_total_attack , unused_cards_total_attack ect...
int all_cards_total_attack =0
int unused_cards_total_attack =0;
float all_cards_average_attack =0f;
// ect...
// it would also be responsible for
// sorting them into new lists for running calculations conditionally such as holding unused card lists ect...
// the methods might be explicit in nature or general , accepting lists and just returning a stat
// such methods would be inside more specific methods that set the variables ect...
public float GetAverageAttack (List <Card> cards)
{
float result =0f;
float sum =0;
float divisor=0;
foreach(Card c in Cards){ sum += c.Attack; divisor ++;}
return (float)(sum/divisor);
}
// you might have methods that are more specific that rely on these generalized methods
public void SetDeckAverageAttack()
{
all_cards_average_attack = GetAverageAttack(playercard_list);
List <Card> unusedlist = new List<Card>();
foreach(Cards c in playercard_list){ if(c.unused){unusedlist.Add(c);} }
unused_cards_average_attack = GetAverageAttack(unusedlist);
}
// you may have have other methods that do things based on many of the most simple or general methods
// we should consider who's responsibility should it be to print or draw display a players data
// that maybe should in fact be the player class itself, in c# every class basically gets a ToString()
// in your case you might have a Print() method or methods in the player class itself that might
// might print to the console
public void Print(){
Console.WriteLine(
"Player: "+ name +
"number of cards in deck "+ playercard_list.Count +
"avg attack power of deck ect... ect..."
);
}
}
// beyond that you might have a class that compares players stats
// it might have its own calculation methods and its own print or display methods
// in much the same way the player class did only one that compares the different players feilds
public class PlayerDeckComparitor{
// responsible for accepting players and comparing their feilds or returned statistics
// this is not responsible for holding players permanantly turns or anything else
// this may end up a static class just to make sure i adhear to my own rule strictly
}
// obviously you will need a game class that chains it all together
// this is equivalent to your program class with main in it
// as it becomes more complex you will make other classes that are called from program or game
//
// you want to also focus on how you want the end result in game class to work
public class Game{
// here we want game to function smoothly almost in human readable format
// to say here we might have simple yet long functional names
// that are quite automated and have many overloads
// so we make simple calls to class's that do all the work
// those classes we built from the simpler class's
// by the time we get here should be able to do amazing things like so
public void Execute()
{
// game running along
// ....
PlayerStatusDisplay();
// ....
}
public void PlayerStatusDisplay()
{
player[0].ShowBreakDownOfPlayersCurrentSituation( OutPut.Options.ToConsole );
player[1].ShowBreakDownOfPlayersCurrentSituation( OutPut.Options.ToConsole );
PlayerDeckComparitor.OutputSituationalDeckComparision(player[0],player[1], OutPut.Options.ToConsole);
}
}
doing it this way you can browse over your strategy and say humm maybe player should be deck
maybe every player can have different decks ect maybe the deck is just one thing a player class is responsible for
when you are done with that you apply the same process for the methods in the class's and ask
what sort of methods is a class like deck need to fulfill its responsibility's
what sort of methods will be expected needed to be available by other class's
does this method belong in the class is the class responisble for this type of action/method
so this is basically a very quick powerful way to rough draft out a application
Last edited by willmotil; June 13th, 2014 at 02:54 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|