Thread not printing to curses screen correctly - c

I have an assignment where I need to make a game using threads and the curses library. I have mostly finished the program, but the player icon is not getting printed to the curses screen even though the function should be doing that. The other functionality of the function is working, it's just that nothing is being printed. For example, when I press q the program will terminate like it is supposed to.
Here is the function itself:
void *run_rocket( void *rocket ) {
object_t * rocket1 = (object_t *) rocket;
int c;
int row = rocket1->row;
mvaddch( rocket1->row, rocket1->col, rock );
curse_refresh();
while( running ){
c = getchar();
if( c == KEY_UP ){
row = row - 1;
rocket1->row = row;
mvaddch( row+1, rocket1->col, ' ' );
mvaddch( row, rocket1->col, rock );
} else if( c == KEY_DOWN ){
if( row!=bheight ){
row = row + 1;
rocket1->row = row;
mvaddch( row-1, rocket1->col, ' ' );
mvaddch( row, rocket1->col, rock );
}
} else if( c == 'q' ){
running = false;
}
if( row == 0 ){
running = false;
int hgt = bheight/2;
mvprintw( hgt, 0, "Congratulations! You won!");
mvprintw( (hgt - 1), 0, "Press 'q' to exit.");
}
curse_refresh();
}
destroy_rocket(rocket);
return NULL;
}
and this is how I start the thread
pthread_t rocket;
check = pthread_create( &rocket, NULL, run_rocket, obj_rock );
if( check != 0 ){
return 1;
}
I have only tried small changes because I have no idea what the problem, since I have another function that calls mvaddch the same way and that one works.

Related

Print the last word of String inside an array with only one loop

The program gets me out of the loop, it shows me in check that it is coming to NULL
Although it should continue to advance to the following letters in the string.
Thanks to all the assistants
void main()
{
char string[2][10] = { "lior","king" };
int words, letter;
for (words=0,letter = 0;words<2 , string[words][letter] != NULL;)
{
letter++;
if (string[words][letter] = NULL)
{
printf("%c\n", string[words][letter - 1]);
words++;
}
}
}
The ambition is that when it reaches the end of the first word, it will print the first letter and advance to the next string
This condition in the loop
words<2 , string[words][letter] != NULL;
is wrong. It seems you mean just
words<2
The first statement in the body pf the loop
letter++;
is also wrong because you skipped the index 0.
If I have understood correctly what you need is the following
#include <stdio.h>
int main(void)
{
enum { N = 10 };
char string[][N] = { "lior","king" };
const size_t M = sizeof( string ) / sizeof( *string );
for ( size_t word = 0, letter = 0; word < M; )
{
if (string[word][letter] == '\0' )
{
if ( letter != 0 ) printf( "%c\n", string[word][letter - 1] );
letter = 0;
++word;
}
else
{
++letter;
}
}
return 0;
}
The program output is
r
g

3D arrays and member functions to check winner to 3D tic-tac-toe

first post here. I'm creating a 3D tic-tac-toe c++ program. My checkWin member function is causing problems. I am curious as to why the test rows nested for loops do not give a winner when the if statement has been satisfied but the test colums and diagonals work fine for layer0. Thanks
void TicTacToe::checkWin(void) {
char returnValue = ' ';
//test for X or O win
//test rows, does not work
for (int k=0; k<=2; k++){
for (int i=0; i<=2; i++){
if ( (space[i][0][k] == space[i][1][k]) && (space[i][0][k] == space[i][2][k]) ){
returnValue = space[i][0][k];
}
}
}
int k=0; //test layer 0 only
//test columns
if ( (space[0][0][k] == space[1][0][k]) && (space[0][0][k] == space[2][0][k]) )
returnValue = space[0][0][k];
else if ( (space[0][1][k] == space[1][1][k]) && (space[0][1][k] == space[2][1][k]) )
returnValue = space[0][1][k];
else if ( (space[0][2][k] == space[1][2][k]) && (space[0][2][k] == space[2][2][k]) )
returnValue = space[0][2][k];
//test diagonals
else if ( (space[0][0][k] == space[1][1][k]) && (space[0][0][k] == space[2][2][k]) )
returnValue = space[0][0][k];
else if ( (space[0][2][k] == space[1][1][k]) && (space[0][2][k] == space[2][0][k]) )
returnValue = space[0][2][k];
if ( returnValue == ' ' ) { //then test for cat
int i, j, k, catCheck=0;
for (i=0; i<=2; i++) {
for (j=0; j<=2; j++) {
for (k=0; k<=2; k++) {
if (space[i][j][k] == ' ' )
catCheck++;
}
}
}
if ( catCheck == 0 )
returnValue = 'C';
}
winner = returnValue;
}
Probably because you should break out of that loop when you find a winner (all three equal to eachother, but not equal to ' '). Even if you find a winner first, if an empty set of three occurs after that, it satisfies the condition but stores a blank in returnvalue, overwriting the previously found winning x or o. Or you could add ... != ' ' to your condition there, because you really don't care if all three are empty at that point. Stepping through a debugger or adding temporary diagnostics print outs could really help you here.
Just a guess at a glance.

end loop based on input from user

My program accepts a number from the user to determine the length of a sequence being recorded. How would i take that number and also let it determine the number of times this loop is performed. While(true) obviously does not allow the loop to end at all.
thanks in advance
here is the function to synthesise sound from midi input
void midisound (int note)
{
int velocity;
int playingNote = -1;
float frequency;
while(true)
{
note = aserveGetNote();
velocity = aserveGetVelocity();
if(velocity > 0)
{
frequency = 440 * pow(2, (note-69) / 12.0);
aserveOscillator(0, frequency, 1.0, 0);
playingNote = note;
}
else if(note == playingNote)
{
aserveOscillator(0, 0, 0, 0);
}
}
}
---here is where function ^ is called in the program----
if (reclayer == 1)
{
//open first text file for layer 1 to be written to
textFilePointer = fopen("recording1.txt", "w+");
if(textFilePointer == NULL)
{
printf("Error Opening File!");
}
else
{
//function call to write notes and vel data
notetofile(input, seqlen, reclayer);
printf("Would you like to record a second layer or re-record? (y or n)\n");
scanf(" %c", &choice2);
}
}
Use a for loop.
for ( i = 0; i < note; i++ ) {
// Your code here
}
This will execute 'note' number of times.
Use can use the scanf() function to input a user number from the console:
void midisound (int note) {
int input;
int velocity;
int playingNote = -1;
float frequency;
printf("Enter integer number of times to loop: ");
scanf("%d", &input);
while(input > 0) {
input = input - 1;
note = aserveGetNote();
velocity = aserveGetVelocity();
if(velocity > 0) {
frequency = 440 * pow(2, (note-69) / 12.0);
aserveOscillator(0, frequency, 1.0, 0);
playingNote = note;
} else if(note == playingNote) {
aserveOscillator(0, 0, 0, 0);
}
}
}
In that first get the number input from the user number of time loop will be run the use following
for ( i=0 ; i < max ; i++ )
First assign a variable which holds the number of times you want to repeat it. For example , let us take int n = 5;. ( You can also make the user input the value of n ( as that is what you have asked ) by calling scanf( "%d " , &n ); and then do the rest )
int n ;
scanf( "%d " , &n );
You can use this as common for all of my below cases
Then, just add a simple for loop, such as
int i;
for ( i = 0 ; i < n ; i++ )
{
// The code that you want to repeat
}
That should do the trick.
In case you want to use a while loop, then as before, let n be the number of times the loop must execute. Then
int i=0;
while ( i < n )
{
// Your code
i++;
}
You can also use the while ( true ) loop as well, but you will just have to give a condition and use break;. Let us take an example as above.
int i=0;
while ( true )
{
// your code
i++;
if ( i == n )
break;
}
These are just many of the different possibilities. You can even come up with your own condition if you try.
Happy coding .... 8-)

Comparing substring of a character array with another character array in C

I have two characters arrays called arraypi and arraye containing numbers that I read from a file. Each have 1,000,000 characters. I need to start from the first character in arraye (In this case, 7) and search for it in arraypi. If 7 exists in arraypi then I have to search for the next substring of arraye(in this case, 71). Then search for 718, 7182 and so on until the substring does not exist in arraypi. Then I have to simply put the length of the biggest substring in a integer variable and print it.
Worth mentioning that arraypi contains a newline every 50 characters whereas arraye contains a newline every 80 although I don't think that will be problem right?
I tried thinking about a way to accomplish this but so far I haven't thought of something.
I am not absolutely sure if I got this right. I have something like this on my mind:
Assume that we have the whole arraypi is in a browser
You use the key combination ctrl+f for find
Start typing the contents of arraye letter by letter until you see the red no match
You want the number of characters you were able to type until then
If that's right, then an algorithm like the following should do the trick:
#include <stdio.h>
#define iswhitespace(X) ((X) == '\n' || (X) == ' ' || (X) == '\t')
int main( ) {
char e[1000] = "somet\n\nhing";
char pi[1000] = "some other t\nhing\t som\neth\n\ning";
int longestlen = 0;
int longestx = 0;
int pix = 0;
int ex = 0;
int piwhitespace = 0; // <-- added
int ewhitespace = 0; // <-- these
while ( pix + ex + piwhitespace < 1000 ) {
// added the following 4 lines to make it whitespace insensitive
while ( iswhitespace(e[ex + ewhitespace]) )
ewhitespace++;
while ( iswhitespace(pi[pix + ex + piwhitespace]) )
piwhitespace++;
if ( e[ex + ewhitespace] != '\0' && pi[pix + ex + piwhitespace] != '\0' && pi[pix + ex + piwhitespace] == e[ex + ewhitespace] ) {
// the following 4 lines are for obtaining correct longestx value
if ( ex == 0 ) {
pix += piwhitespace;
piwhitespace = 0;
}
ex++;
}
else {
if ( ex > longestlen ) {
longestlen = ex;
longestx = pix;
}
pix += piwhitespace + 1;
piwhitespace = 0;
// the two lines above could be replaced with
// pix++;
// and it would work just fine, the injection is unnecessary here
ex = 0;
ewhitespace = 0;
}
}
printf( "Longest sqn is %d chars long starting at %d", longestlen, longestx + 1 );
putchar( 10 );
return 0;
}
What's happening there is, the loop searches for a starting point for match first. Until it finds a match, it increments the index for the array being examined. When it finds a starting point, it then starts incrementing the index for the array containing the search term, keeping the other index constant.
Until a next mismatch, which is when a record-check is made, search term index is reset and examinee index starts getting incremented once again.
I hope this helps, somehow, hopefully more than resolving this single-time struggle.
Edit:
Changed the code to disregard white space characters.
Okay, since you apparently weren't really wanting this for arrays, but rather for two files with text inside, here's an appropriate solution to achieve that:
#include <stdio.h>
#define iswhitespace(X) ((X) == '\n' || (X) == ' ' || (X) == '\t')
int main( ) {
FILE * e;
FILE * pi;
if ( ( e = fopen( "e", "r" ) ) == NULL ) {
printf( "failure at line %d\n", __LINE__ );
return -1;
}
if ( ( pi = fopen( "pi", "r" ) ) == NULL ) {
printf( "failure at line %d\n", __LINE__ );
return -1;
}
int curre = fgetc( e );
int currpi = fgetc( pi );
int currentlength = 0;
int longestlength = 0;
int longestindex = 0;
int whitespaces = 0;
fpos_t startpoint;
if ( curre == EOF || currpi == EOF ) {
printf( "either one of the files are empty\n" );
return -1;
}
while ( 1 ) {
while ( iswhitespace( currpi ) )
currpi = fgetc( pi );
while ( iswhitespace( curre ) )
curre = fgetc( e );
if ( curre == currpi && currpi != EOF ) {
if ( currentlength == 0 && fgetpos( pi, &startpoint ) ) {
printf( "failure at line %d\n", __LINE__ );
return -1;
}
currentlength++;
curre = fgetc( e );
}
else if ( currentlength != 0 ) {
if ( currentlength > longestlength ) {
longestlength = currentlength;
longestindex = startpoint;
}
if ( curre == EOF ) {
printf( "Complete match!\n" );
break;
}
fsetpos( pi, &startpoint );
rewind( e );
curre = fgetc( e );
currentlength = 0;
}
if ( currpi == EOF )
break;
currpi = fgetc( pi );
}
printf( "Longest sequence is %d characters long starting at %d",
longestlength, longestindex );
putchar( 10 );
return 0;
}
It searches for a starting point, stores that starting point to return back to after determining the length of the current match. Determines the length of the current match, disregarding the whitespace on the way. Updates the record length if necessary, completely rewinds the search term file, partially-rewinds the examinee file back to the stored position.
Here's my e file:
somet
hing
And here is my pi file:
some other nhing som
eth
ing
And here's the output I get:
Complete match!
Longest sequence is 9 characters long starting at 20
By the way, fread and fwrite do not function humanly intuitive, as far as I remember. You can think of it like, computer uses a language that it itself understands while issuing those functions.
You can use strstr() function.Consider using it in a loop with return string as one of the argument.

Use of kbhit() with while loop

Here is the program.
void main( )
{
int h, v;
h = 1; v = 10;
while ( !kbhit( ) || h <= 80 )
{
gotoxy( h, v );
printf( "<--->" );
delay( 200 );
clrscr( );
h = h + 1;
}
getch( );
}
I am making a program in C, in which I have used kbhit() to run a loop until a key is pressed.
so here the arrow "<--->" will keep on moving forward until a key is pressed or until it reaches the last pixel of the screen.
What I want is that the program should increment h by 1 everytime 'd' is pressed and decrement by 1 everytime 'a' is pressed. i.e h++; and h--;
and run another loop until a character is pressed.
The idea is more like the Snake game, in which the snake keeps on moving in a certain direction until a key is pressed. Help please!
clrscr() should come before the gotoxy and printf
Anyway, what I would do is create a state variable, just to indicate the direction the snake should go, i.e., something that stores if the user pressed 'a' or 'd'.
And I would not leave the loop, just use a if(kbhit) and get the char.
int direction = 1; char control;
while (1)
{
if(kbhit()){
control = getch();
switch (control){
case 'a': direction = -1; break;
case 'd': direction = +1; break;
default: break;
}
}
clrscr( );
gotoxy( h, v );
printf( "<--->" );
delay( 200 );
h = h + direction;
}

Resources