Advertisement
Guest User

Untitled

a guest
Nov 14th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.16 KB | None | 0 0
  1. public class Controller
  2. {
  3. private final Model model;
  4. private Player client;
  5. private PlayerView view;
  6. private boolean isMyTurn = false;
  7. private boolean gameIsStarted;
  8.  
  9. public Controller(PlayerView v, Model m)
  10. {
  11. model = m;
  12. view = v;
  13. if (view != null)
  14. view.setController(this);
  15. }
  16.  
  17. public void attemptSetPiece(int x, int y)
  18. {
  19. if (!gameIsStarted)
  20. {
  21. return;
  22. }
  23.  
  24. if (!isMyTurn)
  25. {
  26. return;
  27. }
  28.  
  29. if (isGameOver())
  30. {
  31. return;
  32. }
  33. if (isValid(x, y))
  34. {
  35. isMyTurn = false;
  36. client.placePiece(model.getPlayer(), x, y);
  37. }
  38. }
  39.  
  40. private boolean isGameOver()
  41. {
  42. if (hasWon("X", model.getBoardInfo()))
  43. return true;
  44. if (hasWon("O", model.getBoardInfo()))
  45. return true;
  46. return false;
  47. }
  48.  
  49. public boolean hasWon(String player, String[][] board)
  50. {
  51. out: for (int x = 0; x < 3; x++)
  52. {
  53. for (int y = 0; x < 3; y++)
  54. {
  55. if (board[x][y] == null || !board[x][y].equalsIgnoreCase(null))
  56. continue out;
  57. }
  58. }
  59. out: for (int x = 0; x < 3; x++)
  60. {
  61. for (int y = 0; x < 3; y++)
  62. {
  63. if (board[x][y] == null || !board[x][y].equalsIgnoreCase(null))
  64. continue out;
  65. }
  66. }
  67.  
  68. }
  69.  
  70. public boolean isValid(int x, int y)
  71. {
  72. String[][] boardInfo = model.getBoardInfo();
  73. return boardInfo[x][y].isEmpty();
  74. }
  75.  
  76. public void setPiece(String player, int x, int y)
  77. {
  78. if (!player.equalsIgnoreCase(model.getPlayer()))
  79. {
  80. isMyTurn = true;
  81. }
  82.  
  83. model.putPiece(player, x, y);
  84. view.placePiece(player, x, y);
  85. if (hasWon(model.getPlayer(), model.getBoardInfo()))
  86. {
  87. System.out.println("I won!!!");
  88. }
  89. }
  90.  
  91. public String getPlayer()
  92. {
  93. return model.getPlayer();
  94. }
  95.  
  96. public void setPlayer(String p)
  97. {
  98. model.setPlayer(p);
  99. }
  100.  
  101. public void setIsMyTurn(boolean b)
  102. {
  103. isMyTurn = b;
  104. }
  105.  
  106. public void setGameIsStarted()
  107. {
  108. gameIsStarted = true;
  109. }
  110.  
  111. public void setClient(Player client)
  112. {
  113. this.client = client;
  114. }
  115.  
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement