Few questions about minesweeper
| Author |
Message |
31201558
|
Posted: Wed Dec 29, 2010 4:28 pm Post subject: RE:Few questions about minesweeper |
|
|
........
Never mind
I found the tutorial |
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
31201558
|
Posted: Wed Dec 29, 2010 5:34 pm Post subject: RE:Few questions about minesweeper |
|
|
var counter : array 0 .. 16, 0 .. 16 of int
for n : 0 .. 600 by 40
for j : 0 .. 16
for k : 0 .. 16
var boxx : array 1 .. 15 of int := init (n)
var boxy : array 1 .. 15 of int := init (n)
drawfillbox (boxx (j), boxy (k), boxx (j) + 40, boxy (k) + 40, 7)
counter (j, k) := 0
end for
end for
end for
Here's my code for my counter(it can count the mine around it) and to draw all the grid in minesweeper.
I'm trying to set all the cell's counter at 0
But I don't know how to set values for an array
Is that even possible? |
|
|
|
|
 |
Tony

|
Posted: Wed Dec 29, 2010 5:56 pm Post subject: RE:Few questions about minesweeper |
|
|
| First of all, I don't understand why you are even creating boxx and boxy arrays 4000+ times, but to answer your question regarding setting values for an (entire) array -- the error message you receive should be descriptive enough. If not, the correct question to ask would be about that particular message. |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
 |
31201558
|
Posted: Wed Dec 29, 2010 8:13 pm Post subject: RE:Few questions about minesweeper |
|
|
I guess I'm really bad at explain questions
The question I want to ask is how do I give values for each variables in my array
for n : 0 .. 540 by 40 %these are 15 numbers
var boxx : array 1 .. 15 of int := init (n) %These are 15 variables
end for
see: for boxx(1) it value is 0, for boxx(2) it value is 40, for boxx(3) it value is 80. That's what I'm trying to do.
In tutorial it says, var mensNames : array 1 .. 3 of string := init ( "Tom", "Dick", "Harry" )
I guess the init feature is for giving values to arrays. |
|
|
|
|
 |
31201558
|
Posted: Wed Dec 29, 2010 8:16 pm Post subject: RE:Few questions about minesweeper |
|
|
The message says :
Compile time expression expected |
|
|
|
|
 |
31201558
|
Posted: Wed Dec 29, 2010 8:37 pm Post subject: RE:Few questions about minesweeper |
|
|
| Just make sure you saw both of my reply..... |
|
|
|
|
 |
Tony

|
Posted: Wed Dec 29, 2010 9:00 pm Post subject: RE:Few questions about minesweeper |
|
|
One of the best skills you could pick up (and not just for programming) is the ability to ask the right questions. Takes time and practice though. The right questions will usually lead you to the correct documentation -- reading and interpreting that is the next important life skill to have.
The way init works is that it initializes the entire array. In the example you've posted, there are 3 elements and so init has 3 values. 1st element gets 1st value, etc.
"Compile time expression expected" is telling you that the expression (the init statement) needs to be available at the compile time (all the values known at the time when you compile the program, not during the run-time).
To change the values in an array during the run-time, you'd have to do it one element at a time. For-loops are usually helpful. |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
 |
TokenHerbz

|
Posted: Thu Dec 30, 2010 9:20 am Post subject: RE:Few questions about minesweeper |
|
|
| code: |
var numbers: array 1 .. 15 of int
for n: 1 .. 15
numbers(n) := n * 40
put numbers(n), " "..
end for
|
|
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
31201558
|
Posted: Mon Jan 03, 2011 12:29 am Post subject: Re: Few questions about minesweeper |
|
|
Here's my code now
| Turing: |
var mx, my, mb, font : int
var dead := false
var cells : array 1 .. 9, 1 .. 9 of string
var value : array 1 .. 9, 1 .. 9 of int
var minex, miney : array 1 .. 10 of int
buttonchoose ("multibutton")
for a : 0 .. 320 by 40
for b : 0 .. 160 by 20
drawfillbox (a, b, a + 38, b + 38, 7)
end for
end for
for i : 1 .. 9
for j : 1 .. 9
cells (i, j ) := "not mine"
value (i, j ) := 0
end for
end for
%mines
for i : 1 .. 10
minex (i ) := Rand.Int (0, 359)
miney (i ) := Rand.Int (0, 359)
minex (i ) := minex (i ) - minex (i ) mod 40
miney (i ) := miney (i ) - miney (i ) mod 40
drawdot (minex (i ), miney (i ), green)
end for
for i : 1 .. 9
for j : 1 .. 9
cells (minex (i ) div 40, miney (i ) div 40) := "mine"
end for
end for
for i : 1 .. 9
for j : 1 .. 9
if cells (i, j ) = "mine" then
if j + 1 <= 9 then
if cells (i, j + 1) not= "mine" then
value (i, j + 1) + = 1
end if
end if
if i + 1 <= 9 and j + 1 <= 9 then
if cells (i + 1, j + 1) not= "mine" then
value (i + 1, j + 1) + = 1
end if
end if
if i + 1 <= 9 then
if cells (i + 1, j ) not= "mine" then
value (i + 1, j ) + = 1
end if
end if
if i + 1 <= 9 and j - 1 > 0 then
if cells (i + 1, j - 1) not= "mine" then
value (i + 1, j - 1) + = 1
end if
end if
if j - 1 > 0 then
if cells (i, j - 1) not= "mine" then
value (i, j - 1) + = 1
end if
end if
if i - 1 > 0 and j - 1 > 0 then
if cells (i - 1, j - 1) not= "mine" then
value (i - 1, j - 1) + = 1
end if
end if
if i - 1 > 0 then
if cells (i - 1, j ) not= "mine" then
value (i - 1, j ) + = 1
end if
end if
if i - 1 > 0 and j + 1 <= 9 then
if cells (i - 1, j + 1) not= "mine" then
value (i - 1, j + 1) + = 1
end if
end if
end if
end for
end for
for i : 1 .. 9
for j : 1 .. 9
if value (i, j ) > 0 then
cells (i, j ) := "num cells" + intstr (value (i, j ))
end if
end for
end for
loop
mousewhere (mx, my, mb )
mx := mx - mx mod 40
my := my - my mod 40
for i : 1 .. 10
if mx = minex (i ) and my = miney (i ) and mb = 1 then
dead := true
end if
end for
var a : int
var b : int
a := mx div 40
b := my div 40
if cells (a, b ) = "num cells" and mb = 1 then
font := Font.New ("serif:36")
Draw.Text (intstr (value (a, b )), mx, my, font, red)
end if
if cells (a, b ) = "not mine" and mb = 1 then
drawfillbox (mx + 1, my + 1, mx + 37, my + 37, white)
end if
exit when dead
delay (100)
end loop
|
When I run this it says "Array subscript is out of range. Why?
The code is have problem is : cells (minex (i) div 40, miney (i) div 40) := "mine"
Well, it's the midnight. The due time is 15 hours later. Please just tell me the answer completely( Please just give me some code and tell me where do I put it. I know this is not good, but.... I've got no time.....) |
|
|
|
|
 |
Tony

|
Posted: Mon Jan 03, 2011 12:47 am Post subject: RE:Few questions about minesweeper |
|
|
You have 15 hours, that's plenty of time! Try
| code: |
for i : 1 .. 9
for j : 1 .. 9
put "trying to access: ", minex (i) div 40, ", ", miney (i) div 40
cells (minex (i) div 40, miney (i) div 40) := "mine"
end for
end for
|
|
Tony's programming blog. DWITE - a programming contest. |
|
|
|
 |
31201558
|
Posted: Mon Jan 03, 2011 1:00 am Post subject: RE:Few questions about minesweeper |
|
|
I don't really understand
it just output lots of numbers |
|
|
|
|
 |
Tony

|
Posted: Mon Jan 03, 2011 1:12 am Post subject: RE:Few questions about minesweeper |
|
|
| Until it crashes. Then you know what number was out of array's bounds. |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
 |
31201558
|
Posted: Mon Jan 03, 2011 1:29 am Post subject: RE:Few questions about minesweeper |
|
|
| but each time it's different |
|
|
|
|
 |
Tony

|
|
|
|
 |
31201558
|
Posted: Mon Jan 03, 2011 1:33 am Post subject: RE:Few questions about minesweeper |
|
|
| Oh~~~~~ I see, but still don't know what to do. Should I change all the 1..9 to 0..8? |
|
|
|
|
 |
|
|