Jan072010 - UC
Santa
Cruz
...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: UC
Santa
Cruz
 Computer
Science
–
Game
Design
 Switch
statement
 switch (expression) { case constant-expression: statement(s); jump-statement [default: statement(s);] Example: const int raining = 1; const int snowing = 0; int weather = snowing; switch (weather) { case snowing: System.Console.Writeln(“It is snowing!”); goto case raining; case raining; System.Console.Writeln(“I am wet!”); break; default: System.Console.Writeln(“Weath er OK”); break; •  AlternaFve
to
if
 } •  Typically
use
break
 •  Can
use
goto
to
conFnue
to
another
case
 Adapted
from
Jim
Whitehead’s
slides
from
past
CMPS
20/80K
courses
at
UCSC
 UC
Santa
Cruz
 Computer
Science
–
Game
Design
 InformaFon
Hiding
in
OO
Languages
 •  Classes
support
informaFon
hiding
 –  Data
members
of
a
class
can
be
declared
as
private
 –  Create
accessor
methods
to
access
data
 •  Hides
implementaFon
details
of
the
class
 •  Typically
get__()
and
set__(),
which
are
public
 •  “GeUers
and
seUers”
 –  Other
classes
access
data
via
get()
and
set()
method
 –  But,
is
tedious
to
always
access
data
via
accessors
 •  So
long
as
the
interface
to
get
and
set
stay
the
same,
the
class
can
 change
how
it
represents
its
data
 •  InformaFon
hiding
permits
implementaFons
to
change
without
 affecFng
using
classes
 •  x
=
foo.getX()
is
more
tedious
than
x
=
foo.x;
 •  Wouldn’t
it
be
great
to
preserve
benefits
of
accessors,
while
also
 retaining
the
syntax
of
direct
access?
 Adapted
from
Jim
Whitehead’s
slides
from
past
CMPS
20/80K
courses
at
UCSC
 UC
Santa
Cruz
 Computer
Science
–
Game
Design
 C#
ProperFes
 •  Provide
procedural
access
to
data
 –  Like
accessors
 •  But
have
syntax
similar
to
direct
variable
 access
 –  foo.X
instead
of
foo.getX();
 •  Minor
feature,
but
provides
substanFal
 improvement
in
readability
and
fluidity
of
 programming
 Travis
thumbs
his
nose
at
private
 property
 www.flickr.com/photos/sillygwailo/492070136/
 Adapted
from
Jim
Whitehead’s
slides
from
past
CMPS
20/80K
courses
at
UCSC
 UC
Santa
Cruz
 Computer
Science
–
Game
Design
 C#
Property
Syntax
 [access-modifiers] return-type property-name { get { } set { } } … sequence of statements ending in a return (or throw) get
accessor
 … sequence of statements set
accessor
 •  Get
accessor
returns
value
of
same
type
as
“return
type”
 •  Set
accessors
have
implicit
parameter
named
“value”
 –  Use
to
set
internal
data
representaFon
 •  ProperFes
can
be
public,
private,
protected
 –  Public:
any
class
can
access,
private:
only
that
class,

 protected:
that
class
and
children

 •  By
convenFon,
property
names
have
iniFal
capital
(“X”
to
access
“x”)
 Adapted
from
Jim
Whitehead’s
slides
from
past
CMPS
20/80K
courses
at
UCSC
 UC
Santa
Cruz
 Computer
Science
–
Game
Design
 C#
Property
Example
 public class GameInfo { private string name; public string Name { get { return name; } set { name = value; } } // Test code GameInfo g = new GameInfo(); // Call set accessor g.Name = “Radiant Silvergun”; // Call get accessor System.Console.Write(g.Nam e); } Adapted
from
Jim
Whitehead’s
slides
from
past
CMPS
20/80K
courses
at
UCSC
 UC
Santa
Cruz
 Computer
Science
–
Game
Design
 AutomaFc
ProperFes
 •  Very
oken,
properFes
are
straighlorward
geUers
 and
seUers
 –  Get
accessor
just
reads
value
of
one
variable
 –  Set
accessor
just
writes
the
value
of
one
variable
 •  CreaFng
properFes
in
this
situaFon
is
very
 mechanical
 •  With
automa&c
proper&es,
the
compiler
creates
 these
straighlorward
get
and
set
accessors
 –  New
to
C#
3.0
 Adapted
from
Jim
Whitehead’s
slides
from
past
CMPS
20/80K
courses
at
UCSC
 UC
Santa
Cruz
 Computer
Science
–
Game
Design
 C#
AutomaFc
Property
Example
 public class GameInfo { public string Name {get; set;} } // Test code GameInfo g = new GameInfo(); // Call set accessor g.Name = “Radiant Silvergun”; // Call get accessor System.Console.Write(g.Nam e); This
property
behaves
the
same
as
the
first
property
example,
two
slides
ago.
 A
private
class
variable,
private String name,
is
automaFcally
created. Adapted
from
Jim
Whitehead’s
slides
from
past
CMPS
20/80K
courses
at
UCSC
 UC
Santa
Cruz
 Computer
Science
–
Game
Design
 Arrays
 •  Array
is
an
indexed
collecFon
of
objects –  Index
means
you
use
array[i]
syntax
to
access
members
 –  Recall
that
types
like
int,
string,
float,
etc.
are
objects
 –  Can
even
have
arrays
of
arrays
 •  Unlike
C++,
in
C#
array
is
an
object
 •  Arrays
have
many
useful
properFes
and
methods
 –  ProperFes:
 –  Methods:
 •  •  •  •  •  •  Length:
length
of
array
 •  Rank:
number
of
dimensions
of
array
 Sort():
sorts
values
in
one
dimensional
array
 BinarySearch():
searches
one
dimensional
array
 Clear():
sets
range
of
elements
to
0
or
null
reference
 Reverse():
reverses
elements
in
one
dimensional
array
 …
and
many
others
 Adapted
from
Jim
Whitehead’s
slides
from
past
CMPS
20/80K
courses
at
UCSC
 UC
Santa
Cruz
 Computer
Science
–
Game
Design
 Declaring
an
Array
 type array-name; Example: int numbers; numbers = new int[3]; numbers 0 0 0 Technically,
just
creates
a
variable
(numbers)
 that
will
hold
a
reference
to
an
array‐of‐ integers
object.
 Creates
an
array‐of‐integers
object,
of
length
 3,
which
are
iniFalized
to
0.
 •  Array
numbering
follows
C
convenFons
 –  First
element
is
numbers[0]
 –  Upper
bound
is
2,
so
3rd
element
is
numbers[2]
 Adapted
from
Jim
Whitehead’s
slides
from
past
CMPS
20/80K
courses
at
UCSC
 UC
Santa
Cruz
 Computer
Science
–
Game
Design
 public class GameInfo { public string gameName; } Arrays
of
Reference
Types
 GameInfo myGArray = new GameInfo[2]; myGArray null null •  CreaFng
a
“new”
array
of
a
reference
type
 –  Just
creates
a
series
of
null
references
 –  Need
to
assign
object
instances
to
array
elements
 Adapted
from
Jim
Whitehead’s
slides
from
past
CMPS
20/80K
courses
at
UCSC
 UC
Santa
Cruz
 Computer
Science
–
Game
Design
 Arrays
of
Reference
Types
(2)
 public class GameInfo { public string gameName; } GameInfo myGArray = new GameInfo[2]; GameInfo A = new GameInfo(); GameInfo B = new GameInfo(); myGArray[0] = A; myGArray[1] = B; A myGArray [0] [1] B GameInfo’1 gameName: null GameInfo’2 gameName: null There
are
only
two
instances
of
 class
GameInfo.
 There
are
four
reference
 variables
that
point
to
 GameInfo.
 A,
B
 myGArray[0]
 myGArray[1]
 Adapted
from
Jim
Whitehead’s
slides
from
past
CMPS
20/80K
courses
at
UCSC
 UC
Santa
Cruz
 Computer
Science
–
Game
Design
 IniFalizing
Arrays
 int anIntArray = new int[3] { 2, 4, 6 } OR int anIntArray = { 2, 4, 6 } •  Both
syntaxes
have
idenFcal
behavior
 •  Can
also
iniFalize
reference
types:
 string aStringArray = { “The”, “Quick”, “Brown”, “Fox” } AClass AClassArray = { new AClass(), new AClass(), new AClass() } Adapted
from
Jim
Whitehead’s
slides
from
past
CMPS
20/80K
courses
at
UCSC
 UC
Santa
Cruz
 Computer
Science
–
Game
Design
 Two
Dimensional
Arrays
 type [,] array-name int [,] myTwoDimArray = new int[2, 3]; •  Can
have
two
(and
more)
dimensional
arrays
 •  Also
possible
to
iniFalize
 –  Implicitly
sets
bounds
of
the
array
 // Create a 4 x 3 array int [,] myTwoDimArray = { { 0, 1, 2 }, {3, 4, 5}, {6, 7, 8}, {9, 10, 11} } Adapted
from
Jim
Whitehead’s
slides
from
past
CMPS
20/80K
courses
at
UCSC
 UC
Santa
Cruz
 Computer
Science
–
Game
Design
 SorFng
Arrays
 string aStringArray = { “Cherry”, “Apple”, “Banana”, “Peach” }; // Sort elements Array.Sort( aStringArray ); // Elements now: Apple, Banana, Cherry, Peach Array.Reverse( aStringArray ); // Elements now: Peach, Cherry, Banana, Apple •  Call
Array.Sort(),
passing
your
array
to
sort
a
one
 dimensional
array
 •  Call
Array.Reverse()
to
reverse
elements
 Adapted
from
Jim
Whitehead’s
slides
from
past
CMPS
20/80K
courses
at
UCSC
 UC
Santa
Cruz
 Computer
Science
–
Game
Design
 Looping
in
C#
 •  C#
has
four
looping
constructs
 –  for
 •  for
(j
=
0;
j
<
5;
j++)
{
…
}
 •  Classic
loop
syntax
in
C‐like
languages
 •  Possibility
of
off‐by‐one
errors
in
array
indexing
 –  foreach
 •  foreach
(int
j
in
intArray)

 •  Eliminates
array
indexing
errors,
no
need
to
create
index
variable
before
 statement
 –  while
 •  while
(j
<
5)
{
…;
j++;
}
 •  Loop
unFl
an
event
occurs
 –  do
…
while
 •  do
{…;
j++;}
while
(j
<
5)
 •  Uncommon,
perform
acFon,
then
do
condiFon
check
 Adapted
from
Jim
Whitehead’s
slides
from
past
CMPS
20/80K
courses
at
UCSC
 UC
Santa
Cruz
 Computer
Science
–
Game
Design
 Foreach
Statement
 foreach ( type identifier in array-or-collection ) {…} •  Iterates
through
all
elements
in
an
array,
or
collecFon
type
 •  Creates
idenFfier
just
for
the
scope
of
the
statements
inside
the
 foreach
 –  Holds
the
current
element
within
the
array/collecFon
 •  Very
convenient
when
it
can
be
used
 string aStringArray = { “Cherry”, “Apple”, “Banana”, “Peach” }; // Sort elements Array.Sort( aStringArray ); foreach (string s in aStringArray) System.Console.Write (“{0} : ”, s); // Output: “Apple : Banana : Cherry : Peach : ” Adapted
from
Jim
Whitehead’s
slides
from
past
CMPS
20/80K
courses
at
UCSC
 UC
Santa
Cruz
 Computer
Science
–
Game
Design
 List

 •  Arrays
have
problem
that
you
must
know
how
 many
elements
you
want
in
advance
 –  This
is
not
always
known
 •  List
class
is
collecFon
with
variable
size
 –  Dynamically
increases
in
size
if
needed
 –  When
an
array
reaches
its
capacity,
need
to
create
 new
array,
and
copy
all
elements
from
old
array
to
 new
array
 •  Ugh!
 Adapted
from
Jim
Whitehead’s
slides
from
past
CMPS
20/80K
courses
at
UCSC
 UC
Santa
Cruz
 Computer
Science
–
Game
Design
 CreaFng
a
List
 List<type>
listname Example: List<string>
stringList = new List<string>(); Don’t forget () stringList.Add ( “Quick” ); stringList.Add ( “Brown” ); stringList.Add ( “Fox” ); foreach (string s in myStringList) foreach System.Console.Write("{0} ", s); // Lists work with // Create list of string. •  •  •  •  •  Add
elements
with
Add()
method
 Clear()
removes
all
elements
from
list
 Remove()
removes
first
element
from
list
 Sort()
sorts
the
list
 Count
property:
number
of
elements
in
list
 Adapted
from
Jim
Whitehead’s
slides
from
past
CMPS
20/80K
courses
at
UCSC
 UC
Santa
Cruz
 Computer
Science
–
Game
Design
 Queue,
Stack,
DicFonary
 •  C#
provides
queue,
stack,
and
dicFonary
 •  Queue:
first‐in,
first‐out
 •  Stack:
last‐in,
first‐out
 •  Push(),
Pop(),
Peek()
 •  Enqueue(),
Dequeue(),
Peek()
 •  DicFonary
 –  Holds
set
of
key,
value
pairs
 –  Permits
lookup
of
a
value
given
the
key
 –  Example
use:
extensible
character
aUribute
system
 •  Keys:
strings,
names
of
aUribute
 •  Value:
int,
value
of
specific
aUribute
 Adapted
from
Jim
Whitehead’s
slides
from
past
CMPS
20/80K
courses
at
UCSC
 UC
Santa
Cruz
 Computer
Science
–
Game
Design
 Reading
 •  Chapter
4
(Classes
and
Objects)
 Chapter
9
(Arrays,
Indexers,
and
CollecFons)
 from
pp.
155‐176
 in
Programming C# 3.0 •  If
you
are
behind
in
the
reading,
you
need
to
 catch
up
fast.

 –  You
are
already
5‐10
hours
behind.
 –  It
is
hard
to
catch
up
aker
this
point

 Adapted
from
Jim
Whitehead’s
slides
from
past
CMPS
20/80K
courses
at
UCSC
 ...
View Full Document

This note was uploaded on 11/10/2010 for the course CMPS 20 taught by Professor Jhala,a during the Winter '10 term at University of California, Santa Cruz.

Ask a homework question - tutors are online