Advertisement
Guest User

Untitled

a guest
Jun 24th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.34 KB | None | 0 0
  1. /**
  2. * Created by Michael Gold on 6/15/2017.
  3. */
  4. public class GameLogic {
  5. private Board board;
  6. private JBoard jboard;
  7. private Token.Color color;
  8. public GameLogic(Board board, JBoard jboard) {
  9. this.board = board;
  10. this.jboard = jboard;
  11. this.color = Token.Color.Red;
  12. }
  13. public void giveTurn(Position justPlayed) {
  14. if(this.didTheyWin(justPlayed)) {
  15.  
  16. }
  17. if(this.color == Token.Color.Red) this.color = Token.Color.Blue;
  18. else this.color = Token.Color.Red;
  19. //give a color:::
  20. //wait for player to play:::
  21. if(this.didTheyWin(justPlayed)) {
  22. //game is over, there is a winner.
  23. }
  24. //else if(/* no more tiles to play*/) {
  25. //game is over, it's a draw.
  26. //}
  27. this.jboard.update();
  28. //set whose turn it is
  29. //enable for that player
  30. //disable for other player
  31. //wait for them to play
  32. //check if game is over, if so then declare this player winner and other player loser
  33. }
  34. private boolean didTheyWin(Position p) {
  35. //get a player to check for, just after their turn.
  36. //for each direction from the new token:
  37. //check columns, rows, diagonal, diagonal for four in a row.
  38. //Note: this method calculates the total number of connections. There only need to be at least 3 connections to call it "four in a row."
  39.  
  40. //search NE and SW
  41. Position ne = new Position(1, -1);
  42. Position sw = new Position(-1, 1);
  43. if(this.board.sameColorPerDirection(p, ne) + this.board.sameColorPerDirection(p, sw) > 2) {return true;}
  44. //search E and W
  45. Position e = new Position(1, 0);
  46. Position w = new Position(-1, 0);
  47. if(this.board.sameColorPerDirection(p, e) + this.board.sameColorPerDirection(p, w) > 2) {return true;}
  48. //search SE and NW
  49. Position se = new Position(1, 1);
  50. Position nw = new Position(-1, -1);
  51. if(this.board.sameColorPerDirection(p, se) + this.board.sameColorPerDirection(p, nw) > 2) {return true;}
  52. //search S (no need to check north, impossible for anything to be above the just played tile)
  53. Position s = new Position (0, 1);
  54. if(this.board.sameColorPerDirection(p, s) + 1 > 3) {return true;}
  55. return false;
  56. }
  57. }
  58.  
  59. public class Board {
  60. private int width;
  61. private int height;
  62. private Tile[][] tiles;
  63.  
  64. public Board(int width, int height) {
  65. this.width = width;
  66. this.height = height;
  67. this.tiles = new Tile[this.width][this.height];
  68. for(int x=0; x<this.width; x++) {
  69. for(int y=0; y<this.height; y++) {
  70. this.tiles[x][y] = new Tile();
  71. }
  72. }
  73. this.reset();
  74. }
  75. public void reset() {
  76. for(int x=0; x<this.width; x++) {
  77. for(int y=0; y<this.height; y++) {
  78. Tile t = this.tiles[x][y];
  79. if(y == this.height-1) {t.setIsPlayable(true);}
  80. else t.setIsPlayable(false);
  81. }
  82. }
  83. }
  84. public int sameColorPerDirection(Position start, Position direction) {
  85. int counter = 0;
  86. Position nbr = Position.add(start, direction);
  87. if(this.getTile(start).getToken().getColor() == this.getTile(nbr).getToken().getColor()) {
  88. counter++;
  89. counter += sameColorPerDirection(nbr, direction);
  90. }
  91. return counter;
  92.  
  93. //0
  94. //add
  95. //if equals...
  96. //1
  97. //0
  98. //add
  99. //if equals...
  100. //1
  101. //0
  102. //add
  103. //if equals... nope
  104. //returns 0;
  105. //1+0;
  106. //returns 1;
  107. //1+1;
  108. //returns 2;
  109. //this is correct
  110. }
  111. public void updatePlayableTiles(Position curPos) {
  112. //input position is the most recently played tile.
  113. this.tiles[curPos.getX()][curPos.getY()].setIsPlayable(false);
  114. if(curPos.getY() > 0) { this.tiles[curPos.getX()][curPos.getY() - 1].setIsPlayable(true); }
  115. }
  116. public Tile getTile(Position p) {
  117. if (p.getX() < 0 || p.getX() > this.width) return null;
  118. if (p.getY() < 0 || p.getY() > this.height) return null;
  119. return this.tiles[p.getX()][p.getY()];
  120. }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement