-
Minesweeper
Im not done with the game yet, but so far when it prints, the numbering of the rows isnt lined up properly. Ive tried several different ways, but I cant seem to figure it out, yet it seems it should be an easy fix.
Code:
#include <stdio.h>
#include <stdlib.h>
#define MAX_ROW 100
#define MAX_COL 100
char game[MAX_ROW][MAX_COL] = {0};
unsigned char display[MAX_ROW][MAX_COL] = {0};
int m, n;
unsigned char difficulty = 1;
int i, j;
int r, c;
int x, y;
unsigned char hit_a_mine = 0;
int main (int argc, char * argv[]) {
if (argc != 4) {
printf("Invalid\n");
} else {
m = atoi(argv[1]);
n = atoi(argv[2]);
}
play();
}
int play() {
for (x = 0; x < m; x++) {
for (y = 0; y < n; y++) {
game[x][y] = '.';
}
}
for (i = 0; i < 10; i++) {
x = rand() % m;
y = rand() % n;
game[x][y] = '*';
}
for (y = 0; y < n; y++) {
printf("%3s%d", " ", y);
}
for (x = 0; x < m; x++) {
printf("\n%d\n", x);
for (y = 0; y < n; y++) {
printf("%3s%c", " ", game[x][y]);
}
printf("\n");
}
//printf("Enter row and column: ");
//scanf("%d %d", &r, &c);
return 0;
}
-
I understand that the new line im inserting after every row is why its not lining up with game[x][y]. But I didnt want to crunch the game into a small space. I would like to evenly space out everything to read easily.
-
Is it simply because you have a \n at the end of this printf?
Code:
printf("\n%d\n", x);
Other problems:
It's always best to use local instead of global variables when you can.
You're accepting 3 command-line arguments from argv in main, but only using 2 of them. Also, if argc != 4, you print "Invalid" but then continue on with the game. You should exit.
Don't make play return an int for no reason. Make it void. And you should have it's prototype before main.
You should return 0 at the end of main.
-
Thanks oogabooga. Im not quite finished with the game, but the third argument I havent used is for the difficulty level. I'll probably do that last since that will be the easiest thing to do. And yes, the new line im inserting after every row is definitely why its not lining up, but I couldnt figure out how to space the numbering of the rows and columns evenly without the new line there. I guess my question is, how should I insert the new line.
-
I see. Here's a quick rewrite of your program with local variables and (perhaps) somewhat better variable names and better spacing. I decided to interpret your 3rd parameter as the number of mines for now (you can change that).
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_ROWS 40
#define MAX_COLS 40
void play(int cols, int rows, int num_mines);
int main (int argc, char * argv[]) {
int cols, rows, num_mines;
if (argc != 4) {
printf("Invalid parameters.\n");
return 1;
} else {
cols = atoi(argv[1]);
rows = atoi(argv[2]);
// You should check that cols and rows are not out-of-range.
num_mines = atoi(argv[3]);
}
play(cols, rows, num_mines);
return 0;
}
void play(int cols, int rows, int num_mines) {
char game[MAX_ROWS][MAX_COLS];
int x, y, i;
memset(game, '.', MAX_ROW * MAX_COL);
for (i = 0; i < num_mines; i++) {
do {
x = rand() % cols;
y = rand() % rows;
} while (game[y][x] == '*');
game[y][x] = '*';
}
printf(" ");
for (x = 0; x < cols; x++) {
printf(" %2d", x);
}
for (y = 0; y < rows; y++) {
printf("\n%2d", y);
for (x = 0; x < cols; x++) {
printf(" %c", game[y][x]);
}
printf("\n");
}
}