|
Page 1 of 1 |
|
Posted: Mon, 16th Apr 2012 00:12 Post subject: C++ Table to input array data into |
|
 |
Im making a basic mine sweeper game in C++ for college and I have the menu etc done now im onto the harder part, the game. Ive been consentrating so much on HTML, XML, PHP etc for webapp's part of the course that I have forgotten most of my c++
Im starting with a fixed array untill I get it going then will look into a random generated array.
Only one square is revealed at a time, not like actual mine sweeper where if you hit a blank square a load of them are cleared.
The main part Im stuck on is the best method for drawing the game table.
After looking at a few sites I noticed one used a char array to draw a table, is this a good idea or is there a better way when making a game like minesweeper?
so far I have -
Code: |
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
//easya = easy array
//easyb = easy board
const int easya [8][8] = {{0,0,1,1,1,0,0,0},{0,0,1,-1,3,2,1,0},{0,0,2,3,-1,-1,2,1},{0,0,1,-1,3,3,-1,2},{0,0,2,2,2,1,2,-1},{0,0,1,-1,2,1,2,1},{1,1,1,1,2,-1,2,1},{-1,1,0,0,1,1,2,-1}};
int x;
int y;
char easyb[8][8];
for(x=0;x<8;x++)
for (y=0;y<8;y++)
easyb[x][y] = char(254);
for(x=0;x<8;x++)
{
for (y=0;y<8;y++)
{
cout << easyb[x][y]<< " ";
}
cout << endl;
}
return 0;
}
|
is this a good way?
Thanks
George W Bush -
'...more and more of our imports are coming from overseas.'
|
|
Back to top |
|
 |
|
Posted: Mon, 16th Apr 2012 00:40 Post subject: |
|
 |
or am i looking at this completely wrong??
George W Bush -
'...more and more of our imports are coming from overseas.'
|
|
Back to top |
|
 |
|
Posted: Mon, 16th Apr 2012 00:42 Post subject: |
|
 |
You shouldn't be writing down the numbers in the array, but your program should calculate them itself.
So basically you should intiate a 9x9 array, then while i<10 place 10 mines in random positions (increase i if blank spot has been placed).
Initialize the array with zeroes. A placed mine should be a 9. Now check for all fields around the mine and increase the value of the fields +1 if the field value is lower than 8 of course.
Your field should be filled now and the numbers should be correct 
|
|
Back to top |
|
 |
|
Posted: Mon, 16th Apr 2012 00:44 Post subject: |
|
 |
Of course you'll need a second array of 9x9 that holds what the user can see. basically what he has revealed. You'll just display the second array This could be a boolean array for revealed and unrevealed fields. Now for each unrevealed field display a - and for each revealed field (you could initialize the array with false and set it to true for each revealed field) display the value stored in array A (and an X for a 9).
|
|
Back to top |
|
 |
|
Posted: Mon, 16th Apr 2012 00:51 Post subject: |
|
 |
Checking all fields around could be something like this:
Code: |
boolean checkField(x,y){
if (arena[x][y]!=9){ //if there is no mine on this place in the arena (don't forgt to make arena accessible, for example by defining it globally
arena[x][y]==9; //set a mine at this position
for (n=-1;n<2;n++){ //now we go through each field around the selected position
for (m=-1;m<2;m++){
if( (n!=0)&&(m!=0)){ //except for the one we just placed our mine
int xx = n+x;
int yy = m+y;
if ((xx>0)&&(xx<9)) //check if that field is within our arena
if ((yy>0)&&(yy<9)){
int a = arena[xx][yy]; //take the value that is already defined
if (a<8) //and check if it hasn't already reached the maximum amount of mine warning
arena[xx][yy] = a+1; // and increase the mine awareness level
}
}
}
}
return true;
} else return false; //if thee was a mine on this field return false so that the outer loop can select new random numbers
}
|
and in your code your random placement code you just check if checkfield returned true, to increase i, if not loop through the while again without increasing i to generate new random positions for x and y 
|
|
Back to top |
|
 |
|
Posted: Mon, 16th Apr 2012 11:28 Post subject: |
|
 |
Thanks, Will have a read through that
I was going with a fixed array at first to make it easier and then move onto a random array but will look into that way you suggested.
So using a char array for displaying the board is ok? char(254) is a Solid square like this but solid []
Thanks
George W Bush -
'...more and more of our imports are coming from overseas.'
|
|
Back to top |
|
 |
|
Posted: Thu, 19th Apr 2012 23:06 Post subject: |
|
 |
Checked with the tutor and he said dont worry about it, as long as it works in Firefox with no problems then its fine.
He said you can spend days messing around trying to sort it so not to bother.
George W Bush -
'...more and more of our imports are coming from overseas.'
|
|
Back to top |
|
 |
|
Posted: Fri, 20th Apr 2012 00:08 Post subject: |
|
 |
As long as it works in firefox? C++ code? The fuck is your tutor smoking 
|
|
Back to top |
|
 |
|
|
Back to top |
|
 |
Page 1 of 1 |
All times are GMT + 1 Hour |
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB 2.0.8 © 2001, 2002 phpBB Group
|
|
 |
|