I want to make game, like Tribal Wars, but in console and single player. I have Village class and there I have to make something, which contains building level and according to level is production of the building. I tried to do it with multidimension array, like building[level, production] but it haven't worked.
This is full code from class Village (Primary class is Program)
class Village
{
/*
private int mainBuild = 0;
private double[] mainBuildCost = new double[20];
private int woodCutter = 0;
private double[] woodCutterCost = new double[20];
private double[] woodCutterProd = new double[20];*/
public double[,] woodCutter = new double[20, 1];
private int wood = 50;
public Village()
{
woodCutter[0, 0] = 5;
for (int lvl = 1; lvl < woodCutter.GetLength(0); lvl++)
{
for (int prod = 1; prod <= woodCutter.GetLength(1); prod++)
{
woodCutter[lvl, 0] = Math.Round(woodCutter[lvl-1, prod-1])*1.3;
}
woodCutter[lvl, 0] = lvl;
Console.Write(woodCutter);
}
}
}
And yes, it's really but and doesn't work, but i have no idea, how do I make my plan.
I would do it differently. Your production here can be represented as a formula.
Here's a little example:
class Village
{
public double CurrentWood { get; private set; }
public int WoodLevel { get; private set; }
public double WoodProduction { get { return WoodLevel * 1.3; } } // Change the formula to your own
public void Update()
{
// Update all your resources in here
CurrentWood += Production;
}
}
Related
I want to read data from a file or an analog digital converter (ADC) and convert the data with the help of the dynamicdata library. Suppose I read blocks of 2d matrices of ushort numbers from this file or ADC.
In the example below I add a 2d matrix to the FileNode class and that is converted to an IObservableList<double[]> list. Now I want to connect this to the Data property of the SampledSignal class which is of type IObservableList<double>. Does anyone have any idea how I should do this?
public class SampledSignal
{
public SampledSignal(IObservableList<double> data, double sr)
{
Data = data;
Samplerate = sr;
}
public IObservableList<double> Data { get; private set; }
public double Samplerate { get; }
}
public class FileNode : ReactiveObject
{
readonly IObservable<IEnumerable<ushort[]>> _currentDataObsv;
const int _signalCount = 4;
readonly private SampledSignal[] _sampledSignals;
public FileNode()
{
_sampledSignals = Enumerable.Range(0, _signalCount)
.Select(s => new SampledSignal(null, 200)).ToArray();
CurrentData = Enumerable.Empty<ushort[]>();
_currentDataObsv = this.WhenAnyValue(x => x.CurrentData);
IObservableList<double[]> dblArList = _currentDataObsv
.ToObservableChangeSet()
.TransformMany(d => ToEnumerable(d))
.AsObservableList();
// The lines below are just some code to give an idea of what I want
for (int i = 0; i < _signalCount; i++)
{
// maybe here an observablelist should be created?
// IObservableList<double> data = ??
// _sampledSignals[i] = new SampledSignal(data, 200);
}
OutSignals=_sampledSignals;
}
public IEnumerable<double[]> ToEnumerable(ushort[] array)
{
double offset = 0;
double amp = 4;
int len = array.Length;
int cnt = 0;
double[] x = new double[len];
for (int i = 0; i < len; i++)
{
x[i] = Convert.ToDouble(array[i] - short.MaxValue) / ushort.MaxValue * amp + offset;
}
cnt++;
yield return x;
}
[Reactive] public IEnumerable<ushort[]> CurrentData { get; set; }
// maybe this property can also be an IObservableList<SampledSignal> OutSignals
public IEnumerable<SampledSignal> OutSignals { get; private set; }
}
[Fact]
public void Test_SampledData()
{
int signalcount = 20;
IEnumerable<ushort[]> array = Enumerable.Range(0, 4)
.Select(j =>
{
var x = new ushort[signalcount];
for (int i = 0; i < x.Length; i++)
{
x[i] = Convert.ToUInt16(short.MaxValue+(i+j)*4);
}
return x;
});
var filenode = new FileNode();
filenode.CurrentData = array;
// do some testing below
SampledSignal signal = filenode.OutSignals.First();
signal.Should().NotBeNull();
signal.Data.Count.Should().Be(signalcount);
}
I have created a solution that works but I don't like it very much because I have to create a SourceList class in the SampledData class.
I was hoping for a solution were I directly could set the IObservable<double> Data property of the SampledData class.
// The SampledSignal class now looks like this.
public class SampledSignal
{
private readonly ISourceList<double> _sourceList = new SourceList<double>();
public SampledSignal(IObservableList<double> data, double sr)
{
Data = _sourceList;
Samplerate = sr;
}
public ISourceList<double> Data { get; private set; }
public double Samplerate { get; }
}
// The FileNode constructor now looks like this
public FileNode()
{
_sampledSignals = Enumerable.Range(0, _signalCount)
.Select(s => new SampledSignal(null, 200)).ToArray();
CurrentData = Enumerable.Empty<ushort[]>();
_currentDataObsv = this.WhenAnyValue(x => x.CurrentData);
IObservableList<double[]> dblArList = _currentDataObsv
.ToObservableChangeSet()
.TransformMany(d => ToEnumerable(d))
.AsObservableList();
dblArList.Connect().ToCollection()
.Subscribe(sigs =>
{
int cnt = 0;
foreach (var sig in sigs)
{
var regdata = _sampledSignals[cnt++];
regdata.Data.Edit(innerlist =>
{
innerlist.Clear();
innerlist.AddRange(sig);
});
}
});
OutSignals=_sampledSignals;
}
i'm trying to convert a string from file to 2d array. Is it possible to make the array dynamic? And how can i return the array, so i can work with it? I'm really confused.
public interface IMazeLoader
{
Maze LoadMaze(string src);
}
class MazeLoader : IMazeLoader
{
public Maze LoadMaze(string filepath)
{
var width = 5;
var height = 5;
var map = new char[width, height];
var file = new StreamReader(filepath);
string line;
var lineCount = 0;
while ((line = file.ReadLine()) != null)
{
if (lineCount < height)
{
for (int i = 0; i < width && i < line.Length; i++)
{
map[i, lineCount] = line[i];
}
}
lineCount++;
}
file.Close();
return;
}
}
Here is my Maze class :
public class Maze
{
public static readonly char CHAR_WALL = '#';
public static readonly char CHAR_START = 'S';
public static readonly char CHAR_FINISH = 'F';
public int Width { get; internal set; }
public int Height { get; internal set; }
public int StartX { get; private set;}
public int StartY { get; private set; }
public int StartAngle { get; private set; }
private char[,] _plan;
public char[,] Plan { get { return _plan; } }
public Maze(char[,] plan)
{
}
Thanks for any reply, happy Eastern btw. :)
Something like this should allow you to load any rectangular dimension map:
public class MazeLoader : IMazeLoader
{
public Maze LoadMaze(string filepath)
{
char[][] loaded =
File
.ReadLines(filepath)
.Select(x => x.ToCharArray())
.ToArray();
int[] widths =
loaded
.Select(x => x.Length)
.Distinct()
.ToArray();
if (widths.Length != 1)
{
throw new InvalidDataException("Map Data Not Rectangular");
}
else
{
var width = widths[0];
var height = loaded.Length;
var map = new char[height, width];
for (int i = 0; i < height; i++)
for (int j = 0; j < width; j++)
map[i, j] = loaded[i][j];
return new Maze(map);
}
}
}
Now I tested this on this input:
aaaa
aaaa
bbbb
bbbb
bbbb
And got this map:
Please note that the file orientation and the map orientation are the same. I had to flip around how you were building our your map to get that to work.
Here's my alterations to Maze itself:
public class Maze
{
public int Width => this.Plan.GetUpperBound(1) - this.Plan.GetLowerBound(1) + 1;
public int Height => this.Plan.GetUpperBound(0) - this.Plan.GetLowerBound(0) + 1;
public char[,] Plan { get; private set; }
public Maze(char[,] plan)
{
this.Plan = plan;
}
}
This is throwing an error that EdgeList is not initialized. Here is the class (the pertinent part is way at the bottom):
public class TacLineStruct
{
// The number of unit groups in the Army
public int NumGroups
{
get
{
return _NumGroups;
}
set
{
_NumGroups = value;
}
}
private int _NumGroups;
// The number of edges,
public int NumEdges
{
get
{
return _NumEdges;
}
set
{
_NumEdges = value;
}
}
private int _NumEdges;
// The number of units below the threshold
public int NumBelowThreshold
{
get
{
return _NumBelowThreshold;
}
set
{
_NumBelowThreshold = value;
}
}
private int _NumBelowThreshold;
// The specific Group that a unit belongs to
public int[] GroupID
{
get;
set;
}
// The list of all the edges
public int[][] EdgeList
{
get;
set;
}
// The list of all the edge weights
public float[] EdgeWeight
{
get;
set;
}
// The geographical center of each group
public Point[] GroupCenter
{
get;
set;
}
public TacLineStruct(int arrayLength)
{
GroupID = new int[arrayLength];
int[,] EdgeList = new int[(arrayLength * arrayLength),2];
EdgeWeight = new float[arrayLength * arrayLength];
GroupCenter = new Point[arrayLength];
}
}
And this is how I'm calling and initializing it (snippet):
TacLineStruct TLS = new TacLineStruct(Army.Count);
for (int i = 0; i <= Army.Count; i++)
{
for (int j = i + 1; j <= Army.Count; j++)
{
TLS.EdgeList[NumEdges][0] = i; /* first vertex of edge */
TLS.EdgeList[NumEdges][1] = j; /* second vertex of edge */
// ...
}
}
I'm getting a runtime error that EdgeList is not initialized. My best guess is that I'm not doing something correctly with a 2D array with the length set at runtime.
In your constructor, you are doing:
int[,] EdgeList = new int[(arrayLength * arrayLength), 2];
which creates a new (local) variable with the same name as the field. Instead you should do:
this.EdgeList = new int[(arrayLength * arrayLength), 2];
You could omit the this, but it can prevent you from making this mistake again.
Further, you should change the field declaration to
public int[,] EdgeList
Then you can set individual fields in the array via:
EdgeList[i,j] = value;
I have created a class named Card and a class named CardDeck.
In class CardDeck I've declared an array named deck( of type Card ) whose elements are objects of class Card.
Its Card object has its own number and shape.
How can I compare for example deck[0] with deck[1] to see if these obects have the same number or the same shape?
Class CardDeck
using System;
public Class CardDeck
{
private int number_of_elements = 30;
public CardDeck()//constructor
{
int[] arrayNumber = {0,1,2,3,4,5,6,7,8,9};
string[] arrayShape = { "oval" , "diamond", "square" };
deck = new Card[number_of elements];
InitialiseDeck(arrauNumber, arrayShape);
}
private void InitialiseDeck(int[] num, string[] sha)
{
int count = 0;
for( int i = 0; i < 10; i++)
{
for(int j = 0; j < 3; j++)
{
deck[count] = new Card(num[i],sha[j]);
count++;
}
}
}
}
Class Card
using System;
public class Card
{
private int number;
private string shape;
public Card( int cardNumber, string cardShape)
{
number = cardNumber;
shape = cardShape;
}
}
You'll want to make public attributes on the Card class that expose the number and shape variables. Then in code outside the Card class reference those attributes.
For example:
public class Card
{
private int number;
private string shape;
public Card( int cardNumber, string cardShape)
{
number = cardNumber;
shape = cardShape;
}
public int Number { get { return this.number; } }
public string Shape { get { return this.shape; } }
}
Usage:
var card1 = new Card(13, "diamond");
var card2 = new Card(13, "heart");
if (card1.Number == card2.Number && card2.Shape == card2.Shape)
{
// The cards are the same
}
If all you want to do is check for equality of cards, then you can just declare what the == operator will do for the Card class: https://msdn.microsoft.com/en-us/library/8edha89s.aspx
using System;
namespace Matrix_Algebra
{
public struct S_Matrix_size
{
public int size_R, size_C;
public S_Matrix_size(int r, int c)
{
this.size_R = r;
this.size_C = c;
}
}
public class C_Matrix_entries
{
public C_Matrix_entries()
{
int r, c;
Console.WriteLine("Enter number of rows and columns ");
r = Convert.ToInt32(Console.ReadLine());
c = Convert.ToInt32(Console.ReadLine());
S_Matrix_size size = new S_Matrix_size(r,c);
double[,] entry = new double [size.size_R,size.size_C];
Console.WriteLine("Enter the entries from first row [left to right] to the last row ");
for (int i = 0; i<size.size_R; i++)
{
Console.WriteLine("Enter the {0} row", i + 1);
for (int j = 0; j<size.size_C;j++)
{
entry[i, j] = Convert.ToDouble(Console.ReadLine());
}
}
}
}
}
namespace Row_Reduce_Algebra
{
using Matrix_Algebra;
public class TEST
{
static void Main()
{
C_Matrix_entries matrix_no1 = new C_Matrix_entries();
for (int i = 0; i < **matrix_no1.size**; i++)
{
}
}
}
}
As the title says, I'm trying to access a variable from a class instance, but don't know how to do it properly.
You can't access matrix_no1.size because size is inaccessible.
Add a public property to your C_Matrix_entries class, and reference it in Main().
public class C_Matrix_entries
{
public S_Matrix_size size { get; private set; }
public C_Matrix_entries()
{
...
size = new S_Matrix_size(r,c);
As #GrantWinney rightfully pointed out (as I was shaping up a working reply for you), you cannot access matrix_no1.size because it is inaccessible. (It is also out of scope being that matrix_no1 is a local variable declared in the default C_Matrix_entries constructor.)
Based on your code, here is an end-to-end working example of how to fix the problem using a somewhat different public property added to C_Matrix_entries. Beyond the flavor of the new S_Matrix_size public property you add to C_Matrix_entries (i.e. Grant Winney's will work too), you will need to compute the product of its size_R and size_C properties in your loop's setup.
using System;
namespace Matrix_Algebra
{
public struct S_Matrix_size
{
public int size_R, size_C;
public S_Matrix_size(int r, int c)
{
this.size_R = r;
this.size_C = c;
}
}
public class C_Matrix_entries
{
private S_Matrix_size _size;
public C_Matrix_entries()
{
int r, c;
Console.WriteLine("Enter number of rows and columns ");
r = Convert.ToInt32(Console.ReadLine());
c = Convert.ToInt32(Console.ReadLine());
_size = new S_Matrix_size(r,c);
double[,] entry = new double [_size.size_R,_size.size_C];
Console.WriteLine("Enter the entries from first row [left to right] to the last row ");
for (int i = 0; i<_size.size_R; i++)
{
Console.WriteLine("Enter the {0} row", i + 1);
for (int j = 0; j<_size.size_C;j++)
{
entry[i, j] = Convert.ToDouble(Console.ReadLine());
}
}
}
public S_Matrix_size Size { get { return _size; } }
}
}
namespace Row_Reduce_Algebra
{
using Matrix_Algebra;
public class TEST
{
static void Main()
{
C_Matrix_entries matrix_no1 = new C_Matrix_entries();
for (int i = 0; i < matrix_no1.Size.size_R * matrix_no1.Size.size_C; i++)
{
// TODO: something useful
Console.WriteLine(i); // FORNOW
}
}
}
}