File: Board.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 (885 lines) | stat: -rw-r--r-- 19,887 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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
/*  -*-objc-*-
 *  Board.m: Implementation of the Board Class of the GNUstep  
 *  Gomoku game
 *
 *  Copyright (c) 2000 Nicola Pero <n.pero@mi.flashnet.it>
 *  
 *  Author: Nicola Pero
 *  Date: April 2000
 *
 *  Author: David Relson
 *  Date: September 2000, support for boards of arbitrary size
 *
 *  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 "Board.h"

static const tile humanWonPattern[5] = 
{
  human, human, human, human, human
};

static const tile computerWonPattern[5] = 
{ 
  computer, computer, computer, computer, computer 
};

/*
 * I swear I have beaten (at least once) this engine at all levels.  
 * So, don't be afraid of trying yet another time - it *is* possible 
 * to win, no matter how high the level is. :-)
 */

static const tile humanWonNextPattern[5][5] = 
{ 
  {nothing, human, human, human, human},
  {human, nothing, human, human, human},
  {human, human, nothing, human, human},
  {human, human, human, nothing, human},
  {human, human, human, human, nothing}
};

static const tile computerWonNextPattern[5][5] = 
{ 
  {nothing, computer, computer, computer, computer},
  {computer, nothing, computer, computer, computer},
  {computer, computer, nothing, computer, computer},
  {computer, computer, computer, nothing, computer},
  {computer, computer, computer, computer, nothing}
};

static const tile humanWonFarPattern[4][6] = 
{
  {nothing, nothing, human, human, human, nothing},
  {nothing, human, nothing, human, human, nothing},
  {nothing, human, human, nothing, human, nothing},
  {nothing, human, human, human, nothing, nothing}
};

static const tile computerWonFarPattern[4][6] = 
{
  {nothing, nothing, computer, computer, computer, nothing},
  {nothing, computer, nothing, computer, computer, nothing},
  {nothing, computer, computer, nothing, computer, nothing},
  {nothing, computer, computer, computer, nothing, nothing}
};


#define	BOARD(i, j) board[((i) * size + (j))]
#define	INDEX(i, j) ((i) * size + (j))

@implementation Board

+ (id) newWithRows: (int)cnt
{
    return [[self alloc] initWithRows: cnt];
}

- (id) initWithRows: (int)cnt
{
    size = cnt;
    board = malloc (sizeof(tile *) * size * size);

    [self initBoardConstants];
    
    return self;
}

- (void) initBoardConstants
{
    int startRow, startColumn, position; 

    boardConstants = calloc (size * size, sizeof (int));

    /* The board constant of position (i, j) is the number of winning
       patterns which pass through (i, j) */

    /* We compute it in the most stupid way, by enumerating all
       winning combinations and for each winning combination adding
       one to each board cell belonging to the combination.  
       This is stupid and long, but the code is easy to understand and 
       maintain; nowadays computers are fast enough. */
    
    /* Scan horizontal combinations */
    for (startRow = 0; startRow < size; startRow++)
      {
	for (startColumn = 0; startColumn < (size + 1 - 5); startColumn++)
	  {
	    for (position = 0; position < 5; position++)
	      {
		boardConstants[INDEX (startRow, startColumn + position)]++; 
	      }
	  }
      }

    /* Scan vertical combinations */
    for (startColumn = 0; startColumn < size; startColumn++)
      {
	for (startRow = 0; startRow < (size + 1 - 5); startRow++)
	  {
	    for (position = 0; position < 5; position++)
	      {
		boardConstants[INDEX (startRow + position, startColumn)]++; 
	      }
	  }
      }

    /* Scan NW-SE diagonals. */
    for (startColumn = 0; startColumn < (size + 1 - 5); startColumn++)
      {
	for (startRow = 0; startRow < (size + 1 - 5); startRow++)
	  {
	    for (position = 0; position < 5; position++)
	      {
		boardConstants[INDEX (startRow + position, 
				      startColumn + position)]++;
	      }
	  }
      }

    /* Scan NE-SW diagonals. */
    for (startColumn = (5 - 1); startColumn < size; startColumn++)
      {
	for (startRow = 0; startRow < (size + 1 - 5); startRow++)
	  {
	    for (position = 0; position < 5; position++)
	      {
		boardConstants[INDEX (startRow + position, 
				      startColumn - position)]++;
	      }
	}
      }
}

- (void) dealloc
{
  free (board);
  free (boardConstants);
  [super dealloc];
}

// Initialize, reset, set level
- (void) reset
{
  int i, j;

  for (i = 0; i < size; i++)
    for (j = 0; j < size; j++)
      {
	BOARD (i, j) = nothing;
      }

  last_move_row = -1;
  last_move_col = -1;
}

- (id) copyWithZone: (NSZone*)zone
{
  int i, j;
  Board	*c = [isa allocWithZone: zone];

  c->difficultyLevel = difficultyLevel;

  c->board = malloc (sizeof(tile *) * size * size);
  c->boardConstants = malloc (sizeof(int) * size * size);
  
  for (i = 0; i < size; i++)
    for (j = 0; j< size; j++)
      {
	c->BOARD (i, j) = BOARD (i, j);
	c->boardConstants[INDEX (i, j)] = boardConstants[INDEX (i, j)];
      }
  
  c->last_move_row = last_move_row;  
  c->last_move_col = last_move_col;
  
  return c;
}

- (void) setDifficultyLevel: (int) i
{
  difficultyLevel = i;
}

- (int) difficultyLevel
{
  return difficultyLevel;
}

- (tile) tileInRow: (int) i  column: (int) j
{
  return BOARD (i, j);
}

- (void) setTile: (tile) t inRow: (int) i column: (int) j
{
  BOARD (i, j) = t;
}

- (BOOL) isFreeRow: (int) i  column: (int) j
{
  if (BOARD (i, j) == nothing)
    {
      return YES;
    }
  else 
    {
      return NO;  
    }
  
}

- (BOOL) isPattern: (const tile *)t
	    length: (int)e
	inPosition: (patternPosition)pos
{
  int a;
  int i, j;

  i = pos.startRow;
  j = pos.startColumn;
  
  for (a = 0; a < e; a++)
    {
      if (t[a] COMPARE_TILE BOARD (i, j))
	{
	  i += pos.directionRow;
	  j += pos.directionColumn;
	}
      else
	{
	  return NO;
	}
    }
  
  return YES;
}

- (patternPosition) scanBoardForPattern: (const tile *)t
				 length: (int)e
{
  // Extremely simple and lossy algorithm and implementation: we
  // consider all possible pattern positions on the board (easy to
  // enumerate), and for each position we compare the pattern to what
  // it is on the board (easy to do).
  patternPosition pos;
  
  // Scan rows.
  pos.directionRow = 0;
  pos.directionColumn = 1;
  for (pos.startRow = 0; pos.startRow < size; pos.startRow++)
    {
      for (pos.startColumn = 0; pos.startColumn < (size + 1 - e); pos.startColumn++)
	{
	  if ([self isPattern: t length: e inPosition: pos] == YES)
	    return pos;
	}
    }

  // Scan columns.
  pos.directionRow = 1;
  pos.directionColumn = 0;
  for (pos.startColumn = 0; pos.startColumn < size; pos.startColumn++)
    {
      for (pos.startRow = 0; pos.startRow < (size + 1 - e); pos.startRow++)
	{
	  if ([self isPattern: t length: e inPosition: pos] == YES)
	    return pos;
	}
    }

  // Scan NW-SE diagonals.
  pos.directionRow = 1;
  pos.directionColumn = 1;
  for (pos.startColumn = 0; pos.startColumn < (size + 1 - e); pos.startColumn++)
    {
      for (pos.startRow = 0; pos.startRow < (size + 1 - e); pos.startRow++)
	{
	  if ([self isPattern: t length: e inPosition: pos] == YES)
	    return pos;
	}
    }

  // Scan NE-SW diagonals.
  pos.directionRow = 1;
  pos.directionColumn = -1;
  for (pos.startColumn = (e - 1); pos.startColumn < size; pos.startColumn++)
    {
      for (pos.startRow = 0; pos.startRow < (size + 1 - e); pos.startRow++)
	{
	  if ([self isPattern: t length: e inPosition: pos] == YES)
	    return pos;
	}
    }

  // Not Found
  pos.startRow = -1;
  return pos;
}

/* 
 * Computes a number representing the strategical importance of the
 * square in row, column.  This is all pretty unprecise and simple,
 * but that's nice - it looks more human. :-) */
- (float) powerOfSquareInRow: (int)row column: (int)column
{
  /* Temporary board to make computations */
  Board *tmp_board;
  patternPosition pos;
  int index;
  float squarePower = 0;

  if (difficultyLevel >= 4)
    {
      tmp_board = [self copy];
      
      /* Put a computer tile in (row, column) */
      tmp_board->BOARD (row, column) = computer;
      
      /* Check how many next computer wins we would have with that move */
      for (index = 0; index < 5; index++)
	{
	  pos = [tmp_board scanBoardForPattern: computerWonNextPattern[index]
			   length: 5];
	  if (PATTERN_FOUND (pos))  
	    {
	      squarePower += 10;
	    }
	}
      
      /* Put a human tile in (row, column) */
      tmp_board->BOARD (row, column) = human;
      
      /* Check how many next human wins we would have with that move */
      for (index = 0; index < 5; index++)
	{
	  pos = [tmp_board scanBoardForPattern: humanWonNextPattern[index]
			   length: 5];
	  if (PATTERN_FOUND (pos))  
	    {
	      squarePower += 10;
	    }
	}
      
      /* If difficultyLevel => 5, do the same with FarPatterns. */
      if (difficultyLevel >= 5)
	{
	  /* Put a computer tile in (row, column) */
	  tmp_board->BOARD (row, column) = computer;
	  
	  /* Check how many far computer wins we would have with that move */
	  for (index = 0; index < 4; index++)
	    {
	      pos = [tmp_board scanBoardForPattern: 
				 computerWonFarPattern[index]
			       length: 6];
	      if (PATTERN_FOUND (pos))  
		{
		  squarePower += 5;
		}
	    }
	  
	  /* Put a human tile in (row, column) */
	  tmp_board->BOARD (row, column) = human;
	  
	  /* Check how many far human wins we would have with that move */
	  for (index = 0; index < 4; index++)
	    {
	      pos = [tmp_board scanBoardForPattern: humanWonFarPattern[index]
			       length: 6];
	      if (PATTERN_FOUND (pos))  
		{
		  squarePower += 5;
		}
	    }
	}

      RELEASE (tmp_board);
    }
  
  squarePower += boardConstants[INDEX (row, column)];
  
  return squarePower;
}
/*
 * All the following return YES upon succesfully finding a nice move
 * to do; NO otherwise.  */
/* This is always succesful.  Play in a square with lots of tiles
   around (at level 0 - much more refined at higher levels) */
- (BOOL) computerMoveInCrowdedPlace
{
  /* Temporary board to make computations */
  int *tmp_calculate = malloc (sizeof(int) * size * size );
  int max = 0;
  float importance_of_max = 0;
  int i_max = 0;
  int j_max = 0;
  int i,j;
  float tmp_importance;

  /* reset tmp_calculate */
  for (i = 0; i < size; i++)
    {
      for(j = 0; j < size; j++)
	{
	  tmp_calculate[INDEX(i, j)] = 0;
	}    
    }
  
  /* Look for crowded places */
  for (i = 1; i < size - 1; i++)
    {
      for (j = 1; j < size - 1; j++)
	{
	  if (BOARD(i, j) COMPARE_TILE (human | computer))
	    {
	      tmp_calculate[INDEX (i+1, j-1)]++;
	      tmp_calculate[INDEX (i+1, j  )]++;
	      tmp_calculate[INDEX (i+1, j+1)]++;
	      tmp_calculate[INDEX (i,   j-1)]++;
	      tmp_calculate[INDEX (i,   j+1)]++;
	      tmp_calculate[INDEX (i-1, j-1)]++;
	      tmp_calculate[INDEX (i-1, j  )]++;
	      tmp_calculate[INDEX (i-1, j+1)]++;
	    }
	}
    }

  /* avoid squares already occupied */
  for (i = 0; i < size; i++)
    {
      for(j = 0; j < size; j++)
	{	  
	  if (BOARD(i, j) COMPARE_TILE (human | computer))
	    {
	      tmp_calculate[INDEX (i, j)]=0;
	    }
	  else
	    {
	      /* we store here a non-busy cell in case we don't find
		anything better */
	      i_max=i;
	      j_max=j;
	    }
	}    
    }

  /* now we extract the solution */
  for (i = 0; i < size; i++)
    {
      for(j = 0; j < size; j++)
	{
	  if (difficultyLevel >= 3)
	    {
	      if (tmp_calculate[INDEX (i, j)] >= max)
		{
		  tmp_importance = [self powerOfSquareInRow: i column: j];
		  if ((tmp_calculate[INDEX (i, j)] > max) 
		      || (tmp_importance > importance_of_max))
		    {
		      importance_of_max = tmp_importance;
		      max = tmp_calculate[INDEX (i, j)];
		      i_max=i;
		      j_max=j;
		    }
		}
	    }
	  else
	    {
	      /* same without being too smart - just look for the most crowded
		 board square */
	      if (tmp_calculate[INDEX (i, j)] > max)
		{
		  max = tmp_calculate[INDEX (i, j)];
		  i_max = i;
		  j_max = j;
		}
	    }
	}
    }

  BOARD(i_max, j_max) = computer;
  last_move_row = i_max;
  last_move_col = j_max;

  free (tmp_calculate);

  return YES;
}

