Advertisement
NicholasCSW

Connect4TheChosenCode

Mar 14th, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.76 KB | None | 0 0
  1. /**
  2.  * @version 194
  3.  * (Conservatively)
  4.  *
  5.  * @author Ulizio
  6.  *
  7.  * AFTER HOURS OF WORK
  8.  * THE LONG AWAITED CHOSEN CODE
  9.  * HAS BREATHED ITS FIRST BREATH
  10.  *
  11.  *
  12.  * The entire thing is a revamped version of Tic Tac Toe
  13.  * It now uses a char system to minimize complexity
  14.  * Turn system is different
  15.  * I completely scrapped the original I made, so here goes
  16.  *
  17.  * Finally done with no known issues
  18.  */
  19.  
  20.  
  21. import java.util.Scanner;
  22.  
  23. public class Connect4TheChosenCode {
  24.  
  25.     public static void main(String[] args) {
  26.         /**
  27.          *
  28.          * To make things easier, this method is also going to be actually making the moves
  29.          *
  30.          */
  31.        
  32.        
  33.         //Going to do a char array this time to help simplify things
  34.         char[][] board = new char[7][7];
  35.  
  36.        
  37.         resetBoard(board);
  38.         printBoard(board);
  39.  
  40.         //To control whose turn it is using a true false system (switch basically)
  41.         boolean turnSwitch = true;
  42.        
  43.        
  44.         Scanner mover = new Scanner(System.in);
  45.         while (true) {
  46.            
  47.             //Whose turn is it
  48.             if (turnSwitch) {
  49.                
  50.                 System.out.println("Red's turn!");
  51.                
  52.             }
  53.             else {
  54.                
  55.                 System.out.println("Black's turn!");
  56.                
  57.             }
  58.                
  59.             //Get the desired move
  60.             System.out.print("What Column?");
  61.             int column = mover.nextInt();
  62.            
  63.            
  64.            
  65.             if (column < 1 || column > 7) {
  66.                 System.out.println("Don't be that guy");
  67.                 continue;
  68.                 //I think I've figure this one out, we can use it to essentially let the user try again.
  69.             }
  70.            
  71.            
  72.             //First I check if colChecker is false. This tells the program whether or not the column is full (and therefore if the move is possible)
  73.             //The board array is passed as the array
  74.             //The int is pass as column - 1 because the array starts from 0
  75.            
  76.             //I use the ? operator to assign a char value based on the current state of turnSwitch.
  77.             //If turnSwitch is true, then it should be 'R', if it is false it should be 'B'
  78.             if (!colChecker(board, column - 1, turnSwitch ? 'R' : 'B')) {
  79.                 System.out.println("Your overconfidence is your demise. This column is full.");
  80.                 continue;
  81.             }
  82.            
  83.             //Show the board after move is made
  84.             printBoard(board);
  85.                
  86.            
  87.             //Check if somebody won (if they did stop the loop)
  88.             char winner = winnerMasterMethod(board);
  89.             if (winner == 'D') {
  90.                 System.out.println("It is a draw!");
  91.                 break;
  92.             }
  93.             else if (winner == 'R') {
  94.                 System.out.println("Red win!");
  95.                 break;
  96.             }
  97.             else if (winner == 'B') {
  98.                 System.out.println("Black win!");
  99.                 break;
  100.             }
  101.            
  102.            
  103.             //Change turnswitch from state 0 to state 1
  104.             turnSwitch = !turnSwitch;
  105.         }
  106.     }
  107.    
  108.    
  109.    
  110.    
  111.    
  112.     public static boolean colChecker(char[][] board, int column, char color) {
  113.        
  114.         if (board[0][column] != ' ') {
  115.            
  116.             return false;
  117.         }
  118.        
  119.         for (int row = 0; row < 7; row++) {
  120.             if (board[row][column] != ' ') {
  121.                
  122.                 board[row-1][column] = color;
  123.                 return true;
  124.             }
  125.         }
  126.        
  127.        
  128.        
  129.        
  130.         board[6][column] = color;
  131.         return true;
  132.     }
  133.  
  134.  
  135.      
  136.     public static char winConCheckRows(char[][] board) {
  137.        
  138.         int counter = 0;
  139.         for (int row = 0; row < 7; row++) {      
  140.             for (int column = 1; column < 7; column++) {
  141.                 if (board[row][column] != ' ' && board[row][column] == board[row][column-1]) {
  142.                     counter++;
  143.                 }
  144.                
  145.                 else {
  146.                     counter = 1;
  147.                    
  148.                 }
  149.  
  150.                
  151.                 if (counter >= 4) {
  152.                    
  153.                     return board[row][column];
  154.                 }
  155.             }
  156.         }
  157.         return ' ';
  158.        
  159.     }
  160.  
  161.    
  162.    
  163.    
  164.    
  165.     public static char winConCheckColumns(char[][] board) {
  166.        
  167.         int counter = 0;
  168.         for (int column = 0; column < 7; column++) {
  169.             for (int row = 1; row < 7; row++) {
  170.                 if (board[row][column] != ' ' && board[row][column] == board[row-1][column]) {
  171.                     counter++;
  172.                    
  173.                 }
  174.                 else {
  175.                     counter = 1;
  176.                    
  177.                 }
  178.  
  179.                
  180.                 if (counter >= 4) {
  181.                    
  182.                     return board[row][column];
  183.                 }
  184.             }
  185.         }
  186.         return ' ';
  187.         //When we pass an empty char to the final method, it will take that to mean a draw
  188.     }
  189.  
  190.    
  191.    
  192.    
  193.     public static char winConCheckDiagonals(char[][] board) {
  194.        
  195.        
  196.         //Setup to ensure there are no bugs
  197.         int counter = 0;
  198.         int column = 0;
  199.         int row = 0;
  200.        
  201.         //Right to left diagonals
  202.         for (column = 0; column < 7; column++) {
  203.             for (row = 1; row < 7; row++) {
  204.                
  205.                 if (column + row >= 7) {
  206.                
  207.                     break;
  208.                    
  209.                 }
  210.                 if (board[row][column+row] != ' ' && board[row-1][column + row - 1] == board[row][column+row]) {
  211.                     counter++;
  212.                    
  213.                 }
  214.                 else {
  215.                     counter = 1;
  216.                    
  217.                 }
  218.                 if (counter >= 4) {
  219.                
  220.                     return board[row][column+row];
  221.                    
  222.                 }
  223.             }
  224.         }
  225.  
  226.        
  227.         counter = 0;
  228.         column = 0;
  229.         row = 0;
  230.        
  231.        
  232.         //Left to right
  233.         for (row = 0; row < 7; row++) {
  234.             for (column = 1; column < 7; column++) {
  235.                
  236.                 if (column + row >= 7) {
  237.                    
  238.                     break;
  239.                    
  240.                 }
  241.                 if (board[row + column][column] != ' ' && board[row+column - 1][column - 1] == board[row + column][column]) {
  242.                     counter++;
  243.                    
  244.                 }
  245.                 else {
  246.                     counter = 1;
  247.                    
  248.                 }
  249.                 if (counter >= 4) {
  250.                
  251.                     return board[row + column][column];
  252.                    
  253.                 }
  254.             }
  255.         }
  256.  
  257.        
  258.        
  259.         counter = 0;
  260.         column = 0;
  261.         row = 0;
  262.        
  263.         //Top right to bottom left
  264.         for (column = 0; column < 7; column++) {
  265.             for (row = 1; row < 7; row++) {
  266.                
  267.                 if (column - row < 0) {
  268.                    
  269.                     break;
  270.                    
  271.                 }
  272.                 if (board[row][column-row] != ' ' && board[row - 1][column - row + 1] == board[row][column-row]) {
  273.                     counter++;
  274.                 }
  275.                 else {
  276.                     counter = 1;
  277.                    
  278.                 }
  279.                    
  280.                 if (counter >= 4) {
  281.                
  282.                     return board[row][column-row];
  283.                    
  284.                 }
  285.             }
  286.         }
  287.        
  288.        
  289.         counter = 0;
  290.         column = 0;
  291.         row = 0;
  292.        
  293.        
  294.        
  295.         //Left down diagonals
  296.         for (row = 0; row < 7; row++) {
  297.             for (column = 5; column >= 0; --column) {
  298.                
  299.                 if (column - row < 0) {
  300.                    
  301.                     break;
  302.                    
  303.                 }
  304.                 if (board[column - row][column] != ' ' && board[column - row - 1][column + 1] == board[column - row][column]) {
  305.                     counter++;
  306.                    
  307.                 }
  308.                 else {
  309.                     counter = 1;
  310.                    
  311.                 }
  312.                    
  313.                    
  314.                 if (counter >= 4) {
  315.                
  316.                     return board[column - row][column];
  317.                    
  318.                 }
  319.             }
  320.         }
  321.  
  322.         return ' ';
  323.         //When we pass an empty char to the final method, it will take that to mean a draw
  324.     }
  325.  
  326.    
  327.    
  328.    
  329.    
  330.     public static char winnerMasterMethod(char[][] board) {
  331.         /**
  332.          * Within this method I call 3 other methods established earlier
  333.          * Each one checks a different kind of win condition
  334.          * I check diagonals first because this is the most likely win type
  335.          * The verts and horizontals
  336.          *
  337.          * We discussed that we can place brackets anywhere we choose in code
  338.          * Brackets are used to help distinguish different win checks
  339.          *
  340.          */
  341.        
  342.        
  343.        
  344.         {
  345.         char winner = winConCheckDiagonals(board);
  346.        
  347.         if (winner != ' ') return winner;
  348.         }
  349.        
  350.        
  351.        
  352.         {
  353.         char winner = winConCheckRows(board);
  354.        
  355.         if (winner != ' ') return winner;
  356.         }
  357.        
  358.        
  359.        
  360.         {
  361.         char winner = winConCheckColumns(board);
  362.        
  363.         if (winner != ' ') return winner;
  364.         }
  365.        
  366.  
  367.        
  368.         //If all squares are filled, then we're done here (Draw)
  369.         for (int i = 0; i < board.length; ++i)
  370.             for (int j = 0; j < board[i].length; ++j)
  371.                 if (board[i][j] == ' ') return ' ';
  372.  
  373.         return 'D';
  374.     }
  375.    
  376.    
  377.    
  378.    
  379.     public static void resetBoard(char [][] board) {
  380.        
  381.         //Reset the board with pounds (#)
  382.         for (int i = 0; i < 7; ++i) {
  383.             for (int j = 0; j < 7; ++j) {
  384.                 board[i][j] = ' ';
  385.                
  386.             }
  387.         }
  388.        
  389.     }
  390.  
  391.    
  392.    
  393.    
  394.     public static void printBoard(char[][] board) {
  395.        
  396.         //Print out board systematically with formatting
  397.         for (int row = 0; row < 7; row++) {
  398.             System.out.print("| ");
  399.             for (int col = 0; col < 7; ++col)
  400.                 System.out.print(board[row][col] + "| ");
  401.             System.out.println();
  402.         }
  403.  
  404.     }
  405.  
  406.    
  407. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement