Wondering where the error is D:
Hey guys, I'm coding a function for a game somewhat like minesweeper and I have encountered a problem. How this function is suppose to work is the user gives it a movement command and if it is a valid move, the command is executed. I have a test driver to see if the coordinates are actually correct, problem is the coordinates are displayed twice when I hit a valid command. I cant seem to find why this is happening. Any help will be much appreciated#include "util.h"
Code:
/* file: driver */
#include "mine.h"
#include<stdio.h>
main()
{
char command;
int row = 0;
int col = 0;
printf("enter command(press q to quit):");
command = getchar();
while(command != 'q')
{
if(coordinate(command, &row, &col))
printf("new coordinates:[%d][%d]\n",row,col);
else
printf("invalid move\n");
command = getchar();
}
}
/*file: util.c */
#include "tfdef.h"
#include "mine.h"
#include<stdio.h>
int coordinate(char command, int *row, int *col)
{
switch(command)
{
case 'y':
case 'Y':
*row = *row - 1;
*col = *col - 1;
break;
case 'u':
case 'U':
*row = *row - 1;
break;
case 'i':
case 'I':
*row = *row - 1;
*col = *col + 1;
break;
case'h':
case'H':
*col = *col - 1;
break;
case 'k':
case 'K':
*col = *col + 1;
break;
case 'n':
case 'N':
*row = *row + 1;
*col = *col - 1;
break;
case 'm':
case 'M':
*row = *row + 1;
break;
case ',':
case '<':
*row = *row+1;
*col = *col+1;
break;
}
if(*row < START||*col < START||*row >= MAXR)
return FALSE;
else
return TRUE;