Professional Documents
Culture Documents
Report About Sokoban Game Design Project
Report About Sokoban Game Design Project
Report About Sokoban Game Design Project
Implementation
Game.h
#include <stdio.h>
#include <windows.h>
#include <stdlib.h>
HANDLE console;
CONSOLE_CURSOR_INFO cursor;
COORD coord;
DWORD chars_to_write = 0;
#endif
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <termios.h>
#include <unistd.h>
#endif
void InitScreen()
{
// print directly to screen buffer
#ifdef WIN32
console = CreateConsoleScreenBuffer(GENERIC_READ | GENERIC_WRITE, 0, NULL,
CONSOLE_TEXTMODE_BUFFER, NULL);
cursor.dwSize = 100;
cursor.bVisible = FALSE;
coord.X = 0;
coord.Y = 0;
SetConsoleActiveScreenBuffer(console);
SetConsoleCursorInfo(console, &cursor);
#endif
// use ANSI VT100 escape sequences hide cursor and clear screen
#ifdef unix
printf("\x1b[?25l");
printf("\x1b[2J");
#endif
}
void RefreshScreen()
{
// might not be needed on windows natively, needed on wine though
for(int scr_cell = 0; scr_cell < SCREEN_SIZE; scr_cell++)
if(!screen[scr_cell]) screen[scr_cell] = ' ';
void PrintMap(int pos_x, int pos_y, int map_width, int map_height, char *map)
{
for (int row = 0; row < map_height; row++)
{
for (int col = 0; col < map_width; col++)
{
screen[(row + pos_y) * SCREEN_WIDTH + col + pos_x] = map[row * map_width +
col];
}
}
RefreshScreen();
}
GetConsoleMode(console, &mode);
SetConsoleMode(console, mode & ~(ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT));
int key = 0;
ReadConsole(console, &key, 1, &chars_to_read, NULL);
SetConsoleMode(console, mode);
return key;
}
#endif
int getch()
{
struct termios oldattr, newattr;
tcgetattr( STDIN_FILENO, &oldattr );
newattr = oldattr;
newattr.c_lflag &= ~( ICANON | ECHO );
tcsetattr( STDIN_FILENO, TCSANOW, &newattr );
int key = getchar();
tcsetattr( STDIN_FILENO, TCSANOW, &oldattr );
return key;
}
#endif
void Leave()
{
#ifdef WIN32
cursor.bVisible = TRUE;
SetConsoleCursorInfo(console, &cursor);
#endif
Sokoban-game.c
#include "game.h"
#define MAP_WIDTH 8
#define MAP_HEIGHT 10
#define PLAYER_POSITION pos_y * MAP_WIDTH + pos_x
char map[] = {
"##### "
"#xB ### "
"### # "
"#x@B # "
"### Bx# "
"#x##B # "
"# # x ##"
"#B OBBx#"
"# x #"
"########"
};
for(int row = 0; row < MAP_HEIGHT; row++) // loop ower map rows
{
for(int col = 0; col < MAP_WIDTH; col++) // loop ower map columns
{
cell = row * MAP_WIDTH + col; // init current cell index
for(int row = 0; row < MAP_HEIGHT; row++) // loop ower map rows
{
for(int col = 0; col < MAP_WIDTH; col++) // loop ower map columns
{
cell = row * MAP_WIDTH + col; // init current cell index
else
{
map[PLAYER_POSITION - offset] = '@'; // if box hits the wall or
another box
return; // don't push it any further
}
while(1)
{
if(key == 27) break;
key = getch();
GetPosition(&pos_x, &pos_y);
switch(key)
{
case 'w': MoveCharacter(pos_x, pos_y, - MAP_WIDTH); break;
case 's': MoveCharacter(pos_x, pos_y, MAP_WIDTH); break;
case 'a': MoveCharacter(pos_x, pos_y, - 1); break;
case 'd': MoveCharacter(pos_x, pos_y, 1); break;
}
Leave();
return 0;
}
Sokoban.c
#include <stdio.h>
#include <string.h>
#define MAP_WIDTH 8
#define MAP_HEIGHT 9
#define PLAYER_POSITION pos_y * MAP_WIDTH + pos_x
char map[] = {
};
/*#define MAP_WIDTH 14
#define MAP_HEIGHT 10
#define PLAYER_POSITION pos_y * MAP_WIDTH + pos_x
char map[] = {
"##############\n"
"# # xB #\n"
"# x # xB #\n"
"# B # xB #\n"
"# #### #\n"
"# @ # #\n"
"# # #\n"
"# B # #\n"
"# x #\n"
"##############\n"
};*/
for(int row = 0; row < MAP_HEIGHT; row++) // loop ower map rows
{
for(int col = 0; col < MAP_WIDTH; col++) // loop ower map columns
{
cell = row * MAP_WIDTH + col; // init current cell index
for(int row = 0; row < MAP_HEIGHT; row++) // loop ower map rows
{
for(int col = 0; col < MAP_WIDTH; col++) // loop ower map columns
{
cell = row * MAP_WIDTH + col; // init current cell index
else
{
map[PLAYER_POSITION - offset] = '@'; // if box hits the wall or
another box
return; // don't push it any further
}
map[PLAYER_POSITION] = '@'; // draw the player in the new
position
}
int main()
{
int key; // user input key
int pos_x, pos_y; // player's coordinates
int dest_count; // 'x' cells counter
return 0;
}
Relationship between Modules
The game is linked between two files, the first of which is the game file, which is a
file that I use to link it to the other file called sokoban - game, which I mainly use in
the main file of the game that contains the main