C - Trying to create minesweeper game [closed] - c

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed last month.
Improve this question
I am trying to practice my coding skills and recreate the minesweeper game in visual studio 2022. I am having trouble coding how many mines are touching a square within my grid. It keeps coming up with random numbers instead of how many mine it is touching. Hoping someone can point me in the right direction of where I am currently going wrong.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ROWS 10
#define COLUMNS 10
#define MINES 10
int grid[ROWS][COLUMNS];
int revealed[ROWS][COLUMNS];
void init()
{
int i, j;
for (i = 0; i < ROWS; i++)
{
for (j = 0; j < COLUMNS; j++)
{
grid[i][j] = 0;
revealed[i][j] = 0;
}
}
}
void placeMines()
{
int i, x, y;
for (i = 0; i < MINES; i++)
{
x = rand() % ROWS;
y = rand() % COLUMNS;
if (grid[x][y] == -1)
{
i--;
}
else
{
grid[x][y] = -1;
}
}
}
void countAdjacentMines()
{
int i, j, count;
for (i = 0; i < ROWS; i++)
{
for (j = 0; j < COLUMNS; j++)
{
if (grid[i][j] == -1)
{
continue;
}
count = 0;
if (i > 0 && j > 0 && grid[i - 1][j - 1] == -1) count++;
if (i > 0 && grid[i - 1][j] == -1) count++;
if (i > 0 && j < COLUMNS - 1 && grid[i - 1][j + 1] == -1) count++;
if (j < COLUMNS - 1 && grid[i][j + 1] == -1) count++;
if (i < ROWS - 1 && j < COLUMNS - 1 && grid[i + 1][j + 1] == -1) count++;
if (i < ROWS - 1 && grid[i + 1][j] == -1) count++;
if (i < ROWS - 1 && j > 0 && grid[i + 1][j - 1] == -1) count++;
if (j > 0 && grid[i][j - 1] == -1) count++;
grid[i][j] = count;
}
}
}
void printGrid()
{
int i, j;
printf(" ");
for (i = 0; i < COLUMNS; i++)
{
printf("%d ", i);
}
printf("\n");
for (i = 0; i < ROWS; i++)
{
printf("%d ", i);
for (j = 0; j < COLUMNS; j++)
{
if (revealed[i][j])
{
if (grid[i][j] == -1)
{
printf("* ");
}
else
{
printf("%d");
}
}
else
{
printf("X ");
}
}
printf("\n");
}
}
void main()
{
int x, y;
srand((unsigned int)time(NULL));
init();
placeMines();
countAdjacentMines();
while (1)
{
printGrid();
printf("Enter the coordinates of the cell to reveal (x y): ");
scanf("%d %d", &x, &y);
if (x < 0 || x >= ROWS || y < 0 || y >= COLUMNS)
{
printf("Invalid coordinates!\n");
}
else if (revealed[x][y])
{
printf("Cell already revealed!\n");
}
else
{
revealed[x][y] = 1;
if (grid[x][y] == -1)
{
printf("You hit a mine! Game over.\n");
break;
}
}
}
return 0;
}

Look at the print grid function, %d requires a value, in this case i suppose it is
printf("%d ",grid[i][j]);
judging by the rest of the code.
Also I recommend learning how to use gdb for these purposes, placing breakpoints and seeing how the code evolves, or even using the gcc flag -Wall, to find some minor errors that can cause these such anomalies.

Related

Printing a christmas tree using stars and dots in C

I'm trying to write a program that prints out a christmas tree that looks like this:
The user inputs the height, in this example the height is 6. If the input is in range from 0 to 3, the height should be 3, because otherwise it's not printable, and if the input is less than 0, the program should terminate.
My code for some odd reason is infinitely printing the 'Input height'. Where is the error?
Here's my code snippet:
#include <stdio.h>
void main(){
int i, j, n, s;
while (1){
printf("Input height: ");
scanf("%d", &n);
if (n < 0) break;
if (n == 0 || n == 1 || n == 2 || n == 3)
s == 3;
else
s == n;
for (i = 0; i < s; i++){
for (j = 0; j < 2*s - 1; j++){
if (j > s - (i - 1) && j < (s + (i - 1)) - 1)
printf("*.");
if (j == s + (i - 1))
printf("*");
else
printf(" ");
}
printf("\n");
}
for (j = 0; j < 2*s - 1; j++){
if (j == s - 1 || j == s || j == s + 1)
printf("*");
else
printf(" ");
}
}
}
The lines: s == 3; and s == n; do absolutely nothing.
== is a comparison, not an assignment.
Here is much better code:
#include <stdio.h>
int main(void) {
int n = 8;
char row[2*n];
for( int i=0; i<2*n-1; i+=2 )
{
strcpy(row+i, "*.");
}
for(int i=0; i<n; ++i)
{
printf("%*.*s\n", n+i+1, 2*i+1, row);
}
printf("%*s\n", n+2, "***");
return 0;
}
Result:
Success #stdin #stdout 0s 5464KB
*
*.*
*.*.*
*.*.*.*
*.*.*.*.*
*.*.*.*.*.*
*.*.*.*.*.*.*
*.*.*.*.*.*.*.*
***
With a little creativity, I made the program even shorter with only a single for-loop.
#include <stdio.h>
int main(void) {
int n = 8;
char row[2*n];
strcpy(row, "*");
for( int i=0; i<n; ++i )
{
printf("%*s\n", n+i, row);
strcat(row, ".*");
}
printf("%*s\n", n+1, "***");
return 0;
}
As mentioned by others there are some issues with you mixing up == and =.
I will be posting a version that prints out the christmas tree but leaves out the . that you also want to include, as you should be able to finish it yourself.
#include <stdio.h>
int main()
{
int i, j, n, s;
while (1)
{
printf("Input height: ");
scanf("%d", &n);
// if input is negative, exit
if (n < 0)
{
break;
}
// if input is 0,1,2 or 3 change to 3
if (n == 0 || n == 1 || n == 2 || n == 3)
{
s = 3;
}
else
{
s = n;
}
// loop through each row
for (i = 0; i < s; i++)
{
// loop through each column
for (j = 0; j < 2 * s - 1; j++)
{
// if column is within the tree print a star
if (j >= s - i - 1 && j <= s + i - 1)
{
printf("*");
}
else
{
printf(" ");
}
}
printf("\n");
}
// print base of tree
for (j = 0; j < 2 * s - 2; j++)
{
// if column is part of base print star
if (j == s - 2 || j == s - 1 || j == s)
{
printf("*");
}
else
{
printf(" ");
}
}
printf("\n");
}
return 0;
}
A simple solution:
#include <stdio.h>
int main(){
int i=0, j=0, n=0, s=0;
while (1){
printf("Input height: ");
scanf("%d", &n);
printf("\n");
if (n < 0) break;
s = (n <= 3) ? 3 : n;
for (i=0; i < s; ++i){ // rows
for (j=0; j < s-i; ++j) // white spaces
printf(" ");
for (int k=0; k < i; ++k) // *.
printf("*.");
printf("*\n"); // always, unique or last *
}
for (i=0; i < s-1 ; ++i)
printf(" ");
printf("***\n\n");
}
return 0;
}

How to successfully take a step back and preserve the shape? C programming - loops

I wrote this code, but I have one error that I can't fix, the problem is that the code works well but actually what my program prints to the number n: 3 should be to n: 2 you can see it in the picture. when 5 is actually 4 etc... When I fix the loops I move the look of the lines and then it's no longer a diamond.
#include <stdio.h>
int main() {
int i, j, n;
printf("n: ");
scanf("%d", &n);
for (j = 1; j < n; j++) {
printf(" ");
}
printf("+");
printf(" \n");
for (i = 2; i < n; i++) {
for (j = 1; j <= n - 1; j++) {
if ((i + j) == (n + 1))
printf("/");
else
printf(" ");
}
for (j = 1; j < n; j++) {
if (i == j)
printf("\\");
else
printf(" ");
}
printf("\n");
}
for (i = 2; i < n; i++) {
for (j = 1; j < n; j++) {
if (i == j)
printf("\\");
else
printf(" ");
}
for (j = 1; j <= n - 1; j++) {
if ((i + j) == (n + 1))
printf("/");
else
printf(" ");
}
printf("\n");
}
for (j = 1; j < n; j++) {
printf(" ");
}
printf("+");
return 0;
}
A quick and dirty fix is adding 1 to n after the scanf("%d", &n);
Here is a modified version taking advantage of the printf width feature:
#include <stdio.h>
int main() {
int i, n;
printf("n: ");
if (scanf("%d", &n) != 1)
return 1;
printf("%*s\n", n, "+");
for (i = 1; i < n; i++) {
printf("%*s%*s\n", n - i, "/", i * 2, "\\");
}
for (i = 1; i < n; i++) {
printf("%*s%*s\n", i, "\\", (n - i) * 2, "/");
}
printf("%*s\n", n, "+");
return 0;
}
It seems that just adding 1 to n would solve your problem.

