lajdlksadlmla
[rmh3093.git] / project2 / RCS / FrenzyController.java,v
bloba06e89d83e5566363c088fdf067635d91f3e8ee5
1 head    1.5;
2 access;
3 symbols;
4 locks
5         rmh3093:1.5; strict;
6 comment @# @;
9 1.5
10 date    2008.05.17.03.29.37;    author rmh3093; state Exp;
11 branches;
12 next    1.4;
14 1.4
15 date    2008.05.16.23.04.18;    author rmh3093; state Exp;
16 branches;
17 next    1.3;
19 1.3
20 date    2008.05.16.22.06.44;    author rmh3093; state Exp;
21 branches;
22 next    1.2;
24 1.2
25 date    2008.04.30.04.10.52;    author rmh3093; state Exp;
26 branches;
27 next    1.1;
29 1.1
30 date    2008.04.26.15.45.41;    author rmh3093; state Exp;
31 branches;
32 next    ;
35 desc
36 @initial commit, mostly working
40 1.5
41 log
42 @add semi-random movement pattern
44 text
45 @/*
46  * FrenzyController.java
47  *
48  * Version:
49  *     $Id: FrenzyController.java,v 1.4 2008/05/16 23:04:18 rmh3093 Exp rmh3093 $
50  *
51  * Revisions:
52  *     $Log: FrenzyController.java,v $
53  *     Revision 1.4  2008/05/16 23:04:18  rmh3093
54  *     implement scrolling pane for last consumed object
55  *
56  *     Revision 1.3  2008/05/16 22:06:44  rmh3093
57  *     meets final submission goals
58  *
59  *     Revision 1.2  2008/04/30 04:10:52  rmh3093
60  *     break out random number generator, first attempt at moving enemies
61  *
62  *     Revision 1.1  2008/04/26 15:45:41  rmh3093
63  *     Initial revision
64  *
65  */
68 Copyright (c) 2008, Ryan M. Hope
69 All rights reserved.
71 Redistribution and use in source and binary forms, with or without modification,
72 are permitted provided that the following conditions are met:
74  * Redistributions of source code must retain the above copyright notice,
75           this list of conditions and the following disclaimer.
76  * Redistributions in binary form must reproduce the above copyright notice,
77           this list of conditions and the following disclaimer in the documentation
78           and/or other materials provided with the distribution.
79  * Neither the name of the project nor the names of its contributors may be
80           used to endorse or promote products derived from this software without
81           specific prior written permission.
83 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
84 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
85 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
86 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
87 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
88 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
89 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
90 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
91 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
92 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
93  */
95 import java.awt.event.*;
96 import java.util.TreeSet;
98 /**
99  * Controls the Frenzy view and updates the model
100  * 
101  * @@author Ryan Hope
102  */
103 public class FrenzyController {
105         // Random number generator
106         private BetterRandom betterRandom;
108         private FrenzyModel f_model;
109         private FrenzyView  f_view;
111         /**
112          * Create a new Frenzy Controller
113          * 
114          * @@param f_model the Frenzy game f_model
115          * @@param f_view the Frenzy game f_view
116          */
117         FrenzyController(FrenzyModel f_model, FrenzyView f_view, 
118                         BetterRandom betterRandom) {
119                 this.f_model = f_model;
120                 this.f_view  = f_view;
121                 this.betterRandom = betterRandom;
123                 // Add player to board in random position
124                 f_model.player = new FrenzyPlayer();
125                 spawnPlayer();
127                 // Add listeners to f_view
128                 f_view.addQuitListener(new QuitListener());
129                 f_view.addResetListener(new ResetListener());
130                 f_view.addSpawnListener(new SpawnListener());
131                 f_view.addPauseListener(new PauseListener());
132                 f_view.addPlayerControlsListener(new PlayerControlsListener());
133                 f_view.addEnemySpawnListener(new SpawnListener());
134                 f_view.addFasterEnemyListener(new FasterEnemyListener());
135                 f_view.addSlowerEnemyListener(new SlowerEnemyListener());
136         }
138         private void resumeGame() {
139                 TreeSet<String> keys = 
140                         new TreeSet<String>(f_model.enemies.keySet());
141                 for (String key : keys) {
142                         FrenzyEnemy enemy = f_model.enemies.get(key);
143                         if (enemy != null) {
144                                 synchronized (enemy.moverThread) {
145                                         enemy.moverThread.notifyAll();
146                                 }
147                                 // Sleep for a random amount of time so that when enemies wake up 
148                                 // from sleeping they dont look like they are moving in sync
149                                 try {
150                                         Thread.sleep(betterRandom.rand.nextInt(50));
151                                 } catch (InterruptedException e) {
152                                 }
153                         }
154                 }
155                 synchronized (f_view.enemySpawnerThread) {
156                         f_view.enemySpawnerThread.notifyAll();
157                 }
158         }
160         protected synchronized void spawnEnemy() {
161                 if (f_model.gamestate) {
162                         // Can't spawn new enemies if there are no unique ASCII characters
163                         if (f_model.enemyCount < FrenzyModel.MAX_ENIMES) {
165                                 while (true) {
167                                         int num = betterRandom.rand.nextInt(255);
169                                         // These characters don't print
170                                         if (num < 33 || ((num > 126) && (num < 161)) || num == 173)
171                                                 continue;
173                                         // Can't use players symbol
174                                         if (String.valueOf((char)num) == FrenzyPlayer.symbol)
175                                                 continue;
177                                         // The symbol for the new enemy
178                                         String symbol = String.valueOf((char)num);
180                                         // Check to make sure the symbol is already in use
181                                         if (f_model.enemies.containsKey(symbol)) continue;
183                                         // Find a random unoccupied location for the enemy to spawn
184                                         int x = -1;
185                                         int y = -1;
186                                         while (true) {
187                                                 x = betterRandom.rand.nextInt(f_model.getBoardSize());
188                                                 y = betterRandom.rand.nextInt(f_model.getBoardSize());
189                                                 if (f_view.boardSquares[y][x].isOccupied() == 
190                                                         FrenzyModel.OCCUPANT.NONE) {
191                                                         break;
192                                                 }
193                                         }
195                                         int move_x = betterRandom.rand.nextInt(2);
196                                         int move_y = betterRandom.rand.nextInt(2);
197                                         FrenzyEnemy.MOVEMENT_PATTERN mp = null;
198                                         // Spawn the new enemy
199                                         if ((move_x == 1) && (move_y == 1)) {
200                                                 mp = FrenzyEnemy.MOVEMENT_PATTERN.NE;
201                                         } else if ((move_x == 1) && (move_y == 0)) {
202                                                 mp = FrenzyEnemy.MOVEMENT_PATTERN.SE;
203                                         } else if ((move_x == 0) && (move_y == 1)) {
204                                                 mp = FrenzyEnemy.MOVEMENT_PATTERN.NW;
205                                         } else if ((move_x == 0) && (move_y == 0)) {
206                                                 mp = FrenzyEnemy.MOVEMENT_PATTERN.SW;
207                                         }
208                                         FrenzyEnemy     enemy = 
209                                                 new FrenzyEnemy(f_model, x, y, symbol, mp,
210                                                                 betterRandom.rand.nextInt(3),
211                                                                 new EnemyMover(), betterRandom);
212                                         f_model.enemies.put(symbol, enemy);
213                                         f_view.addOccupant(enemy, f_view.boardSquares[y][x]);
214                                         f_model.increaseEnemyCount();
215                                         enemy.moverThread.start();
216                                         break;
217                                 }
219                         }
220                 } else {
221                         synchronized (f_view.enemySpawnerThread) {
222                                 try {
223                                         f_view.enemySpawnerThread.wait();
224                                 } catch (InterruptedException e) {
225                                 }
226                         }
227                 }
228         }
230         /**
231          * Handles moving the Frenzy player around the game board
232          * 
233          * @@author Ryan Hope
234          */
235         class PlayerControlsListener implements KeyListener {
237                 public void keyPressed(KeyEvent e) {
238                 }
240                 public void keyTyped(KeyEvent e) {              
241                 }
243                 // Only perform an action on the release of the key
244                 public void keyReleased(KeyEvent e) {
246                         if (f_model.gamestate) {
248                                 int keycode = e.getKeyCode();
249                                 int x_new = -1;
250                                 int y_new = -1;
251                                 int x_old = f_model.player.getXposition();
252                                 int y_old = f_model.player.getYposition();
253                                 if ((keycode > 36 && keycode < 41) && f_model.gamestate) {
255                                         // Remove player from current location
256                                         f_view.removeOccupant(f_view.boardSquares[y_old][x_old]);
258                                         switch (keycode) {
260                                         // Move left
261                                         case 37: 
262                                                 if (x_old == 0) {
263                                                         x_new = f_model.boardsize-1;
264                                                 } else {
265                                                         x_new = x_old-1;
266                                                 }
267                                                 y_new = y_old;
268                                                 f_model.player.setXposition(x_new);
269                                                 break;
271                                                 // Move up      
272                                         case 38:
273                                                 if (y_old == 0) {
274                                                         y_new = f_model.boardsize-1;
275                                                 } else {
276                                                         y_new = y_old-1;
277                                                 }
278                                                 x_new = x_old;
279                                                 f_model.player.setYposition(y_new);
280                                                 break;
282                                                 // Move right   
283                                         case 39:
284                                                 if (x_old == f_model.boardsize-1) {
285                                                         x_new = 0;
286                                                 } else {
287                                                         x_new = x_old+1;
288                                                 }
289                                                 y_new = y_old;
290                                                 f_model.player.setXposition(x_new);
291                                                 break;
293                                                 // Move down    
294                                         case 40:
295                                                 if (y_old == f_model.boardsize-1) {
296                                                         y_new = 0;
297                                                 } else {
298                                                         y_new = y_old+1;
299                                                 }
300                                                 x_new = x_old;
301                                                 f_model.player.setYposition(y_new);
302                                                 break;
303                                         }
305                                         // If new location is occupied by an enemy, eat it
306                                         if (f_view.boardSquares[y_new][x_new].isOccupied() == 
307                                                 FrenzyModel.OCCUPANT.ENEMY) {
309                                                 // Eat enemy
310                                                 String key = 
311                                                         f_view.boardSquares[y_new][x_new].getText();
312                                                 f_model.player.eatEnemy();
313                                                 f_model.decreaseEnemyCount();
314                                                 if (f_model.player.getConsumedCount() > 0 && 
315                                                                 f_model.enemyCount==0) {
316                                                         f_model.gamestate = false;
317                                                         f_view.statusLabel.setText("Game over.");
318                                                 }
320                                                 // Remove key from list so that the symbol can be reused
321                                                 f_model.enemies.remove(key);
323                                                 // ====================================================
324                                                 // These should be handled by events
326                                                 // Update the last action display
327                                                 f_view.lastActionArea.setText(
328                                                                 f_model.player.getSymbol() + " ate " + key +
329                                                                 "\n" + f_view.lastActionArea.getText());
331                                                 // Update the consumed enemy count display
332                                                 int count = f_model.player.getConsumedCount();
333                                                 f_view.consumedLabel.setText(String.valueOf(count));
335                                                 // ====================================================
336                                         }
338                                         // Add player to new square
339                                         f_view.addOccupant(f_model.player, 
340                                                         f_view.boardSquares[y_new][x_new]);
341                                 }
342                         }
343                 }
344         }
346         /**
347          * Handles quitting Frenzy
348          * 
349          * @@author Ryan Hope
350          */
351         class QuitListener implements ActionListener {
352                 public void actionPerformed(ActionEvent e) {
353                         f_view.dispose();
354                         System.exit(0);
355                 }
356         }
358         /**
359          * Handles resetting Frenzy
360          * 
361          * @@author Ryan Hope
362          */
363         class ResetListener implements ActionListener {
364                 public void actionPerformed(ActionEvent e) {
365                         f_model.gamestate = true;
366                         f_view.statusLabel.setText("Game Running!");
367                         f_view.pauseControl.setSelected(false);
368                         f_model.player.setConsumedCount(0);
369                         f_view.consumedLabel.setText("0");
370                         if (f_model.player.dead) {
371                                 spawnPlayer();
372                                 f_model.player.dead = false;
373                         }
374                         resumeGame();
375                 }
376         }
378         /**
379          * Handles resetting Frenzy
380          * 
381          * @@author Ryan Hope
382          */
383         class PauseListener implements ActionListener {
384                 public void actionPerformed(ActionEvent e) {
385                         if (f_view.pauseControl.isSelected()) {
386                                 f_model.gamestate = false;
387                                 f_view.statusLabel.setText("Game Paused!");
388                         } else {
389                                 f_model.gamestate = true;
390                                 if (!f_model.player.dead) resumeGame();
391                                 f_view.statusLabel.setText("Game Running!");
392                         }
393                 }
394         }
396         /**
397          * Handles spawning of new enemies
398          * 
399          * @@author Ryan Hope
400          */
401         class SpawnListener implements ActionListener {
403                 public void actionPerformed(ActionEvent e) {
404                         if (f_model.gamestate) spawnEnemy();
405                 }
407         }
409         /**
410          * Handles increasing enemy speed
411          * 
412          * @@author Ryan Hope
413          */
414         class FasterEnemyListener implements ActionListener {
416                 public void actionPerformed(ActionEvent e) {
417                         f_model.enemyMoveInterval = f_model.enemyMoveInterval - 10; 
418                 }
420         }
422         /**
423          * Handles decreasing enemy speed
424          * 
425          * @@author Ryan Hope
426          */
427         class SlowerEnemyListener implements ActionListener {
429                 public void actionPerformed(ActionEvent e) {
430                         f_model.enemyMoveInterval = f_model.enemyMoveInterval + 10; 
431                 }
433         }
435         /**
436          * Handles moving enemies
437          * 
438          * @@author Ryan Hope
439          */
440         class EnemyMover {
442                 public synchronized void move(String symbol) {
443                         FrenzyEnemy enemy = f_model.enemies.get(symbol);
444                         FrenzyEnemy eaten = null;
445                         if (enemy != null) {
446                                 int x = enemy.getXposition();
447                                 int y = enemy.getYposition();
448                                 FrenzyModel.OCCUPANT occupant_t = 
449                                         f_view.boardSquares[y][x].isOccupied();
450                                 if (occupant_t != FrenzyModel.OCCUPANT.NONE) {
451                                         String occupant = f_view.boardSquares[y][x].getText();
452                                         if (occupant_t == FrenzyModel.OCCUPANT.PLAYER) {
453                                                 f_model.gamestate = false;
454                                                 f_model.player.dead = true;
455                                                 f_view.statusLabel.setText("Game over.");
456                                         } else {
457                                                 eaten = f_model.enemies.get(occupant);
458                                                 if (eaten == null) {
459                                                         // what causes this? 
460                                                         System.out.println("Oh no!");
461                                                 } else {
462                                                         eaten.move = false;
463                                                 }
464                                                 f_model.enemies.remove(occupant);
465                                         }
466                                         f_view.lastActionArea.setText(
467                                                         symbol + " ate " + occupant +
468                                                         "\n" + f_view.lastActionArea.getText());
469                                 }
470                                 f_view.removeOccupant(
471                                                 f_view.boardSquares[enemy.Yposition_old][enemy.Xposition_old]);
472                                 f_view.addOccupant(enemy, f_view.boardSquares[y][x]);
473                         }
474                 }
476         }
478         private void spawnPlayer() {
479                 while (true) {
480                         int x = betterRandom.rand.nextInt(f_model.boardsize);
481                         int y = betterRandom.rand.nextInt(f_model.boardsize);
482                         if (f_view.boardSquares[y][x].isOccupied() == 
483                                 FrenzyModel.OCCUPANT.NONE) {
484                                 f_model.player.setXposition(x);
485                                 f_model.player.setYposition(y);
486                                 break;
487                         }
488                 }
489                 f_view.spawnPlayer();
490         }
497 @implement scrolling pane for last consumed object
499 text
500 @d5 1
501 a5 1
502  *     $Id: FrenzyController.java,v 1.3 2008/05/16 22:06:44 rmh3093 Exp rmh3093 $
503 d9 3
504 d166 2
505 a167 1
506                                                                 new EnemyMover());
512 @meets final submission goals
514 text
515 @d5 1
516 a5 1
517  *     $Id: FrenzyController.java,v 1.2 2008/04/30 04:10:52 rmh3093 Exp $
518 d9 3
519 d279 3
520 a281 2
521                                                 f_view.lastActionLabel.setText(
522                                                                 f_model.player.getSymbol() + " ate " + key);
523 d418 3
524 a420 2
525                                         f_view.lastActionLabel.setText(
526                                                         symbol + " ate " + occupant);
532 @break out random number generator, first attempt at moving enemies
534 text
535 @d5 1
536 a5 1
537  *     $Id: FrenzyController.java,v 1.1 2008/04/26 15:45:41 rmh3093 Exp rmh3093 $
538 d9 3
539 d46 1
540 d74 2
541 a75 3
542                 f_model.player = new FrenzyPlayer(
543                                 betterRandom.rand.nextInt(f_model.boardsize),
544                                 betterRandom.rand.nextInt(f_model.boardsize));
545 d84 3
546 d88 20
547 d110 1
548 a110 1
549         private synchronized void spawnEnemy() {
550 d131 1
551 a131 1
552                                         if (f_model.enimies.containsKey(symbol)) continue;
553 d161 1
554 a161 1
555                                         f_model.enimies.put(symbol, enemy);
556 d169 7
557 d195 1
558 a195 1
559                         if (!f_view.pauseControl.isSelected()) {
560 d270 1
561 a270 1
562                                                 f_model.enimies.remove(key);
563 d318 5
564 d338 1
565 d358 26
566 d390 2
567 a391 2
568                 public void move(String symbol) {
569                         FrenzyEnemy enemy = f_model.enimies.get(symbol);
570 d402 1
571 a402 1
572                                                 f_model.player.died = true;
573 d404 9
574 a413 2
575                                         eaten = f_model.enimies.get(occupant);
576                                         eaten.move = false;
577 a416 3
578                                 while (eaten != null && eaten.moverThread.isAlive()) {
579                                         Thread.yield();
580                                 }
581 d425 14
587 @Initial revision
589 text
590 @d5 1
591 a5 1
592  *     $Id$
593 d8 4
594 a11 1
595  *     $Log$
596 a42 5
597 import java.io.File;
598 import java.io.FileInputStream;
599 import java.io.FileNotFoundException;
600 import java.io.IOException;
601 import java.util.Random;
602 d44 5
603 d51 17
604 a67 34
605         protected FrenzyModel m_model;
606         protected FrenzyView  m_view;
607         public Random rand;
608         private int boardsize = -1;
610         FrenzyController(FrenzyModel model, FrenzyView view) {
611                 m_model = model;
612                 m_view  = view;
613                 boardsize = m_model.getBoardSize();
615                 // Get better random numbers
616                 File urandom = new File("/dev/urandom");
617                 byte[] seed = new byte[4];
618                 if (urandom.exists()) {
619                         FileInputStream streamer = null;
620                         try {
621                                 streamer = new FileInputStream(urandom);
622                         } catch (FileNotFoundException e) {
623                                 e.printStackTrace();
624                         }
625                         try {
626                                 streamer.read(seed);
627                         } catch (IOException e) {
628                                 e.printStackTrace();
629                         }
630                         int value = 0;
631                         for (int i = 0; i < 4; i++) {
632                                 int shift = (4 - 1 - i) * 8;
633                                 value += (seed[i] & 0x000000FF) << shift;
634                         }
635                         rand = new Random(value);
636                 } else {
637                         rand = new Random();
638                 }
639 d70 13
640 a82 2
641                 m_model.player = m_model.new Player(rand.nextInt(model.getBoardSize()),
642                                 rand.nextInt(model.getBoardSize()));
643 d84 4
644 a87 5
645                 // Add listeners to view
646                 view.addQuitListener(new QuitListener());
647                 view.addResetListener(new ResetListener());
648                 view.addSpawnListener(new SpawnListener());
649                 view.addPlayerControlsListener(new PlayerControlsListener());
650 d89 55
651 a143 1
652                 view.updateBoard();
653 d146 5
654 d159 1
655 d161 58
656 a218 13
657                         int keycode = e.getKeyCode();
658                         int x_new = -1;
659                         int y_new = -1;
660                         int x_old = m_model.player.getXposition();
661                         int y_old = m_model.player.getYposition();
662                         if (keycode > 36 && keycode < 41) {
663                                 m_view.removeOccupant(m_view.boardSquares[y_old][x_old]);
664                                 switch (keycode) {
665                                 case 37: 
666                                         if (x_old == 0) {
667                                                 x_new = boardsize-1;
668                                         } else {
669                                                 x_new = x_old-1;
670 d220 31
671 a250 8
672                                         y_new = y_old;
673                                         m_model.player.setXposition(x_new);
674                                         break;
675                                 case 38:
676                                         if (y_old == 0) {
677                                                 y_new = boardsize-1;
678                                         } else {
679                                                 y_new = y_old-1;
680 d252 4
681 a255 21
682                                         x_new = x_old;
683                                         m_model.player.setYposition(y_new);
684                                         break;
685                                 case 39:
686                                         if (x_old == boardsize-1) {
687                                                 x_new = 0;
688                                         } else {
689                                                 x_new = x_old+1;
690                                         }
691                                         y_new = y_old;
692                                         m_model.player.setXposition(x_new);
693                                         break;
694                                 case 40:
695                                         if (y_old == boardsize-1) {
696                                                 y_new = 0;
697                                         } else {
698                                                 y_new = y_old+1;
699                                         }
700                                         x_new = x_old;
701                                         m_model.player.setYposition(y_new);
702                                         break;
703 a256 12
704                                 if (m_view.boardSquares[y_new][x_new].isOccupied() == 
705                                         FrenzyModel.OCCUPANT.ENEMY) {
706                                         String key = m_view.boardSquares[y_new][x_new].getText();
707                                         m_model.player.eatEnimie();
708                                         m_model.enimies.remove(key);
709                                         m_view.lastActionLabel.setText(m_model.player.getSymbol() + 
710                                                         " ate " + key);
711                                         int count = m_model.player.getConsumedCount();
712                                         m_view.consumedLabel.setText(String.valueOf(count));
713                                 }
714                                 m_view.addOccupant(m_model.player, 
715                                                 m_view.boardSquares[y_new][x_new]);
716 d261 5
717 d268 1
718 a268 1
719                         m_view.dispose();
720 d273 5
721 d280 5
722 d288 22
723 d311 1
724 d313 26
725 a338 16
726                         if (m_model.enimieCount <= FrenzyModel.MAX_ENIMES) {
727                                 while (true) {
728                                         int num = rand.nextInt(127);
729                                         if (num < 33) continue;
730                                         if ((char)num == 'P') continue;
731                                         String symbol = String.valueOf((char)num);
732                                         if (m_model.enimies.containsKey(symbol)) continue;
733                                         int x = -1;
734                                         int y = -1;
735                                         while (true) {
736                                                 x = rand.nextInt(m_model.getBoardSize());
737                                                 y = rand.nextInt(m_model.getBoardSize());
738                                                 if (m_view.boardSquares[y][x].isOccupied() == 
739                                                         FrenzyModel.OCCUPANT.NONE) {
740                                                         break;
741                                                 }
742 d340 7
743 a346 6
744                                         FrenzyModel.Enemy enemy = 
745                                                 m_model.new Enemy(x, y, symbol); 
746                                         m_model.enimies.put(symbol, enemy);
747                                         m_view.addOccupant(enemy, m_view.boardSquares[y][x]);
748                                         m_model.enimieCount++;
749                                         break;
750 d348 3
751 d353 1