Week 1 Lecture 3
Week 1 Lecture 3
Toyota 18 11 15 17 2
Nissan 23 19 12 16 14
BMW 7 12 16 0 2
Audi 3 5 6 2 1
Solution
In C++, we initialized the 2D array as follows.
int cars[5][5] = {{10, 7, 12, 10, 4}, int [,] cars = {{10, 7, 12, 10, 4},
{18, 11, 15, 17, 2}, {18, 11, 15, 17, 2},
{23, 19, 12, 16, 14}, {23, 19, 12, 16, 14},
{7, 12, 16, 0, 2}, {7, 12, 16, 0, 2},
{3, 5, 6, 2, 1} {3, 5, 6, 2, 1}
}; };
Solution
In C#, if we just want to declare the 2D array then
it is as follows.
Toyota 18 11 15 17 2
Nissan 23 19 12 16 14
BMW 7 12 16 0 2
Audi 3 5 6 2 1
Accessing the Elements
In C++, we accessed the elements of the 2D array as
follows.
cars[0][2];
Toyota 18 11 15 17 2
Nissan 23 19 12 16 14
BMW 7 12 16 0 2
Audi 3 5 6 2 1
Accessing the Elements
In C#, we access the elements of the 2D array as
follows.
cars[0][2]; cars[0,2];
Toyota 18 11 15 17 2
Nissan 23 19 12 16 14
BMW 7 12 16 0 2
Audi 3 5 6 2 1
Working Example: Vision
Write a Function that returns the sum of all the
colors of all the cars.
Toyota 18 11 15 17 2
Nissan 23 19 12 16 14
BMW 7 12 16 0 2
Audi 3 5 6 2 1
Solution
static void Main(string[] args)
static int printSum(int [,] cars) {
{ int [,] cars = {
int sum = 0; { 10, 7, 12, 10, 4},
for (int x = 0; x < 5; x++) { 18, 11, 15, 17, 2},
{ { 23, 19, 12, 16, 14},
for (int y = 0; y < 5; y++) { 7, 12, 16, 0, 2},
{ { 3, 5, 6, 2, 1}
sum = sum + cars[x, y]; };
} int sum;
} sum = printSum(cars);
return sum; Console.WriteLine("Sum is: {0}", sum);
} Console.Read();
}
Game Development
in C#
PacMan Game
PacMan Game: Maze
char[,] maze = new char[10,10] {
{ '%', '%', '%', '%', '%', '%', '%', '%', '%', '%'},
{ '%', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '%'},
{ '%', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '%'},
{ '%', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '%'},
{ '%', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '%'},
{ '%', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '%'},
{ '%', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '%'},
{ '%', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '%'},
{ '%', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '%'},
{ '%', '%', '%', '%', '%', '%', '%', '%', '%', '%'}
};
Solution C++
In C++, we used gotoxy(x,y)
To display something on the specific location on the console.
void movePacmanRight()
{
if (maze[pacmanX][pacmanY + 1] == ' ' || maze[pacmanX][pacmanY + 1] == '.')
{
maze[pacmanX][pacmanY] = ' ';
gotoxy(pacmanY, pacmanX);
cout << " ";
pacmanY = pacmanY + 1;
gotoxy(pacmanY, pacmanX);
cout << "P";
}
}
Solution C#: MovePacManUp
In C#, we will use Console.SetCursorPosition(x,y)
To display something on the specific location on the console.
static void movePacManUp(char[,] maze, ref int pacmanX, ref int pacmanY)
{
if (maze[pacmanX - 1,pacmanY] == ' ' || maze[pacmanX - 1,pacmanY] == '.')
{
maze[pacmanX,pacmanY] = ' ';
Console.SetCursorPosition(pacmanY, pacmanX);
Console.Write(" ");
pacmanX = pacmanX - 1;
Console.SetCursorPosition(pacmanY, pacmanX);
Console.Write("P");
}
}
Solution C#: MovePacManDown
In C#, we will use Console.SetCursorPosition(x,y)
To display something on the specific location on the console.
static void movePacManDown(char[,] maze, ref int pacmanX, ref int pacmanY)
{
if (maze[pacmanX + 1,pacmanY] == ' ' || maze[pacmanX + 1,pacmanY] == '.')
{
maze[pacmanX,pacmanY] = ' ';
Console.SetCursorPosition(pacmanY, pacmanX);
Console.Write(" ");
pacmanX = pacmanX + 1;
Console.SetCursorPosition(pacmanY, pacmanX);
Console.Write("P");
}
}
Solution C#: MovePacManLeft
In C#, we will use Console.SetCursorPosition(x,y)
To display something on the specific location on the console.
static void movePacManLeft(char[,] maze, ref int pacmanX, ref int pacmanY)
{
if (maze[pacmanX,pacmanY - 1] == ' ' || maze[pacmanX,pacmanY - 1] == '.')
{
maze[pacmanX,pacmanY] = ' ';
Console.SetCursorPosition(pacmanY, pacmanX);
Console.Write(" ");
pacmanY = pacmanY - 1;
Console.SetCursorPosition(pacmanY, pacmanX);
Console.Write("P");
}
}
Solution C#: MovePacManRight
In C#, we will use Console.SetCursorPosition(x,y)
To display something on the specific location on the console.
static void movePacManRight(char[,] maze, ref int pacmanX, ref int pacmanY)
{
if (maze[pacmanX,pacmanY + 1] == ' ' || maze[pacmanX,pacmanY + 1] == '.')
{
maze[pacmanX,pacmanY] = ' ';
Console.SetCursorPosition(pacmanY, pacmanX);
Console.Write(" ");
pacmanY = pacmanY + 1;
Console.SetCursorPosition(pacmanY, pacmanX);
Console.Write("P");
}
}
Solution C#: MovePacManRight
In C#, we will use Console.SetCursorPosition(x,y)
To display something on the specific location on the console.
static void movePacManRight(char[,] maze, ref int pacmanX, ref int pacmanY)
{
if (maze[pacmanX,pacmanY + 1] == ' ' || maze[pacmanX,pacmanY + 1] == '.')
{
maze[pacmanX,pacmanY] = ' '; Important thing to note here
Console.SetCursorPosition(pacmanY, pacmanX); is that we are passing the X
Console.Write(" "); and Y coordinates of Pacman
pacmanY = pacmanY + 1;
by reference to this
Console.SetCursorPosition(pacmanY, pacmanX);
function, so the changes are
Console.Write("P");
done in the single variable.
}
}
main(){
Solution C#
{
int pacmanX = 4; int pacmanY = 4;
char[,] maze = new char[10,10] {
{ '%', '%', '%', '%', '%', '%', '%', '%', '%', '%'},
{ '%', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '%'},
{ '%', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '%'},
{ '%', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '%'},
{ '%', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '%'},
{ '%', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '%'},
{ '%', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '%'},
{ '%', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '%'},
{ '%', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '%'},
{ '%', '%', '%', '%', '%', '%', '%', '%', '%', '%'}};
printMaze(maze);
Console.SetCursorPosition(pacmanY, pacmanX);
Console.Write("P");
while (true){
Thread.Sleep(150);
In C#, we will use if (Keyboard.IsKeyPressed(Key.UpArrow)){
movePacManUp(maze, ref pacmanX, ref pacmanY);
Keyboard.IsKeyPressed(Key.UpArrow) }
if (Keyboard.IsKeyPressed(Key.DownArrow)){
To detect if the key is }
movePacManDown(maze, ref pacmanX, ref pacmanY);
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using EZInput;
EZInput Package
In order to use the function
Keyboard.IsKeyPressed(Key.UpArrow)
We have to install and include the EZInput package
first.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using EZInput;
EZInput Package
In order to install the Package, right click on the
references in Solution Explorer Window
EZInput Package
Then Click on the Manage NuGet Packages.
EZInput Package
Then Click on the Manage NuGet Packages.
EZInput Package
Search EZInput in the browser and install the
package.
EZInput Package
And then include the EZInput package using the
following instruction.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using EZInput;
Learning Objective
Fun fact:
for most probability matrices M (for example, if M has
no zero entries), the matrix powers M^n converge (as n
increases) to a matrix where all rows are identical.
Self Assessment
Test Cases:
[ true
[0.5, 0.5, 0.0],
[0.2, 0.5, 0.3],
[0.1, 0.2, 0.7]
]
isProbMatrix()