-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
155 lines (121 loc) · 4.46 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
using System;
using System.IO;
namespace Skiing_Amongst_Trees
{
/* holds our character array of trees and the number of rows and columns in that array*/
class DataNode
{
private char[,] dataArray;
private int numberRows;
private int numberColumns;
public DataNode(char[,] array, int r, int c)
{
dataArray = array;
numberRows = r;
numberColumns = c;
}
public int getNumberRows()
{
return numberRows;
}
public int getNumberColumns()
{
return numberColumns;
}
public char[,] getDataArray()
{
return dataArray;
}
}
/* holds the number of hits and misses */
class hitMiss
{
private int hits = 0;
private int miss = 0;
//constructor
public hitMiss(int h, int m)
{
hits = h;
miss = m;
}
//Getters
public int getHits() => hits;
public int getMiss() => miss;
//Setters
public void updateHits(int h) => hits += h;
public void updateMiss(int m) => miss += m;
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
//Read from file into our dataMap array
var dataMap = readIntoArray(@"C:\Users\hojay\Downloads\SkiWorking\Skiing\Skiing_Amongst_Trees\TreeMap.txt");
int run = 3;
int rise = 2;
// call the ski function passing in the slope (over (run) 3 down (rise) 1 and the DataMap
hitMiss hmcount = ski(dataMap, run, rise);
//output the resutls to the viewer
Console.WriteLine("With a slope of over " + run + " down " + rise + ", we hit " + hmcount.getHits() + " trees and missed the other " + hmcount.getMiss() + " trees.");
}
public static DataNode readIntoArray(string filename)
{
//Open the file
var fileAccessor = new StreamReader(File.OpenRead(filename));
//Count the number of columns in our file
var line1 = fileAccessor.ReadLine();
var numCols = line1.Length;
//Count how many rows are in our file
int numRows = 1;
while (!fileAccessor.EndOfStream) // credit https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/arrays/multidimensional-arrays
{
//read the next line
fileAccessor.ReadLine();
numRows++;
}
//allocate our Char array
char[,] treeMap = new char[numRows, numCols];
//Read the file into our array.
fileAccessor.Close();
fileAccessor = new StreamReader(File.OpenRead(filename));
for (int r = 0; r < numRows; r++)
{
var line = fileAccessor.ReadLine();
//var charArrayLine = line.ToCharArray();
for (int c = 0; c < numCols; c++)
{
treeMap[r, c] = line[c];
}
}
//close the file
fileAccessor.Close();
//return our array
return new DataNode(treeMap, numRows, numCols);
}
/* ski accepts the data node, run, and rise to calculate how many times the skiier hits a tree */
public static hitMiss ski(DataNode dnode, int over, int down)
{
int hits = 0;
var miss = 0;
int colPosition = 0;
var numRows = dnode.getNumberRows();
var numCols = dnode.getNumberColumns();
char[,] data = dnode.getDataArray();
var hitMissCounts = new hitMiss(0, 0); // to count our number of hits and misses
for (int rowPosition = 0; rowPosition < numRows; rowPosition += down, colPosition += over)
{
//did we hit a tree?
if (data[rowPosition, (colPosition % numCols)] == '#')
{
hitMissCounts.updateHits(1);
}
else
{
hitMissCounts.updateMiss(1);
}
}
return hitMissCounts;
}
}
}