170 likes | 280 Views
Writing a GamesCrafters Game in C. Monday, Sept. 22, 2003 Judy Chen. What we’ll cover today. Quick introduction to C Hash Functions Sample game (Snake). Local variables have to be at the top No Boolean type Structures /* … */ printf(“Foo is %d and bar is %f<br>”, 5, 3.5);
E N D
Writing a GamesCrafters Game in C Monday, Sept. 22, 2003 Judy Chen
What we’ll cover today • Quick introduction to C • Hash Functions • Sample game (Snake)
Local variables have to be at the top No Boolean type Structures /* … */ printf(“Foo is %d and bar is %f\n”, 5, 3.5); for loops syntax variables aren’t initialized to anything anywhere Classes /* ... */ or // System.out.print C versus Java
Arrays • C syntax for declaring an array is slightly different: int myarray[5]; int yourarray[] = { 1, 2, 3, 4, 5 }; • Accessing stuff in arrays is the same: yourarray[2] = ? yourarray[2] = 3 • Be careful b/c array bounds aren’t checked!
Pointers • Pointers represent raw memory addresses. x y int *x, y; ? ? x y y = 5; ? 5 Note that x still hasn’t been initialized.
Pointers (cont’d) • Use the & operator to get the address of a variable. x y x = &y; 5 • Use the * operator to dereference a pointer. x y *x = 7; 7
GamesCrafters • Download the gamesman package for Unix. As of right now, there is no version for Windows. http://www.cs.berkeley.edu/~ddgarcia/research/gametheory/current • The GamesCrafters website has the 10-step tutorial for writing a game module. http://www.cs.berkeley.edu/~ddgarcia/software/gamesman
Hash Functions • A hash function takes your board position and converts it into a number. • Each board position will have a unique number that it is hashed into. • You’ll need a hash function and an unhash function, which will take a number and give you the board position.
Hashing Tic Tac Toe • Ex: Tic Tac Toe 0 for a blank square 1 for an X 2 for an O
The Makefile • There are 4 places where you need to add your game. • Pick one of the current games (i.e. TTT) and every time you see that game, copy and paste it and change TTT to your game.
The Makefile: change #1 • TTT_EXE = $(BINDIR)/mttt TTT_OBJ = mttt.o TTT_SO = $(LIBDIR)/libmttt.so
The Makefile: change #2 • text_all: ${1210_EXE} ${TTT_EXE} ... so_all: ${1210_SO} ${TTT_SO} ...
The Makefile: change #3 • ${TTT_OBJ} : ${SOLVER_INCLUDE} ${TTT_EXE}: ${TTT_OBJ} ... ${TTT_SO}: ${TTT_OBJ} ...
The Makefile: change #4 • At the very end of the makefile... clean: rm –f ${1210_EXE} ${1210_OBJ} ... minimal: rm –f ${1210_OBJ} ${TTT_OBJ} ...
Compiling and running • Log on to rhombus.cs (the most lenient compiler) • Add your mgame.c code the gamesman/src directory > cd gamesman/src > make to compile > cd ../bin > ./mgame to execute
Functions you’ll have to write • GameSpecificMenu (maybe.. if you want to add Variants) • GetInitialPosition • DoMove • Primitive • PrintPosition • GenerateMoves • Hash and Unhash