Advertisement
Guest User

Untitled

a guest
Apr 24th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.76 KB | None | 0 0
  1. import java.util.Scanner;
  2. public class board {
  3. String[][] board= new String[8][8];
  4. String[][] whiteLocation = new String[8][8];
  5. String[][] blackLocation = new String[8][8]; //Have first hold row, second holds column
  6. Scanner input = new Scanner(System.in);
  7. boolean valid;
  8. String lastColourLanded;
  9.  
  10. public board(){
  11. setup();
  12.  
  13. lastColourLanded = null;
  14. whiteLocation[7][0]="OR ";
  15. whiteLocation[7][1]="RE ";
  16. whiteLocation[7][2]="GR ";
  17. whiteLocation[7][3]="PI ";
  18. whiteLocation[7][4]="YE ";
  19. whiteLocation[7][5]="BL ";
  20. whiteLocation[7][6]="PU ";
  21. whiteLocation[7][7]="BR ";
  22.  
  23. blackLocation[0][0]="BR ";
  24. blackLocation[0][1]="PU ";
  25. blackLocation[0][2]="BL ";
  26. blackLocation[0][3]="YE ";
  27. blackLocation[0][4]="PI ";
  28. blackLocation[0][5]="GR ";
  29. blackLocation[0][6]="RE ";
  30. blackLocation[0][7]="OR ";
  31. }
  32.  
  33. public void BoardLayout(){
  34. board[0][0]="BROWN"; board[0][1]="PURPL"; board[0][2]= "BLUE "; board[0][3]="YELLO"; board[0][4]="PINK "; board[0][5]= "GREEN"; board[0][6]= "RED "; board[0][7]="ORANG";
  35. board[1][0]="GREEN"; board[1][1]="BROWN"; board[1][2]= "YELLO"; board[1][3]="RED "; board[1][4]="PURPL"; board[1][5]= "PINK "; board[1][6]= "ORANG"; board[1][7]="BLUE ";
  36. board[2][0]="RED "; board[2][1]="YELLO"; board[2][2]= "BROWN"; board[2][3]="GREEN"; board[2][4]="BLUE "; board[2][5]= "ORANG"; board[2][6]= "PINK "; board[2][7]="PURPL";
  37. board[3][0]="YELLO"; board[3][1]="BLUE "; board[3][2]= "PURPL"; board[3][3]="BROWN"; board[3][4]="ORANG"; board[3][5]= "RED "; board[3][6]= "GREEN"; board[3][7]="PINK ";
  38. board[4][0]="PINK "; board[4][1]="GREEN"; board[4][2]= "RED "; board[4][3]="ORANG"; board[4][4]="BROWN"; board[4][5]= "PURPL"; board[4][6]= "BLUE "; board[4][7]="YELLO";
  39. board[5][0]="PURPL"; board[5][1]="PINK "; board[5][2]= "ORANG"; board[5][3]="BLUE "; board[5][4]="GREEN"; board[5][5]= "BROWN"; board[5][6]= "YELLO"; board[5][7]="RED ";
  40. board[6][0]="BLUE "; board[6][1]="ORANG"; board[6][2]= "PINK "; board[6][3]="PURPL"; board[6][4]="RED "; board[6][5]= "YELLO"; board[6][6]= "BROWN"; board[6][7]="GREEN";
  41. board[7][0]="ORANG"; board[7][1]="RED "; board[7][2]= "GREEN"; board[7][3]="PINK "; board[7][4]="YELLO"; board[7][5]= "BLUE "; board[7][6]= "PURPL"; board[7][7]="BROWN";
  42.  
  43.  
  44.  
  45. }
  46.  
  47. public String[][] getBlackLoc(){return blackLocation;}
  48. public String[][] getWhiteLoc(){return whiteLocation;}
  49. public void setup(){
  50. BoardLayout();
  51.  
  52.  
  53.  
  54. }
  55.  
  56.  
  57. public void forceUndo(String[][] lastWhiteLocation,String[][] lastBlackLocation,String lastColour){
  58. // Forces the gameboard to update and undo the last moves alongside bypassing the verification
  59. undoBlack(lastBlackLocation);
  60. undoWhite(lastWhiteLocation);
  61. undoColourLandedOn(lastColour);
  62.  
  63.  
  64. }
  65.  
  66. public String[][] undoBlack(String[][] lastBlackLocation){
  67. for (int x=0;x<8;x++){
  68. for(int y=0;y<8;y++){
  69. blackLocation[x][y]=lastBlackLocation[x][y];
  70. }
  71. }
  72. return blackLocation;
  73. }
  74. public String[][] undoWhite(String[][] lastWhiteLocation){
  75. for (int x=0;x<8;x++){
  76. for(int y=0;y<8;y++){
  77. whiteLocation[x][y]=lastWhiteLocation[x][y];
  78. }
  79. }
  80. return whiteLocation;
  81.  
  82. }
  83.  
  84. public String undoColourLandedOn(String lastColour){
  85. lastColourLanded=lastColour;
  86. return lastColourLanded;
  87.  
  88. }
  89. public void prntboard(){
  90. System.out.println(); // prints out the board row by row, replaces board squares with a tower token if one is present
  91. for (int p= 0;p<8;p++)
  92.  
  93. System.out.print(" "+ p);
  94. System.out.println();
  95. for (int x=0 ;x<8;x++){
  96. System.out.println();
  97. System.out.println();
  98. System.out.print(x +" - |");
  99. for (int y=0;y<8;y++){
  100.  
  101.  
  102.  
  103.  
  104. if(whiteLocation[x][y]!=null){
  105. System.out.print("O-"+whiteLocation[x][y]+"|");}
  106.  
  107. else if(blackLocation[x][y]!=null){
  108. System.out.print("X-"+blackLocation[x][y] +"|");}
  109. else{
  110. System.out.print(board[x][y]+" |");
  111. }
  112.  
  113. }
  114.  
  115. }
  116. }
  117.  
  118. public String[][] getControlInput(String row1, String row2, String column1, String column2,int contPlayer){
  119. int startRow = Integer.parseInt(row1); // converts the row and column strings given to the class into ints to be used as array indices
  120. int startCol = Integer.parseInt(column1);
  121.  
  122. int destRow= Integer.parseInt(row2);
  123. int destCol= Integer.parseInt(column2);
  124. //lastColour(startRow,startCol,destRow,destCol,contPlayer);
  125. update(startRow,startCol,destRow,destCol,contPlayer,whiteLocation,blackLocation); //calls update to update the gameboard
  126.  
  127. if (contPlayer == 1){return whiteLocation; }
  128. if (contPlayer ==2){return blackLocation;}
  129. return whiteLocation;
  130. }
  131.  
  132.  
  133.  
  134. public String returnLastColour(){
  135.  
  136. return lastColourLanded;
  137. }
  138.  
  139. /* public String lastColour(int startRow,int startCol,int destRow,int destCol,int contPlayer){
  140. if(lastColourLanded==null && checkValid(startRow, startCol,destRow,destCol,contPlayer) ){
  141.  
  142. return lastColourLanded;
  143. }/*else{
  144. if(checkValid(startRow, startCol,destRow,destCol,contPlayer)){lastColourLanded=board[destRow][destCol]; }
  145. }
  146. return lastColourLanded;
  147. }
  148. */
  149.  
  150.  
  151. public String updateLastColour(int destRow,int destCol){
  152.  
  153. lastColourLanded=board[destRow][destCol];
  154. return lastColourLanded;
  155. }
  156.  
  157.  
  158. public String[][] update(int startRow,int startCol,int destRow,int destCol,int contPlayer,String[][] whiteLocation,String[][] blackLocation){
  159.  
  160. if(contPlayer==1){ //if its the white player's move
  161.  
  162. if(whiteLocation[startRow][startCol]==null){
  163. System.out.println("You have selected the wrong token or not a valid location you are white player (o)");
  164. valid=false;}
  165.  
  166. if(whiteLocation[startRow][startCol]!=null){
  167. checkValid(startRow, startCol,destRow,destCol,contPlayer); // checks if move is valid
  168.  
  169. }
  170.  
  171.  
  172. if (!valid){
  173. System.out.println();
  174. prntboard();
  175. System.out.println();
  176. System.out.println("Invalid Move");
  177. System.out.println("Remember you must move the " + lastColourLanded +"Coloured token." );
  178. System.out.println("Select token to move to perform a valid move, (Row then column, with no space) or e to exit: ");
  179.  
  180. String selected = input.next();
  181. if (selected.equals("e")){
  182. System.exit(0);
  183. }else{
  184. String[] parts = {selected.substring(0,1),selected.substring(1) };
  185. String row1 = parts[0];
  186. String column1 = parts[1];
  187.  
  188. System.out.println("Select space to move to or e to exit: ");
  189. String dest = input.next();
  190. if (dest.equals("e")){
  191. System.exit(0);
  192. }
  193.  
  194. String[] destParts= {dest.substring(0,1),dest.substring(1) };
  195. String row2 = destParts[0];
  196. String column2 = destParts[1];
  197.  
  198.  
  199. getControlInput(row1,row2,column1,column2,contPlayer);
  200. System.out.println();
  201. contPlayer=2;
  202.  
  203.  
  204. }
  205. }
  206.  
  207. if (valid){
  208.  
  209.  
  210. String temp = whiteLocation[startRow][startCol];
  211. whiteLocation[destRow][destCol]= temp;
  212. whiteLocation[startRow][startCol]=null;
  213. System.out.println();
  214.  
  215. updateLastColour(destRow,destCol);
  216.  
  217. if(destRow==0){System.out.println("GAME OVER WHITE WINS!");System.exit(0);}
  218. prntboard();
  219. }
  220.  
  221. return whiteLocation;
  222.  
  223. }
  224.  
  225.  
  226.  
  227.  
  228.  
  229.  
  230.  
  231.  
  232. if(contPlayer==2){
  233. checkValid(startRow, startCol,destRow,destCol,contPlayer);
  234.  
  235.  
  236. if(blackLocation[startRow][startCol]==null){
  237. System.out.println("You have selected the wrong token or not a valid location you are black player (x)");
  238.  
  239. valid=false;}
  240.  
  241.  
  242.  
  243. if (!valid){
  244.  
  245. System.out.println();
  246.  
  247. prntboard();
  248. System.out.println();
  249. System.out.println("Invalid Move");
  250. System.out.println("Select token to move to make a valid move, (Row then column, with no space) or e to exit: ");
  251.  
  252. String selected = input.next();
  253. if (selected.equals("e")){
  254. System.exit(0);
  255. }else{
  256. String[] parts = {selected.substring(0,1),selected.substring(1) };
  257. String row1 = parts[0];
  258. String column1 = parts[1];
  259.  
  260. System.out.println("Select space to move to or e to exit: ");
  261. String dest = input.next();
  262. if (dest.equals("e")){
  263. System.exit(0);
  264. }
  265.  
  266. String[] destParts= {dest.substring(0,1),dest.substring(1) };
  267. String row2 = destParts[0];
  268. String column2 = destParts[1];
  269.  
  270.  
  271. getControlInput(row1,row2,column1,column2,contPlayer);
  272.  
  273. }}
  274. if (valid){
  275. //System.out.println(valid);
  276. String temp = blackLocation[startRow][startCol];
  277. blackLocation[destRow][destCol]= temp;
  278.  
  279. blackLocation[startRow][startCol]=null;
  280. updateLastColour(destRow,destCol);
  281. if(destRow==7){System.out.println("GAME OVER BLACK WINS!");System.exit(0);}
  282.  
  283. prntboard();
  284.  
  285.  
  286.  
  287. return blackLocation;
  288.  
  289.  
  290. }
  291.  
  292. }
  293.  
  294.  
  295.  
  296.  
  297. return whiteLocation;
  298.  
  299.  
  300. }
  301.  
  302.  
  303. public boolean checkValid(int startRow,int startCol,int destRow,int destCol,int controller){
  304. valid=true;
  305.  
  306.  
  307. /*
  308. if(controller ==1){
  309. if(!lastColourLanded.contains(whiteLocation[startRow][startCol])){
  310. valid=false;
  311. return valid;
  312. }
  313. if(lastColourLanded.contains(whiteLocation[startRow][startCol])){
  314. lastColourLanded=board[destRow][destCol];
  315. valid=true;
  316. return valid;
  317. }
  318. }
  319. if(controller ==2){
  320. if(!lastColourLanded.contains(blackLocation[startRow][startCol])){
  321. valid=false;
  322. return valid;
  323. }
  324. if(lastColourLanded.contains(blackLocation[startRow][startCol])){
  325. lastColourLanded=board[destRow][destCol];
  326. valid=true;
  327. return valid;
  328. }
  329.  
  330. }
  331. */
  332.  
  333.  
  334. if(controller == 1){ //Checks if white player is moving
  335. String tokenColour=whiteLocation[startRow][startCol].substring(0,2);
  336. if(lastColourLanded==null){valid=true;}
  337. else{
  338. if(!lastColourLanded.contains(tokenColour)&& lastColourLanded!=null){
  339. valid=false;
  340. return valid;
  341. }
  342. }
  343.  
  344. if(startRow<destRow){
  345. valid=false;
  346. return valid;}
  347.  
  348. int temp = Math.abs(destRow-startRow);
  349. int temp2 = Math.abs(destCol-startCol); //checks its not moving backwards and only moves forward or diagonally
  350. /*int lowestVal;
  351. int highestVal;
  352. if(startCol>=destCol){lowestVal=destCol; highestVal=startCol;}
  353. else{ lowestVal=startCol; highestVal = destCol;}
  354. */
  355.  
  356. if ( startCol!=destCol && temp != temp2){
  357. valid=false;
  358. return valid;}
  359. if(startCol>destCol){ //checks diagonally left
  360.  
  361. for (int x=startRow-1;x<=destRow;x--){
  362. int y=startCol-1;
  363. if(whiteLocation[x][y]!=null || blackLocation[x][y]!=null){
  364.  
  365. valid=false;
  366. return valid;}
  367. y--;
  368.  
  369.  
  370. }
  371. }
  372. if(startCol<destCol){
  373.  
  374. for (int x=startRow-1;x<=destRow;x--){
  375. for (int y=startCol+1;y<=destCol;y++){
  376. if(whiteLocation[x][y]!=null || blackLocation[x][y]!=null){
  377.  
  378. valid=false;
  379. return valid;}
  380.  
  381. }
  382.  
  383. }
  384. }
  385. // the above checks that there is no towers in the path.
  386.  
  387. if(startCol==destCol){
  388.  
  389. for(int x=startRow-1;x<=destRow;x--){
  390. if(whiteLocation[x][startCol]!=null || blackLocation[x][startCol]!=null){
  391.  
  392. valid=false;
  393. return valid;}
  394. }
  395. }
  396.  
  397.  
  398. if(blackLocation[destRow][destCol]!=null || whiteLocation[destRow][destCol]!=null){
  399. valid=false;
  400. return valid;
  401. }
  402.  
  403.  
  404. if(startRow == destRow && startCol == destCol){
  405. valid=false;
  406. return valid;}
  407.  
  408. }
  409.  
  410.  
  411.  
  412. if(controller==2){
  413. String tokenColour="";
  414. if(blackLocation[startRow][startCol]!=null){
  415. tokenColour=blackLocation[startRow][startCol].substring(0,2);}
  416. if(lastColourLanded==null){valid=true;}
  417.  
  418. else{
  419. boolean contains =lastColourLanded.contains(tokenColour);
  420. if(!contains&& lastColourLanded!=null){
  421. valid=false;
  422. return valid;
  423. } }
  424.  
  425. if(startRow>destRow){valid=false; return valid;}
  426.  
  427. int temp = Math.abs(destRow-startRow);
  428. int temp2 = Math.abs(destCol-startCol);
  429. /* int lowestVal;
  430. int highestVal;
  431.  
  432. if(startCol>destCol){lowestVal=destCol; highestVal=startCol;}
  433. else{ lowestVal=startCol; highestVal = destCol;}
  434.  
  435.  
  436. for (int x=startRow+1;x<=destRow;x++){
  437.  
  438. for (int y=lowestVal;y<=highestVal;y++){
  439. if(whiteLocation[x][y]!=null && blackLocation[x][y]!=null){
  440. valid=false;
  441. return valid;}
  442.  
  443. }
  444.  
  445. } */
  446.  
  447. if(startCol!=destCol && temp != temp2){valid=false; return valid;}
  448.  
  449. if(startRow == destRow && startCol == destCol){
  450. valid=false;
  451. return valid;}
  452.  
  453. if(blackLocation[destRow][destCol]!=null || whiteLocation[destRow][destCol]!=null){
  454. valid=false;
  455. return valid;
  456. }
  457.  
  458. if(startCol<destCol){ //checks diagonally right for things in path
  459.  
  460. for (int x=startRow+1;x<=destRow;x++){
  461. int y=startCol+1;
  462. if(whiteLocation[x][y]!=null || blackLocation[x][y]!=null){
  463.  
  464. valid=false;
  465. return valid;}
  466. y++;
  467.  
  468.  
  469.  
  470. }
  471. }
  472. if(startCol>destCol){ //checks diag left
  473.  
  474. for (int x=startRow+1;x<=destRow;x++){
  475. int y=startCol-1;
  476. if(whiteLocation[x][y]!=null || blackLocation[x][y]!=null){
  477.  
  478. valid=false;
  479. return valid;}
  480. y--;
  481.  
  482.  
  483. }
  484. }
  485.  
  486. if(startCol==destCol){
  487.  
  488. for(int x=startRow+1;x<=destRow;x++){
  489. if(whiteLocation[x][startCol]!=null || blackLocation[x][startCol]!=null){
  490.  
  491. valid=false;
  492. return valid;}
  493. }
  494. }
  495.  
  496.  
  497. }
  498.  
  499.  
  500.  
  501. updateLastColour(destRow,destCol);
  502. valid =true;
  503. return valid;
  504. }
  505.  
  506.  
  507. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement