Movement issues, skipping fields - c

I'm making a game, where you (the player) are represented by a number in a 2D matrix. The problem is that when I move the player, it skips fields, and does not respond very well. The movement is very inaccurate. If you could run the program, you could get a better understanding of the issue. How can I fix this problem?
#include <stdio.h>
#include <windows.h>
#include <time.h>
void delay(unsigned int mseconds)
{
clock_t goal = mseconds + clock();
while (goal > clock())
;
}
int map[5][5] = {{0, 0, 0, 0, 0},
{0, 0, 0, 0, 0},
{0, 0, 0, 0, 0},
{0, 0, 0, 0, 0},
{0, 0, 0, 0, 0}};
int mUpdate(int x)
{
int i, j;
for (i = 0; i < 5; i++)
{
for (j = 0; j < 5; j++)
{
printf("%d ", map[i][j]);
}
printf("\n");
}
}
int mRefresh(int x)
{
int i, j;
for (i = 0; i < 5; i++)
{
for (j = 0; j < 5; j++)
{
if (map[i][j] > 0)
{
map[i][j] = 0;
}
}
}
}
int main()
{
int c;
int i, j;
int p_num;
printf("Hey! Choose a number from 1-9 that will represent you!: ");
scanf("%d", &p_num);
printf("Choose a starting position by inputting two coordinates from 1-4!: ");
scanf("%d %d", &i, &j);
printf("\nVery well, this is your starting position!\n");
map[i][j] = p_num;
mUpdate(map[i][j]);
printf("\nYou are now the number %d !\n", p_num);
printf("\nPlease wait for the map to load!");
printf("\nMap loading will start soon!");
delay(3000);
for (c = 0; c < 5; c++)
{
printf("\nLoading.");
delay(500);
system("cls");
printf("\nLoading..");
delay(500);
system("cls");
printf("\nLoading...");
delay(500);
system("cls");
}
printf("Map loaded!\n");
printf("\nUse arrow keys to move thru the matrix!\n");
mUpdate(map[i][j]);
int game_running = true;
while (game_running = true)
{
if (GetAsyncKeyState(VK_DOWN))
{
// delay(200);
map[i++][j] = p_num;
system("cls");
mUpdate(map[i][j]);
mRefresh(map[i][j]);
}
else if (GetAsyncKeyState(VK_UP))
{
// delay(200);
map[i--][j] = p_num;
system("cls");
mUpdate(map[i][j]);
mRefresh(map[i][j]);
}
else if (GetAsyncKeyState(VK_RIGHT))
{
// delay(1000);
map[i][j++] = p_num;
system("cls");
mUpdate(map[i][j]);
mRefresh(map[i][j]);
}
else if (GetAsyncKeyState(VK_LEFT))
{
// delay(200);
map[i][j--] = p_num;
system("cls");
mUpdate(map[i][j]);
mRefresh(map[i][j]);
}
}
system("cls");
}

Related

Havel-Hakimi Algorithm in C

I'm trying to write havel-hakimi theorem in C. But I have a problem with the while loop. The program doesn't sort array again in the while loop and that's why output prints the wrong answer. Could show me what's my fault please?
# include <stdio.h>
int main(){
int j,i,vertex_number,temp1,temp2,a=0,b=0;
printf("Vertex Number:");
scanf("%d",&vertex_number);
int graph[vertex_number];
for(i=0;i<vertex_number;i++){
scanf("%d",&graph[i]);
}
while(1){
//SORTING ARRAY
for(i=0;i<vertex_number;i++){
for(j=i+1;j<vertex_number;j++){
if(graph[i]<graph[j]){
temp1=graph[i];
graph[i]=graph[j];
graph[j]=temp1;
}
}
}
//IF ALL VERTEX DEGREES EQUAL 0 GRAPH EXIST
for(i=0;i<vertex_number;i++){
if(graph[i]==0){
a++;
}
}
if(a==vertex_number){
printf(" graph exist.");
return 0;
}
//NEGATIVE VERTEX DEGREE NOT EXIST
for(i=0;i<vertex_number;i++){
if(graph[i]<0){
b++;
}
}
if(b>0){
printf("graph not exist.");
return 0;
}
temp2=graph[0];
for(i=0;i<temp2;i++){
graph[i]=graph[i+1];
}
vertex_number--;
for(i=0;i<temp2;i++){
graph[i]-=1;
}
printf("-------------\n");
for(i=0;i<vertex_number;i++){
printf("%d\n",graph[i]);
}
}
}
Your have 2 issues, the exist if graph[i]-=1; is negative and the removing loop doing should be done for vertex_number and not temp2
# include <stdio.h>
int main(void) {
int i, j, vertex_number, temp1, temp2;
printf("Vertex Number:");
scanf("%d", &vertex_number);
int graph[vertex_number];
for (i = 0; i < vertex_number; i++){
scanf("%d", &graph[i]);
}
while (1) {
//SORTING ARRAY
for (i = 0; i < vertex_number; i++) {
for (j = i+1; j < vertex_number; j++) {
if (graph[i] < graph[j]) {
temp1 = graph[i];
graph[i] = graph[j];
graph[j] = temp1;
}
}
}
//IF ALL VERTEX DEGREES EQUAL 0 GRAPH EXIST
if (graph[0] == 0) {
printf(" graph exist.");
return 0;
}
//NEGATIVE VERTEX DEGREE NOT EXIST
for (i = 0; i < vertex_number; i++) {
if (graph[i] < 0){
printf("graph not exist.");
return 0;
}
}
temp2 = graph[0];
vertex_number--;
for (i = 0; i < vertex_number; i++) { // HERE was your issue
graph[i] = graph[i + 1];
}
for (i = 0; i < temp2; i++) {
graph[i]-=1;
if (graph[i] < 0) {
printf("graph not exist.");
return 0;
}
}
printf("-------------\n");
for (i = 0; i < vertex_number; i++) {
printf("%d\n",graph[i]);
}
}
}
You don't need a, if the first is null all the other are null or negative
You don't need b neither just stop on first occurence
Not an answer, but a cleaned-up version that works follows.
The key is that it literally removes the first element from the array by advancing the pointer and reducing the size of the array. That way, we're always working with elements 0..s-1 or 0..n-1.
// Destroys the contents of the provided array.
int havel_hakimi(unsigned *degrees, size_t n) {
while (1) {
// Yuck
for (size_t i=0; i<n; ++i) {
for (size_t j=i+1; j<n; ++j) {
if (degrees[i] < degrees[j]) {
int temp = degrees[i];
degrees[i] = degrees[j];
degrees[j] = temp;
}
}
}
if (degrees[0] == 0)
return 1; // Has a simple graph.
// Remove first element.
unsigned s = degrees[0];
++degrees;
--n;
if (s > n)
return 0; // Invalid input!
if (degrees[s-1] == 0)
return 0; // Doesn't have a simple graph.
for (size_t i=s; i--; )
--degrees[i];
}
}
Tested using the following:
#include <stdio.h>
int main(void) {
{
unsigned degrees[] = { 6, 3, 3, 3, 3, 2, 2, 2, 2, 1, 1 };
printf("%d\n", havel_hakimi(degrees, sizeof(degrees)/sizeof(degrees[0]))); // 1
}
{
unsigned degrees[] = { 6, 5, 5, 4, 3, 2, 1 };
printf("%d\n", havel_hakimi(degrees, sizeof(degrees)/sizeof(degrees[0]))); // 0
}
return 0;
}

Dijkstra algorithm~

If you apply the ijkstra algorithm several times, you can see the effect of the Floyd algorithm. We've created a program that uses the Dijkstra algorithm to get the shortest path from every vertex in the graph to every other vertex in the graph, and we've created a Dijkstra code.The values shown in the image are not shown. What is the reason?
enter code here
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#define TRUE 1
#define FALSE 0
#define MAX_VERTICES 100
#define INF 1000000
typedef struct GraphType {
int n;
int weight[MAX_VERTICES][MAX_VERTICES];
} GraphType;
int distance[MAX_VERTICES];
int found[MAX_VERTICES];
int choose(int distance[], int n, int found[])
{
int i, min, minpos;
min = INT_MAX;
minpos = -1;
for (i = 0; i < n; i++)
if (distance[i] < min && !found[i]) {
min = distance[i];
minpos = i;
}
return minpos;
}
void print_status(GraphType* g)
{
static int step = 1;
printf("STEP %d: ", step++);
printf("distance: ");
for (int i = 0; i < g->n; i++) {
if (distance[i] == INF)
printf(" * ");
else
printf("%2d ", distance[i]);
}
printf("\n");
printf(" found: ");
for (int i = 0; i < g->n; i++)
printf("%2d ", found[i]);
printf("\n\n");
}
void shortest_path(GraphType * g, int start)
{
int i, u, w;
for (i = 0; i < g->n; i++)
{
distance[i] = g->weight[start][i];
found[i] = FALSE;
}
found[start] = TRUE;
distance[start] = 0;
for (i = 0; i < g->n - 1; i++) {
print_status(g);
u = choose(distance, g->n, found);
found[u] = TRUE;
for (w = 0; w < g->n; w++)
if (!found[w])
if (distance[u] + g->weight[u][w] < distance[w])
distance[w] = distance[u] + g->weight[u][w];
}
}
int main(void)
{
GraphType g = { 7,
{{ 0, 7, INF, INF, 3, 10, INF },
{ 7, 0, 4, 10, 2, 6, INF },
{ INF, 4, 0, 2, INF, INF, INF },
{ INF, 10, 2, 0, 11, 9, 4 },
{ 3, 2, INF, 11, 0, INF, 5 },
{ 10, 6, INF, 9, INF, 0, INF },
{ INF, INF, INF, 4, 5, INF, 0 } }
};
for(int i=0; i<g.n; i++){
printf("from %d to other nodes>\n", i);
shortest_path(&g, i);
}
return 0;
}

Ccrabble C program compiling but is not allowing the user to execute the file

