Download presentation
Presentation is loading. Please wait.
Published byPeregrine French Modified over 9 years ago
1
Writing a GamesCrafters Game in C Monday, Sept. 22, 2003 Judy Chen
2
What we’ll cover today Quick introduction to C Quick introduction to C Hash Functions Hash Functions Sample game (Snake) Sample game (Snake)
3
C versus Java Local variables have to be at the top Local variables have to be at the top No Boolean type No Boolean type Structures Structures /* … */ /* … */ printf(“Foo is %d and bar is %f\n”, 5, 3.5); printf(“Foo is %d and bar is %f\n”, 5, 3.5); for loops syntax for loops syntax variables aren’t initialized to anything variables aren’t initialized to anything anywhere Classes /*... */ or // System.out.print
4
Arrays C syntax for declaring an array is slightly different: C syntax for declaring an array is slightly different: int myarray[5]; int yourarray[] = { 1, 2, 3, 4, 5 }; int yourarray[] = { 1, 2, 3, 4, 5 }; Accessing stuff in arrays is the same: Accessing stuff in arrays is the same: yourarray[2] = ? yourarray[2] = ? yourarray[2] = 3 yourarray[2] = 3 Be careful b/c array bounds aren’t checked! Be careful b/c array bounds aren’t checked!
5
Pointers Pointers represent raw memory addresses. Pointers represent raw memory addresses. ?? int *x, y; y y = 5; x ?5 yx Note that x still hasn’t been initialized.
6
Pointers (cont’d) Use the & operator to get the address of a variable. Use the & operator to get the address of a variable. x = &y;5 yx *x = 7;7 yx Use the * operator to dereference a pointer. Use the * operator to dereference a pointer.
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 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 http://www.cs.berkeley.edu/~ddgarcia/ research/gametheory/current http://www.cs.berkeley.edu/~ddgarcia/ research/gametheory/current The GamesCrafters website has the 10- step tutorial for writing a game module. The GamesCrafters website has the 10- step tutorial for writing a game module. http://www.cs.berkeley.edu/~ddgarcia/ software/gamesman http://www.cs.berkeley.edu/~ddgarcia/ software/gamesman
8
Hash Functions A hash function takes your board position and converts it into a number. 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. 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. You’ll need a hash function and an unhash function, which will take a number and give you the board position.
9
Hashing Tic Tac Toe Ex: Tic Tac Toe Ex: Tic Tac Toe 0 for a blank square 0 for a blank square 1 for an X 1 for an X 2 for an O
10
The Makefile There are 4 places where you need to add your game. 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. 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.
11
The Makefile: change #1 TTT_EXE= $(BINDIR)/mttt TTT_EXE= $(BINDIR)/mttt TTT_OBJ= mttt.o TTT_SO = $(LIBDIR)/libmttt.so
12
The Makefile: change #2 text_all: ${1210_EXE} ${TTT_EXE}... text_all: ${1210_EXE} ${TTT_EXE}... so_all: ${1210_SO} ${TTT_SO}... so_all: ${1210_SO} ${TTT_SO}...
13
The Makefile: change #3 ${TTT_OBJ} :${SOLVER_INCLUDE} ${TTT_OBJ} :${SOLVER_INCLUDE} ${TTT_EXE}:${TTT_OBJ}... ${TTT_SO}:${TTT_OBJ}...
14
The Makefile: change #4 At the very end of the makefile... At the very end of the makefile...clean: rm –f ${1210_EXE} ${1210_OBJ}... minimal: rm –f ${1210_OBJ} ${TTT_OBJ}...
15
Compiling and running Log on to rhombus.cs (the most lenient compiler) Log on to rhombus.cs (the most lenient compiler) Add your mgame.c code the gamesman/src directory Add your mgame.c code the gamesman/src directory > cd gamesman/src > make to compile > cd../bin >./mgame to execute
16
Functions you’ll have to write GameSpecificMenu (maybe.. if you want to add Variants) GameSpecificMenu (maybe.. if you want to add Variants) GetInitialPosition GetInitialPosition DoMove DoMove Primitive Primitive PrintPosition PrintPosition GenerateMoves GenerateMoves Hash and Unhash Hash and Unhash
17
Questions?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.