Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * This plays a game of the new and updated "Connect 420".
- * Almost 500 lines of code later.....
- *
- * @author Ulizio
- * @version 56
- */
- import java.util.Scanner;
- public class Connect420 {
- public static void main (String [] args) {
- String [][] board = new String [6][7];
- //First num in array is rows, second is columns
- resetBoard(board);
- while(winConCheckerO(board) == false && winConCheckerX(board) == false) {
- moveMakerBlack(board);
- printBoard(board);
- moveMakerRed(board);
- printBoard(board);
- }
- if(winConCheckerO(board) == true) {
- System.out.println("O Won!");
- }
- if(winConCheckerX(board) == true) {
- System.out.println("X Won!");
- }
- }
- public static void moveMakerBlack (String [][] board) {
- System.out.println("It's O's Turn!");
- Scanner scan = new Scanner(System.in);
- /**
- System.out.println("Which Row?");
- int row = scan.nextInt();
- */
- System.out.println("Which Column?");
- int col = scan.nextInt();
- int colProcessed = col - 1;
- if(vacBanner(colProcessed) == true) {
- 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 ");
- }
- int y = 0;
- int counter = 0;
- //counter will find how many pieces are already in that row
- for(y=0;y<6;y++) {
- if(board[y][colProcessed] == "O" || board[y][colProcessed] == "X") {
- counter++;
- }
- if(counter == 6) {
- System.out.println("WARNING: Invalid Move");
- }
- }
- int placementRow = 5 - counter;
- if(placementRow == -1) {
- System.out.println("Moving on, your move was invalid and wasted.");
- }
- if(placementRow != -1) {
- board[placementRow][colProcessed] = "O";
- }
- /**
- for(y=5;y>0;y--) {
- if(board[y][colProcessed] == "#") {
- }
- if(y == 0) {
- board[y][colProcessed] = "O";
- break;
- }
- if(board[y][colProcessed] == "O" ||board[y][colProcessed] == "X") {
- board[y][colProcessed] = "O";
- break;
- }
- }
- */
- /**
- System.out.println("Please Confirm Move- Row: " + row + " Col: " + col + ". Press Y or N.");
- char confirm = scan.nextInt();
- if(confirm == 'y') {
- }
- if(confirm == 'n') {
- }
- else {
- System.out.println("Deleting Windows.");
- }
- */
- }
- public static void moveMakerRed (String [][] board) {
- System.out.println("It's X's Turn!");
- Scanner scan = new Scanner(System.in);
- /**
- System.out.println("Which Row?");
- int row = scan.nextInt();
- */
- System.out.println("Which Column?");
- int col = scan.nextInt();
- int colProcessed = col - 1;
- if(vacBanner(colProcessed) == true) {
- 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 ");
- }
- int y = 0;
- int counter = 0;
- //counter will find how many pieces are already in that row
- for(y=0;y<6;y++) {
- if(board[y][colProcessed] == "O" || board[y][colProcessed] == "X") {
- counter++;
- }
- }
- int placementRow = 5 - counter;
- if(placementRow == -1) {
- System.out.println("Moving on, your move was invalid and wasted.");
- }
- if(placementRow != -1) {
- board[placementRow][colProcessed] = "X";
- }
- /**
- for(y=5;y>0;y--) {
- if(board[y][colProcessed] == "#") {
- }
- if(y == 0) {
- board[y][colProcessed] = "O";
- }
- if(board[y][colProcessed] == "O" ||board[y][colProcessed] == "X") {
- board[y][colProcessed] = "X";
- }
- }
- */
- /**
- System.out.println("Please Confirm Move- Row: " + row + " Col: " + col + ". Press Y or N.");
- char confirm = scan.nextInt();
- if(confirm == 'y') {
- }
- if(confirm == 'n') {
- }
- else {
- System.out.println("Deleting Windows.");
- }
- */
- }
- public static void printBoard (String [][] boardToPrint) {
- int i = 0;
- int x = 0;
- int a = 0;
- for(i=0;i<6;i++) {
- for(x=0;x<7;x++) {
- System.out.print("|" + boardToPrint[i][x]);
- a++;
- if (a == 7) {
- System.out.print("|");
- System.out.print("\n");
- a = 0;
- }
- }
- }
- }
- public static void resetBoard (String [][] boardToReset) {
- int i = 0;
- int x = 0;
- int a = 0;
- for(i=0;i<6;i++) {
- for(x=0;x<7;x++) {
- boardToReset[i][x] = "#";
- }
- }
- }
- public static boolean vacBanner(int num) {
- boolean vacBanned = false;
- if(num > 6 || num < 0) {
- vacBanned = true;
- }
- return(vacBanned);
- }
- public static boolean winConCheckerO (String [][] board) {
- boolean didWin = false;
- int i = 0;
- //For loop checks for top row right-down diagonals.
- for(i=0;i<3;i++) {
- }
- int counter = 0;
- int a = 0;
- int b = 0;
- //This loop will check for vertical wins
- for(a=0;a<7;a++){
- for(b=6;b<6;b++) {
- if(counter == 4) {
- didWin = true;
- }
- if(board[b][a] == "O") {
- counter++;
- }
- /**
- if(board[b][a] != "O") {
- counter = 0;
- }
- */
- }
- }
- counter = 0;
- a = 0;
- b = 0;
- //This loop checks for horizontal wins
- for(b=6;b<6;b++) {
- for(a=0;a<7;a++) {
- if(counter == 4) {
- didWin = true;
- }
- if(board[b][a] == "O") {
- counter++;
- }
- /**
- if(board[b][a] != "O") {
- counter = 0;
- }
- */
- }
- }
- counter = 0;
- int d = 0; //Width
- int e = 0; //Height
- int f = 0;
- boolean shouldContinue = true;
- int currentRow = 1;
- int currentCol = 1;
- //And finally, this loop(MONSTROSITY) checks for wins in a diagonal pattern (from top-left to bottom-right)
- for(f=0;f<3;f++) { //How many times to check diagonals
- for(d = 0; 3 > d; d += 1){ //This line regulates width
- for(e = 0; 3 > e; e += 1){ //This line regulates height
- //now we will use the same counter method to count pieces found
- if(board[d][e] == "O") {
- counter++;
- while (shouldContinue == true) { //Regulates if we should still be checking
- if (currentCol + d <= 3 - 1 && currentRow + e <= 3-1) { //Regulates where we're checking
- if(board[e + currentRow][d + currentCol] == "O") { //If we find an O
- counter++;
- }
- }
- currentRow++;
- currentCol++;
- if(counter >= 4){
- System.out.println("WINNER: O"); //If Connected 4, then win!
- shouldContinue = false; //Stop ze Checks!
- didWin = true; //Somebody won
- break; //Stop the loop
- }
- if(currentCol == 3 - 1 || currentRow == 3 - 1) { //If this is true then you've left the board
- shouldContinue = false; //Stop the loop
- }
- }
- }
- if(counter >= 4){ //If didn't Connect 4, then lose!
- didWin = false;
- break;
- }
- //Prep for next run through
- counter = 0;
- currentRow = 1;
- currentCol = 1;
- }
- }
- }
- return(didWin);
- }
- public static boolean winConCheckerX (String [][] board) {
- boolean didWin = false;
- int i = 0;
- //For loop checks for top row right-down diagonals.
- for(i=0;i<3;i++) {
- }
- int counter = 0;
- int a = 0;
- int b = 0;
- //This loop will check for vertical wins
- for(a=0;a<7;a++){
- for(b=6;b<6;b++) {
- if(counter == 4) {
- didWin = true;
- }
- if(board[b][a] == "X") {
- counter++;
- }
- if(board[b][a] != "X") {
- counter = 0;
- }
- }
- }
- counter = 0;
- a = 0;
- b = 0;
- //This loop checks for horizontal wins
- for(b=6;b<6;b++) {
- for(a=0;a<7;a++) {
- if(counter == 4) {
- didWin = true;
- }
- if(board[b][a] == "X") {
- counter++;
- }
- if(board[b][a] != "X") {
- counter = 0;
- }
- }
- }
- counter = 0;
- int d = 0; //Width
- int e = 0; //Height
- int f = 0;
- boolean shouldContinue = true;
- int currentRow = 1;
- int currentCol = 1;
- //And finally, this loop(MONSTROSITY) checks for wins in a diagonal pattern (from top-left to bottom-right)
- for(f=0;f<3;f++) { //How many times to check diagonals
- for(d = 0; 3 > d; d += 1){ //This line regulates width
- for(e = 0; 3 > e; e += 1){ //This line regulates height
- //now we will use the same counter method to count pieces found
- if(board[d][e] == "X") {
- counter++;
- while (shouldContinue == true) { //Regulates if we should still be checking
- if (currentCol + d <= 3 - 1 && currentRow + e <= 3-1) { //Regulates where we're checking
- if(board[e + currentRow][d + currentCol] == "X") { //If we find an O
- counter++;
- }
- }
- currentRow++;
- currentCol++;
- if(counter >= 4){
- System.out.println("WINNER: X"); //If Connected 4, then win!
- shouldContinue = false; //Stop ze Checks!
- didWin = true; //Somebody won
- break; //Stop the loop
- }
- if(currentCol == 3 - 1 || currentRow == 3 - 1) { //If this is true then you've left the board
- shouldContinue = false; //Stop the loop
- }
- }
- }
- if(counter >= 4){ //If didn't Connect 4, then lose!
- didWin = false;
- break;
- }
- //Prep for next run through
- counter = 0;
- currentRow = 1;
- currentCol = 1;
- }
- }
- }
- return(didWin);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement