bitwise operators??!?

This is a discussion on bitwise operators??!? within the C++ Programming forums, part of the General Programming Boards category; I am making minesweeper for the standard console... So every space is represented by a unsigned char. The first four ...

  1. #1
    Registered User Mr_Jack's Avatar
    Join Date
    Oct 2003
    Posts
    63

    bitwise operators??!?

    I am making minesweeper for the standard console... So every space is represented by a unsigned char. The first four bits tell how many mines are touching the space. The 5th bit is set if it has been revealed. The 6th bit is set if it is a mine. So I have 2 "mine fields". The real one and the one the user sees. The real one just keeps track of what spaces the mines are on. The user one is the one that keeps track of how many mines each space is touching. To reveal the board, there's a function called burn. It calls itself recursively. It's supposed to only call itself if the space it's looking at isn't revealed, but it does so anyway. Or it could be that the revealed bit wasn't set, but unless I am COMPLETELY stoned, this should set the revealed bit:
    Code:
    #define REVEALED 16
    user_field[number] |= REVEALED;
    That's excactly how I set it
    I don't know, I'm almost insane with frustration. I beg you to help me. Well, here's the code:
    Code:
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    using namespace std;
    
    #define MINE			32
    #define REVEALED		16
    #define MINECOUNT_MASK	15
    #define RM				9
    #define CM				6
    
    unsigned int 	field_size = 1;
    unsigned int 	difficulty;
    unsigned short 	field_len;
    unsigned char *	user_field;
    unsigned char *	real_field;
    
    void initarrays()
    {
    	int loop;
    	for (loop = 0; loop < field_len; loop++)
    		real_field[loop] = 0;
    	
    	for (loop = 0; loop < field_len; loop++)
    		user_field[loop] = 0;
    }
    
    void draw_board(bool board)
    {
    	unsigned short loop;
    	unsigned short rowcount = 0;
    	
    	if (board == true)
    	{
    		for (loop = 0; loop < field_len+1; loop++)
    		{
    			if (rowcount == field_size * RM)
    			{
    				cout << endl;
    				rowcount = 0;
    			}
    			rowcount++;
    			
    			if (user_field[loop] & REVEALED)
    			{
    				if (real_field[loop] & MINE)
    					cout << '*';
    				else
    					cout << (int)(real_field[loop] & MINECOUNT_MASK);
    			}
    			else
    				cout << "#";
    		}
    	}
    	
    	if (board == false)
    	{
    		for (loop = 0; loop < field_len+1; loop++)
    		{
    			if (rowcount == field_size * RM)
    			{
    				cout << endl;
    				rowcount = 0;
    			}
    			rowcount++;
    			
    			if (real_field[loop] & MINE)
    				cout << '*';
    			else
    				cout << (int)(real_field[loop] & MINECOUNT_MASK);
    		}
    	}
    }
    
    void burn(unsigned short s)
    {
    	//for testing
    	if (user_field[s] & REVEALED)
    	{
    		cout << "\nrevealed\n" << endl;
    		return;
    	}
    	
    	if (real_field[s] & MINE)
    	{
    		cout << "YOU LOSE" << endl;
    		return;
    	}
    	
    	user_field[s] |= REVEALED;
    	signed short up, down, left, right;
    	up = -(field_size * RM);
    	down = field_size * RM;
    	left = -1;
    	right = 1;
    	
    	char t = 0;
    	
    	if (s + up >= 0)
    	{
    		cout << "up" << endl;
    		if (real_field[s + up] & MINE)
    			user_field[s] += 1;
    		if ((!(real_field[s + up] & MINE)) && (!(user_field[s + up] & REVEALED)))
    			burn(s + up);
    	}
    	
    	if (s + down <= field_len)
    	{
    		cout << "down" << endl;
    		if (real_field[s + down] & MINE)
    			user_field[s] += 1;
    		if ((!(real_field[s + down] & MINE)) && (!(user_field[s + down] & REVEALED)))
    			burn(s + down);
    	}
    	
    	if ((field_size * RM) % s != 0)
    	{
    		cout << "left" << endl;
    		if (real_field[s + left] & MINE)
    			user_field[s] += 1;
    		if ((!(real_field[s + left] & MINE)) && (!(user_field[s + right] & REVEALED)))
    			burn(s + left);
    	}
    	
    	if ((field_size * RM) % (s + 1) != 0)
    	{
    		cout << "right" << endl;
    		if (real_field[s + right] & MINE)
    			user_field[s] += 1;
    		if ((!(real_field[s + right] & MINE)) && (!(user_field[s + right] & REVEALED)))
    			burn(s + right);
    	}
    	
    }
    
    void scatter_mines()
    {
    	unsigned short x, y, loop;
    	for (loop = 0; loop < field_size * difficulty * 3; loop++)
    	{
    		x = rand()%(field_size * RM);
    		y = rand()%(field_size * CM);
    		real_field[x + y * field_size * RM] |= MINE;
    	}
    }
    
    
    int main()
    {
    	srand(time(0));
    	
    	bool ok = false;
    	
    	while (!ok)
    	{
    		cout << "Enter size (1-7)" << endl;
    		cin >> field_size;
    		if (field_size <= 7 && field_size >= 1)
    			ok = true;
    	}
    	field_len = (field_size*RM*field_size*CM) - 1;
    	
    	ok = false;
    	while (!ok)
    	{
    		cout << "Enter difficulty (1-7)" << endl;
    		cin >> difficulty;
    		if (difficulty <= 7 && difficulty >= 1)
    			ok = true;
    	}
    	
    	user_field = new unsigned char[field_len];
    	real_field = new unsigned char[field_len];
    	
    	initarrays();
    	draw_board(true);
    	scatter_mines();
    	int burnn;
    	cout << "enter burn: ";
    	cin >> burnn;
    	burn(burnn);
    	draw_board(true);
    	
    	return 0;
    }
    I realize all of the things I have missed, and I have noted them and will fix them as soon as I get this part to work. Thanks for your help.

  2. #2
    Registered User
    Join Date
    Apr 2004
    Posts
    2

    usestd::bitset for bit manip..

    STL define bitset for bit manipulation so why reinvent the wheel..

  3. #3
    Registered User Mr_Jack's Avatar
    Join Date
    Oct 2003
    Posts
    63
    THE STL REINVENTED THE WHEEL YOU FOOL!!!! why else do you think the bitwise operators are built into the language?? PLEASE, all's I asked for is help!!! If you can't help, don't pick at my code! That is the one thing that ........es me off more than ANYTHING in the world

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >That is the one thing that ........es me off more than ANYTHING in the world
    And what is that? People suggesting improvements for your code?
    My best code is written with the delete key.

  5. #5
    Registered User Mr_Jack's Avatar
    Join Date
    Oct 2003
    Posts
    63
    Well, perhaps I was I bit harsh, but instead of trying to make me change my style, which I'm sure I asked no one to do, I would expect someone to help with the problem I asked for help for. This plus my bad mood cause I can't figure out what's going on = outburst of frustration. sorry...

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >but instead of trying to make me change my style
    If that's what you see it as...

    >I would expect someone to help with the problem I asked for help for
    You weren't very detailed in your bug description. Not many people are willing to trace your code and run test cases just to see what's wrong, they expect you to describe the problem exactly.

    The problem as I see it is that your recursion goes too far and you end up dividing by 0. The crashing problem is fixed by changing
    Code:
    if (s + up >= 0)
    to
    Code:
    if (s + up > 0)
    However, I'm not familiar enough with the game to know if the end result is what you want or not.
    My best code is written with the delete key.

  7. #7
    Registered User Mr_Jack's Avatar
    Join Date
    Oct 2003
    Posts
    63

    Talking

    thanks a lot that was the problem!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bitwise operators
    By gnewfenix in forum C Programming
    Replies: 2
    Last Post: 05-16-2009, 09:43 PM
  2. Bitwise Operators
    By rrc55 in forum C Programming
    Replies: 6
    Last Post: 04-30-2009, 12:37 PM
  3. Palindromes and Bitwise operators
    By Dr Tornillo in forum C Programming
    Replies: 8
    Last Post: 08-02-2007, 03:31 PM
  4. bitwise and arithmetic Operators
    By Whiteghost in forum C Programming
    Replies: 4
    Last Post: 12-28-2006, 02:13 PM
  5. Bitwise Operators, Help!!
    By Mini__C in forum C Programming
    Replies: 6
    Last Post: 07-14-2004, 05:20 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21