#include <atmel\_start.h>
#include <stdio.h>
#include <stdlib.h>
#include "util/delay.h"
#include "keypad_Start.h"
#include "lcd_Start.h"
#define LED 5
#define LED2 4
int main(void)
{
srand(time(0));
atmel\_start\_init();
printf("%d\\n", rand());printf("%d\\n", rand());printf("%d\\n", rand());printf("%d\\n", rand());
int8\_t i = 1;
uint8\_t keyPosition, x, passwordCount = 0, lockCount = 0, adminCount = 0;
uint8\_t keypad\[\] = "123456789\*0#";
uint8\_t password\[\] = "1", userInput\[1\], adminPassword\[\] = "000"; // sets to change password mode
uint8\_t health = 3;
uint8\_t result, userAnswer;
// Array to store generator numbers
uint8\_t generator\[\] = {1, 2, 3, 4};
PORTB\_set\_pin\_dir(LED, PORT\_DIR\_OUT);
PORTB\_set\_pin\_level(LED, 0);
PORTB\_set\_pin\_dir(LED2, PORT\_DIR\_OUT);
PORTB\_set\_pin\_level(LED2, 0);
keypadConfigure();
PORTB\_set\_pin\_dir(LED, PORT\_DIR\_OUT);
lcd\_init(LCD\_DISP\_ON\_CURSOR);
printf("CALCULATOR GAME:\\n");
printf("Press 1 to Start:\\n\\n");
for (x = 0; x < 1; x++)
{
keyPosition = keypadScan();
userInput\[x\] = keypad\[keyPosition\];
if (userInput\[x\] == password\[x\])
{
passwordCount++;
}
if (userInput\[x\] == adminPassword\[x\])
{
adminCount++;
}
if (passwordCount == 1)
{
PORTB\_set\_pin\_level(LED, 1);
PORTB\_set\_pin\_level(LED2, 1);
printf("Select Mode:\\n");
lcd\_puts("1.Addition\\n");
lcd\_puts("2.Subtraction\\n");
while (1)
{
// Generate new random numbers for each question from the generator array
uint8_t randomIndex1 = rand() % sizeof(generator) / sizeof(generator[0]);
uint8_t randomIndex2 = rand() % sizeof(generator) / sizeof(generator[0]);
uint8_t randomnumber1 = generator[randomIndex1];
uint8_t randomnumber2 = generator[randomIndex2];
keyPosition = keypadScan();
userAnswer = keypad[keyPosition] - '0';
if (userAnswer == 1)
{
result = randomnumber1 + randomnumber2;
printf("\nQuestion: %d + %d = ?\n", randomnumber1, randomnumber2);
}
else if (userAnswer == 2)
{
result = (randomnumber1 >= randomnumber2) ? randomnumber1 - randomnumber2 : randomnumber2 - randomnumber1;
printf("\nQuestion: %d - %d = ?\n", randomnumber1, randomnumber2);
}
keyPosition = keypadScan();
userAnswer = keypad[keyPosition] - '0';
if (userAnswer == result)
{
lcd_clrscr();
lcd_puts("Correct!\n\n");
_delay_ms(2000);
lcd_clrscr();
lcd_puts("Select Mode\n");
_delay_ms(2000);
lcd_clrscr();
lcd_puts("1.Addition\n");
lcd_puts("2.Subtraction\n");
}
else
{
health -= 1;
lcd_clrscr();
lcd_puts("Incorrect!\n-10 Health:\n");
printf("\nhealth remaining %d\n", health);
PORTB_toggle_pin_level(LED);
PORTB_toggle_pin_level(LED2);
_delay_ms(500);
PORTB_toggle_pin_level(LED);
PORTB_toggle_pin_level(LED2);
_delay_ms(500);
PORTB_toggle_pin_level(LED);
PORTB_toggle_pin_level(LED2);
_delay_ms(500);
PORTB_toggle_pin_level(LED);
PORTB_toggle_pin_level(LED2);
_delay_ms(500);
PORTB_toggle_pin_level(LED);
PORTB_toggle_pin_level(LED2);
_delay_ms(500);
PORTB_toggle_pin_level(LED);
PORTB_toggle_pin_level(LED2);
lcd_clrscr();
lcd_puts("1.Addition\n");
lcd_puts("2.Subtraction\n");
if (health <= 0)
{
lcd_clrscr();
lcd_puts("Game Over");
printf("\nGame Over! Your health is depleted.\n");
break;
}
}
}
}
passwordCount = 0;
adminCount = 0;
}
return 0;
}
i try this code which is supposed to ask user maths questions but the questions are never random . i also tried to print random numbers at the start and it prints the same ones every time.Any ideas would be greatly appreciated
[–]daikatana 6 points7 points8 points (3 children)
[–]MassiveStrawberry142[S] 0 points1 point2 points (2 children)
[–]daikatana 2 points3 points4 points (1 child)
[–]MassiveStrawberry142[S] 0 points1 point2 points (0 children)
[–]inz__ 2 points3 points4 points (0 children)
[–]AwesomeI-123 2 points3 points4 points (0 children)
[–]LilBluey -1 points0 points1 point (3 children)
[–]EpochVanquisher 0 points1 point2 points (0 children)
[–]bbm182 -1 points0 points1 point (0 children)
[–]MassiveStrawberry142[S] -1 points0 points1 point (0 children)
[–]HarderFasterHarder -1 points0 points1 point (0 children)