File: Controller.m

package info (click to toggle)
gomoku.app 1.2.9-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 308 kB
  • sloc: objc: 1,184; makefile: 16
file content (231 lines) | stat: -rw-r--r-- 5,920 bytes parent folder | download | duplicates (4)
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*
 *  Controller.m: Controller Object of Gomoku.app
 *
 *  Copyright (c) 2000 Nicola Pero <n.pero@mi.flashnet.it>
 *  
 *  Author: Nicola Pero
 *  Date: April, September 2000
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include "Controller.h"
#include "Board.h"

@interface DifficultyPanel : NSPanel
{
  MainBoard *board;
}
- (id) initWithBoard: (MainBoard *)b;
- (void) changeLevel: (id)sender;
@end

@implementation DifficultyPanel
- (id) initWithBoard: (MainBoard *)b
{
  NSBox *box;
  NSBox *box_b;
  NSMatrix *matrix;
  NSButtonCell *cell;
  NSRect winFrame;

  board = b;

  cell = [[NSButtonCell alloc] init];
  [cell setButtonType: NSRadioButton];
  [cell setBordered: NO];
  [cell setImagePosition: NSImageLeft]; 
  
  matrix = [[NSMatrix alloc] initWithFrame: NSZeroRect
				 mode: NSRadioModeMatrix
				 prototype: cell
				 numberOfRows: 6
				 numberOfColumns: 1];   
  
  [matrix setIntercellSpacing: NSMakeSize (0, 4) ];
  [matrix setTarget: self];
  [matrix setAutosizesCells: NO];
  [matrix setTarget: self];
  [matrix setAction: @selector(changeLevel:)];
  
  cell = [matrix cellAtRow: 0 column: 0];
  [cell setTitle: _(@"Level 0 (Trivial)")];
  [cell setTag: 0];
  cell = [matrix cellAtRow: 1 column: 0];
  [cell setTitle: _(@"Level 1 (Beginner)")];
  [cell setTag: 1];
  cell = [matrix cellAtRow: 2 column: 0];
  [cell setTitle: _(@"Level 2 (Easy)")];
  [cell setTag: 2];
  cell = [matrix cellAtRow: 3 column: 0];
  [cell setTitle: _(@"Level 3 (Medium)")];
  [cell setTag: 3];
  cell = [matrix cellAtRow: 4 column: 0];
  [cell setTitle: _(@"Level 4 (Advanced)")];
  [cell setTag: 4];
  cell = [matrix cellAtRow: 5 column: 0];
  [cell setTitle: _(@"Level 5 (Difficult)")];
  [cell setTag: 5];

  [matrix selectCellWithTag: [board difficultyLevel]];
  
  [matrix sizeToFit];
  
  box = [NSBox new];
  [box setTitle: _(@"Choose Difficulty Level:")];
  [box setTitlePosition: NSAtTop];
  [box setBorderType: NSGrooveBorder];
  [box addSubview: matrix];
  RELEASE (matrix);
  [box sizeToFit];
  [box setAutoresizingMask: (NSViewWidthSizable | NSViewHeightSizable)];

  box_b = [NSBox new];
  [box_b setTitlePosition: NSNoTitle];
  [box_b setBorderType: NSNoBorder];
  [box_b addSubview: box];
  RELEASE (box);
  [box_b sizeToFit];
  [box_b setAutoresizingMask: (NSViewWidthSizable | NSViewHeightSizable)];

  winFrame.size = [box_b frame].size;
  winFrame.origin = NSMakePoint (100, 100);

  // Now we can make the window of the exact size 
  self = [[isa alloc] initWithContentRect: winFrame
		     styleMask: (NSTitledWindowMask 
				 | NSClosableWindowMask 
				 | NSMiniaturizableWindowMask)
		      backing: NSBackingStoreBuffered
		      defer: YES];
  [self setTitle: _(@"Difficulty level Panel")];
  [self setReleasedWhenClosed: NO];
  [self setContentView: box_b];
  RELEASE (box_b);
  [self changeLevel: matrix];
  return self;
}

- (void) changeLevel: (id)sender
{
  int level = [[sender selectedCell] tag];
  
  [board setDifficultyLevel: level];
  
  [[NSUserDefaults standardUserDefaults] setInteger: level 
					 forKey: @"DifficultyLevel"];
  [[NSUserDefaults standardUserDefaults] synchronize];
}
@end

@implementation Controller

+ new: (int) cnt
{
    return [[self alloc] init: cnt];
}

- init: (int) cnt
{
  NSMatrix *matrix;
  NSRect winFrame;
  NSImageCell *cell;
  int level;
  
  cell = [[NSCell alloc] init];
  AUTORELEASE (cell);
  [cell setBordered: NO];
  [cell setBezeled: NO];
  [cell setEditable: NO];
  [cell setSelectable: NO];
  [cell setImage: [NSImage imageNamed: @"Empty"]];

  matrix = [[NSMatrix alloc] initWithFrame: NSZeroRect
			     mode: NSTrackModeMatrix 
			     prototype: cell
			     numberOfRows: cnt 
			     numberOfColumns: cnt];
  AUTORELEASE (matrix);
  [matrix setIntercellSpacing: NSMakeSize (0, 0)];
  [matrix setAutoresizingMask: NSViewNotSizable];
  [matrix sizeToFit];


  winFrame.size = [matrix frame].size;
  winFrame.origin = NSMakePoint (100, 100);

  self = [super initWithContentRect: winFrame
		styleMask: (NSTitledWindowMask | NSClosableWindowMask 
			    | NSMiniaturizableWindowMask)
		backing: NSBackingStoreBuffered
		defer: YES];
  [self setTitle: _(@"Gomoku Game")];
  [self setReleasedWhenClosed: NO];
  [self setContentView: matrix];

  board = [[MainBoard alloc] initWithMatrix: matrix];
  [matrix setTarget: self];
  [matrix setAction: @selector (userMove:)];

  level = [[NSUserDefaults standardUserDefaults] 
	    integerForKey: @"DifficultyLevel"];
  if (level >= 0 && level <= 5)
    {
      [board setDifficultyLevel: level];
    }
  
  return self;
}

-(void) dealloc
{
  TEST_RELEASE (diffPanel);
  RELEASE (board);
  [super dealloc];
}

- (void) applicationDidFinishLaunching: (NSNotification *)aNotification;
{
  [self center];
  [self orderFront: self];
}

- (void) runDifficultyLevelPanel: (id) sender
{
  if (diffPanel == nil)
    {
      diffPanel = [[DifficultyPanel alloc] initWithBoard: board];
    }
  
  [diffPanel orderFront: self];
}

- (void) newGame: (id) sender
{
  [self orderFront: self];
  [board newGame];
}

- (void) userMove: (id) sender
{
  if ([sender isKindOfClass: [NSMatrix class]])
    {
      [board userMoveInRow: [(NSMatrix *)sender selectedRow]
	     column: [(NSMatrix *)sender selectedColumn]];
    }
}

@end