/src/ru/spbau/martynov/tictactoe/Game.java

https://github.com/SemenMartynov/SPbAU_Java · Java · 82 lines · 22 code · 6 blank · 54 comment · 4 complexity · acf04ec3f7647f25b73221bfac45e258 MD5 · raw file

  1. /**
  2. * Copyright 2013 Semen A Martynov <semen.martynov@gmail.com>
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining
  5. * a copy of this software and associated documentation files (the
  6. * "Software"), to deal in the Software without restriction, including
  7. * without limitation the rights to use, copy, modify, merge, publish,
  8. * distribute, sublicense, and/or sell copies of the Software, and to
  9. * permit persons to whom the Software is furnished to do so, subject to
  10. * the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be
  13. * included in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  19. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  20. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  21. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. package ru.spbau.martynov.tictactoe;
  24. import java.util.Iterator;
  25. import java.util.List;
  26. /**
  27. *
  28. * @author Semen Martynov
  29. *
  30. * Class of the abstract game. Allows to check passed classes on
  31. * compliance to required game.
  32. *
  33. */
  34. public abstract class Game implements Iterable<Position> {
  35. /**
  36. * Constructor. Receives two players.
  37. *
  38. * @param player1
  39. * - The first player.
  40. * @param player2
  41. * - The second player.
  42. */
  43. public Game(Player player1, Player player2) {
  44. // Window -> Preferences -> Java -> Compiler -> Errors/Warnings ->
  45. // Deprecated and restricted API
  46. if (sun.reflect.Reflection.getCallerClass(2) != player1.getGameType()) {
  47. throw new RuntimeException(
  48. "The first player don't know how to play...");
  49. }
  50. if (sun.reflect.Reflection.getCallerClass(2) != player2.getGameType()) {
  51. throw new RuntimeException(
  52. "The second player don't know how to play...");
  53. }
  54. }
  55. /**
  56. * Abstract method of determining the winner.
  57. *
  58. * @return 1 if X wins, 2 if O wins, 0 if it is possible to continue to
  59. * play, -1 if the draw.
  60. */
  61. public abstract int getWinner();
  62. /**
  63. * Implementing an iterator to iterate field states.
  64. *
  65. * @return iterator of field states.
  66. */
  67. @Override
  68. public Iterator<Position> iterator() {
  69. Iterator<Position> iterator = positions.iterator();
  70. return iterator;
  71. }
  72. /**
  73. * List of field states.
  74. */
  75. protected List<Position> positions;
  76. }