File: game.cpp

package info (click to toggle)
wxwin2-doc 2.01-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 6,540 kB
  • ctags: 5,968
  • sloc: cpp: 15,157; makefile: 434; sh: 6
file content (105 lines) | stat: -rw-r--r-- 3,074 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
///////////////////////////////////////////////////////////////////////////////
// Name:        bombs1.cpp
// Purpose:     Implementation of the class BombsGame
// Author:      P. Foggia 1996
// Modified by:
// Created:     1996
// RCS-ID:      $Id: game.cpp,v 1.1 1998/12/31 10:53:59 JS Exp $
// Copyright:   (c) 1996 P. Foggia
// Licence:     wxWindows licence
///////////////////////////////////////////////////////////////////////////////

#ifdef __GNUG__
#pragma implementation
#endif

#include "wx/wxprec.h"

#ifndef  WX_PRECOMP
  #include "wx/wx.h"
#endif //precompiled headers

#include "game.h"
#include <stdlib.h>
#include <limits.h>

#define PROB 0.2

#ifndef RAND_MAX
#define RAND_MAX INT_MAX
#endif


/*--------------------  BombsGame::~BombsGame()  ---------------------*/
/*--------------------------------------------------------------------*/
BombsGame::~BombsGame()
  { if (field)
      free(field);
  }

/*------------------  int BombsGame::Init(width,height)  -------------------*/
/* Initialize the play field.   Returns 0 on failure                        */
/*--------------------------------------------------------------------------*/
int BombsGame::Init(int aWidth, int aHeight)
  { int x, y;
    int xx, yy;

    if (field)
      free(field);
    field=(short *)malloc(aWidth*aHeight*sizeof(short));
    if (!field)
      { width=height=0;
        return 0;
      }
    width=aWidth;
    height=aHeight;

    for(x=0; x<width; x++)
      for(y=0; y<height; y++)
        { field[x+y*width] = ((float)rand()/RAND_MAX <PROB)?
                             BG_HIDDEN | BG_BOMB :
                             BG_HIDDEN;
        }

    bombs=0;
    for(x=0; x<width; x++)
      for(y=0; y<height; y++)
        if (field[x+y*width] & BG_BOMB)
          { bombs++;
            for(xx=x-1; xx<=x+1; xx++)
              if (xx>=0 && xx<width)
                for(yy=y-1; yy<=y+1; yy++)
                  if (yy>=0 && yy<height && (yy!=y || xx!=x))
                    field[xx+yy*width]++;
          }
    normal_cells=height*width-bombs;
    return 1;
  }

/*----------------------  BombsGame::Mark(x,y)  -------------------------*/
/* Marks/unmarks a cell                                                  */
/*-----------------------------------------------------------------------*/
void BombsGame::Mark(int x, int y)
  {
      field[x+y*width] ^= BG_MARKED;
  }

/*-------------------  BombsGame::Unhide(x,y)  ------------------------*/
/* Unhides a cell                                                      */
/*---------------------------------------------------------------------*/
void BombsGame::Unhide(int x, int y)
  { if (!IsHidden(x,y))
      return;
    field[x+y*width] &= ~BG_HIDDEN;
    if (!IsBomb(x,y))
      normal_cells--;
  }

/*-------------------  BombsGame::Explode(x,y)  ------------------------*/
/* Makes a cell exploded                                                */
/*----------------------------------------------------------------------*/
void BombsGame::Explode(int x, int y)
  {
    field[x+y*width] |= BG_EXPLODED;
  }