- (BOOL) computerOrHumanWinNext 
{
  patternPosition pos;
  int index;

  for (index = 0; index < 5; index++)
    {
      pos = [self scanBoardForPattern: computerWonNextPattern[index]
		  length: 5];
      if (PATTERN_FOUND (pos))
	{
	  int a, i, j;
	  
	  i = pos.startRow;
	  j = pos.startColumn;
	  for (a = 0; a < index; a++)
	    {
	      i += pos.directionRow;
	      j += pos.directionColumn;
	    }
	  BOARD(i, j) = computer;
	  last_move_row = i;
	  last_move_col = j;
	  return YES;
	}
    }

  for (index = 0; index < 5; index++)
    {
      pos = [self scanBoardForPattern: humanWonNextPattern[index]
		  length: 5];
      if (PATTERN_FOUND (pos))
	{
	  int a, i, j;
	  
	  i = pos.startRow;
	  j = pos.startColumn;
	  for (a = 0; a < index; a++)
	    {
	      i += pos.directionRow;
	      j += pos.directionColumn;
	    }
	  BOARD(i, j) = computer;
	  last_move_row = i;
	  last_move_col = j;
	  return YES;
	}
    }
  return NO;
}

- (BOOL) computerOrHumanWinFar
{
  patternPosition pos;
  int index;

  for (index = 0; index < 4; index++)
    {
      pos = [self scanBoardForPattern: computerWonFarPattern[index]
		  length: 6];
      if (PATTERN_FOUND (pos))
	{
	  int a, i, j;
	  
	  i = pos.startRow;
	  j = pos.startColumn;
	  for (a = 0; a < (index + 1); a++)
	    {
	      i += pos.directionRow;
	      j += pos.directionColumn;
	    }
	  BOARD(i, j) = computer;
	  last_move_row = i;
	  last_move_col = j;
	  return YES;
	}
    }

  for (index = 0; index < 4; index++)
    {
      pos = [self scanBoardForPattern: humanWonFarPattern[index]
		  length: 6];
      if (PATTERN_FOUND (pos))
	{
	  int a, i, j;
	  
	  i = pos.startRow;
	  j = pos.startColumn;
	  for (a = 0; a < (index + 1); a++)
	    {
	      i += pos.directionRow;
	      j += pos.directionColumn;
	    }
	  BOARD(i, j) = computer;
	  last_move_row = i;
	  last_move_col = j;
	  return YES;
	}
    }
  return NO;
}

- (void) performComputerMove
{
  switch (difficultyLevel)
    {
    case 5:
    case 4:
    case 3:
      if ([self computerOrHumanWinNext] == YES)
	{
	  break;
	}
      if ([self computerOrHumanWinFar] == YES)
	{
	  break;
	}
      /* All the refinement for higher levels goes here */
      [self computerMoveInCrowdedPlace];
      break;    
    case 2:
      if ([self computerOrHumanWinNext] == YES)
	{
	  break;
	}
      if ([self computerOrHumanWinFar] == YES)
	{
	  break;
	}
      [self computerMoveInCrowdedPlace];
      break;    
    case 1:
      if ([self computerOrHumanWinNext] == YES)
	{
	  break;
	}
      /* else fall back on level 0 strategy */
    case 0:
    default:
      [self computerMoveInCrowdedPlace];
      break;    
    }
  
  return;
}
/* Last row and column computer moved to */
- (int) lastRow
{
  return last_move_row;
}

- (int) lastColumn
{
  return last_move_col;
}

@end

@implementation MainBoard
- (id) initWithMatrix: (NSMatrix *)m
{
  self = [super init];
  ASSIGN (matrix, m);
  size = [m numberOfRows];
  board = [Board newWithRows: size ];
  [self newGame];
  return self;
}

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

- (void) newGame
{
  int i, j;
  
  /* Reset board */
  [board reset];
  for (i = 0; i < size; i++)
    {
      for (j = 0; j < size; j++)
	{
	  [self setTile: nothing
		inRow: i  
		column: j];
	}    
    }
  turnsPlayed = 0;
  /* Answer to user input */
  isGameActive = YES;
}

- (BOOL) isFreeRow: (int) i  column: (int) j
{
  if ([board tileInRow: i column: j] == nothing)
    return YES;
  else
    return NO;
}

- (void) userMoveInRow: (int) i  column: (int) j
{
  patternPosition pos;

  if (isGameActive == NO)
    return;

  if ([board tileInRow: i column: j] != nothing)
    return;
  
  [board setTile: human inRow: i column: j];
  [self setTile: human inRow: i column: j];
  pos = [board scanBoardForPattern: humanWonPattern length: 5];
  if (PATTERN_FOUND (pos))
    {
      // Human won
      int a, i, j;

      isGameActive = NO;
      i = pos.startRow;
      j = pos.startColumn;
      for (a = 0; a < 5; a++)
	{
	  [self setWinningTile: human  inRow: i  column: j];
	  i += pos.directionRow;
	  j += pos.directionColumn;
	}
      [self panel: _(@"Game Over")  info: _(@"Great! You win!")];
      return;
    }
  
  // NB: Human plays first, and the number of available squares is
  // even, so that if we are here, there still is a void square.
  [board performComputerMove];
  [self setTile: computer  inRow: [board lastRow] 
	column: [board lastColumn]];	  
  pos = [board scanBoardForPattern: computerWonPattern length: 5];
  if (PATTERN_FOUND (pos))
    {
      // Computer won
      int a, i, j;

      isGameActive = NO;
      i = pos.startRow;
      j = pos.startColumn;
      for (a = 0; a < 5; a++)
	{
	  [self setWinningTile: computer  inRow: i  column: j];
	  i += pos.directionRow;
	  j += pos.directionColumn;
	}
      [self panel: _(@"Game Over")  info: _(@"Sorry, you loose.")];
    }  
  turnsPlayed++;
  if (turnsPlayed >= ((size * size) / 2))
    {
      isGameActive = NO;
      [self panel: _(@"Game Over")  info: _(@"Quits")];
    }  
}

- (void) setDifficultyLevel: (int) i
{
  if ((i >=0) && (i <= 5))
    {
      difficultyLevel = i;
      [board setDifficultyLevel: i];
    }
  else
    {
      NSLog (_(@"Internal Error: unknown difficultyLevel"));
    }
}

- (int) difficultyLevel
{
  return difficultyLevel;
}

/* 
 * Methods used to display the board. 
 * Override/rewrite the following methods to port the game 
 * to another GUI framework.
 */
// Change contents of (row i, column j) to display tile t (which can
// be nothing, computer or human, see enum above)
- (void) setTile: (tile) t
	   inRow: (int) i  
	  column: (int) j
{
  NSImageCell *cell;
  NSRect rect;
  NSString *position;
  NSString *imageName;

  /* Determine the position of the cell */
  position = @"";
  
  if (i == 0)
    {
      position = @"Top";
    }
  else if (i == size - 1)
    {
      position = @"Bottom";
    }
  
  if (j == 0)
    {
      position = [NSString stringWithFormat: @"%@Left", position];
    }
  else if (j == size - 1)
    {
      position = [NSString stringWithFormat: @"%@Right", position];
    }

  switch (t)
    {
    case human:
      imageName = [NSString stringWithFormat: @"Human%@", position];
      break;

    case computer:
      imageName = [NSString stringWithFormat: @"Computer%@", position];
      break;

    case nothing:
    default:
      imageName = [NSString stringWithFormat: @"Empty%@", position];
      break;
    }

  cell = [matrix cellAtRow: i column: j];
  [cell setImage: [NSImage imageNamed: imageName]];
  rect = [matrix cellFrameAtRow: i column: j];
  [matrix setNeedsDisplayInRect: rect];
}

// Change contents of (row i, column j) to display a winning tile of
// type t (which can be nothing, computer or human, see enum above).
// This is invoked when one the two players win, to highlight the
// winning combination.
- (void) setWinningTile: (tile) t
		  inRow: (int) i
		 column: (int) j
{
  // TODO
}

// Display an alert to the user through a pop up panel; typically
// invoked as: [self panel: @"Game Over" info: @"Human won"];
- (void) panel: (NSString *)s
	  info: (NSString *)t
{
  NSRunAlertPanel (s, t, _(@"OK"), NULL, NULL);
}

@end