Posted: Wed Mar 18, 2009 4:43 pm Post subject: Need help for my minesweeper game
I am trying to make a minesweeper game but i got stuck and don't know what to do
My intention is to check the surrounding tiles for every tile that does not contain any mine
and put the number of mines around it in that tile
but when i execute it, only 1s appear in the tiles and some tiles don't even have a number in it even though it's beside a mine
Can anyone help me?
These are the codes so far
Turing:
("graphics:240;240, title: Minesweeper, nobuttonbar, offscreenonly") Mouse.ButtonChoose("multibutton") const LEN_X :=9 const LEN_Y :=9 var tile :array1.. LEN_X, 1.. LEN_Y ofstring var mine :int var mine_counter :=0 var num :array1.. 8ofint var reveal :array1.. LEN_X, 1.. LEN_Y ofboolean var num_counter :=0 var mx, my, button :=0
for i :1.. LEN_X
for j :1.. LEN_Y
tile (i, j):="b"
reveal (i, j):=false endfor endfor
loop for i :1.. LEN_X
for j :1.. LEN_Y
randint(mine, 1, 100) if mine =1and mine_counter not= 10then if tile (i, j)="b"then
tile (i, j):="m"
mine_counter +=1 endif endif endfor endfor exitwhen mine_counter =10 endloop
proc up (i, j :int) if tile (i, j + 1)="m"then
num_counter +=1 endif end up
proc down (i, j :int) if tile (i, j - 1)="m"then
num_counter +=1 endif end down
proc left (i, j :int) if tile (i - 1, j)="m"then
num_counter +=1 endif end left
proc right (i, j :int) if tile (i + 1, j)="m"then
num_counter +=1 endif end right
proc up_left (i, j :int) if tile (i - 1, j + 1)="m"then
num_counter +=1 endif end up_left
proc up_right (i, j :int) if tile (i + 1, j + 1)="m"then
num_counter +=1 endif end up_right
proc down_left (i, j :int) if tile (i - 1, j - 1)="m"then
num_counter +=1 endif end down_left
proc down_right (i, j :int) if tile (i + 1, j - 1)="m"then
num_counter +=1 endif end down_right
for i :1.. LEN_X
for j :1.. LEN_Y
if tile (i, j)="b"then if i =1and j =1then
up (i, j)
up_right (i, j)
right (i, j) elsif j > 1and j < LEN_Y and i =1then
up (i, j)
up_right (i, j)
right (i, j)
down_right (i, j)
down (i, j) elsif i =1and j = LEN_Y then
right (i, j)
down_right (i, j)
down (i, j) elsif i > 1and i < LEN_X and j = LEN_Y then
right (i, j)
down_right (i, j)
down (i, j)
down_left (i, j)
left (i, j) elsif i = LEN_X and j = LEN_Y then
down (i, j)
down_left (i, j)
left (i, j) elsif i = LEN_X and j > 1and j < LEN_Y then
down (i, j)
down_left (i, j)
left (i, j)
up_left (i, j)
up (i, j)
elsif i = LEN_X and j =1then
left (i, j)
up_left (i, j)
up (i, j) elsif i > 1and i < LEN_X and j =1then
right (i, j)
left (i, j)
up_left (i, j)
up (i, j)
up_right (i, j) else
up (i, j)
up_right (i, j)
right (i, j)
down_right (i, j)
down (i, j)
down_left (i, j)
left (i, j)
up_left (i, j) endif if num_counter > 1then
tile (i, j):="n" + intstr(mine_counter) endif
num_counter :=0 endif endfor endfor
proc back
for i :0.. 160by20 for j :0.. 160by20 drawbox(i + 30, j + 30, i + 50, j + 50, black) ifnot reveal (i div20 + 1, j div20 + 1)then
drawfill (i + 40, j + 40, grey, black) endif endfor endfor end back
back
Posted: Wed Mar 18, 2009 6:06 pm Post subject: RE:Need help for my minesweeper game
Think about how you would do this on pencil and paper. Get some graph paper, place some mines on it, and solve the problem manually. Either use that as the inspiration for how to solve the problem algorithmically, or tell us what you came up with, and we'll help you.
TheFerret
Posted: Wed Mar 18, 2009 6:14 pm Post subject: RE:Need help for my minesweeper game
Something that will help with this problem is recursion...
The_Bean
Posted: Wed Mar 18, 2009 6:36 pm Post subject: Re: Need help for my minesweeper game
He actually has it working somehow in that mess of stuff, he just messed up 2 simple things.
Turing:
if num_counter > 1then
tile (i, j):="n" + intstr(mine_counter)
Should be
Turing:
if num_counter >= 1then
tile (i, j):="n" + intstr(num_counter)
There is a much shorter and simpler way though.
Johnny19931993
Posted: Wed Mar 18, 2009 7:26 pm Post subject: Re: Need help for my minesweeper game
The_Bean @ Wed Mar 18, 2009 6:36 pm wrote:
There is a much shorter and simpler way though.
really?! Can you teach me? Cuz this program I made confused myself
DemonWasp
Posted: Thu Mar 19, 2009 12:38 am Post subject: RE:Need help for my minesweeper game
You already know the locations of all of the mines (either because you've got them marked in a grid, or because you've got a list of all their locations). For each mine, just add 1 to all of the counters in the 8 neighbouring squares.
I'm confused as to how one would use recursion to solve this though - seems like a lot of effort for what amounts to a for-loop or two.
Johnny19931993
Posted: Thu Mar 19, 2009 6:46 pm Post subject: Re: Need help for my minesweeper game
I took your advice and modified part of the program that calculate the number on the tile into this
Turing:
for i :1.. LEN_X
for j :1.. LEN_Y
if tile (i, j)="m"then if j + 1 < LEN_Y then if tile (i, j + 1)not= "m"then
num_counter (i, j + 1) +=1 endif endif if i + 1 < LEN_X and j + 1 < LEN_Y then if tile (i + 1, j + 1)not= "m"then
num_counter (i + 1, j + 1) +=1 endif endif if i + 1 < LEN_X then if tile (i + 1, j)not= "m"then
num_counter (i + 1, j) +=1 endif endif if i + 1 < LEN_X and j - 1 > 0then if tile (i + 1, j - 1)not= "m"then
num_counter (i + 1, j - 1) +=1 endif if j - 1 > 0then if tile (i, j - 1)not= "m"then
num_counter (i, j - 1) +=1 endif endif if j + 1 < LEN_Y then if tile (i, j + 1)not= "m"then
num_counter (i, j + 1) +=1 endif endif if i - 1 > 0and j - 1 > 0then if tile (i - 1, j - 1)not= "m"then
num_counter (i - 1, j - 1) +=1 endif endif if i - 1 > 0then if tile (i - 1, j)not= "m"then
num_counter (i - 1, j) +=1 endif endif if i - 1 > 0and j + 1 < LEN_Y then if tile (i - 1, j + 1)not= "m"then
num_counter (i - 1, j + 1) +=1 endif endif endif endif endfor endfor
for i :1.. LEN_X
for j :1.. LEN_Y
if num_counter (i, j) > 0then
tile (i, j):="n" + intstr(num_counter (i, j)) endif endfor endfor
Instead of adding the number from the blank tiles, I changed it so it adds the number from the mine tiles
But something's still wrong...
Some tiles that are suppose to have a number in it apparently don't have a number
This is an example :
The 4 that i circle should be a 3
And the other circles are tiles that are suppose to have a number in it
DemonWasp
Posted: Fri Mar 20, 2009 1:05 am Post subject: RE:Need help for my minesweeper game
It looks to me like you have a misplaced end-if (based on the intenting). Press F2 to indent properly and check that it looks like you expected.
Sponsor Sponsor
Johnny19931993
Posted: Fri Mar 20, 2009 2:50 pm Post subject: Re: Need help for my minesweeper game
Some numbers are still wrong though....
DemonWasp
Posted: Fri Mar 20, 2009 2:55 pm Post subject: RE:Need help for my minesweeper game
Can you post your code as it exists now? I tried assembling your new method with your existing code, but you've made substantial other edits.
Johnny19931993
Posted: Fri Mar 20, 2009 5:09 pm Post subject: Re: Need help for my minesweeper game
Yes of course
Turing:
setscreen("graphics:240;240, title: Minesweeper, nobuttonbar, offscreenonly") Mouse.ButtonChoose("multibutton") const LEN_X :=9 const LEN_Y :=9 var tile :array1.. LEN_X, 1.. LEN_Y ofstring var mine :int var mine_counter :=0 var num :array1.. 8ofint var reveal :array1.. LEN_X, 1.. LEN_Y ofboolean var num_counter :array1.. LEN_X, 1.. LEN_Y ofint var mx, my, button :=0
Posted: Sat Mar 21, 2009 8:25 am Post subject: RE:Need help for my minesweeper game
I haven't really tried to slove this problem but I know if you were to redo your program, you may solve the problem. The problem I think is just in your math somewhere. So redoing the program may eliminate the problem. Don't just copy and paste if you do redo it because that won't solve any problems. Also it would be good to redo your program now while you only have 150 lines.
copthesaint
Posted: Sat Mar 21, 2009 8:42 am Post subject: Re: Need help for my minesweeper game
Hmm I think I found your problem...
Look at this.
Turing:
for i :1.. LEN_X
for j :1.. LEN_Y
if tile (i, j)="m"then/*1 If condition */ if j + 1 <=/*ADDED = TO FIX MISSING NUMBERS*/ LEN_Y then if tile (i, j + 1)not= "m"then
num_counter (i, j + 1) +=1 endif endif if i + 1 <=/*ADDED = TO FIX MISSING NUMBERS*/ LEN_X and j + 1 <=/*ADDED = TO FIX MISSING NUMBERS*/ LEN_Y then/*2 If condition */ if tile (i + 1, j + 1)not= "m"then
num_counter (i + 1, j + 1) +=1 endif endif if i + 1 <=/*ADDED = TO FIX MISSING NUMBERS*/ LEN_X then/*3 If condition */ if tile (i + 1, j)not= "m"then
num_counter (i + 1, j) +=1 endif endif if i + 1 <=/*ADDED = TO FIX MISSING NUMBERS*/ LEN_X and j - 1 > 0then/*4 If condition */ if tile (i + 1, j - 1)not= "m"then
num_counter (i + 1, j - 1) +=1 endif endif if j - 1 > 0then/*5 If condition */ if tile (i, j - 1)not= "m"then
num_counter (i, j - 1) +=1 endif endif if j + 1 <=/*ADDED = TO FIX MISSING NUMBERS*/ LEN_Y then/*6 If condition */ if tile (i, j + 1)not= "m"then
num_counter (i, j + 1) +=1 endif endif if i - 1 > 0and j - 1 > 0then/*7 If condition */ if tile (i - 1, j - 1)not= "m"then
num_counter (i - 1, j - 1) +=1 endif endif if i - 1 > 0then/*8 If condition */ if tile (i - 1, j)not= "m"then
num_counter (i - 1, j) +=1 endif endif if i - 1 > 0and j + 1 <=/*ADDED = TO FIX MISSING NUMBERS*/ LEN_Y then/*9 If condition */ if tile (i - 1, j + 1)not= "m"then
num_counter (i - 1, j + 1) +=1 endif endif endif endfor endfor
Well this may be your problem. you have 1 to many if conditions. when normally you would have 8 for this.
Eg.
xxx
xMx
xxx
Count the X's you only want to increase the number at the x's once
BTW I have the fixed program but I'm not allowed to give you it (compsci's rules)
So you can fix this but look for repition in your code.
Johnny19931993
Posted: Sat Mar 21, 2009 11:04 am Post subject: RE:Need help for my minesweeper game
Thank you very much
I fixed the problem now and the program can finally run normally
Thanks for your help
BigBear
Posted: Sat Mar 21, 2009 11:09 am Post subject: RE:Need help for my minesweeper game