Don't allow out of board

Hi guys,

So I have a question... I have a board, and I don't want the user to write anything where the board isn't defined.
For example, if I have a 10X10 board, and a ship of size 4, I don't want the user to type "I a Vertical" or "9 1 Vertical", because that way it will go down the board...
So, I have this piece of code, but it doesn't work the way I want!
Can you tell me what's wrong?

PS: In this case, the n_columns would be 10 and n_lines would be 10 too. The column_position would be 1, and the line_position 9. The ship_size 4.

1
2
3
4
5
6
7
8
9
10
11
if (orientacion == 'h' || orientacion == 'H')
	while ((int)colum_position + ship_size > 97 + n_columns)
	{
           //do stuff
	}

if (orientation== 'v' || orientacion == 'V')
	while ((int)line_position + ship_size > 65 + n_lines)
	{
	   //do stuff
	}
Last edited on
What do 97 and 65 mean? If these are supposed to be ASCII 'a' and 'A' then write those constants. The code will be easier to read.

Otherwise, I'll point out that if colum_position is 10,000,000 then the while loop condition at line 2 will be true. Should the condition be less-than instead of greater-than?
Oh, I didn't knew that was possible. Thanks! :D

But if the condition is less-than, if I do like "1 1 H" (which would be possible), it will trigger that code and don't allow to write on the board...
Topic archived. No new replies allowed.