The user is supposed to be prompted with several letters that they are to decode and then input their word which returns a score for that word.
I can get the program to compile but the program does not actually run. So I am not entirely sure where the issue lies within my code.I am relatively new at this.
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <math.h>
#define num_letters_input 7
int main()
{
const int alphabet_count[26] = {8, 2, 2, 4, 12, 2, 3, 2, 9, 1, 1, 4, 2, 6, 8, 2, 1, 6, 4, 6, 4, 2, 2, 1, 2, 1};
const int alphabet_value[26] = {1,3,3,2,1,4,2,4,1,8,5,1,3,1,1,3,10,1,1,1,1,4,4,8,4,10};
int letter_set[26];
int size_letter_set = 26;
void generate_letter_set(int letter_set[], int size_letter_set, int num_letters) {
int random_a = rand() % 26;
int random_b = rand() % (alphabet_count[random_a]);
letter_set[random_a] = random_b;
printf("Your letters are: ");
for(int i=0; i < size_letter_set; i++) {
if(letter_set[i])
{
int num = letter_set[i];
while(num--)
printf("%c ", i+97);
}
}
printf("\n");
}
int read_word(char word[], int max_size_word) {
printf("Enter your word: ");
scanf("%s", word);
printf("%s \n", word);
int size_word = strlen(word);
if(size_word > max_size_word)
read_word(word, max_size_word);
return size_word;
}
bool check_word(char word[], int size_word, int letter_set[], int size_letter_set) {
int count_array[26];
for(int i=0; i<size_word; i++) {
count_array[word[i]-97]++;
}
for(int i=0; i<size_letter_set; i++) {
if(count_array[i] <= letter_set[i])
continue;
else {
return false;
}
return true;
}
int compute_word_value(char word[], int size_word) {
int word_value = 0;
for(int i=0; i<size_word; i++) {
word_value = word_value + alphabet_value[word[i]-97];
}
return word_value;
}
int main(void) {
int max_size_word = 7;
int size_word, word_value;
bool validity = false;
char word[95];
printf("This program plays the game of scrabble.\n");
for(int i=0; i < size_letter_set; i++) {
letter_set[i] = 0;
}
generate_letter_set(letter_set, size_letter_set, num_letters_input);
while(!validity) {
size_word = read_word(word, max_size_word);
validity = check_word(word, size_word, letter_set, size_letter_set);
if(!validity)
{
printf("The word is not valid. Use your letters: ");
for(int i=0; i < size_letter_set; i++) {
if(letter_set[i])
{
int num = letter_set[i];
while(num--) {
printf("%c ", i+97);
}
}
printf("\n");
}
}
printf("The value of your word is: ");
word_value = compute_word_value(word, size_word);
printf("%d", word_value);
printf("Thank you for playing.");
}
}
}
return 0;
}
It may be somewhat different from what you intend,
I think it will be useful to start with the following modifications.
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <time.h>
#define num_letters_input 7
const int alphabet_count[26] = {8,2,2,4,12,2,3,2,9,1,1,4,2,6,8,2, 1,6,4,6,4,2,2,1,2, 1};
const int alphabet_value[26] = {1,3,3,2, 1,4,2,4,1,8,5,1,3,1,1,3,10,1,1,1,1,4,4,8,4,10};
void generate_letter_set(int letter_set[], int size_letter_set, int num_letters) {
printf("Your letters are: ");
for(int i = 0; i < num_letters; i++){
int random_a = rand() % 26;
int random_b = rand() % alphabet_count[random_a];
letter_set[random_a] = random_b;
}
for(int i = 0; i < size_letter_set; ++i){
if(letter_set[i]){
int num = letter_set[i];
while(num--)
printf("%c ", 'a' + i);
}
}
printf("\n");
}
int read_word(char word[], int max_size_word) {
int size_word = 0;
do {
printf("Enter your word: ");
scanf("%s", word);
size_word = strlen(word);
} while(size_word > max_size_word);
return size_word;
}
bool check_word(char word[], int size_word, int letter_set[], int size_letter_set) {
int count_array[26] = {0};
for(int i = 0; i < size_word; i++) {
count_array[word[i]-'a']++;
}
for(int i=0; i < size_letter_set; i++) {
if(count_array[i] <= letter_set[i])
continue;
else
return false;
}
return true;
}
int compute_word_value(char word[], int size_word) {
int word_value = 0;
for(int i=0; i<size_word; i++) {
word_value += alphabet_value[word[i]-97];
}
return word_value;
}
int main(void){
int letter_set[26] = {0};
int size_letter_set = 26;
int max_size_word = num_letters_input;
int size_word, word_value;
bool validity = false;
char word[96];
srand(time(NULL));
printf("This program plays the game of scrabble.\n");
generate_letter_set(letter_set, size_letter_set, num_letters_input);
while(!validity) {
size_word = read_word(word, max_size_word);
validity = check_word(word, size_word, letter_set, size_letter_set);
if(!validity){
printf("The word is not valid.\nUse your letters: ");
}
}
printf("The value of your word is: ");
word_value = compute_word_value(word, size_word);
printf("%d\n", word_value);
printf("Thank you for playing.\n");
return 0;
}

