Advertisement
Guest User

Untitled

a guest
Nov 15th, 2018
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.32 KB | None | 0 0
  1. import java.util.*;
  2. public class assignment
  3. {
  4. private static int [][] map = new int[6][6];
  5. public static void main(String[]args){
  6. //display maping
  7. initMap();
  8. showMap();
  9. //both player move(including filp & error checking)
  10. playerMove();
  11. //counting the Chess of both side
  12. countChess();
  13. //tell winner
  14. gameFinish(int count[]);
  15. }
  16.  
  17. private static void initMap() {
  18. // inital map
  19. // loop for y
  20. for(int i = 0; i < 6; i++) {
  21. // loop for x
  22. for (int j = 0; j < 6; j++) {
  23. // init 1 to map;
  24. if ((i == 2 && j == 2) || (i == 3 && j == 3)){
  25. map[i][j] = 1;
  26. }
  27. // init 2 to map
  28. else if ((i == 3 && j == 2) || (i == 2 && j == 3)){
  29. map[i][j] = 2;
  30. }
  31. // init blank space to map
  32. else {
  33. map[i][j] = 0;
  34. }
  35.  
  36. }
  37. }
  38. }
  39.  
  40. public static void showMap() {
  41. System.out.println(" 0 1 2 3 4 5");
  42. System.out.println(" -----------");
  43.  
  44. for(int i = 0; i < 6; i++){
  45. System.out.print(i + " | ");
  46.  
  47. for(int j = 0; j < 6; j++){
  48. System.out.print(map[i][j] + " ");
  49. }
  50. System.out.println();
  51. }
  52. }
  53.  
  54. public static void playerMove(){
  55. Scanner input = new Scanner(System.in);
  56. int x;
  57. int y;
  58. boolean error = true;
  59. for(int i = 1 , j = 0;j < 32; j++){
  60. System.out.print("please enter your position '" + i + "':");
  61. y = input.nextInt();
  62. x = input.nextInt();
  63. error = errorChecking(y , x);
  64. if (error == false){
  65. continue;
  66. }
  67. map[y][x] = i;
  68. filpChess(y , x , i);
  69. showMap();
  70. i = i == 1 ? ++i : --i;
  71. }
  72. }
  73.  
  74. public static void filpChess(int y ,int x, int player){
  75. int start_point1 = y;
  76. int end_point1 = y;
  77. int start_point2 = x;
  78. int end_point2 = x;
  79.  
  80. //vertical up
  81. for (int col = y - 1; col >= 0; col--){
  82. if (map[col][x] == 0){
  83. // if the grid is empty.
  84. break;
  85. }else if (map[col][x] == player){
  86. start_point1 = col;
  87. break;
  88. }
  89. }
  90.  
  91. //vertical down
  92. for (int col = y + 1; col < 6; col++){
  93. if (map[col][x] == 0){
  94. // if the grid is empty0
  95. break;
  96. }else if (map[col][x] == player){
  97. // if found same color chess.
  98. end_point1 = col;
  99. break;
  100. }
  101. }
  102.  
  103. //vertical flip
  104. for (int col = start_point1; col <= end_point1; col++){
  105. map[col][x] = player;
  106. }
  107.  
  108. start_point1 = end_point1 = x;
  109.  
  110. //horizontal left
  111. for (int row = x - 1;row >= 0; row--){
  112. if (map[y][row] == 0){
  113. // if the grid is empty.
  114. break;
  115. }else if (map[y][row] == player){
  116. start_point1 = row;
  117. break;
  118. }
  119. }
  120.  
  121. //horizontal right
  122. for (int row = x + 1; row < 6; row++){
  123. if (map[y][row] == 0){
  124. // if the grid is empty
  125. break;
  126. }else if (map[y][row] == player){
  127. // if found same color chess.
  128. end_point1 = row;
  129. break;
  130. }
  131. }
  132.  
  133. //horizontal flip
  134. for (int row = start_point1; row <= end_point1; row++){
  135. map[y][row] = player;
  136. }
  137.  
  138. start_point1 = end_point1 = x;
  139. start_point2 = end_point2 = y;
  140.  
  141. //slash x-- & y--
  142. for (int col = y - 1 , row = x - 1;col >= 0 && row >= 0; col-- , row--){
  143. if (map[col][row] == 0){
  144. // if the grid is empty.
  145. break;
  146. }else if (map[col][row] == player){
  147. start_point1 = row;
  148. start_point2 = col;
  149. break;
  150. }
  151. }
  152.  
  153. //slash x++ & y++
  154. for (int col = y + 1 , row = x + 1; col < 6 && row < 6; col++ , row++){
  155. if (map[col][row] == 0){
  156. // if the grid is empty
  157. break;
  158. }else if (map[col][row] == player){
  159. // if found same color chess.
  160. end_point1 = col;
  161. end_point2 = row;
  162. break;
  163. }
  164. }
  165.  
  166. for (int col = start_point2 , row = start_point1;col <= end_point2 &&row <= end_point1;col++ , row++){
  167. map[col][row] = player;
  168. }
  169.  
  170. start_point1 = end_point1 = y;
  171. start_point2 = end_point2 = x;
  172.  
  173. //slash x-- & y++
  174. for (int col = y + 1, row = x - 1; col < 6 && row >= 0; col++, row--){
  175. if (map[col][row] == 0){
  176. // if the grid is empty.
  177. break;
  178. }else if (map[col][row] == player){
  179. start_point1 = col;
  180. start_point2 = row;
  181. break;
  182. }
  183. }
  184.  
  185. //slash x++ & y--
  186. for (int col = y - 1 , row = x + 1; col >= 0 && row < 6; col--, row++){
  187. if (map[col][row] == 0){
  188. // if the grid is empty
  189. break;
  190. }else if (map[col][row] == player){
  191. // if found same color chess.
  192. end_point1 = col;
  193. end_point2 = row;
  194. break;
  195. }
  196. }
  197.  
  198. for (int col = start_point1, row = start_point2; col >= end_point1 && row <= end_point2; col--, row++){
  199. map[col][row] = player;
  200. }
  201. }
  202.  
  203. public static boolean errorChecking(int y ,int x){
  204. boolean check = true;
  205. if (x < 0 || x > 5 || y < 0 || y > 5){
  206. System.out.println("Error - input numbers should be 0 to 5!");
  207. check = false;
  208. }else if (map[y][x] > 0){
  209. System.out.println("Error - input cell is not empty.");
  210. check = false;
  211. }
  212. return check;
  213. }
  214.  
  215. public static int[] countChess(){
  216. int blackChess = 0;
  217. int whiteChess = 0;
  218. int count[] = new int [2];
  219. for (int col = 0; col < map.length; col++){
  220. for (int row = 0; row < map[0].length; row++){
  221. if (map[col][row] == 1){
  222. blackChess++;
  223. count[0] = blackChess;
  224. }
  225. if (map[col][row] == 2){
  226. whiteChess++;
  227. count[1] = whiteChess;
  228. }
  229. }
  230. }
  231. return count;
  232. }
  233.  
  234. public static int gameFinish(int count[]){
  235. System.out.println("Game Finish");
  236. System.out.println("'1' - " + count[0]);
  237. System.out.println("'2' - " + count[1]);
  238. if (count[0] > count[1]){
  239. System.out.println("Black wins");
  240. }
  241. else if(count[0] < count[1]){
  242. System.out.println("white wins");
  243. }
  244. else
  245. System.out.println("That is a draw");
  246. }
  247.  
  248.  
  249. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement