]> git.saurik.com Git - wxWidgets.git/blame_incremental - samples/bombs/game.cpp
compilation fixes
[wxWidgets.git] / samples / bombs / game.cpp
... / ...
CommitLineData
1///////////////////////////////////////////////////////////////////////////////
2// Name: bombs1.cpp
3// Purpose: Implementation of the class BombsGame
4// Author: P. Foggia 1996
5// Modified by:
6// Created: 1996
7// RCS-ID: $Id$
8// Copyright: (c) 1996 P. Foggia
9// Licence: wxWindows licence
10///////////////////////////////////////////////////////////////////////////////
11
12#ifdef __GNUG__
13#pragma implementation
14#endif
15
16#include "wx/wxprec.h"
17
18#ifndef WX_PRECOMP
19 #include "wx/wx.h"
20#endif //precompiled headers
21
22#include "game.h"
23#include <stdlib.h>
24#include <limits.h>
25
26#define PROB 0.2
27
28#ifndef RAND_MAX
29#define RAND_MAX INT_MAX
30#endif
31
32
33/*-------------------- BombsGame::~BombsGame() ---------------------*/
34/*--------------------------------------------------------------------*/
35BombsGame::~BombsGame()
36 { if (field)
37 free(field);
38 }
39
40/*------------------ int BombsGame::Init(width,height) -------------------*/
41/* Initialize the play field. Returns 0 on failure */
42/*--------------------------------------------------------------------------*/
43int BombsGame::Init(int aWidth, int aHeight)
44 { int x, y;
45 int xx, yy;
46
47 if (field)
48 free(field);
49 field=(short *)malloc(aWidth*aHeight*sizeof(short));
50 if (!field)
51 { width=height=0;
52 return 0;
53 }
54 width=aWidth;
55 height=aHeight;
56
57 for(x=0; x<width; x++)
58 for(y=0; y<height; y++)
59 { field[x+y*width] = ((float)rand()/RAND_MAX <PROB)?
60 BG_HIDDEN | BG_BOMB :
61 BG_HIDDEN;
62 }
63
64 bombs=0;
65 for(x=0; x<width; x++)
66 for(y=0; y<height; y++)
67 if (field[x+y*width] & BG_BOMB)
68 { bombs++;
69 for(xx=x-1; xx<=x+1; xx++)
70 if (xx>=0 && xx<width)
71 for(yy=y-1; yy<=y+1; yy++)
72 if (yy>=0 && yy<height && (yy!=y || xx!=x))
73 field[xx+yy*width]++;
74 }
75 normal_cells=height*width-bombs;
76 return 1;
77 }
78
79/*---------------------- BombsGame::Mark(x,y) -------------------------*/
80/* Marks/unmarks a cell */
81/*-----------------------------------------------------------------------*/
82void BombsGame::Mark(int x, int y)
83 {
84 field[x+y*width] ^= BG_MARKED;
85 }
86
87/*------------------- BombsGame::Unhide(x,y) ------------------------*/
88/* Unhides a cell */
89/*---------------------------------------------------------------------*/
90void BombsGame::Unhide(int x, int y)
91 { if (!IsHidden(x,y))
92 return;
93 field[x+y*width] &= ~BG_HIDDEN;
94 if (!IsBomb(x,y))
95 normal_cells--;
96 }
97
98/*------------------- BombsGame::Explode(x,y) ------------------------*/
99/* Makes a cell exploded */
100/*----------------------------------------------------------------------*/
101void BombsGame::Explode(int x, int y)
102 {
103 field[x+y*width] |= BG_EXPLODED;
104 }
105