Advertisement
Guest User

Connect4

a guest
Nov 17th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.15 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class ct4 {
  4.  
  5. static char[][]board = new char [6][7];
  6. static Scanner kb = new Scanner(System.in);
  7. static int player = 0;
  8. static int redWins =0, yellowWins=0;
  9. static int r = 0,c = 0;
  10. static char color;
  11.     static int totalMoves = 42;
  12. static boolean result1, result2;
  13.  
  14. public static void  initboard(char[][] b){
  15. for (int r=0; r<b.length; r++) {
  16. for (int c=0; c<b[0].length; c++) {
  17. b[r][c]= ' ';
  18. }
  19. }
  20. }
  21.  
  22. public static  char[][] displayBoard(char[][] b){
  23. System.out.println();
  24. for (int r = 0; r< b.length; r++){
  25. System.out.print(" |");
  26. for (int c = 0; c< b[0].length; c++){
  27. System.out.print(" " + b[r][c] + " |");
  28. }
  29. System.out.println();
  30.  
  31. }
  32. System.out.println(" -----------------------------");
  33. System.out.println(" | 1 | 2 | 3 | 4 | 5 | 6 | 7 |");
  34. return board;
  35.  
  36. }
  37.  
  38. public static  void move(int c) {
  39. Scanner kb= new Scanner(System.in);
  40.  
  41.         if(player%2==0)
  42.  
  43.         {
  44.                System.out.println("Player 1");
  45.                System.out.println("Which column do you want to move?");
  46.                do{
  47.                       c = kb.nextInt();
  48.                      
  49.                       if(c<0 || c>7){
  50.                             System.out.println("Invalid, choose another column");
  51.  
  52.                       }                    
  53.                }while(c<0 || c>7);
  54.                c--;
  55.  
  56.                for(int r=5;r>-1;r--){
  57.                       if(board[r][c] == ' '){
  58.                              board[r][c]= color;
  59.                              break;
  60.                       }
  61.  
  62.                       else if(board[0][c]!=' '){
  63.                             System.out.println("Invalid, Column full choose another column");
  64.                              do{
  65.                                    c = kb.nextInt();
  66.                                    if(c<0 || c>7){
  67.                                          System.out.println("Invalid, choose another column");
  68.                              }
  69.                              board[r][c-1]=color;
  70.                                    
  71.                              }while(c<0 || c>7);
  72.                              break;
  73.  
  74.                       }
  75.                }
  76.         }
  77.  
  78.         else{
  79.                System.out.println("Player 2");
  80.                System.out.println("Which column do you want to move?");
  81.  
  82.                do{
  83.                       c = kb.nextInt();
  84.                       if(c<0 || c>7){
  85.                             System.out.println("Invalid, choose another column");
  86.  
  87.                       }      
  88.                }while(c<0 || c>7);
  89.                c--;    
  90.                
  91.                for(int r=5;r>-1;r--){
  92.  
  93.                       if(board[r][c] == ' '){
  94.                              board[r][c]= color;
  95.                              break;
  96.                       }
  97.  
  98.                       else if(board[0][c]!=' '){
  99.  
  100.                             System.out.println("Invalid, Column full choose another column");
  101.                              
  102.                             do{
  103.                                    c = kb.nextInt();
  104.  
  105.                                    if(c<0 || c>7){
  106.                                          System.out.println("Invalid, choose another column");
  107.  
  108.                                    }
  109.                                    board[r][c-1]= color;
  110.  
  111.                              }while(c<0 || c>7);
  112.                              break;
  113.                       }
  114.                }
  115.         }
  116.  
  117.         player++;
  118.   }
  119.  
  120.  
  121. public static void whichPlayer () {
  122. if(player%2==0) {
  123.  
  124. System.out.println("Player 1's Turn");
  125. color ='Y';
  126. }
  127. else {
  128. System.out.println("Player 2's Turn");
  129. color ='R';
  130. }
  131. }
  132.  
  133. public static boolean checkRedWin(char[][]b, int r, int c, char R) {
  134. if((r+3)< b[0].length){
  135. if((b[r][c]=='R')&&(b[r][c+1]=='R')&&(b[r][c+2]=='R')&&(b[r][c+3]=='R')) {
  136.  
  137. result1 = true;
  138. redWins++;
  139. System.out.println("Red is the winner!");
  140. return true;
  141.  
  142. }
  143. if((r+3)<b.length){
  144. if((b[r][c]=='R')&&(b[r+1][c]=='R')&&(b[r+2][c]=='R')&&(b[r+3][c]=='R')) {
  145.  
  146. result1 = true;
  147. redWins++;
  148. System.out.println("Red is the winner!");
  149. return true;
  150. }
  151. if ((r+3)<b.length &&((c+3)<b[0].length)){
  152. if((b[r][c]=='R')&&(b[r+1][c+1]=='R')&&(b[r+2][c+2]=='R')&&(b[r+3][c+3]=='R')) {
  153.  
  154. result1 = true;
  155. redWins++;
  156. System.out.println("Red is the winner!");
  157. return true;
  158. }
  159. }
  160. if ((r+3)>0&&((c+3)<b[0].length)){
  161. if((b[r][c]=='R')&&(b[r-1][c+1]=='R')&&(b[r-2][c+2]=='R')&&(b[r-3][c+3]=='R')) {
  162.  
  163. result1 = true;
  164. redWins++;
  165. System.out.println("Red is the winner!");
  166. return true;
  167. }
  168.  
  169. }
  170. }
  171. }
  172. return false;
  173. }
  174.  
  175. public static boolean checkYellowWin(char[][]b, int r, int c, char Y) {
  176. if((r+3)< b[0].length){
  177. if((b[r][c]=='Y')&&(b[r][c+1]=='Y')&&(b[r][c+2]=='Y')&&(b[r][c+3]=='Y')) {
  178.  
  179. result2 = true;
  180. yellowWins++;
  181. System.out.println("Yellow is the winner!");
  182. return true;
  183.  
  184. }
  185. if((r+3)<b.length){
  186. if((b[r][c]=='Y')&&(b[r+1][c]=='Y')&&(b[r+2][c]=='Y')&&(b[r+3][c]=='Y')) {
  187.  
  188. result2 = true;
  189. yellowWins++;
  190. System.out.println("Yellow is the winner!");
  191. return true;
  192. }
  193. if ((r+3)<b.length &&((c+3)<b[0].length)){
  194. if((b[r][c]=='Y')&&(b[r+1][c+1]=='Y')&&(b[r+2][c+2]=='Y')&&(b[r+3][c+3]=='Y')) {
  195.  
  196. result2 = true;
  197. yellowWins++;
  198. System.out.println("Yellow is the winner!");
  199. return true;
  200. }
  201. }
  202. if ((r+3)>0&&((c+3)<b[0].length)){
  203. if((b[r][c]=='Y')&&(b[r-1][c+1]=='Y')&&(b[r-2][c+2]=='Y')&&(b[r-3][c+3]=='Y')) {
  204.  
  205. result2 = true;
  206. yellowWins++;
  207. System.out.println("Yellow is the winner!");
  208. return true;
  209. }
  210.  
  211. }
  212. }
  213. }
  214. return false;
  215. }
  216.  
  217.  
  218. public static boolean checkWinner(char[][]b) {
  219.  
  220. boolean red, yellow;
  221. for (int r=0; r<b.length; r++) {
  222. for (int c=0; c<b[0].length;c++) {
  223.  
  224. red = checkRedWin(displayBoard(b),r,c,'R');
  225.  
  226.             yellow = checkYellowWin(displayBoard(b),r,c,'Y');
  227.  
  228.             if(red==true || yellow==true) {
  229.  
  230.                    return true;
  231.  
  232.             }
  233. }
  234. }
  235. return false;
  236. }
  237.  
  238. public static void checkdraw(char[][]b){
  239.  
  240.      for(int r=0; r<b.length; r++){
  241.          for(int c=0;c<b[0].length;c++){
  242.                          if(b[r][c]!=' '){
  243.                                 System.out.println("The game is a draw!");
  244.                                 break;
  245.                          }
  246.                   }
  247.            }
  248.            }
  249.  
  250. public static void main(String[] args) {
  251. // TODO Auto-generated method stub
  252.  
  253. System.out.println("Player 1 is: Y = Yellow");
  254. System.out.println("Player 2 is: R = Red ");
  255.  
  256. int c=0;
  257. int redWins=0, yellowWins=0;
  258. int playAgain = 0;
  259. int moves = 0;
  260.  
  261. do {
  262. initboard(board);
  263. displayBoard(board);
  264. result1=false;
  265. result2=false;
  266. player=0;
  267. do {
  268.  
  269. whichPlayer();
  270. move(c);
  271. checkWinner(board);
  272. moves++;
  273.  
  274.         if(totalMoves==moves){
  275.                checkdraw(board);
  276.                break;
  277.         }
  278.        
  279. } while(result1!= true && result2!= true);
  280.  
  281. System.out.println("Game Over!");
  282.         System.out.println("Do you want to play again? 1 for Yes, 0 for No");
  283.  
  284.         playAgain = kb.nextInt();
  285.  
  286.         if(result1==true){
  287.                redWins++;
  288.         }
  289.         System.out.println("Red wins: " + redWins);
  290.  
  291.         if(result2==true){
  292.                yellowWins++;
  293.         }
  294.         System.out.println("Yellow wins: " + yellowWins);
  295.  
  296. } while(playAgain==1);
  297. }
  298. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement