ClassNotes
ClassNotes
------------------------------
"extern" can be used to have a variable declared in a header file and have access
to it in the .c file. (super global variable)
In general, everything in a header file should be extern.
HW 1.04:
--------
4 possible personalities of monsters:
1. smart
2. telepathic
3. tunnelling
4. eratic
hardcode individual monsters with personalities and test it out, then once
everything works, randomize it.
They also have a speed in the range(5, 20);
PC ( player character ) speed is always 10.
Example:
Monster | Speed | Next turn
---------------------------
@ | 10 | 0
F | 10 | 0
7 | 5 | 0
9 | 20 | 0
PC goes first - pull it out of heap, pc's turn is its current turn +
1000/speed. So it's next turn is going to be 100.
Pull next thing (F) - do the same as above
Loop through all monsters taking the lowest value of Next turn and doing
the above. PC always starts first then it's the lowest turn value
Then move 9 again. because it's the smallest. Then it'll be 100, so move up to
the lowest turn value at the highest point ( PC ).
When we decide where the monster moves, it is going to check the surrounding
blocks value from dykstras algorithm and randomly decide where to move
Visualization:
2 2 2 2 2
2 1 1 1 2
2 1 F 1 2
2 1 1 1 2
2 2 2 2 2
After each time the PC moves, the gradient has to update (run dykstras again to
get the updated values)
currently the attacker will always win, and monsters can kill eachother.
basically they just take over each other.
"remove top thing in the heap, do a move based on what was removed, then add
the after move monsters back onto the heap"
Instructor reccomends:
Character Struct {
PC Struct (pointer)
NPC Struct (pointer)
} // One of theses will have a value and the other will have a NULL (this just
tells whether or not the Character is the PC or NPC)
NEW MATERIAL:
-------------
Representation:
Basically just a rehash of what we learned in CS 321 and CE 281 with number
representation in binary
Bitwise Operators:
AND - ( logical AND is && Bitwise AND is & )
takes 2 operands and compairs then bit by bit, and does a logical AND
on those bits:
EX:
1 0 1 1
1 1 0 1 &
---------
1 0 0 1
OR - ( Logical OR is || Bitwise OR is | )
Works the same as AND except for OR
Exclusive OR - ( Bitwise is ^ )
NOT - (Bitwise is ~)
Reverse every bit
BitFields:
Allows us to manipulate numbers as though they are arrays of boolean
values.
Example:
# define NPC_SMART 0x00000001
# define NPC_TELEPATH 0x00000002
Example:
10 | 20 | 40 = 70 ( 0111 0000 )
0000 0001
0111 0000 &
-----------
0000 0000 So it doesn't have any of these characteristics
0000 0001
0101 0101 &
-----------
0000 0001 So it has whatever thing that is... kinda lost
Integer version:
----------------
void insertion_sort(int *a, int n){
int i, j, t; // t = temporary
Test code:
----------
End Test
--------
- Stdlib has something identical called qsort which does the same thing as
our generic_insertion_sort
- qsort_r - Thread safe version
-----------------------------------------------------------------------------------
---------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
---------------------------------------------------------------------------------
#include <stdio.h>
//
-----------------------------------------------------------------------------------
------------------------
// Concatination
#define concatinate(x, y) (x ## y) // Concatinates x and y
together
// Don't use this unless necessary
// Stringification?
#define to_string(s) #s // turns the thing to a string
// how is it useful? lookup tables.... I guess
int main(){
printf("%d \n", FOO); // Will print "5"
printf("%d \n", min(3, 5 )); // Should print the lowest
number... 3
printf("%f \n", min(8.777, 7.5)); // Prints 7.5
//printf("%d \n", min(foo(x), bar(y))); // Bad because
it'll run the functions several times, and could possibly not return the same thing
everytime
printf("%d \n", max(foo(x), bar(y))); // this fixes the
whole "bad because blah blah blah"
-----------------------------------------------------------------------------------
---------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
---------------------------------------------------------------------------------
HW 04:
------
- PC doesnt have to move?
- only going to check some for some tunnelling monsters and some not
tunnelling... if i cant get anything else working
New Notes:
----------
Exam in a week and 2 days.
Keywords:
---------
- auto
everything is auto by default, using this is very rare, dont
worry about it.
- consts
Makes the variable immutable so it cannot be changed
- extern
compiler: dont allocate space "here" allocate space somewhere
else. There is just a variable somewhere
- static
scope doesn't change
lifetime becomes the lifetime of the program
it's perfectly safe to return the address of a static variable
in a function
think "Private"
- register
ex: register int i;
hint to compiler that we intend to use this variable a lot so
map this into a register instead of in memory.
not guaranteed to work, but if it works, the program will run
much faster
- violate
ex: violate int i;
tells the compiler that this value could change outside of the
control of normal program flow
useful for multithreaded programming
- inline
optimization. only for functions, Similar to function-type
macros. puts function body inside caller without function call.
another compiler hint, doesn't necessary work every time
- goto
unconditional jump
Generally DO NOT USE
however, sometimes it can be okay
GDB:
----
"call" whatever - runs the whatever code during debugging so we can
test stuff
Extras:
-------
- compile with g++
- standard files dont have ".h"
- tons of file extensions, typically use .cpp
- can compile with gcc but add -lstdc++
- const is used a shit-ton in C++, however const is SLIGHTLY different,
use this kind of like a define, so cannot change at all, no hackery
- volatile: tell compiler "dont optimize this variable" so setting
const volatile i = 3 then try to change with hackery works.
- References:
-------------
- why do references exist? : they are "syntactic sugar"
- use the address operator in a declaration
- Must initialize immediately
- int &r = i;
- after this r and i are 2 names for the exact same thing
- references are pointers that you dont have to dereference
impliment a couple of swap functions:
-------------------------------------
#include <iostream>
tmp = *a;
*a = *b;
*b = tmp;
}// C style swap
tmp = a;
a = b;
b = tmp;
}// C++ style swap
int *p;
c_swap(x,y);
cpp_swap(x,y);
return 0;
}
#include <stdio.h>
printf("Hello world\n");
return 0;
} // exact same as C
// Now the same thing but the C++ way... HOWEVER, both work
prefectly in C++
#include <iostream>
// using namespace std; // Use this or use std::cout... however
using namespace std; is bad form because you can have more namespace collisions
int main(int argc, char *argv[]){
return 0;
}
Next Assignment:
----------------
port to c++:
------------
- change extensions
- change makefile
- fix errors
Fog of War:
-----------
- remember what we have seen
- if something changes we wont know (ex. monster moves into a room that
we have left)
- create another map of the dungeon that is waht the PC has seen.
- so when we render the dungeon we render the remembered terrain map
instead of the pc map
- Instructors notes:
------------------
- Need a new map "remembered terrain", which holds our terrain
which the PC has seen.
- The remembered terrain map is rendered by the game
- display like 3 unit's up, down, left, right, diagonal (ie. square
line of site)
- dont need to remember where monsters are (causes some issues, much
easier to not remember monsters)
- DO NOT HAVE TO UPDATE monster list
-
Teleport:
---------
- my gosh, figure this out too
i = f.get();
cout << i << endl; // output is the character value of l: 108
(ascii value of l)
cout << (char)i << endl; // output the character: l
cout << (char) f.get() << endl; // prints the next value in the
file.
string s;
getline(f, s);
cout << s << endl; // prints the next line
vector<int> v;
vector<int>::iterator it;
return 0;
}
line 5
line 7
*/
Misc Notes:
-----------
unsigned char; <--\
signed char; <---|---- none of these are the same thing
char; <--/
Class Notes:
------------
- Basically we just implemented our own version of String with operators n
shit
- can confirm, python is better.
- going over throw - catch and null handling. His notes suck but they cover all
of this.
- didnt give extension, savage