Forums

Forums (http://www.abandonia.com/vbullet/index.php)
-   Programming (http://www.abandonia.com/vbullet/forumdisplay.php?f=25)
-   -   Console Minesweeper (http://www.abandonia.com/vbullet/showthread.php?t=11135)

Shadikka 23-07-2006 04:11 PM

So, I just got the idea to practice my Python skills a bit and try the Console module there is for Python.

I wanted to get it under 4kB, so that's why the variable names are so strange. I wanted to keep the structure readable, though. (I could make it much shorter, but it was just a strange idea.)

The Console module (http://effbot.org/downloads/#console) is for Windows only, but I'm sure that this can be converted quite easily for *nix etc.

There are certainly some very strange things in the code. Also, it's a known bug that the game crashes with too big width and/or height. Probably something in reveal().

Code:

import Console
import sys
import random
import string
import time

def game(w,h,m):
****def setm():
********x = random.randint(1,w-2)
********y = random.randint(1,h-2)
********if r[x][y] != ' ': setm()
********else: r[x][y] = '*'
****
****def sett(x,y,s,o):
********c.text(x,y,s,o)
********k[x][y] = s
********
****def mcur(x,y):
********cx = cur[0]
********cy = cur[1]
********if cx+x < 1 or cx+x > w-2 or cy+y < 1 or cy+y > h-2: return cur
********c.text(cx,cy,k[cx][cy],col[k[cx][cy]])
********c.text(cx+x,cy+y,k[cx+x][cy+y],112)********
********return (cx+x,cy+y)
********
****def flag(x,y):
********if k[x][y] == 'F':
************sett(x,y,'.',127)
************return f-1
********if k[x][y] != '.': return f****
********sett(x,y,'F',124)
********if (f+1 == m): checkvictory(1)
********return f+1
********
****def checkvictory(m=0):
********if m == 1:
************v = 1;
************for y in range(h):
****************for x in range(w):
********************if k[x][y] == 'F' and r[x][y] != '*': v = 0
************if (v != 0): victory()
************
****def victory():
********c.text(1,h+1,"You win",13)
********c.text(1,h+2,"Press a key",5)
********while 1:
************if c.peek() is not None and c.get().type == 'KeyPress': sys.exit()
****
****def lose():
********c.text(1,h+1,"You lose",12)
********c.text(1,h+2,"Press a key",4)
********while 1:
************if c.peek() is not None and c.get().type == 'KeyPress': sys.exit()
****
****def reveal(x,y,m=0):
********if k[x][y] in string.digits or k[x][y] != '.': return
********at = str(r[x][y])
********if at == '*':
************sett(x,y,'*',124)
************lose()
********if at in string.digits:
************color = col[at]
************if m<1: color = color+112
************sett(x,y,str(at),color)
********if at == ' ':
************color = 15
************if (m < 1): color = color+112
************sett(x,y,' ',color)
************for z in range(-1,2):
****************for a in range(-1,2):
********************if z == 0 and a == 0: continue
********************reveal(x+z,y+a,1)
****
****col = {'1':9,'2':2,'3':14,'4':3,'5':6,'6':10,'7':11,'8':13,'.':15,' ':15,'*':12,'0':11,'F':12}****
****r = [ [' ' for e in range(h)] for i in range(w)]
****k = [ ['.' for e in range(h)] for i in range(w)]
****************
****for x in range(m): setm()
****
****for y in range(h):
********for x in range(w):
************if x == 0 or y == 0 or x == w - 1 or y == h - 1:
****************c.text(x,y,'#',8)
****************r[x][y] = '#'
****************k[x][y] = '#'
****************
************elif r[x][y] == '*':
****************k[x][y] == '.'
****************c.text(x,y,'.',15)
****************
************elif r[x][y] == ' ':
****************num = 0
****************for z in range(-1,2):
********************for a in range(-1,2):
************************if z == 0 and a == 0: continue
************************if r[x+z][y+a] == '*': num = num+1
****************if num > 0: r[x][y] = num;
****************k[x][y] == '.'
****************c.text(x,y,'.',15)

****cur = (1,1)
****c.text(1,1,'.',112)
****f = 0
****
****while 1:
********if c.peek() is not None:
************e = c.get()
************if e.type == 'KeyPress':
****************if e.char == 'w': cur = mcur(0,-1)
****************elif e.char == 's': cur = mcur(0,1)
****************elif e.char == 'a': cur = mcur(-1,0)
****************elif e.char == 'd': cur = mcur(1,0)
****************elif e.char == 'z': reveal(cur[0],cur[1])
****************elif e.char == 'x': f = flag(cur[0],cur[1])****************
****************elif e.char == 'q': break

def menu(w=20,h=20,m=20):
****c.page(15,' ')
****c.text(9,6,"Minesweeper",13)
****c.text(1,8,"Q - Quit")
****c.text(1,9,"U/Y - Adjust level width")
****c.text(1,10,"H/J - Adjust level height")
****c.text(1,11,"N/M - Adjust mines")
****c.text(1,13,"Keys in game:")
****c.text(2,14,"WASD: move")
****c.text(2,15,"Z: Reveal tile")
****c.text(2,16,"X: (un)Flag tile")
****c.text(25,14," Width: ",11)
****c.text(25,15,"Height: ",11)
****c.text(25,16," Mines: ",11)
****c.text(33,14,str(w),11)
****c.text(33,15,str(h),11)
****c.text(33,16,str(m),11)
****def sm():
********c.text(33,16,"**")
********c.text(33,16,str(m),11)
****def sw():
********c.text(33,14,"**")
********c.text(33,14,str(w),11)
****def sh():
********c.text(33,15,"**")
********c.text(33,15,str(h),11)
****while 1:
********if c.peek() is not None:
************e = c.get()
************if e.type == 'KeyPress':
****************if e.char == 'q': sys.exit()
****************elif e.char == 'u':
********************if w<60: w=w+1; sw()
****************elif e.char == 'y':
********************if w>3: w=w-1; sw()
********************if m>h*w-2: m=h*w-2; sm()
****************elif e.char == 'j':
********************if h<60: h=h+1; sh()
****************elif e.char == 'h':
********************if h>3: h=h-1; sh()
********************if m>h*w-2: m=h*w-2; sm()
****************elif e.char == 'm':
********************if m<h*w-2: m=m+1; sm()
****************elif e.char == 'n':
********************if m>7: m=m-1; sm()
****************else:
********************c.page(15,' ')
********************game(w+2,h+2,m)
********************
c = Console.getconsole()
c.title("Minesweeper")
c.cursor(0)
menu()


guesst 23-07-2006 07:59 PM

Wow. Not knowing a thing about Python I wonder if you'd help me get started.

First of all, what complier do you suggest I install? How do I make the game run?

velik_m 24-07-2006 10:42 AM

<div class='quotetop'>QUOTE(guesst @ Jul 23 2006, 07:59 PM) [snapback]244655[/snapback]</div>
Quote:

Wow. Not knowing a thing about Python I wonder if you'd help me get started.

First of all, what complier do you suggest I install? How do I make the game run?
[/b]
Python is a script language, you don't need a compiler.

i don't remember much of Python, but my guess would be you just need to install python and then run this as python <filename> (like java). i'm sure shadikka will know more.

Shadikka 24-07-2006 05:27 PM

I suggest you to get the official Python interpreter.
Yes, an interpreter, since no compiler is needed, it's an interpreted language.

http://www.python.org and http://docs.python.org have all you need.
A good tutorial to start with is here: http://docs.python.org/tut/tut.html


The current time is 07:02 AM (GMT)

Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.