Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.57 KB | None | 0 0
  1. package sample;
  2.  
  3.  
  4. import java.util.Arrays;
  5.  
  6. public class LinkedList {
  7. Node head;
  8. LinkedList(Model model,Update update){
  9. head=new Node(model,update);
  10.  
  11.  
  12. }
  13.  
  14. }
  15.  
  16. class Node {
  17. static int search=2;
  18. static int t=0;
  19. static boolean turn;
  20. boolean playerOneVictory;
  21. boolean playerTwoVictory;
  22. int sumPlayerOne;
  23. int sumPlayerTwo;
  24. int[][]currentGame;
  25. static Model model;
  26. static Update update;
  27. Node columnOne;
  28. Node columnTwo;
  29. Node columnThree;
  30. Node columnFour;
  31. Node columnFive;
  32. Node columnSix;
  33. Node columnSeven;
  34. String coordinate="";
  35.  
  36. Node (Model model, Update update){
  37. this.model=model;
  38. this.update=update;
  39. turn=model.isPlayerTurn();
  40. if(turn==false) {
  41. addMoves(model.getGridAllDiscs(), 0,"start");
  42. }
  43. }
  44.  
  45. Node(int[][] previousGrid, String coordinate, boolean playerOneVictory, boolean playerTwoVictory,int column, String coords, boolean turn){
  46. this.playerOneVictory=playerOneVictory;
  47. this.playerTwoVictory=playerTwoVictory;
  48. this.coordinate=coordinate;
  49. currentGame=previousGrid;
  50. t=t+1;
  51. System.out.println(t);
  52. String [] xy=coords.split(",");
  53. System.out.println(xy[0]+","+xy[1]);
  54. if(turn==true){
  55. currentGame[Integer.parseInt(xy[0])][Integer.parseInt(xy[1])]=1;
  56. }else{
  57. currentGame[Integer.parseInt(xy[0])][Integer.parseInt(xy[1])]=2;
  58. }
  59. if(search>-1&&search<9) {
  60. this.turn=!this.turn;
  61. addMoves(currentGame, column,coords);
  62. }
  63. }
  64.  
  65. public void addMoves(int[][]currentState,int column, String coord){
  66. System.out.println(search+"Search");
  67. coordinate=coord;
  68. int[][]tempGrid=currentState;
  69. if(coord.equals("start")) {
  70. coordinate = "3,5";
  71. tempGrid[3][5]=2;
  72. columnOne=new Node(tempGrid,coordinate,playerOneVictory,playerTwoVictory,0,"0,5",turn);
  73. turn=!turn;
  74. }else{
  75. String[]temp=coordinate.split(",");
  76. int newY=Integer.parseInt(temp[1]);
  77. if(newY>0){
  78. newY = newY - 1;
  79. coordinate=column+","+newY;
  80. System.out.println("HEY");
  81. System.out.println(Arrays.deepToString(tempGrid).replace("], ", "]\n"));
  82. columnOne=new Node(tempGrid,coordinate,playerOneVictory,playerTwoVictory,0,"0,"+columnHeight(tempGrid,0),turn);
  83. System.out.println("HELLO");
  84. }}
  85.  
  86. search=search-1;}
  87.  
  88.  
  89.  
  90. public int columnHeight(int[][]grid,int column) {
  91. int height = 5;
  92. while (grid[column][height] != 0) {
  93. if(height!=0) {
  94. height = height - 1;
  95. }
  96. }
  97. return height;
  98. }
  99.  
  100.  
  101.  
  102.  
  103.  
  104. public void checkFour(int[][]currentState){
  105. connectFourHorizontal(currentState);
  106. connectFourVertical(currentState);
  107. connectFourDiagonal(currentState);
  108. }
  109. public void connectFourHorizontal(int[][]currentState) {
  110. int[][]currentGrid=currentState;
  111. //If neither player has won
  112. if (playerOneVictory == false && playerTwoVictory == false) {
  113.  
  114. //Double nested for loops to check the grid for four in a row horizontally.
  115. for (int j = 0; j < model.getYLength(); j++) {
  116.  
  117. //Count for the amount of discs in a row for either player.
  118. sumPlayerOne = 0;
  119. sumPlayerTwo = 0;
  120. for (int i = 0; i < model.getXLength(); i++) {
  121.  
  122. //If (i,j) is equal to 1 meaning a disc of player one is there.
  123. if (currentGrid[i][j] == 1) {
  124.  
  125. //sumPlayerOne is incremented by 1.
  126. sumPlayerOne = sumPlayerOne + 1;
  127. sumPlayerTwo = 0;
  128. }
  129.  
  130. //If (i,j) is equal to 2 meaning a disc of player two is there.
  131. if (currentGrid[i][j] == 2) {
  132.  
  133. //sumPlayerOne is incremented by 1.
  134. sumPlayerTwo = sumPlayerTwo + 1;
  135. sumPlayerOne = 0;
  136. }
  137.  
  138. //if (i,j) is empty.
  139. if (currentGrid[i][j] == 0){
  140.  
  141. //sumPlayerTwo and sumPlayerOne is set to equal 0.
  142. sumPlayerOne = 0;
  143. sumPlayerTwo = 0;
  144. }
  145.  
  146. //If sumPlayerOne is equal to 4 meaning there are four discs in a row.
  147. if (sumPlayerOne == 4) {
  148.  
  149. //Player one wins
  150. playerOneVictory = true;
  151. }
  152.  
  153. //If sumPlayerTwo is equal to 4 meaning there are four discs in a row.
  154. if (sumPlayerTwo == 4) {
  155.  
  156. //Player two wins
  157. playerTwoVictory = true;
  158. }
  159. }
  160. }
  161. }
  162. }
  163.  
  164. //Method used to check if four discs are in a row vertically.
  165. public void connectFourVertical(int[][]currentState) {
  166. int[][]currentGrid=currentState;
  167. //If neither player has won
  168. if (playerOneVictory == false && playerTwoVictory == false) {
  169.  
  170. //Double nested for loops to check the grid for four in a row vertically.
  171. for (int i = 0; i < model.getXLength(); i++) {
  172.  
  173. //Count for the amount of discs in a row for either player.
  174. sumPlayerOne = 0;
  175. sumPlayerTwo = 0;
  176.  
  177. for (int j = 0; j < model.getYLength(); j++) {
  178. //If (i,j) is equal to 1 meaning a disc of player one is there.
  179. if (currentGrid[i][j] == 1) {
  180.  
  181. //sumPlayerOne is incremented by 1.
  182. sumPlayerOne = sumPlayerOne + 1;
  183.  
  184. //Sum of playerTwo discs reset
  185. sumPlayerTwo = 0;
  186. }
  187.  
  188. //if (i,j) is either empty or contains a disc belonging to player two.
  189. if (currentGrid[i][j] == 0){
  190.  
  191. //sumPlayerOne and sumPlayerTwo is set to equal 0.
  192. sumPlayerOne = 0;
  193. sumPlayerTwo = 0;
  194. }
  195.  
  196. //If (i,j) is equal to 2 meaning a disc of player two is there.
  197. if (currentGrid[i][j] == 2) {
  198.  
  199. //sumPlayerOne is incremented by 1.
  200. sumPlayerTwo = sumPlayerTwo + 1;
  201.  
  202. //Sum of playerOne discs reset
  203. sumPlayerOne = 0;
  204. }
  205.  
  206. //If sumPlayerOne is equal to 4 meaning there are four discs in a row.
  207. if (sumPlayerOne == 4) {
  208.  
  209. //Player one wins
  210. playerOneVictory = true;
  211. }
  212.  
  213. //If sumPlayerTwo is equal to 4 meaning there are four discs in a row.
  214. if (sumPlayerTwo == 4) {
  215.  
  216. //Player two wins
  217. playerTwoVictory = true;
  218. }
  219. }
  220. }
  221. }
  222. }
  223.  
  224. //Method used to check if four discs are in a row diagonally
  225. public void connectFourDiagonal(int[][]currentState) {
  226.  
  227. int[][]currentGrid=currentState;
  228. //If neither player has won
  229. if (playerOneVictory == false && playerTwoVictory == false) {
  230.  
  231. //Double nested for loop to check for four in a row diagonally to the right.
  232. for (int i = 0; i < model.getXLength() - 3; i++) {
  233.  
  234. for (int j = 3; j < model.getYLength(); j++) {
  235.  
  236. //If four discs are in a row diagonally to the right belonging to player one.
  237. if (currentGrid[i][j] == 1 && currentGrid[i + 1][j - 1] == 1 && currentGrid[i + 2][j - 2] == 1 && currentGrid[i + 3][j - 3] == 1) {
  238.  
  239. //Player one wins.
  240. playerOneVictory = true;
  241. }
  242.  
  243. //If four discs are in a row diagonally to the right belonging to player two.
  244. if (currentGrid[i][j] == 2 && currentGrid[i + 1][j - 1] == 2 && currentGrid[i + 2][j - 2] == 2 && currentGrid[i + 3][j - 3] == 2) {
  245.  
  246. //Player two wins.
  247. playerTwoVictory = true;
  248. }
  249. }
  250. }
  251.  
  252. //Double nested for loop to check for four in a row diagonally to the left.
  253. for (int x = 0; x < model.getXLength() - 3; x++) {
  254. for (int y = 0; y < model.getYLength() - 3; y++) {
  255.  
  256. //If four discs are in a row diagonally to the left belonging to player one.
  257. if (currentGrid[x][y] == 1 && currentGrid[x + 1][y + 1] == 1 && currentGrid[x + 2][y + 2] == 1 && currentGrid[x + 3][y + 3] == 1) {
  258.  
  259. //Player one wins.
  260. playerOneVictory = true;
  261. }
  262. //If four discs are in a row diagonally to the left belonging to player two.
  263. if (currentGrid[x][y] == 2 && currentGrid[x + 1][y + 1] == 2 && currentGrid[x + 2][y + 2] == 2 && currentGrid[x + 3][y + 3] == 2) {
  264.  
  265. //Player two wins.
  266. playerTwoVictory = true;
  267. }
  268. }
  269. }
  270. }
  271. }
  272.  
  273. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement