source: trunk/ui_cli.cxx @ 14

Last change on this file since 14 was 14, checked in by jtv, 14 years ago

Seed randomizer at start of game

File size: 3.8 KB
Line 
1/*
2This file is part of libmines.
3
4Copyright (C) 2005, Jeroen T. Vermeulen <jtv@xs4all.nl>
5
6libmines is free software; you can redistribute it and/or modify it under the
7terms of the GNU General Public License as published by the Free Software
8Foundation; either version 2 of the License, or (at your option) any later
9version.
10
11libmines is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13PARTICULAR PURPOSE.  See the GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License along with
16libmines; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
17Suite 330, Boston, MA  02111-1307  USA
18*/
19
20// A very basic sample user interface for a Minesweeper game based on libmines
21
22#include <cassert>
23#include <cerrno>
24#include <iostream>
25#include <stdexcept>
26
27#include <fcntl.h>
28#include <unistd.h>
29
30#include "gamelogic.hxx"
31
32using namespace std;
33
34
35namespace
36{
37void coordsbar(int cols)
38{
39  cout << "\t ";
40  for (int i=0; i<cols; ++i)
41  {
42    if (i<10) cout << ' ';
43    cout << i;
44  }
45  cout << endl;
46}
47
48void save_game(const Lake &L)
49{
50  char *const buf = new char[L.rows()*L.cols()+100];
51  int fd = -1;
52  try
53  {
54    int bytes = L.save(buf);
55    fd = creat("mines.savedgame", 0744);
56    if (fd == -1) throw runtime_error(strerror(errno));
57    write(fd, buf, bytes);
58
59#ifndef NDEBUG
60    // See if we can restore this game, save that, and get the same data back
61    const Lake L2(buf);
62    char *const check = new char[L.rows()*L.cols()+100];
63    bytes = L2.save(check);
64    assert(strcmp(buf,check) == 0);
65    delete [] check;
66    cout << "(Game saved and checked)" << endl;
67#endif
68  }
69  catch (const exception &e)
70  {
71    cerr << e.what() << endl;
72  }
73  delete [] buf;
74  if (fd != -1) close(fd);
75}
76
77
78
79} // namespace
80
81
82int main()
83{
84  int rows=20, cols=30, mines=200;
85
86  srand(getpid()^time(NULL));
87
88  try
89  {
90    cout << "Creating minefield of "
91         <<rows<<"x"<<cols<<" fields, "<<mines<<" mines." << endl;
92    Lake L(rows,cols,mines);
93    L.set_intelligence(Lake::max_intelligence()+1);
94    while (L.to_go())
95    {
96      save_game(L);
97      cout << "Clear patches to go: " << L.to_go() << "..." << endl;
98      coordsbar(cols);
99      cout << endl;
100      for (int r=-1; r<=rows; ++r)
101      {
102        if (r>=0 && r<rows) cout << r;
103        cout << '\t';
104        for (int c=-1; c<=cols; ++c) cout << L.status_at(r,c) << ' ';
105        if (r >= 0 && r<rows)
106        {
107          if (r <= 9) cout << ' ';
108          cout << r;
109        }
110        /*
111        cout << '\t';
112        for (int c=-1; c<=cols; ++c) cout << L.at(r,c).near_hiddenmines();
113        cout << '\t';
114        for (int c=-1; c<=cols; ++c) cout << L.at(r,c).near_unknown();
115        cout << '\t';
116        for (int c=-1; c<=cols; ++c) cout << (L.at(r,c).mined()?'*':'^');
117        */
118        cout << endl;
119      }
120      cout << endl;
121      coordsbar(cols);
122      cout <<
123        "Please enter row and column (zero-based, separated by space) of next "
124        "patch you think is mine-free, or make either coordinate negative if "
125        "you want to indicate a mine:"
126        << endl;
127      int r, c;
128      cin >> r >> c;
129      cout << endl;
130      set<Coords> changes;
131      bool thinksismine = false;
132      if (r < 0 || c < 0)
133      {
134        thinksismine = true;
135        r = abs(r);
136        c = abs(c);
137      }
138      if (r >= rows || c >= cols) cout<<"Out of range!"<<endl;
139      else L.probe(r,c,changes,thinksismine);
140    }
141
142    cout << "Field cleared.  Congratulations!" << endl;
143  }
144  catch (const Boom &b)
145  {
146    cout << "** BOOM! **" << endl
147         << "Failed after " << b.moves << " moves." << endl;
148    if (b.mined) cout << "The fatal mine was at: ";
149    else cout << "This location was not mined like you said: ";
150    cout << '(' << b.position.row << ',' << b.position.col << ')' << endl;
151  }
152  catch (const exception &e)
153  {
154    cerr << e.what() << endl;
155    return 1;
156  }
157  return 0;
158}
159
Note: See TracBrowser for help on using the repository browser.