How to print diamond shape pattern in C?

how to print diamond shape pattern like this?
the pattern isn't commonly using "*" only but it't modified with "."
The input:
5
The output:
....0.....
...000....
..0.0.0...
.0..0..0..
000000000.
.0..0..0..
..0.0.0...
...000....
....0.....
Here my code:
#include <stdio.h>
int main()
{
int number, i, k, count = 1;
scanf("%d", &number);
count = number - 1;
for (k = 1; k <= number; k++)
{
for (i = 1; i <= count; i++)
printf(".");
count--;
for (i = 1; i <= 2 * k - 1; i++)
printf("0");
printf("\n");
}
count = 1;
for (k = 1; k <= number - 1; k++)
{
for (i = 1; i <= count; i++)
printf(".");
count++;
for (i = 1 ; i <= 2 *(number - k)- 1; i++)
printf("0");
printf("\n");
}
return 0;
}
Think of a Cartesian coordinate system whose origin is the center of symmetry of the diamond pattern to be drawn. The 0s reside in the lines y = x + (n - 1), y = -x + (n - 1), y = x - (n - 1), y = -x - (n - 1), and the axes y = 0, x = 0. Then we can easily codify this information to draw the diamond pattern:
#include <stdio.h>
void print_diamond (int n)
{
--n;
for (int y = n; y >= -n; --y) {
for (int x = -n; x <= n; ++x)
putchar(x == 0 || y == 0
|| y == x + n || y == -x + n || y == x - n || y == -x - n
? '0' : '.');
putchar('\n');
}
}
int main (void)
{
print_diamond(5);
}
The line
|| y == x + n || y == -x + n || y == x - n || y == -x - n
can be reduced to
|| abs(x - y) == n || abs(x + y) == n
after stdlib.h is #included.
I rapidly wrote this for fun keeping your approach, however this is not the most efficient nor the most elegant way of doing this
int number, i, k, count = 1;
scanf("%d", &number);
for (k = 0; k < number-1; k++){
i = 1;
while(i < number-k){
printf(".");
i++;
}
printf("0");
i++;
while(i< number){
printf(".");
i++;
}
if(i==number){
printf("0");
i++;
}
while(i<number+k){
printf(".");
i++;
}
if(k>0){
printf("0");
i++;
}
while(i<number*2){
printf(".");
i++;
}
printf("\n");
}
You will probably find better answers if you search a bit deeper
Also, you can just
void printpir(int number, int k){
int i = 1;
while(i < number-k){
printf(".");
i++;
}
printf("0");
i++;
while(i< number){
printf(".");
i++;
}
if(i==number){
printf("0");
i++;
}
while(i<number+k){
printf(".");
i++;
}
if(k>0){
printf("0");
i++;
}
while(i<number*2){
printf(".");
i++;
}
printf("\n");
}
int main(int argc, char** argv) {
int number, i, k, count = 1;
scanf("%d", &number);
for (k = 0; k < number-1; k++){
printpir(number, k);
}
for(i= 1; i<number*2; i++){
printf("0");
}
printf("\n");
k--;
for (; k >= 0; k--)
{
printpir(number, k);
}
return 0;
}
Here is a solution with only 2 for-loops. I can explain my answer if you need it.
Hope you can understand...
#include <stdio.h>
#include <stdlib.h>
int main() {
int n, _i, i, _j, j, k;
printf("Number: ");
scanf("%d", &n);
for (_i = 1; _i < 2*n; _i++) {
i = abs(_i - n);
for (_j = 1; _j <= 2*n; _j++) {
j = abs(_j - n);
if (j < n-i) {
if (i==0 || j == 0 || (i+j==n-1)) {
printf("0");
} else {
printf(".");
}
} else {
printf(".");
}
}
printf("\n");
}
return 0;
}

Printf spaces in front of pattern in C