Wave Algorithm (Lee's Algorithm): incorrect final matrix

I'm writing a program calculating the shortest way from point A to point B.
I have a map (matrix) with values:
0 is block (wall, no way to pass);
1 is free way (you can pass);
2 is start point;
In the code below I declare 2 arrays: an array " map"and changed array "visited" while running program demonstrating visited points.
I check the cells in 4 directions (not diagonals) for 1 or 0. If it's 1 (possible to pass), I increase the counter for 1. For do not count the previous cell I'm trying to avoid it by the condition. I realized that in two one-dimensional arrays {1 0 -1 0} and { 0, 1, 0, -1 } to check neighbor points (what mean i check [i+1][j], [i-1][j], [i][j+1] and [i][j-1]).
As a result I wanna see "visited" matrix with a few lines which shows the way to reach to the point B (1, 2, 3, ... 15). I wanna find the way to map[7][7] point.
Right now the error here that I do count++ for the previous position. How to avoid that?
Thank you.
P.S. I wrote a few functions implementing a new array with 0 values, counting free to go cells and printing arrays.
main.c:
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#define WIDTH 8
#define HEIGHT 8
int mapZero(int map[WIDTH][HEIGHT]);
int mapPrint(int map[WIDTH][HEIGHT]);
int mapInit(int map[WIDTH][WIDTH]);
int findFreeToGoCells(int map[WIDTH][HEIGHT]);
int main(int argc, char * argv[])
{
bool stop;
unsigned int count;
unsigned int max;
int visited[WIDTH][HEIGHT];
int map[WIDTH][HEIGHT] =
{
{ 1, 1, 1, 1, 1, 0, 0, 1 },
{ 0, 1, 1, 1, 1, 1, 0, 1 },
{ 0, 0, 1, 0, 1, 1, 1, 0 },
{ 1, 0, 1, 1, 1, 0, 1, 1 },
{ 0, 0, 0, 1, 0, 0, 0, 1 },
{ 1, 0, 1, 1, 1, 0, 0, 1 },
{ 0, 0, 0, 0, 1, 0, 0, 1 },
{ 0, 1, 1, 1, 1, 1, 1, 1 },
};
mapZero(visited);
printf("Matrix of zeroed-visited cells:\n\n");
mapPrint(visited);
printf("Matrix of the map:\n\n");
mapPrint(map);
printf("Free to go cells: %d\n\n", findFreeToGoCells(map));
max = WIDTH * HEIGHT - 1;
visited[0][0] = map[0][0];
count = 0;
visited[0][0] = 0;
int di[4] = { 1, -1, 0, 0 };
int dj[4] = { 0, 0, 1, -1 };
//do
{
for (int i = 0; i < WIDTH; ++i)
{
for (int j = 0; j < HEIGHT; ++j)
{
if (visited[i][j] == count)
{
for (int k = 0; k < 4; ++k)
{
int i_check = i + di[k];
int j_check = j + dj[k];
if ((i_check >= 0 && i_check < WIDTH) && (j_check >= 0 && j_check < HEIGHT) && (map[i_check][j_check] != 0))
{
visited[i_check][j_check] = count + 1;
}
}
count++;
}
}
}
}// while (visited[7][7] == 0);
if (count > max + 99999)
printf("The way couldn't be found\n");
else
{
printf("Matrix of visited cells:\n\n");
mapPrint(visited);
printf("Free to go cells from [0][0] to [7][7]: %d\n", findFreeToGoCells(visited));
}
/*************************************************************************************/
/*************************************************************************************/
int len;
int x = 7;
int y = 7;
int x_path[WIDTH * HEIGHT];
int y_path[WIDTH * HEIGHT];
len = visited[7][7];
count = len;
while (count > 0)
{
x_path[count] = x;
y_path[count] = y;
count--;
for (int k = 0; k < 4; ++k)
{
int i_check = x + di[k];
int j_check = y + dj[k];
if ((i_check >= 0 && i_check < WIDTH) && (j_check >= 0 && j_check < HEIGHT) && (map[i_check][j_check] == count))
{
x = x + di[k];
y = y + dj[k];
break;
}
}
}
x_path[0] = 0;
y_path[0] = 0;
printf("\nThe shortest way consist of %d cells\nThere are %d the shortest way to reach th the final point\n\n", len, findFreeToGoCells(visited)-len);
system("pause");
return 0;
}
int mapZero(int map[WIDTH][HEIGHT])
{
for (int i = 0; i < WIDTH; ++i)
{
for (int j = 0; j < HEIGHT; ++j)
{
map[i][j] = 0;
}
}
return 0;
}
int mapPrint(int map[WIDTH][HEIGHT])
{
for (int i = 0; i < WIDTH; ++i)
{
for (int j = 0; j < HEIGHT; ++j)
{
printf("%2d ", map[i][j]);
}
printf("\n\n");
}
printf("\n");
return 0;
}
int findFreeToGoCells(int map[WIDTH][HEIGHT])
{
int count = 0;
for (int i = 0; i < WIDTH; ++i)
{
for (int j = 0; j < HEIGHT; ++j)
{
if (map[i][j] != 0) count++;
}
}
return count;
}
Result:

swapping odd and even numbers inside an Array in C

Input array is for example Array[10] = {12,23,0,-7,138,22,7,99,10,-2}
I want to print out the array with even numbers on one end and odd numbers on the other, something like this: Array[10] = {12,0,-2,10,22,138,-7,99,7,23}
int main()
{
int N = 0, i = 0, j = N-1, index = 0;
printf("Enter N (0>N<10): ");
scanf("%d", &N);
int Array[N];
for(i=0;i<N;i++){
printf("Enter the value number %d :", i+1);
scanf("%d", &Array[i]);
}
printf("Array[%d] ={",N);
for(i=0;i<N;i++){
printf("%d\t", Array[i]);
}
printf("}");
// swaping odd and even numbers
int Array2[N];
while (i < j){
if(Array[i]%2 == 0){
Array2[i] = Array[index];
i++;
}
else{
Array2[j] = Array[index];
j++;
}
index++;
}
// view the new array
printf("\n Array[%d] ={",N);
for(i=0;i<N;i++){
printf("%d\t", Array2[i]);
}
printf("}");
return 0;
}
This doesn't seem to work. Any help would be appreciated.
Note: I know the Array[N] part is not how it's supposed to be done, it's just to simplify things.
In your else statement:
else{
Array2[j] = Array[index];
j++;
}
You need j--; not j++.
In your else statement:
if(Array[i]%2 == 0){
don't you want to inspect the item you are sorting (Array[index]) ?
At the beginning of the program in this declaration
int N = 0, i = 0, j = N-1, index = 0;
j is set to -1 because N is initialized by 0.
So this loop
while (i < j){
//...
will be iterate never independing on whether it makes any sense.:)
As for the loop then it does not swap even and odd numbers.
If I have understood your approach correctly you need something like the folloing. You can modify the demonstrative program such a way that the values of elements of the array will be enetered by the user.
#include <stdio.h>
#define N 10
int main( void )
{
int a[N] = { 12, 23, 0, -7, 138, 22, 7, 99, 10, -2 };
int i, j;
printf( "Array[%d] = { ", N );
i = 0;
do
{
printf( "%d", a[i] );
} while ( ++i < N && printf( ", " ) );
printf( " };\n");
i = 0; j = N;
while ( i != j )
{
if ( a[i] % 2 == 0 )
{
++i;
}
else if ( a[--j] % 2 == 0 )
{
int tmp = a[j];
a[j] = a[i];
a[i] = tmp;
}
}
printf( "Array[%d] = { ", N );
i = 0;
do
{
printf( "%d", a[i] );
} while ( ++i < N && printf( ", " ) );
printf( " };\n");
return 0;
}
The program output is
Array[10] = { 12, 23, 0, -7, 138, 22, 7, 99, 10, -2 };
Array[10] = { 12, -2, 0, 10, 138, 22, 7, 99, -7, 23 };
Dry run it, and you will understand the concept
#include <stdio.h>
int main()
{
int arr[10];
int i,temp;
printf("Enter elements into arry\n");
for(i=0;i<=9;i++)
{
printf("Element #%d-->",i);
scanf("%d",&arr[i]);
}
for(i=0;i<=9;i=i+2)
{
if((arr[i]%2)!=0)
{
if((arr[i+1]%2)!=0)
{
arr[i]=arr[i];
arr[i+1]=arr[i+1];
}
else if((arr[i+1]%2)==0)
{
temp=arr[i];
arr[i]=arr[i+1];
arr[i+1]=temp;
}
}
else
{
if((arr[i+1]%2)==0)
{
arr[i]=arr[i];
arr[i+1]=arr[i+1];
}
else if((arr[i+1]%2)!=0)
{
temp=arr[i];
arr[i]=arr[i+1];
arr[i+1]=temp;
}
}
}
for(i=0;i<=9;i++)
{
printf("Element #%d-->%d\n",i,arr[i]);
}
return 0;
}

Resources