Advertisement
Guest User

Singleplayer

a guest
Apr 23rd, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.44 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class singleplayer {
  4. boolean gameOver;
  5.  
  6. int control;
  7. int blackPoints;
  8. int whitePoints;
  9. Scanner input = new Scanner(System.in);
  10. board startBoard = new board();
  11.  
  12. int AIDifficulty;
  13.  
  14. public void begin(){
  15. blackPoints=0;
  16. whitePoints=0;
  17.  
  18. control=1;
  19.  
  20. gameOver=false;
  21.  
  22. startBoard.prntboard();
  23.  
  24. getAiDiff();
  25. control();
  26. }
  27.  
  28. public int getAiDiff(){
  29. System.out.println();
  30. System.out.println("What difficulty of AI would you like, 1 for easy, 2 for medium");
  31. int temp = input.nextInt();
  32. if (temp ==1){AIDifficulty=1; return AIDifficulty;}
  33. if (temp ==2){AIDifficulty=2; return AIDifficulty;}
  34.  
  35. return AIDifficulty;
  36. }
  37.  
  38.  
  39.  
  40. public void control(){
  41. while(!gameOver){
  42.  
  43. while(control ==1){
  44.  
  45. System.out.println();
  46. System.out.println();
  47. System.out.println("Current Score is: "+whitePoints+" for White and "+blackPoints+ " for Black!.");
  48.  
  49. System.out.println("You are white -o");
  50. System.out.println("Select token to move, (Row then column, with no space) or e to exit: ");
  51.  
  52. String selected = input.next();
  53. if (selected.equals("e")){
  54. System.exit(0);
  55. }else{
  56. String[] parts = {selected.substring(0,1),selected.substring(1) };
  57. String row1 = parts[0];
  58. String column1 = parts[1];
  59.  
  60. System.out.println("Select space to move to or e to exit: ");
  61. String dest = input.next();
  62.  
  63. if (dest.equals("e")){
  64. System.exit(0);
  65. }
  66.  
  67. String[] destParts= {dest.substring(0,1),dest.substring(1) };
  68. String row2 = destParts[0];
  69. String column2 = destParts[1];
  70.  
  71.  
  72. startBoard.getControlInput(row1,row2,column1,column2,control);
  73.  
  74. //if(row2.equals(0)){pointForwhite();}
  75. control=2;
  76. }
  77.  
  78. }
  79.  
  80. while(control==2){
  81. if (AIDifficulty == 1){EzAI();}
  82. if (AIDifficulty ==2){HrdAI();}
  83.  
  84. control =1;
  85.  
  86. }
  87.  
  88.  
  89. }
  90. } //End of Control method
  91. public void EzAI(){
  92. String[][] blackLoc=startBoard.blackLocation;
  93. int direction = (int) Math.random()*3 +1;
  94.  
  95. switch(direction){
  96.  
  97. case 1: { String startRow;
  98. String startCol;
  99.  
  100. int tempRow=0;
  101. int tempCol=0;
  102.  
  103.  
  104. for(int x=0;x<8;x++){
  105. for (int y=0;y<8;y++){
  106. if(blackLoc[x][y]==null){
  107. y++;}else{
  108. String tokenColour = blackLoc[x][y].substring(0, 2);
  109. if(startBoard.lastColourLanded.contains(tokenColour)){
  110. tempRow= x;
  111. tempCol=y;
  112.  
  113.  
  114. break;}
  115. }}}
  116. startRow =String.valueOf(tempRow);
  117. startCol=String.valueOf(tempCol);
  118. int distance = (int)Math.random() * 4 +1;
  119. int tempDestCol=tempCol -distance;
  120. int tempDestRow=tempRow+distance;
  121. String destRow = String.valueOf(tempDestRow);
  122.  
  123.  
  124. startBoard.checkValid(tempRow, tempCol, tempDestRow, tempDestCol, 2);
  125.  
  126.  
  127. while(!startBoard.valid){
  128. int distance2 = (int)Math.random() * 3 +1;
  129. int tempDestCol2=tempCol -distance2;
  130. int tempDestRow2=tempRow+distance2;
  131. String destRow2 = String.valueOf(tempDestRow2);
  132.  
  133. startBoard.checkValid(tempRow, tempCol, tempDestRow2, tempDestCol2, 2);
  134. }
  135.  
  136. if(startBoard.valid){
  137. startBoard.getControlInput(startRow, destRow, startCol, startCol, 2);
  138. } //diagonal Left Move
  139. break;
  140. }
  141. case 2:{ //StraightForward
  142. String startRow;
  143. String startCol;
  144.  
  145. int tempRow=0;
  146. int tempCol=0;
  147. //String lastColourLanded= board.LastColour();
  148. for(int x=0;x<8;x++){
  149. for (int y=0;y<8;y++){
  150. if(blackLoc[x][y]==null){y++;}else{
  151. if(startBoard.lastColourLanded.contains(blackLoc[x][y])){
  152. tempRow= x;
  153. tempCol=y;
  154.  
  155.  
  156. break;
  157. }}}}
  158.  
  159. startRow =String.valueOf(tempRow);
  160. startCol=String.valueOf(tempCol);
  161. int distance = (int)Math.random() * 4 +1;
  162. int tempDestRow=tempRow-distance;
  163. String destRow = String.valueOf(tempDestRow);
  164. startBoard.checkValid(tempRow, tempCol, tempDestRow, tempCol, 2);
  165.  
  166.  
  167. while(!startBoard.valid){
  168. int distance2 = (int)Math.random() * 4 +1;
  169. int tempDestRow2=tempRow-distance2;
  170. String destRow2 = String.valueOf(tempDestRow2);
  171.  
  172. startBoard.checkValid(tempRow, tempCol, tempDestRow2, tempCol, 2);
  173. }
  174.  
  175. if(startBoard.valid){
  176. startBoard.getControlInput(startRow, destRow, startCol, startCol, 2);
  177. } break;
  178.  
  179. }
  180. case 3:{ //Diagonal Right
  181. String startRow;
  182. String startCol;
  183.  
  184. int tempRow=0;
  185. int tempCol=0;
  186.  
  187. for(int x=0;x<8;x++){
  188. for (int y=0;y<8;y++){
  189. if(blackLoc[x][y]==null){y++;}else{
  190. if(startBoard.lastColourLanded.contains(blackLoc[x][y])){
  191. tempRow= x;
  192. tempCol=y;
  193.  
  194. break;
  195. }
  196. }}}
  197. startRow =String.valueOf(tempRow);
  198. startCol=String.valueOf(tempCol);
  199. int distance = (int)Math.random() * 3 +1;
  200. int tempDestCol=tempCol +distance;
  201. int tempDestRow=tempRow-distance;
  202. String destRow = String.valueOf(tempDestRow);
  203.  
  204.  
  205. startBoard.checkValid(tempRow, tempCol, tempDestRow, tempDestCol, 2);
  206.  
  207.  
  208. while(!startBoard.valid){
  209. int distance2 = (int)Math.random() * 4 +1;
  210. int tempDestRow2=tempRow-distance2;
  211. String destRow2 = String.valueOf(tempDestRow2);
  212.  
  213. startBoard.checkValid(tempRow, tempCol, tempDestRow2, tempCol, 2);
  214. }
  215.  
  216. if(startBoard.valid){
  217. startBoard.getControlInput(startRow, destRow, startCol, startCol, 2);
  218. }
  219. break;
  220.  
  221. }
  222.  
  223. }
  224.  
  225. }
  226.  
  227. public void HrdAI(){
  228.  
  229.  
  230. }
  231.  
  232.  
  233. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement