I am implementing simon says as a small weekly project for school. Using arduino Uno, I'm making 10 levels each level has an extra pattern inside. example: level 1: [1], level 2: [1,2], etc... I have three buttons on my shield. the interrupts work and everything is gucci. My problem here is in this snippet
bool readInput(uint8_t pattern[], uint8_t length)
{
sei();
uint8_t current = 0;
while (current < length)
{
btnPushed = false;
while (!btnPushed)
{
#ifdef DEBUG
_delay_ms(1);
#endif
}
printf("here");
cli();
_delay_ms(200);
if (currentPushed == pattern[current])
{
printf("correct, you pushed %d\n", currentPushed);
}
else
{
printf("incorrect, lets try again\n");
return false;
}
}
btnPushed = false;
return true;
}
so basically I set my buttonPushed to false, and start listening for interrupts, once its true after clicking, I expect to exit the loop and check the input, however my interrupt is correct and I get visual feedback with a light that lights up once i push a button.
this is my ISR
ISR(PCINT1_vect)
{
uint8_t buttonCurr = currentButton();
if (buttonCurr != -1)
{
if (!btn1Pushed && buttonCurr == 0)
{
btn1Pushed = true;
}
currentPushed = buttonCurr;
blinkLed(currentPushed, 1);
btnPushed = true;
}
}
my current button returns 0-2 for buttons that are clicked, and -1 if nothing was clicked.
this is the rest of my code, which is working pretty much
int currentPushed = -1;
bool won;
bool btnPushed = false;
bool btn1Pushed = false;
ISR(PCINT1_vect)
{
uint8_t buttonCurr = currentButton();
if (buttonCurr != -1)
{
if (!btn1Pushed && buttonCurr == 0)
{
btn1Pushed = true;
}
currentPushed = buttonCurr;
blinkLed(currentPushed, 1);
btnPushed = true;
}
}
int main(void)
{
enableAllButtons();
enableAllLeds();
lightDownAllLeds();
prepareButtonsForInterrupt();
initUSART();
init();
play();
if (won)
{
printf("Simon says you win");
}
else
{
printf("simon says do better");
}
return 0;
}
void init(void)
{
printf("LETS PLAY SIMON SAYS\nPress button 1 to start!\n");
int seed = 0;
while (!btn1Pushed)
{
blinkLed(3, 4);
seed++;
}
srand(seed);
printf("Get ready!\n");
btnPushed = false;
cli();
}
void createRandomPattern(uint8_t array[], uint8_t length)
{
for (int i = 0; i < length; i++)
{
array[i] = rand() % 3;
}
}
void play(uint8_t pattern[])
{
uint8_t fullPattern[MAX_PATTERN_LENGTH];
createRandomPattern(fullPattern, MAX_PATTERN_LENGTH);
for (int i = 1; i <= MAX_PATTERN_LENGTH; i++)
{
printf("========LEVEL %d===========\n", i);
playPuzzle(fullPattern, i);
#ifdef DEBUG
printPuzzle(fullPattern, i);
#endif
readInput(fullPattern, i) ?: i--;
}
}
bool readInput(uint8_t pattern[], uint8_t length)
{
sei();
uint8_t current = 0;
while (current < length)
{
btnPushed = false;
while (!btnPushed)
{
}
cli();
if (currentPushed == pattern[current])
{
printf("correct, you pushed %d\n", currentPushed);
}
else
{
printf("incorrect, lets try again\n");
return false;
}
current++;
}
btnPushed = false;
return true;
}
void printPuzzle(uint8_t pattern[], uint8_t length)
{
printf("[ ");
for (int i = 0; i < length; i++)
{
printf("%d ", pattern[i]);
}
printf("]\n");
}
void playPuzzle(uint8_t pattern[], uint8_t length)
{
for (int i = 0; i < length; i++)
{
lightUpOneLed(pattern[i]);
_delay_ms(500);
lightDownOneLed(pattern[i]);
}
}
btnPushed is defined as bool btnPushed = false;.
So when you write:
while (!btnPushed)
{
#ifdef DEBUG
_delay_ms(1);
#endif
}
Nothing in the loop will change btnPushed so there is no point for the compiler to ever check btnPushed again. So what the compiler sees is this:
if (!btnPushed)
while(true)
{
#ifdef DEBUG
_delay_ms(1);
#endif
}
You have to tell the compiler that the value of btnPushed will change unexpectantly when the interrupt fires by using:
volatile bool btnPushed = false;
I know that there are similar questions but I think I have not found an answer to my question. I am getting the error from the topic. I am not sure why this is as all controlled paths IMO return a value. I believe that the problem lies in a first for loop - when I commented it out code complied. But I do not know why a value should be returned after the loop, the function should proceed further. Am I wrong?
bool is_cycle (int winner, int loser)
{
bool loser_is_winner = false;
for (int i = 0; i < candidate_count; i ++)
{
if (locked[loser][i] == true)
{
loser_is_winner = true;
}
}
if (loser_is_winner == false)
{
return false;
}
else if (loser == winner_fixed)
{
return true;
}
else if (loser_is_winner == true)
{
for (int i = 0; i < candidate_count; i ++)
{
if (locked[loser][i] == true)
{
is_cycle(loser,i);
}
}
}
else
{
return false;
}
}
This case doesn't have a return statement:
else if (loser_is_winner == true)
{
for (int i = 0; i < candidate_count; i ++)
{
if (locked[loser][i] == true)
{
is_cycle(loser,i);
}
}
}
If you're going to return a value, you need to return a value on all paths, otherwise the return value is garbage.
Do you mean:
else if (loser_is_winner == true)
{
for (int i = 0; i < candidate_count; i ++)
{
if (locked[loser][i] == true)
{
return is_cycle(loser,i);
}
}
return false;
}
Incidentally, this block is unreachable:
else
{
return false;
}
Fun fact: if you access the non-returned return value on Itanium, your code might crash.
Every function that you expect that returns a value, you must sure that always reach the code(return). You have something like the next:
bool myFunction(someParam) {
//some code
if(condition1){
// do something
return value1;
} else if(condition2) {
//do something
return value2;
} else if(condition3) {
// do something
} else {
// do something
return value4;
}
}
You can notice that in if with the condition3 not has a return, if you enter in this condition never reach any return. Check part of your code to try avoid this case.
I'm trying to write a function, that would check for a matching parentheses.
For example, if the given string is "(1+1))" it would print false otherwise it's true.
However, in my code it's printing false no matter what the case is.
bool isMatched(char pran[]) {
bool completetd = true;
int count = 0;
for (int i = 0; pran[i] != '\0'; i++) {
if (pran[i] == '('){
count++;
}
else {
// It is a closing parenthesis
count--;
}
if (count < 0) {
// there are more Closing parenthesis
completetd = false;
break;
}
// If count is not zero, there are more opening parenthesis
if (count != 0) {
completetd = false;
}
}
return completetd;
}
int main() {
char arr[] = "((1+a))";
if (isMatched(arr)) {
printf("TRUE \n");
}
else {
printf("FALSE \n");
}
return 0;
}
I would appreciate any help.
You can try this not sure if this is what you are looking for.
bool isMatched(char pran[]) {
int open = 0;
int close = 0;
for (int i = 0; pran[i] != '\0'; i++) {
if (pran[i] == '('){
open++;
}
if (pran[i] == ')'){
close++;
}
}
// Check if both match
if(open == close){
return true;
}
return false;
}
int main() {
char arr[] = "((1+a))";
if (isMatched(arr)) {
printf("TRUE \n");
}
else {
printf("FALSE \n");
}
return 0;
}
By adding a
printf("got 1 (!\n"); next to count++;
and a
printf("got 1 )!\n"); next to count--;,
you get:
Got 1 (!
Got 1 (!
Got 1 )!
Got 1 )!
Got 1 )!
FALSE
This shows that you have a validation problem with your checking logic
As pointed-out in the comments, replace your else with else if (pran[i] == ')') { will fix that part for you.
But the real problem lies with your last validation.
Take it out of the for loop. It sets the value to false as soon as you detect a parenthesis.
Thus, take this:
// If count is not zero, there are more opening parenthesis
if (count != 0) {
printf("Count: %d\n",count);
completetd = false;
}
}
and make it this:
}
// If count is not zero, there are more opening parenthesis
if (count != 0) {
printf("Count: %d\n",count);
completetd = false;
}
I need to create a Robot Simulator programmed in C. The Robot has to find the Exit of a 2d labirinth using a Recursive Backtracker algorithm, i understood how does this algorithm work but i don't know how to implement it. I Think i can use a Binary Tree using Pointers but i don't know how to do this, can you try to explain it to me?
This is the program that i've created, now the Robot is entering a loop because of the method that changes direction
#ifdef __unix__
#include <unistd.h>
#elif defined _WIN32
#include <windows.h>
#define sleep(x) Sleep(1000 * x)
#endif
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
void goUp();
void goDown();
void goLeft();
void goRight();
typedef struct robot {
int direction;
bool is_moving;
}robot;
typedef struct room {
robot robot;
bool is_robot;
int obstacle;
}room;
room Room[20][20];
int r = 12;
int c = 10;
void generation(room matrix[20][20])
{
srand(time(NULL));
int x,i,j;
x=0;
for(i=0;i<20;i++)
{
for(j=0;j<20;j++)
{
matrix[i][j].is_robot=false;
x=rand()%100+1;
if(x==1||x==50||x==100)
{
matrix[i][j].obstacle=1;
}
else
{
matrix[i][j].obstacle=0;
}
}
}
}
void print_matrix(room matrix[20][20])
{
int i,j;
for(i=0;i<20;i++)
{
for(j=0;j<20;j++)
{
if(matrix[i][j].obstacle==0)
{
if(matrix[i][j].is_robot==true)
{
printf("I");
}
else
{
printf(" ");
}
}
else
{
if(matrix[i][j].is_robot==true)
{
printf("I");
}
else
{
printf("o");
}
}
}
printf("\n");
}
}
bool changeDirection(room Room[20][20],int i,int j)
{
if(Room[i][j].robot.direction == 1)
{
if(Room[i-1][j].obstacle == 1 || i-1 == 0)
{
if(Room[i+1][j].obstacle == 1 || i+1 == 19)
{
Room[i][j].robot.direction = 2;
return true;
}
else
{
Room[i][j].robot.direction = 4;
return true;
}
}
else
{
Room[i][j].robot.direction = 3;
return true;
}
}
if(Room[i][j].robot.direction == 2)
{
if(Room[i-1][j].obstacle == 1 || i-1 == 0)
{
if(Room[i+1][j].obstacle == 1 || i+1 == 19)
{
Room[i][j].robot.direction = 1;
return true;
}
else
{
Room[i][j].robot.direction = 4;
return true;
}
}
else
{
Room[i][j].robot.direction = 3;
return true;
}
}
if(Room[i][j].robot.direction == 3)
{
if(Room[i][j+1].obstacle == 1 || j+1 == 19)
{
if(Room[i][j-1].obstacle == 1 || j-1 == 0)
{
Room[i][j].robot.direction = 4;
return true;
}
else
{
Room[i][j].robot.direction = 2;
return true;
}
}
else
{
Room[i][j].robot.direction = 1;
return true;
}
}
if(Room[i][j].robot.direction == 4)
{
if(Room[i][j+1].obstacle == 1 || j+1 == 19)
{
if(Room[i][j-1].obstacle == 1 || j-1 == 0)
{
Room[i][j].robot.direction = 3;
return true;
}
else
{
Room[i][j].robot.direction = 2;
return true;
}
}
else
{
Room[i][j].robot.direction = 1;
return true;
}
}
}
void goRight()
{
c=c+1;
Room[r][c].robot.direction=1;
Room[r][c].is_robot=true;
Room[r][c-1].is_robot=false;
}
void goLeft()
{
c=c-1;
Room[r][c].robot.direction=2;
Room[r][c].is_robot=true;
Room[r][c+1].is_robot=false;
}
void goUp()
{
r=r-1;
Room[r][c].robot.direction=3;
Room[r][c].is_robot=true;
Room[r+1][c].is_robot=false;
}
void goDown()
{
r=r+1;
Room[r][c].robot.direction=4;
Room[r][c].is_robot=true;
Room[r-1][c].is_robot=false;
}
int main()
{
generation(Room);
Room[r][c].robot.direction = 1;
Room[r][c].robot.is_moving = true;
Room[r][c].is_robot = true;
do
{
Room[r][c].robot.is_moving = true;
if (Room[r][c].robot.direction == 1 && Room[r][c].robot.is_moving == true) // destra
{
if(Room[r][c +1].obstacle == 1 || c+1 == 19)
{
changeDirection(Room,r,c);
}
else
{
goRight();
}
}
if (Room[r][c].robot.direction == 2 && Room[r][c].robot.is_moving == true) // sinistra
{
if(Room[r][c -1].obstacle == 1 || c-1 == 0)
{
changeDirection(Room,r,c);
}
else
{
goLeft();
}
}
if (Room[r][c].robot.direction == 3 && Room[r][c].robot.is_moving == true) // su
{
if(Room[r-1][c].obstacle == 1 || r-1 == 0)
{
changeDirection(Room,r,c);
}
else
{
goUp();
}
}
if (Room[r][c].robot.direction == 4 && Room[r][c].robot.is_moving == true) // giu
{
if(Room[r+1][c].obstacle == 1 || r+1 == 19)
{
changeDirection(Room,r,c);
}
else
{
goDown();
}
}
print_matrix(Room);
sleep(0.1);
system("cls");
}
while(1);
print_matrix(Room);
}
I'm having a hard time understanding how a binary tree would be useful in finding a path in a labyrinth (maybe it's used to represent the labyrinth?) but maybe I'm blind. I would simply make a 2d int array and let 0 mean the position is blocked (there's a wall there or something) and 1 mean it's open (you can move there). The brute force backtrack procedure, going off orthogonal movement (left, right, up, down) would be:
f(x,y){
// you found the place your want to go to
if (x,y) is (destinationX,destinationY)
return true
block the position (x,y) // i.e. mark current position as visited
if there is an open spot at (x,y-1) AND f(x,y-1)
return true
if there is an open spot at (x,y+1) AND f(x,y+1)
return true
if there is an open spot at (x-1,y) AND f(x-1,y)
return true
if there is an open spot at (x+1,y) AND f(x+1,y)
return true
return false
}
Suppose you had the labyrinth looking like:
"+" is where you start ([1][1])
"-" is your destination ([3][1])
"#" is a blocked region
===========
|#|#|#|#|#|
|#|+| |#|#|
|#|#| |#|#|
|#|-| | |#|
|#|#|#|#|#|
===========
Using the above idea I have:
#include <stdio.h>
#define width 5
#define height 5
// print maze
void print(char arr[][width]){
for (int i = 0; i < 2*width+1; i++) printf("=");
printf("\n");
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
printf("|%c",arr[i][j]);
}
printf("|\n");
}
for (int i = 0; i < 2*width+1; i++) printf("=");
}
// starting from (x,y) to (destX,destY)
int path(int arr[][width],int x,int y,int destX,int destY,char toDest[][width]){
if (x==destX && y==destY) {
toDest[y][x] = '*';
print(toDest);
return 1;
}
// mark current position as visited
arr[y][x] = 0;
toDest[y][x] = '*';
// left
if (arr[y][x-1] && path(arr,x-1,y,destX,destY,toDest))
return 1;
// right
if (arr[y][x+1] && path(arr,x+1,y,destX,destY,toDest))
return 1;
// up
if (arr[y-1][x] && path(arr,x,y-1,destX,destY,toDest))
return 1;
// down
if (arr[y+1][x] && path(arr,x,y+1,destX,destY,toDest))
return 1;
return 0;
}
int main () {
// use this to store path
// and then print it out if found
char toDest[height][width] = {
{'#','#','#','#','#'},
{'#',' ',' ','#','#'},
{'#','#',' ','#','#'},
{'#',' ',' ',' ','#'},
{'#','#','#','#','#'}
};
// 0 -> position is blocked
// 1 -> position is open
int maze[height][width] = {
{0,0,0,0,0},
{0,1,1,0,0},
{0,0,1,0,0},
{0,1,1,1,0},
{0,0,0,0,0}
};
path(maze,1,1,1,3,toDest);
}
Output:
===========
|#|#|#|#|#|
|#|*|*|#|#|
|#|#|*|#|#|
|#|*|*| |#|
|#|#|#|#|#|
===========
In output the path is designated by the *s
I am trying to create buffer to store infinity size of symbols.
I have this structure:
typedef struct buffer {
int bufferSize;
int literalSize;
int allocatedSize;
char *bufferPtr;
} bufferStruct;
In my file.h.
I have also functions for buffer:
bufferStruct *BufferInitialize(int size) {
bufferStruct *tempBuff;
tempBuff = (bufferStruct *)malloc(sizeof(bufferStruct));
if (tempBuff == NULL) {
exit(99); // MEMORY_ERROR
}
tempBuff->bufferSize = size;
tempBuff->literalSize = 0;
tempBuff->bufferPtr = NULL;
tempBuff->allocatedSize = 0;
return (tempBuff);
}
int addToBuffer(bufferStruct *buffer, char c) {
if (buffer == NULL) {
return 99; // MEMORY_ERROR
}
if (buffer->allocatedSize > buffer->literalSize) {
buffer->bufferPtr[buffer->literalSize++] = c;
} else {
buffer->bufferPtr = realloc(buffer->bufferPtr, (buffer->allocatedSize + buffer->bufferSize) * sizeof(char));
if (buffer->bufferPtr == NULL) {
return 99; // MEMORY_ERROR
}
buffer->allocatedSize += buffer->bufferSize;
buffer->bufferSize <<= 1; // bS = bS * 2
buffer->bufferPtr[buffer->literalSize++] = c;
}
return 0;
}
int bufferDestroy(bufferStruct *buffer) {
if (buffer == NULL) {
return 99; // MEMORY_ERROR
}
free(buffer->bufferPtr);
free(buffer);
return 0;
}
In my file.c I am trying to create buffer:
token *getNextToken(token *tokenT) {
token *actualToken = NULL;
char *bufferData = NULL;
int charFromFile;
eState state = stateInit;
bufferStruct *bufferT = NULL;
while ((charFromFile = fgetc(fp))) {
switch (state) {
case stateInit: {
if (isdigit(charFromFile)) {
bufferT = BufferInitialize(8);
addToBuffer(bufferT, charFromFile);
state = stateInt;
} else
if (isalpha(charFromFile) || (charFromFile == '_')) {
state = stateId;
bufferT = BufferInitialize(16);
addToBuffer(bufferT, charFromFile);
} else
if (isspace(charFromFile)) {
state = stateInit;
... some more conditions ... it's similar, a lot.
case stateInt: {
if (isdigit(charFromFile)) {
state = stateInt;
addToBuffer(bufferT, charFromFile);
} else
if ((charFromFile == 'e') || (charFromFile == 'E')) {
state = stateExp;
addToBuffer(bufferT, charFromFile);
} else
if (charFromFile = '.') {
state = stateDouble;
addToBuffer(bufferT, charFromFile);
} else {
bufferData = bufferT->bufferPtr;
//strcpy(actualToken->content, bufferData);
addToBuffer(bufferT, '\0');
bufferDestroy(bufferT);
actualToken->type = tokenInt;
return actualToken;
}
} break;
... other similar cases ...
}
}
}
The problem is when I am trying to do this, Visual studio give me error:
One or more multiply defined symbols found
Also gives me
already defined in main.obj
for every function I have.
I don't see the way out. What am I doing wrong ?
There are multiple issues in your code:
You should not put code in header files. The function BufferInitialize should not be located in file.h unless it is defined inline.
The test while (c = fgetc(fp)) is incorrect: you use an assignment as a test expression, it is very error prone, you should at least parenthesize the assignment expression, and probably test for EOF instead of '\0': while ((c = fgetc(fp)) != EOF). Furthermore, c must be defined as an int. Post actual code, not pseudo-code.
You initialize tempBuff->bufferSize to a potentially non zero value, whereas the allocatedSize is 0 and the buffer is unallocated. This seems incorrect.
There could be many more issues in your actual code, we cannot see what the code, how can be tell you about those? Always post a complete, compilable code that demonstrates the problem.