File: main.c

package info (click to toggle)
freesweep 0.87-6
  • links: PTS
  • area: main
  • in suites: potato
  • size: 364 kB
  • ctags: 260
  • sloc: ansic: 3,754; sh: 1,484; makefile: 108
file content (204 lines) | stat: -rw-r--r-- 4,350 bytes parent folder | download | duplicates (3)
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/**********************************************************************
*  This source code is copyright 1999 by Gus Hartmann & Peter Keller  *
*  It may be distributed under the terms of the GNU General Purpose   *
*  License, version 2 or above; see the file COPYING for more         *
*  information.                                                       *
*                                                                     *
*  $Id: main.c,v 1.34 1999/08/09 05:25:36 hartmann Exp $
*                                                                     *
**********************************************************************/

#include "sweep.h"

int main(int argc, char** argv)
{
	GameStats* Game;
	chtype Input=0;

	/* Set up the curses cleaner in case of disaster */
	signal(SIGSEGV, sighandler);
	signal(SIGBUS, sighandler);
	signal(SIGILL, sighandler);

#ifdef DEBUG_LOG
	if ((DebugLog=fopen("debug.log","a"))==0)
	{
		perror("Main::OpenDebugLog");
		return 1;
	}
#endif /* DEBUG_LOG */
	
	if ((Game=malloc(sizeof(GameStats)))==NULL)
	{
		perror("Main::AllocGame");
		return 1;
	}

	InitGame(Game);

	SourceGlobalFile(Game);
	SourceHomeFile(Game);

	if (ParseArgs(Game,argc,argv)>0)
	{
		return 1;
	}

	/* Output all the relevant information about the compilation. */
	printf("Freesweep v%s by\nGus Hartmann (hartmann@cs.wisc.edu) and Pete Keller (psilord@cs.wisc.edu).\n",VERSION);
	printf("Freesweep comes with ABSOLUTELY NO WARRANTY; see the file COPYING for more info.\n");
#ifdef DEBUG_LOG
	fprintf(DebugLog,"Freesweep v%s by Gus Hartmann and Pete Keller.\n",VERSION);
#endif /* DEBUG_LOG */
	
	/* Start ncurses */
	StartCurses();

	/* Prompt the user for new setings, if necessary. */
	if (Game->Fast==0)
	{
		AskPrefs(Game);
	}

	clear();
	noutrefresh();

	if (InitErrorWin(Game) + InitStatsWin() > 0)
	{
		/* Do something bad. */
	}
	ReadyGame(Game);
	noutrefresh();

	/* set up signal handler and stuff */

	while (1)
	{
		/* Make sure the right character set is in use. */
		InitCharSet(Game,Game->LineDraw);

		/* Touch a couple windows */
		RedrawErrorWin();
		RedrawStatsWin();

		ReReadyGame(Game);
		PrintInfo();
		noutrefresh();
		PrintStats(Game);
		doupdate();
		Center(Game);

		StartTimer();
	/*	This is the main loop.*/
		while (Game->Status==INPROG)
		{
			PrintStats(Game);
			Pan(Game);
			DrawCursor(Game);
			DrawBoard(Game);
			doupdate();
			UndrawCursor(Game);
			SweepError(NULL);
			GetInput(Game);
			Game->Time = g_tick;
		}
		g_tick = 0;
		Game->Time = g_tick;	/* If we won, the game is already saved */
		StopTimer();

		/* Update the final action of the player */
		Pan(Game);
		DrawCursor(Game);
		DrawBoard(Game);
		doupdate();
		UndrawCursor(Game);

		/* see what happened */
		switch(Game->Status)
		{
			case ABORT:
				ReReadyGame(Game);
				break;
			case RECONF:
				StopTimer();
				clear();
				noutrefresh();
				Wipe(Game);
				AskPrefs(Game);
				ReadyGame(Game);
				Center(Game);
				clear();
				noutrefresh();
				StartTimer();
				break;
			case LOSE:
				StopTimer();
				DrawCursor(Game);
				SweepMessage("BOOM! Any key to continue, 'q' to quit.");
				nodelay(Game->Board,FALSE);
				Input=mvwgetch(Game->Board,0,0);
				wnoutrefresh(Game->Board);
				refresh();

				if (Input == 'q')
				{
					clear();
					refresh();
					endwin();
#ifdef DEBUG_LOG
					fprintf(DebugLog,"========================================\n");
					fclose(DebugLog);
#endif /* DEBUG_LOG */
					return 0;
				}
				else
				{
					SweepMessage(NULL);
					UndrawCursor(Game);
				}
				break;
			case WIN:
				StopTimer();
				DrawCursor(Game);
				SweepMessage("You Win! Press a key to continue.");
				nodelay(Game->Board,FALSE);
				Input=mvwgetch(Game->Board,0,0);
				wnoutrefresh(Game->Board);
				refresh();

				if (Input == 'q')
				{
					clear();
					refresh();
					endwin();
#ifdef DEBUG_LOG
					fprintf(DebugLog,"========================================\n");
					fclose(DebugLog);
#endif /* DEBUG_LOG */
					return 0;
				}
				else
				{
					SweepMessage(NULL);
					UndrawCursor(Game);
				}
				break;
			default:
				break;
		}

		/* It might be nice to log this before reseting it. */
		Game->Status=INPROG;

		flushinp();
	}

	endwin();

#ifdef DEBUG_LOG
	fprintf(DebugLog,"========================================\n");
	fclose(DebugLog);
#endif /* DEBUG_LOG */

	return 0;
}