I have completed the code to run the box inside of game. It is called Guess the right number! which means you can select a number from between 0 to 10, if it is not right number it will show message to try again, otherwise to appears a message you are winner with choices quit or restart. When I try to use gcc
to compile it but it looks like I need to create a makefile
as I was told but I have never done to make makefile for a single file or a second file would be called answer
?
I have been doing C programming for 4 months so far. My program name is game.c
to create Game
(./Game
) to start a program (game). How can I write a makefile for this?
I have no idea what is "curses" so I wanted to learn how to use with curses (<ncurses.h>
) for myself when I did a lot of research so far I don't see the different betweens curses and ncurses. I have created the compile error after I use gcc
in the below.
Snippet:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ncurses.h>
#include <time.h>
// PREDEFINED VALUES FOR DEFINING NON CHANGING VALUES IN CODE THIS CASE
#define WINDOWHEIGHT 20
#define WINDOWWIDTH 60
#define WINDOWSTARTX 0
#define WINDOWSTARTY 0
#define CORRECT 1
#define INCORRECT 0
#define START 2
#define WRONGFORMAT 3
#define MAXVALUE 10//You may change MAXVALUE to any num, i.e. `100` = (0-100).
#define MINVALUE 0
// PREDEFINED VALUES FOR DEFINING NON CHANGING VALUES IN THIS CASE
// initialising global structure for saving amount of right and wrong guesses and number to compare with.
struct game {
int rightGuesses;
int wrongGuesses;
int rightNumber;
} myGame;
void initializeGame()
{
// Returns a pseudo-random integer between 0 and MAXVALUE.
int randomNumber = rand() % MAXVALUE;
myGame.rightGuesses=0;
myGame.rightNumber=randomNumber;
}
WINDOW *create_newwin(int height, int width, int starty, int startx)
{
WINDOW *local_win;
local_win = newwin(height, width, starty, startx);
box(local_win, 0, 0);
wrefresh(local_win);
return local_win;
}
int getGuess()
{
int guess=0;
char guessString[32];
scanf("%s", guessString);
// Read number as string by using scanf, but convert to int for comparison with atoi()
guess= atoi(guessString);
size_t allowedEntries = strspn(guessString, "0123456789");
// Some checking if guess was between allowed range + its a number + checking if answer is correct or not
if(guess>=MINVALUE && guess<=MAXVALUE && guessString[allowedEntries] == '\0')
{
if(guess==myGame.rightNumber)
return CORRECT;
else
return INCORRECT;
}
else
return WRONGFORMAT;
}
/**
Function for updating views regarding the input values...
**/
void updateWindowTexts(WINDOW* window, int state)
{
char* greetingsString = "Guess the correct number!";
char* instructionsString = "Enter number 0-10 and press enter";
char* correctGuess = "That was correct! Lets play again";
char* incorrectGuess = "Sorry that was not right";
char* wrongFormat = "incorrect number, please enter number between 0-10";
char* correctAnswersString = "Correct answers:";
char correctAnswers[32];
char wrongAnswers[32];
const char rightAnswersBase[] = "Right numbers so far: ";
sprintf(correctAnswers, "%s%d", rightAnswersBase, myGame.rightGuesses);
const char wrongAnswersBase[] = "Wrong numbers so far: ";
sprintf(wrongAnswers, "%s%d", wrongAnswersBase, myGame.wrongGuesses);
wclear(window);
box (window, 0, 0);
mvwprintw (window, 1, (WINDOWWIDTH/2)-(strlen(greetingsString)/2), greetingsString);
mvwprintw (window, (WINDOWHEIGHT-3), (WINDOWWIDTH/2)-(strlen(correctAnswers)/2), correctAnswers);
mvwprintw (window, (WINDOWHEIGHT-2), (WINDOWWIDTH/2)-(strlen(wrongAnswers)/2), wrongAnswers);
mvwprintw (window, (WINDOWHEIGHT/2), (WINDOWWIDTH/2)-(strlen(instructionsString)/2), instructionsString);
switch (state) {
case START:
break;
case CORRECT:
mvwprintw (window, WINDOWHEIGHT-5, (WINDOWWIDTH/2)-(strlen(correctGuess)/2), correctGuess);
myGame.rightGuesses++;
break;
case INCORRECT:
mvwprintw (window, (WINDOWHEIGHT-5), (WINDOWWIDTH/2)-(strlen(incorrectGuess)/2), incorrectGuess);
myGame.wrongGuesses++;
break;
case WRONGFORMAT:
mvwprintw (window, (WINDOWHEIGHT-5), (WINDOWWIDTH/2)-(strlen(wrongFormat)/2), wrongFormat);
break;
}
wrefresh (window);
}
int main (int argc, char **argv)
{
WINDOW *my_win;
initscr();
// Here we call crea_newwin to make new window, paremeters are static and defined at the top of file
// You can try to play with these numbers
my_win = create_newwin(WINDOWHEIGHT, WINDOWWIDTH, WINDOWSTARTY, WINDOWSTARTX);
// Initialization of random generator, should only be called once.
srand(time(NULL));
initializeGame();
// Update window once before enteringing loop
updateWindowTexts(my_win,START);
while(1)
{
updateWindowTexts(my_win,getGuess());
}
return 0;
}
gcc version:
/u1/stuff/C/projectFinal> gcc game.c
/usr/bin/ld: /tmp/ccOy8YhK.o: in function `create_newwin':
game.c:(.text+0x73): undefined reference to `newwin'
/usr/bin/ld: game.c:(.text+0xa8): undefined reference to `wborder'
/usr/bin/ld: game.c:(.text+0xb8): undefined reference to `wrefresh'
/usr/bin/ld: /tmp/ccOy8YhK.o: in function `updateWindowTexts':
game.c:(.text+0x251): undefined reference to `wclear'
/usr/bin/ld: game.c:(.text+0x285): undefined reference to `wborder'
/usr/bin/ld: game.c:(.text+0x2c5): undefined reference to `mvwprintw'
/usr/bin/ld: game.c:(.text+0x301): undefined reference to `mvwprintw'
/usr/bin/ld: game.c:(.text+0x33d): undefined reference to `mvwprintw'
/usr/bin/ld: game.c:(.text+0x379): undefined reference to `mvwprintw'
/usr/bin/ld: game.c:(.text+0x3f4): undefined reference to `mvwprintw'
/usr/bin/ld: /tmp/ccOy8YhK.o:game.c:(.text+0x444): more undefined references to `mvwprintw' follow
/usr/bin/ld: /tmp/ccOy8YhK.o: in function `updateWindowTexts':
game.c:(.text+0x4a3): undefined reference to `wrefresh'
/usr/bin/ld: /tmp/ccOy8YhK.o: in function `main':
game.c:(.text+0x4ba): undefined reference to `initscr'
collect2: error: ld returned 1 exit status