Advertisement
Guest User

board

a guest
Apr 23rd, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.34 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 part 1 hold location, ie 1,1 (Store as 11, part 2 holds colour of token
  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 prntboard(){
  58. System.out.println(); // prints out the board row by row, replaces board squares with a tower token if one is present
  59. for (int p= 0;p<8;p++)
  60.  
  61. System.out.print(" "+ p);
  62. System.out.println();
  63. for (int x=0 ;x<8;x++){
  64. System.out.println();
  65. System.out.println();
  66. System.out.print(x +" - ");
  67. for (int y=0;y<8;y++){
  68.  
  69.  
  70.  
  71.  
  72. if(whiteLocation[x][y]!=null){
  73. System.out.print("o-"+whiteLocation[x][y]);}
  74.  
  75. else if(blackLocation[x][y]!=null){
  76. System.out.print("x-"+blackLocation[x][y]);}
  77. else{
  78. System.out.print(board[x][y]+" ");
  79. }
  80.  
  81. }
  82.  
  83. }
  84. }
  85.  
  86. public String[][] getControlInput(String row1, String row2, String column1, String column2,int contPlayer){
  87. int startRow = Integer.parseInt(row1); // converts the row and column strings given to the class into ints to be used as array indices
  88. int startCol = Integer.parseInt(column1);
  89.  
  90. int destRow= Integer.parseInt(row2);
  91. int destCol= Integer.parseInt(column2);
  92. lastColour(startRow,startCol,destRow,destCol,contPlayer);
  93. update(startRow,startCol,destRow,destCol,contPlayer,whiteLocation,blackLocation); //calls update to update the gameboard
  94.  
  95. if (contPlayer == 1){return whiteLocation; }
  96. if (contPlayer ==2){return blackLocation;}
  97. return whiteLocation;
  98. }
  99.  
  100.  
  101.  
  102. public String returnLastColour(){
  103.  
  104. return lastColourLanded;
  105. }
  106.  
  107. public String lastColour(int startRow,int startCol,int destRow,int destCol,int contPlayer){
  108. if(lastColourLanded==null && checkValid(startRow, startCol,destRow,destCol,contPlayer) ){
  109. lastColourLanded=board[destRow][destCol];
  110. return lastColourLanded;
  111. }/*else{
  112. if(checkValid(startRow, startCol,destRow,destCol,contPlayer)){lastColourLanded=board[destRow][destCol]; }
  113. }*/
  114. return lastColourLanded;
  115. }
  116.  
  117.  
  118.  
  119. public String updateLastColour(int destRow,int destCol){
  120.  
  121. lastColourLanded=board[destRow][destCol];
  122. return lastColourLanded;
  123. }
  124.  
  125.  
  126. public String[][] update(int startRow,int startCol,int destRow,int destCol,int contPlayer,String[][] whiteLocation,String[][] blackLocation){
  127.  
  128. if(contPlayer==1){ //if its the white player's move
  129. checkValid(startRow, startCol,destRow,destCol,contPlayer); // checks if move is valid
  130. if(whiteLocation[startRow][startCol]==null){
  131. System.out.println("You have selected the wrong token or not a valid location you are white player (o)");}
  132.  
  133.  
  134.  
  135. if (!valid){
  136. System.out.println();
  137. prntboard();
  138. System.out.println();
  139. System.out.println("Invalid Move");
  140. System.out.println("Remember you must move the " + lastColourLanded +"Coloured token." );
  141. System.out.println("Select token to move to perform a valid move, (Row then column, with no space) or e to exit: ");
  142.  
  143. String selected = input.next();
  144. if (selected.equals("e")){
  145. System.exit(0);
  146. }else{
  147. String[] parts = {selected.substring(0,1),selected.substring(1) };
  148. String row1 = parts[0];
  149. String column1 = parts[1];
  150.  
  151. System.out.println("Select space to move to or e to exit: ");
  152. String dest = input.next();
  153. if (dest.equals("e")){
  154. System.exit(0);
  155. }
  156.  
  157. String[] destParts= {dest.substring(0,1),dest.substring(1) };
  158. String row2 = destParts[0];
  159. String column2 = destParts[1];
  160.  
  161.  
  162. getControlInput(row1,row2,column1,column2,contPlayer);
  163. System.out.println();
  164. contPlayer=2;
  165.  
  166.  
  167. }
  168. }
  169.  
  170. if (valid){
  171.  
  172.  
  173. String temp = whiteLocation[startRow][startCol];
  174. whiteLocation[destRow][destCol]= temp;
  175. whiteLocation[startRow][startCol]=null;
  176. System.out.println();
  177.  
  178. updateLastColour(destRow,destCol);
  179. prntboard();
  180. }
  181.  
  182. return whiteLocation;
  183.  
  184. }
  185.  
  186.  
  187.  
  188.  
  189.  
  190.  
  191.  
  192.  
  193. if(contPlayer==2){
  194. checkValid(startRow, startCol,destRow,destCol,contPlayer);
  195. if(blackLocation[startRow][startCol]==null){
  196. System.out.println("You have selected the wrong token or not a valid location you are black player (x)");}
  197.  
  198.  
  199.  
  200.  
  201. if (!valid){
  202.  
  203. System.out.println();
  204.  
  205. prntboard();
  206. System.out.println();
  207. System.out.println("Invalid Move");
  208. System.out.println("Select token to move to make a valid move, (Row then column, with no space) or e to exit: ");
  209.  
  210. String selected = input.next();
  211. if (selected.equals("e")){
  212. System.exit(0);
  213. }else{
  214. String[] parts = {selected.substring(0,1),selected.substring(1) };
  215. String row1 = parts[0];
  216. String column1 = parts[1];
  217.  
  218. System.out.println("Select space to move to or e to exit: ");
  219. String dest = input.next();
  220. if (dest.equals("e")){
  221. System.exit(0);
  222. }
  223.  
  224. String[] destParts= {dest.substring(0,1),dest.substring(1) };
  225. String row2 = destParts[0];
  226. String column2 = destParts[1];
  227.  
  228.  
  229. getControlInput(row1,row2,column1,column2,contPlayer);
  230.  
  231. }}
  232. if (valid){
  233. //System.out.println(valid);
  234. String temp = blackLocation[startRow][startCol];
  235. blackLocation[destRow][destCol]= temp;
  236. blackLocation[startRow][startCol]=null;
  237. updateLastColour(destRow,destCol);
  238. prntboard();
  239.  
  240.  
  241.  
  242. return blackLocation;
  243.  
  244.  
  245. }
  246.  
  247. }
  248.  
  249.  
  250.  
  251.  
  252. return whiteLocation;
  253.  
  254.  
  255. }
  256.  
  257.  
  258. public boolean checkValid(int startRow,int startCol,int destRow,int destCol,int controller){
  259. valid=true;
  260.  
  261.  
  262. /*
  263. if(controller ==1){
  264. if(!lastColourLanded.contains(whiteLocation[startRow][startCol])){
  265. valid=false;
  266. return valid;
  267. }
  268. if(lastColourLanded.contains(whiteLocation[startRow][startCol])){
  269. lastColourLanded=board[destRow][destCol];
  270. valid=true;
  271. return valid;
  272. }
  273. }
  274. if(controller ==2){
  275. if(!lastColourLanded.contains(blackLocation[startRow][startCol])){
  276. valid=false;
  277. return valid;
  278. }
  279. if(lastColourLanded.contains(blackLocation[startRow][startCol])){
  280. lastColourLanded=board[destRow][destCol];
  281. valid=true;
  282. return valid;
  283. }
  284.  
  285. }
  286. */
  287.  
  288.  
  289. if(controller == 1){ //Checks if white player is moving
  290. String tokenColour=whiteLocation[startRow][startCol].substring(0,2);
  291. if(lastColourLanded==null){valid=true;}
  292. else{
  293. if(lastColourLanded.contains(tokenColour)){
  294. valid=false;
  295. return valid;
  296. }
  297. }
  298.  
  299. if(startRow<destRow){
  300. valid=false;
  301. return valid;}
  302.  
  303. int temp = Math.abs(destRow-startRow);
  304. int temp2 = Math.abs(destCol-startCol); //checks its not moving backwards and only moves forward or diagonally
  305. /*int lowestVal;
  306. int highestVal;
  307. if(startCol>=destCol){lowestVal=destCol; highestVal=startCol;}
  308. else{ lowestVal=startCol; highestVal = destCol;}
  309. */
  310.  
  311. if ( startCol!=destCol && temp != temp2){
  312. valid=false;
  313. return valid;}
  314. if(startCol>destCol){ //checks diagonally left
  315.  
  316. for (int x=startRow-1;x<=destRow;x--){
  317. int y=startCol-1;
  318. if(whiteLocation[x][y]!=null || blackLocation[x][y]!=null){
  319.  
  320. valid=false;
  321. return valid;}
  322. y--;
  323.  
  324.  
  325. }
  326. }
  327. if(startCol<destCol){
  328.  
  329. for (int x=startRow-1;x<=destRow;x--){
  330. for (int y=startCol+1;y<=destCol;y++){
  331. if(whiteLocation[x][y]!=null || blackLocation[x][y]!=null){
  332.  
  333. valid=false;
  334. return valid;}
  335.  
  336. }
  337.  
  338. }
  339. }
  340. // the above checks that there is no towers in the path.
  341.  
  342.  
  343. if(blackLocation[destRow][destCol]!=null || whiteLocation[destRow][destCol]!=null){
  344. valid=false;
  345. return valid;
  346. }
  347.  
  348.  
  349. if(startRow == destRow && startCol == destCol){
  350. valid=false;
  351. return valid;}
  352.  
  353. }
  354.  
  355.  
  356.  
  357. if(controller==2){
  358. String tokenColour=blackLocation[startRow][startCol].substring(0, 2);
  359. if(lastColourLanded==null){valid=true;}
  360.  
  361. else{
  362. boolean contains =lastColourLanded.contains(tokenColour);
  363. if(!contains&& lastColourLanded!=null){
  364. valid=false;
  365. return valid;
  366. } }
  367.  
  368. if(startRow>destRow){valid=false; return valid;}
  369.  
  370. int temp = Math.abs(destRow-startRow);
  371. int temp2 = Math.abs(destCol-startCol);
  372. /* int lowestVal;
  373. int highestVal;
  374.  
  375. if(startCol>destCol){lowestVal=destCol; highestVal=startCol;}
  376. else{ lowestVal=startCol; highestVal = destCol;}
  377.  
  378.  
  379. for (int x=startRow+1;x<=destRow;x++){
  380.  
  381. for (int y=lowestVal;y<=highestVal;y++){
  382. if(whiteLocation[x][y]!=null && blackLocation[x][y]!=null){
  383. valid=false;
  384. return valid;}
  385.  
  386. }
  387.  
  388. } */
  389.  
  390. if(startCol!=destCol && temp != temp2){valid=false; return valid;}
  391.  
  392. if(startRow == destRow && startRow == destRow){
  393. valid=false;
  394. return valid;}
  395.  
  396. if(blackLocation[destRow][destCol]!=null || whiteLocation[destRow][destCol]!=null){
  397. valid=false;
  398. return valid;
  399. }
  400.  
  401. if(startCol<destCol){ //checks diagonally right for things in path
  402.  
  403. for (int x=startRow+1;x<=destRow;x++){
  404. int y=startCol+1;
  405. if(whiteLocation[x][y]!=null || blackLocation[x][y]!=null){
  406.  
  407. valid=false;
  408. return valid;}
  409. y++;
  410.  
  411.  
  412.  
  413. }
  414. }
  415. if(startCol>destCol){ //checks diag left
  416.  
  417. for (int x=startRow+1;x<=destRow;x++){
  418. int y=startCol-1;
  419. if(whiteLocation[x][y]!=null || blackLocation[x][y]!=null){
  420.  
  421. valid=false;
  422. return valid;}
  423. y--;
  424.  
  425.  
  426. }
  427. }
  428.  
  429. if(startCol==destCol){
  430.  
  431. for(int x=startRow+1;x<=destRow;x++){
  432. if(whiteLocation[x][startCol]!=null || blackLocation[x][startCol]!=null){
  433.  
  434. valid=false;
  435. return valid;}
  436. }
  437. }
  438.  
  439.  
  440. }
  441.  
  442.  
  443.  
  444.  
  445. valid =true;
  446. return valid;
  447. }
  448.  
  449.  
  450. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement