Advertisement
Guest User

Untitled

a guest
Feb 20th, 2018
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.74 KB | None | 0 0
  1.  
  2. Scanner netbean = new Scanner(System.in);
  3.  
  4. int board[][] = new int[8][8];
  5. int player=1;
  6. int x=0;
  7.  
  8. Functions.draw(board);
  9.  
  10. do {
  11.  
  12. //inputs
  13. player=1;
  14.  
  15. do {
  16. System.out.println("Player 1 enter a letter: ");
  17. x=(netbean.next().toUpperCase().charAt(0))-65;
  18.  
  19. } while (Functions.possible(board, x) != true);
  20.  
  21. Functions.drop(board, x, player);
  22. Functions.draw(board);
  23.  
  24. //game end statements
  25. if (Functions.checkdone(board, x)) {
  26. System.out.println("Tie");
  27. break;
  28. }
  29. if (Functions.checkwinZ(board, player)) {
  30. System.out.println("Player 1 wins");
  31. break;
  32. }
  33. if (Functions.checkwinS(board, player)) {
  34. System.out.println("Player 1 wins");
  35. break;
  36. }
  37. if (Functions.checkwinV(board, player)) {
  38. System.out.println("Player 1 wins");
  39. break;
  40. }
  41. if (Functions.checkwinUV(board, player)) {
  42. System.out.println("Player 1 wins");
  43. break;
  44. }
  45.  
  46. player=2;
  47.  
  48. //player2 inputs
  49. do {
  50. System.out.println("Player 2 enter a letter: ");
  51. x=(netbean.next().toUpperCase().charAt(0))-65;
  52.  
  53. } while (Functions.possible(board, x) != true);
  54.  
  55. Functions.drop(board, x, player);
  56. Functions.draw(board);
  57.  
  58. //game end statements
  59. if (Functions.checkdone(board, x)) {
  60. System.out.println("Tie");
  61. break;
  62. }
  63. if (Functions.checkwinZ(board, player)) {
  64. System.out.println("Player 2 wins");
  65. break;
  66. }
  67. if (Functions.checkwinS(board, player)) {
  68. System.out.println("Player 2 wins");
  69. break;
  70. }
  71. if (Functions.checkwinV(board, player)) {
  72. System.out.println("Player 2 wins");
  73. break;
  74. }
  75. if (Functions.checkwinUV(board, player)) {
  76. System.out.println("Player 2 wins");
  77. break;
  78. }
  79.  
  80. } while (1==1);
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87. public static String letters = "ABCDEFGH";
  88. public static int k = 0;
  89. public static boolean win=false;
  90.  
  91. public static void drop(int board[][], int x, int player) {
  92.  
  93. for (int i = 7; i > -1; i--) {
  94. if (board[x][i] == 0) {
  95. if (player==1) {
  96. board[x][i] = 1;
  97. k = i;
  98. }
  99. if (player==2) {
  100. board[x][i] = 2;
  101. }
  102. break;
  103. }
  104. }
  105.  
  106. }
  107.  
  108. public static boolean possible(int board[][], int x) {
  109. boolean node = false;
  110. for (int i = 7; i > -1; i--) {
  111. if (board[x][i] == 0) {
  112. node = true;
  113. break;
  114. }
  115. }
  116. return node;
  117. }
  118.  
  119. public static void draw(int board[][]) {
  120. System.out.println("");
  121. for (int i = 0; i < 8; i++) {
  122. for (int j = 0; j < 8; j++) {
  123. System.out.print(board[j][i] + " ");
  124. }
  125. System.out.println("");
  126. }
  127. for (int i = 0; i <letters.length(); i++) {
  128. System.out.print(letters.substring(i,i+1) + " ");
  129. }
  130. System.out.println("\n");
  131. }
  132.  
  133. public static boolean checkdone(int board[][], int x) {
  134. boolean end = true;
  135.  
  136. for (int i = 0; i < 8; i++) {
  137. for (int j = 0; j < 8; j++) {
  138. if (board[j][i] == 0) {
  139. end = false;
  140. }
  141. }
  142. }
  143. return end;
  144. }
  145.  
  146. public static boolean checkwinZ(int board[][], int player){
  147. for (int i = 0; i <board.length-2; i++) {
  148. for (int j = 0; j <board[0].length-2; j++) {
  149. if (board[i][j]==player && board[i][j]==board[i+1][j] && board[i][j]==board[i+1][j+1] && board[i][j]==board[i+1][j+2] && board[i][j]==board[i+2][j+2]) {
  150. win=true;
  151. }
  152. }
  153. }
  154. return win;
  155. }
  156.  
  157. public static boolean checkwinS(int board[][], int player){
  158. for (int i = 0; i <board.length-2; i++) {
  159. for (int j = 2; j <8; j++) {
  160. if (board[i][j]==player && board[i][j]==board[i+1][j] && board[i][j]==board[i+1][j-1] && board[i][j]==board[i+1][j-2] && board[i][j]==board[i+2][j-2]) {
  161. win=true;
  162. }
  163. }
  164. }
  165. return win;
  166.  
  167. }
  168.  
  169. public static boolean checkwinV(int board[][], int player){
  170. for (int i = 0; i <4; i++) {
  171. for (int j = 0; j <board[0].length-2; j++) {
  172. if (board[i][j]==player && board[i][j]==board[i+1][j+1] && board[i][j]==board[i+2][j+2] && board[i][j]==board[i+3][j+1] && board[i][j]==board[i+4][j]) {
  173. win=true;
  174. }
  175. }
  176. }
  177. return win;
  178. }
  179.  
  180. public static boolean checkwinUV(int board[][], int player){
  181. for (int i = 0; i <4; i++) {
  182. for (int j = 2; j<8; j++) {
  183. if (board[i][j]==player && board[i][j]==board[i+1][j-1] && board[i][j]==board[i+2][j-2] && board[i][j]==board[i+3][j-1] && board[i][j]==board[i+4][j]) {
  184. win=true;
  185. }
  186. }
  187. }
  188. return win;
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement