Advertisement
NicholasCSW

Connect420

Mar 12th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 14.99 KB | None | 0 0
  1. /**
  2.  * This plays a game of the new and updated "Connect 420".
  3.  * Almost 500 lines of code later.....
  4.  *
  5.  * @author Ulizio  
  6.  * @version 56
  7.  */
  8. import java.util.Scanner;
  9. public class Connect420 {
  10.  
  11.     public static void main (String [] args) {
  12.        
  13.         String [][] board = new String [6][7];
  14.        
  15.         //First num in array is rows, second is columns
  16.        
  17.         resetBoard(board);
  18.        
  19.         while(winConCheckerO(board) == false && winConCheckerX(board) == false) {
  20.        
  21.            
  22.             moveMakerBlack(board);
  23.             printBoard(board);
  24.             moveMakerRed(board);
  25.             printBoard(board);
  26.        
  27.         }
  28.        
  29.         if(winConCheckerO(board) == true) {
  30.             System.out.println("O Won!");
  31.            
  32.         }
  33.         if(winConCheckerX(board) == true) {
  34.             System.out.println("X Won!");
  35.            
  36.         }
  37.        
  38.     }
  39.    
  40.     public static void moveMakerBlack (String [][] board) {
  41.        
  42.         System.out.println("It's O's Turn!");
  43.        
  44.         Scanner scan = new Scanner(System.in);
  45.        
  46.         /**
  47.         System.out.println("Which Row?");
  48.         int row = scan.nextInt();
  49.         */
  50.        
  51.         System.out.println("Which Column?");
  52.         int col = scan.nextInt();
  53.        
  54.         int colProcessed = col - 1;
  55.        
  56.         if(vacBanner(colProcessed) == true) {
  57.             System.out.println("BAN BAN BAN BAN BAN BAN BAN BAN BAN BAN BAN BAN BAN BAN BAN BAN BAN BAN BAN BAN BAN BAN BAN BAN BAN BAN BAN BAN BAN BAN BAN BAN BAN BAN BAN BAN BAN BAN BAN BAN BAN BAN BAN BAN BAN BAN BAN BAN BAN BAN BAN BAN BAN BAN BAN BAN ");
  58.            
  59.         }
  60.        
  61.         int y = 0;
  62.        
  63.         int counter = 0;
  64.         //counter will find how many pieces are already in that row
  65.        
  66.         for(y=0;y<6;y++) {
  67.             if(board[y][colProcessed] == "O" || board[y][colProcessed] == "X") {
  68.                
  69.                 counter++;
  70.                
  71.             }
  72.             if(counter == 6) {
  73.                 System.out.println("WARNING: Invalid Move");
  74.                
  75.             }
  76.         }
  77.        
  78.         int placementRow = 5 - counter;
  79.        
  80.         if(placementRow == -1) {
  81.             System.out.println("Moving on, your move was invalid and wasted.");
  82.            
  83.         }
  84.         if(placementRow != -1) {
  85.             board[placementRow][colProcessed] = "O";
  86.            
  87.         }
  88.        
  89.        
  90.        
  91.        
  92.        
  93.        
  94.        
  95.         /**
  96.         for(y=5;y>0;y--) {
  97.             if(board[y][colProcessed] == "#") {
  98.                
  99.                
  100.             }
  101.            
  102.             if(y == 0) {
  103.                
  104.                 board[y][colProcessed] = "O";
  105.                
  106.                 break;
  107.             }
  108.            
  109.             if(board[y][colProcessed] == "O" ||board[y][colProcessed] == "X") {
  110.                 board[y][colProcessed] = "O";
  111.                
  112.                 break;
  113.             }
  114.            
  115.         }
  116.        
  117.         */
  118.        
  119.         /**
  120.         System.out.println("Please Confirm Move- Row: " + row + " Col: " + col + ". Press Y or N.");
  121.        
  122.         char confirm = scan.nextInt();
  123.        
  124.         if(confirm == 'y') {
  125.            
  126.            
  127.         }
  128.         if(confirm == 'n') {
  129.            
  130.            
  131.         }
  132.         else {
  133.             System.out.println("Deleting Windows.");
  134.            
  135.         }
  136.         */
  137.     }
  138.    
  139.     public static void moveMakerRed (String [][] board) {
  140.        
  141.         System.out.println("It's X's Turn!");
  142.        
  143.         Scanner scan = new Scanner(System.in);
  144.        
  145.         /**
  146.         System.out.println("Which Row?");
  147.         int row = scan.nextInt();
  148.         */
  149.        
  150.         System.out.println("Which Column?");
  151.         int col = scan.nextInt();
  152.        
  153.         int colProcessed = col - 1;
  154.        
  155.         if(vacBanner(colProcessed) == true) {
  156.             System.out.println("BAN BAN BAN BAN BAN BAN BAN BAN BAN BAN BAN BAN BAN BAN BAN BAN BAN BAN BAN BAN BAN BAN BAN BAN BAN BAN BAN BAN BAN BAN BAN BAN BAN BAN BAN BAN BAN BAN BAN BAN BAN BAN BAN BAN BAN BAN BAN BAN BAN BAN BAN BAN BAN BAN BAN BAN ");
  157.            
  158.         }
  159.        
  160.         int y = 0;
  161.        
  162.         int counter = 0;
  163.         //counter will find how many pieces are already in that row
  164.        
  165.         for(y=0;y<6;y++) {
  166.             if(board[y][colProcessed] == "O" || board[y][colProcessed] == "X") {
  167.                
  168.                 counter++;
  169.                
  170.             }
  171.         }
  172.        
  173.         int placementRow = 5 - counter;
  174.        
  175.         if(placementRow == -1) {
  176.             System.out.println("Moving on, your move was invalid and wasted.");
  177.            
  178.         }
  179.         if(placementRow != -1) {
  180.             board[placementRow][colProcessed] = "X";
  181.            
  182.         }
  183.        
  184.        
  185.        
  186.        
  187.        
  188.        
  189.        
  190.        
  191.        
  192.        
  193.         /**
  194.        
  195.         for(y=5;y>0;y--) {
  196.             if(board[y][colProcessed] == "#") {
  197.                
  198.                
  199.                
  200.             }
  201.             if(y == 0) {
  202.                
  203.                 board[y][colProcessed] = "O";
  204.                
  205.             }
  206.             if(board[y][colProcessed] == "O" ||board[y][colProcessed] == "X") {
  207.                 board[y][colProcessed] = "X";
  208.             }
  209.            
  210.         }
  211.        
  212.        
  213.         */
  214.         /**
  215.         System.out.println("Please Confirm Move- Row: " + row + " Col: " + col + ". Press Y or N.");
  216.        
  217.         char confirm = scan.nextInt();
  218.        
  219.         if(confirm == 'y') {
  220.            
  221.            
  222.         }
  223.         if(confirm == 'n') {
  224.            
  225.            
  226.         }
  227.         else {
  228.             System.out.println("Deleting Windows.");
  229.            
  230.         }
  231.         */
  232.        
  233.     }
  234.    
  235.     public static void printBoard (String [][] boardToPrint) {
  236.        
  237.         int i = 0;
  238.         int x = 0;
  239.         int a = 0;
  240.        
  241.         for(i=0;i<6;i++) {
  242.             for(x=0;x<7;x++) {
  243.                 System.out.print("|" + boardToPrint[i][x]);
  244.                 a++;
  245.                 if (a == 7) {
  246.                     System.out.print("|");
  247.                     System.out.print("\n");
  248.                    
  249.                     a = 0;
  250.                 }
  251.                
  252.             }
  253.         }
  254.        
  255.     }
  256.    
  257.     public static void resetBoard (String [][] boardToReset) {
  258.        
  259.         int i = 0;
  260.         int x = 0;
  261.         int a = 0;
  262.        
  263.         for(i=0;i<6;i++) {
  264.             for(x=0;x<7;x++) {
  265.                 boardToReset[i][x] = "#";
  266.             }
  267.         }
  268.        
  269.        
  270.     }
  271.    
  272.     public static boolean vacBanner(int num) {
  273.        
  274.         boolean vacBanned = false;
  275.        
  276.         if(num > 6 || num < 0) {
  277.             vacBanned = true;
  278.         }
  279.        
  280.         return(vacBanned);
  281.     }
  282.      
  283.     public static boolean winConCheckerO (String [][] board) {
  284.        
  285.         boolean didWin = false;
  286.         int i = 0;
  287.        
  288.         //For loop checks for top row right-down diagonals.
  289.         for(i=0;i<3;i++) {
  290.            
  291.            
  292.         }
  293.        
  294.         int counter = 0;
  295.         int a = 0;
  296.         int b = 0;
  297.        
  298.         //This loop will check for vertical wins
  299.         for(a=0;a<7;a++){
  300.             for(b=6;b<6;b++) {
  301.                 if(counter == 4) {
  302.                    
  303.                     didWin = true;
  304.                 }
  305.                 if(board[b][a] == "O") {
  306.                    
  307.                     counter++;
  308.                 }
  309.                 /**
  310.                 if(board[b][a] != "O") {
  311.                    
  312.                     counter = 0;
  313.                 }
  314.                 */
  315.             }
  316.         }
  317.        
  318.         counter = 0;
  319.         a = 0;
  320.         b = 0;
  321.        
  322.         //This loop checks for horizontal wins
  323.         for(b=6;b<6;b++) {
  324.             for(a=0;a<7;a++) {
  325.                 if(counter == 4) {
  326.                    
  327.                     didWin = true;
  328.                 }
  329.                 if(board[b][a] == "O") {
  330.  
  331.                     counter++;
  332.                 }
  333.                 /**
  334.                 if(board[b][a] != "O") {
  335.                    
  336.                     counter = 0;
  337.                 }
  338.                 */
  339.             }
  340.         }
  341.        
  342.        
  343.         counter = 0;
  344.         int d = 0; //Width
  345.         int e = 0; //Height
  346.         int f = 0;
  347.         boolean shouldContinue = true;
  348.         int currentRow = 1;
  349.         int currentCol = 1;
  350.         //And finally, this loop(MONSTROSITY) checks for wins in a diagonal pattern (from top-left to bottom-right)
  351.        
  352.         for(f=0;f<3;f++) { //How many times to check diagonals
  353.             for(d = 0; 3 > d; d += 1){ //This line regulates width
  354.                 for(e = 0; 3 > e; e += 1){ //This line regulates height
  355.                  
  356.                     //now we will use the same counter method to count pieces found
  357.                     if(board[d][e] == "O") {
  358.                         counter++;
  359.                        
  360.                         while (shouldContinue == true) { //Regulates if we should still be checking
  361.                            
  362.                             if (currentCol + d <= 3 - 1 && currentRow + e <= 3-1) { //Regulates where we're checking
  363.                                 if(board[e + currentRow][d + currentCol] == "O") { //If we find an O
  364.                                    
  365.                                     counter++;
  366.                                 }
  367.                             }
  368.                            
  369.                            
  370.                             currentRow++;
  371.                             currentCol++;
  372.                            
  373.                             if(counter >= 4){
  374.                                 System.out.println("WINNER: O"); //If Connected 4, then win!
  375.                                 shouldContinue = false; //Stop ze Checks!
  376.                                 didWin = true; //Somebody won
  377.                                 break; //Stop the loop
  378.                             }
  379.                            
  380.                             if(currentCol == 3 - 1 || currentRow == 3 - 1) { //If this is true then you've left the board
  381.                                
  382.                                 shouldContinue = false; //Stop the loop
  383.                             }
  384.                            
  385.                            
  386.                         }
  387.                     }
  388.                    
  389.                     if(counter >= 4){ //If didn't Connect 4, then lose!
  390.                         didWin = false;
  391.                         break;
  392.                     }
  393.                    
  394.                    
  395.                     //Prep for next run through
  396.                     counter = 0;
  397.                     currentRow = 1;
  398.                     currentCol = 1;
  399.                    
  400.                 }
  401.             }
  402.         }
  403.        
  404.        
  405.         return(didWin);
  406.     }
  407.    
  408.     public static boolean winConCheckerX (String [][] board) {
  409.        
  410.         boolean didWin = false;
  411.         int i = 0;
  412.        
  413.         //For loop checks for top row right-down diagonals.
  414.         for(i=0;i<3;i++) {
  415.            
  416.            
  417.         }
  418.        
  419.         int counter = 0;
  420.         int a = 0;
  421.         int b = 0;
  422.        
  423.         //This loop will check for vertical wins
  424.         for(a=0;a<7;a++){
  425.             for(b=6;b<6;b++) {
  426.                 if(counter == 4) {
  427.                    
  428.                     didWin = true;
  429.                 }
  430.                 if(board[b][a] == "X") {
  431.                    
  432.                     counter++;
  433.                 }
  434.                 if(board[b][a] != "X") {
  435.                    
  436.                     counter = 0;
  437.                 }
  438.             }
  439.         }
  440.        
  441.         counter = 0;
  442.         a = 0;
  443.         b = 0;
  444.        
  445.         //This loop checks for horizontal wins
  446.         for(b=6;b<6;b++) {
  447.             for(a=0;a<7;a++) {
  448.                 if(counter == 4) {
  449.                    
  450.                     didWin = true;
  451.                 }
  452.                 if(board[b][a] == "X") {
  453.  
  454.                     counter++;
  455.                 }
  456.                 if(board[b][a] != "X") {
  457.                     counter = 0;
  458.                 }
  459.             }
  460.         }
  461.        
  462.        
  463.         counter = 0;
  464.         int d = 0; //Width
  465.         int e = 0; //Height
  466.         int f = 0;
  467.         boolean shouldContinue = true;
  468.         int currentRow = 1;
  469.         int currentCol = 1;
  470.         //And finally, this loop(MONSTROSITY) checks for wins in a diagonal pattern (from top-left to bottom-right)
  471.        
  472.         for(f=0;f<3;f++) { //How many times to check diagonals
  473.             for(d = 0; 3 > d; d += 1){ //This line regulates width
  474.                 for(e = 0; 3 > e; e += 1){ //This line regulates height
  475.                  
  476.                     //now we will use the same counter method to count pieces found
  477.                     if(board[d][e] == "X") {
  478.                         counter++;
  479.                        
  480.                         while (shouldContinue == true) { //Regulates if we should still be checking
  481.                            
  482.                             if (currentCol + d <= 3 - 1 && currentRow + e <= 3-1) { //Regulates where we're checking
  483.                                 if(board[e + currentRow][d + currentCol] == "X") { //If we find an O
  484.                                    
  485.                                     counter++;
  486.                                 }
  487.                             }
  488.                            
  489.                            
  490.                             currentRow++;
  491.                             currentCol++;
  492.                            
  493.                             if(counter >= 4){
  494.                                 System.out.println("WINNER: X"); //If Connected 4, then win!
  495.                                 shouldContinue = false; //Stop ze Checks!
  496.                                 didWin = true; //Somebody won
  497.                                 break; //Stop the loop
  498.                             }
  499.                            
  500.                             if(currentCol == 3 - 1 || currentRow == 3 - 1) { //If this is true then you've left the board
  501.                                
  502.                                 shouldContinue = false; //Stop the loop
  503.                             }
  504.                            
  505.                            
  506.                         }
  507.                     }
  508.                    
  509.                     if(counter >= 4){ //If didn't Connect 4, then lose!
  510.                         didWin = false;
  511.                         break;
  512.                     }
  513.                    
  514.                    
  515.                     //Prep for next run through
  516.                     counter = 0;
  517.                     currentRow = 1;
  518.                     currentCol = 1;
  519.                    
  520.                 }
  521.             }
  522.         }
  523.        
  524.        
  525.         return(didWin);
  526.     }
  527.  
  528. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement