I have a project which is to design a came called Miensfeld.
In Miensfeld, the player, Timmy, is located on one side of a 8 by 10 cell area minefield. My task is to move Timmy safely through the minefield to get to the other side, earning points as he moves:

[IMG]http://sphotos.ak.fbcdn.net/hphotos-ak-ash1/hs316.ash1/27958_1427876422044_1386835711_1152539_8060903_n.jpg[/IMG]

* Timmy is located in the first box at the top left corner

To move around in miensfeld, I would need to use the keyboard. I can move Timmy in any of the 8 directions from his current position by using the 8 keys surrounding the 'j' key:

[IMG]http://sphotos.ak.fbcdn.net/hphotos-ak-sjc1/hs276.snc3/27958_1427876462045_1386835711_1152540_4231101_n.jpg[/IMG]

So far, I've created the display module consisting of the game's layout (grid).

/* File:   grid.c */

/* This program prints the grid for the minefield */
#include <stdio.h>
#include "rows.h"
#include "tfdef.h"

/*
#define DEBUG
*/

main()
{
/* Declare variables */
int row = 1;
int count = 0;

while(row <= 23)
	{
	/* Prints the first three tabs before each line */
	printf("\t\t\t");

	/* Check if it is a blue line */
	if(row==1||row==2||row==4||row==5||row==7||row==8||row==10||row==11||row==13||row==14||row==16||row==17||row==19||row==20||row==22||row==23)
	{
	blue_row();
	if(row==2) printf("   MINES");
	if(row==16) printf("   SCORE");
	}
	
	/* Check if it is a pink line */
	if(row==3||row==6||row==9||row==12||row==15||row==18||row==21)
	{
	pink_row();
	if(row==9) printf("   FLAGS");
	}

	/* Prints a new line after each line */
	printf("\n");

	/* Increase the row by one */
	row++;
	}
}
/* File:   rows.h */

#define BLUE(c) (((c)==1)||((c)==2)||((c)==4)||((c)==5)||((c)==7)||((c)==8)||((c)==10)||((c)==11)||((c)==13)||((c)==14)||((c)==16)||((c)==17)||((c)==19)||((c)==20)||((c)==22)||((c)==23))

#define PINK(c) (((c)==3)||((c)==6)||((c)==9)||((c)==12||((c)==15)||((c)==18)||((c)==21))


/* This function prints out the blue row */
void blue_row(void);
/* Given: nothing
 * Returns: nothing
 */

/* This function prints out the pink row */
void pink_row(void);
/* Given: nothing
 * Returns: nothing
 */
/* File:   rows.c */

/* This program prints out a first attempt of the blue 
 * and pink rows. The rows are categorized as either
 * blue, pink, green, or orange to differentiate
 * between the different types of output it will produce. */

#include <stdio.h>
#include "rows.h"
#include "tfdef.h"

#define DEBUG


void blue_row(void)
{
/* Declare variables */
int count=0;

/* Print the tabs and '|' characters */
while(count < 10)
	{
	/* Print the three spaces */
	printf("   ");

	/* If less than 9 tabs have been 
 	   printed print the '|' character */
	if(count < 9)
	printf("|");

	/* Increase the count by one */
	count++;
	}
return;
}




void pink_row(void)
{
/* Declare variable */
int count = 0;

while(count < 10)
	{
	/* Print the three '-' characters */
	printf("---");

	/* If less than 9 '---' characters have been printed */
	if(count < 9)
	printf("|");

	/* Increase count by one */
	count++;
	}
return;
}
/* File: tfdef.h */

/* Header file defining true and false macros */

#define FALSE 0
#define TRUE  1

I am confused as to how I could get Timmy to move about in the minefield. My idea is to create an array and use if-else statements for each of the letters used to navigate Timmy, to move him about, over the grid. However, I have a feeling this would not work due to the format of the grid. Does anyone have any tips or suggestions? What would be the easiest way to do this?

Quick question, what is the relevance of the pink rows and blue rows ?

Also I am not sure I follow the explanation of the problem. Does it happen that when you display the grid the location of the mines are visible or are they hidden like the game minesweeper ?

Quick question, what is the relevance of the pink rows and blue rows ?

Also I am not sure I follow the explanation of the problem. Does it happen that when you display the grid the location of the mines are visible or are they hidden like the game minesweeper ?

They are hidden like the game minesweeper.
The problem is more detailed on this link:
http://www-ee.eng.hawaii.edu/~tep/EE160/S10/Hw/Proj/finalproject.html

The part that I am working on and need help with is "Moving Around"

OK, let's go with "moving around". ;)

If your compiler has the header file "conio.h", then the problem is trivial.

This program shows how to pick up the scan codes from a keyboard
These define the scan codes(IBM) for the keys. All numbers are in decimal.

#include <stdio.h>
#include <conio.h>

#define ESC 27
#define F1 59
#define F2 60
#define F3 61
#define F4 62
#define F5 63
#define F6 64
#define F7 65
#define F8 66
#define F9 67
#define F10 68
#define HOME 71
#define UP 72
#define PAGE_UP 73
#define LEFT 75
#define RIGHT 77
#define END 79
#define DOWN 80
#define PAGE_DOWN 81

int main(void) {

  char key;
  char msg[20];
  printf("\n\n\t\t\t     press escape to quit\n\n") ;
  do {
    key = getch();
    if (key == 0) {
      key = getch(); //key code has two keys - read the second one
      switch (key) {
        case F1: memcpy(msg,"F1", sizeof(msg)); break;
        case F2: memcpy(msg,"F2", sizeof(msg)); break;
        case F3: memcpy(msg,"F3", sizeof(msg)); break;
        case F4: memcpy(msg,"F4", sizeof(msg)); break;
        case F5: memcpy(msg,"F5", sizeof(msg)); break;
        case F6: memcpy(msg,"F6", sizeof(msg)); break;
        case F7: memcpy(msg,"F7", sizeof(msg)); break;
        case F8: memcpy(msg,"F8", sizeof(msg)); break;
        case F9: memcpy(msg,"F9", sizeof(msg)); break;
        case F10: memcpy(msg,"F10", sizeof(msg)); break;
        case PAGE_UP: memcpy(msg,"PAGE UP", sizeof(msg)); break;
        case PAGE_DOWN: memcpy(msg,"PAGE DOWN", sizeof(msg)); break;
        case HOME: memcpy(msg,"HOME", sizeof(msg)); break;
        case END: memcpy(msg,"END", sizeof(msg)); break;
        case UP: memcpy(msg,"UP", sizeof(msg)); break;
        case DOWN: memcpy(msg,"DOWN", sizeof(msg)); break;
        case LEFT: memcpy(msg,"LEFT", sizeof(msg)); break;
        case RIGHT: memcpy(msg,"RIGHT", sizeof(msg)); break;
        default:  memcpy(msg,"unknown key", sizeof(msg)); break;
      }
      printf("\n Key: %s", msg);
      continue;
    }
    if(key == ESC)
      printf("\n Key: ESCAPE");
    else 
      printf("\n Key: %c", key);
   
  }while (key != ESC); 

  printf("\n\n\t\t\t     press enter when ready");
  key = getchar();  //hold the console window open
  return 0;
}

If you don't have "conio.h", then you need to either use another library like Ncurses or Curses, or use the Windows, API, or use any of several GUI libraries.

Let's look at the Windows API. You can test a single key at a time, or the keyboard as a whole. A skeleton example, in C++:

#include <iostream>
#include <windows.h>

int main() {

//These are the four arrow keys virtual key designations:
//the table of all keys is at: 
http://msdn.microsoft.com/en-us/library/dd375731%28v=VS.85%29.aspx

  if (GetAsyncKeyState (VK_LEFT) 

  if (GetAsyncKeyState (VK_RIGHT)

  if (GetAsyncKeyState (VK_UP) 

  if (GetAsyncKeyState (VK_DOWN)

  return 0;
}
/* between each if(), you need to add your code to handle the
movement you wanted when the key is pressed

The best tutorial I've found for it is here:
http://www.mpgh.net/forum/31-c-c/120656-proper-use-getasynckeystate.html

I tried compiling, but I got an error:

scan.c: In function ‘main’:
scan.c:34: warning: incompatible implicit declaration of built-in function ‘memcpy’

What is this memcy function?

I tried compiling, but I got an error:

scan.c: In function ‘main’:
scan.c:34: warning: incompatible implicit declaration of built-in function ‘memcpy’

What is this memcy function?

Sorry for that. My compiler has a "smart" feature which will add in parts of other header files, if it see's that they're needed.

Add this line:

#include <string.h>

to the top of the program.

memcpy() copies a bit of memory from one address or variable, to another.

This article has been dead for over six months. Start a new discussion instead.