my code is the following:
if (a == 4)
{
if (n >= 3 && n <= 18)
{
width = n + 2;
for (a = 1; a <= width; a++)
{
printf("*");
}
printf("\n");
for (i = 1; i <= n; i++)
{
for (j = 1; j <= n; j++)
{
if (i == 1 || i == n || j == 1 || j == n)
{
printf("*");
}
else
{
printf(" ");
}
}
printf("\n");
}
}
This code prints a pattern of lines depending on n.
e.g. For n = 7
*********
*******
* *
* *
* *
* *
* *
*******
What I am trying to do is to print a space in front of every line like this
*********
*******
* *
* *
* *
* *
* *
*******
It should print the empty box in the middle of the upper line.
Add a printf(" "); between the 2 loops.
for (i = 1; i <= n; i++)
{
printf(" ");
for (j = 1; j <= n; j++)
{
...
}
printf(" \n");
}
like this code:
#include<stdio.h>
int main() {
int n = 7;
if (n >= 3 && n <= 18)
{
int width = n + 2;
for (int i = 0; i != width; ++i)
printf("*");
printf("\n");
for (int i = 0; i != n; ++i)
{
printf(" ");
for (int j = 0; j != n; ++j)
{
if (i == 0 || i == n - 1 || j == 0 || j == n - 1)
printf("*");
else
printf(" ");
}
printf("\n");
}
}
return 0;
}

C programming two-hop neighbors

I am not sure how to get my two-hop neighbors correctly. It's almost correct but on my output, I don't want to include the same vertex. For my output now if vertex 0 is 0, it says "vertex 0: 0.....
I want to skip the vertex it is currently looking at.
Please help me, are my codes for two hop wrong?
this is my codes:
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<time.h>
#define M 20
#define N 20
int main()
{
int i, j, x, a, b;
int G[20][20] = { { 0 } };
/*creaate random adjaceney matrix*/
printf("==================================================\n");
printf("Welcome to my Graph Processing tool!\n\n");
srand(time(NULL));
for (i = 0; i < M; i++) {
for (j = 0; j < N; j++) {
if (i == j) {
G[i][j] = 0;
}
else {
G[i][j] = rand() % 2;
G[j][i] = G[i][j];
}
}
}
/*check whether the whole row equals to 0*/
for (j = 0; j < N; j++) {
if (G[j] == 0) {
x = rand() % 20 + 1;
G[x][j] = G[j][x] = 1;
}
/*print the matrix G*/
else
{
printf("The adjacency for graph G is\n");
for (i = 0; i < M; i++) {
for (j = 0; j < N; j++) {
printf("%d ", G[i][j]);
}
printf("\n");
}
}
}
/*all one-hop neighbors*/
printf("\nList of one-hop neighbors:");
for (i = 0; i < M; i++) {
printf("\nVertex %d: ", i);
for (j = 0; j < N; j++) {
if (G[i][j] == 1) {
printf("%d ", j);
}
}
}
printf("\n===================================\n\n");
/*two-hop neighbors*/
for (i = 0; i < M; i++) {
printf("\nVertex %d: ", i);
for (j = 0; j < N; j++) {
if (G[i][j] == 0) {
printf("%d ", j);
}
}
}
}
printf("\n============================================\n");
system("pause");
return 0;
}
This is my output:
One hop
Two hop
The answer provided by #Jake only works for node 0. For only looking at different nodes you need to do
for (j = 0; j < N; j++) {
if (i != j && G[i][j] == 0) {
printf("%d ", j);
}
}
Furthermore, you are assuming that all nodes without an edge are two-hop neighbors. This is not correct. One way to calculate the actual two-hop neighbors would be ((A + I)^2 > 0) - ((A + I) > 0), where I is the identity matrix.
Also, you can code this via a three-layer loop:
int node, neighbor, neighbor2;
for (node = 0; node < N; node++) {
printf("\nVertex %d: ", node);
for (neighbor = 0; neighbor < N; neighbor++) {
if (G[node][neighbor] == 1) {
for (neighbor2 = 0; neighbor2 < N; neighbor2++) {
if (node != neighbor2 && G[neighbor][neighbor2] == 1) {
printf("%d ", neighbor2);
}
}
}
}
}
Note that per definition M=N, so I've just used N. Also, this might print some 2-hop neighbors twice. You might want to do some filtering before printing.
Couple things to note here.
Be more descriptive with your variable naming, it would have made this a lot easier to read.
M-ROWS, N-COLS, G-Graph
When you loop through each row, you initialize j to 0. This includes the vertex that you are wanting to leave out.
for (j = 1; j < N; j++)

Resources