Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * @version 194
- * (Conservatively)
- *
- * @author Ulizio
- *
- * AFTER HOURS OF WORK
- * THE LONG AWAITED CHOSEN CODE
- * HAS BREATHED ITS FIRST BREATH
- *
- *
- * The entire thing is a revamped version of Tic Tac Toe
- * It now uses a char system to minimize complexity
- * Turn system is different
- * I completely scrapped the original I made, so here goes
- *
- * Finally done with no known issues
- */
- import java.util.Scanner;
- public class Connect4TheChosenCode {
- public static void main(String[] args) {
- /**
- *
- * To make things easier, this method is also going to be actually making the moves
- *
- */
- //Going to do a char array this time to help simplify things
- char[][] board = new char[7][7];
- resetBoard(board);
- printBoard(board);
- //To control whose turn it is using a true false system (switch basically)
- boolean turnSwitch = true;
- Scanner mover = new Scanner(System.in);
- while (true) {
- //Whose turn is it
- if (turnSwitch) {
- System.out.println("Red's turn!");
- }
- else {
- System.out.println("Black's turn!");
- }
- //Get the desired move
- System.out.print("What Column?");
- int column = mover.nextInt();
- if (column < 1 || column > 7) {
- System.out.println("Don't be that guy");
- continue;
- //I think I've figure this one out, we can use it to essentially let the user try again.
- }
- //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)
- //The board array is passed as the array
- //The int is pass as column - 1 because the array starts from 0
- //I use the ? operator to assign a char value based on the current state of turnSwitch.
- //If turnSwitch is true, then it should be 'R', if it is false it should be 'B'
- if (!colChecker(board, column - 1, turnSwitch ? 'R' : 'B')) {
- System.out.println("Your overconfidence is your demise. This column is full.");
- continue;
- }
- //Show the board after move is made
- printBoard(board);
- //Check if somebody won (if they did stop the loop)
- char winner = winnerMasterMethod(board);
- if (winner == 'D') {
- System.out.println("It is a draw!");
- break;
- }
- else if (winner == 'R') {
- System.out.println("Red win!");
- break;
- }
- else if (winner == 'B') {
- System.out.println("Black win!");
- break;
- }
- //Change turnswitch from state 0 to state 1
- turnSwitch = !turnSwitch;
- }
- }
- public static boolean colChecker(char[][] board, int column, char color) {
- if (board[0][column] != ' ') {
- return false;
- }
- for (int row = 0; row < 7; row++) {
- if (board[row][column] != ' ') {
- board[row-1][column] = color;
- return true;
- }
- }
- board[6][column] = color;
- return true;
- }
- public static char winConCheckRows(char[][] board) {
- int counter = 0;
- for (int row = 0; row < 7; row++) {
- for (int column = 1; column < 7; column++) {
- if (board[row][column] != ' ' && board[row][column] == board[row][column-1]) {
- counter++;
- }
- else {
- counter = 1;
- }
- if (counter >= 4) {
- return board[row][column];
- }
- }
- }
- return ' ';
- }
- public static char winConCheckColumns(char[][] board) {
- int counter = 0;
- for (int column = 0; column < 7; column++) {
- for (int row = 1; row < 7; row++) {
- if (board[row][column] != ' ' && board[row][column] == board[row-1][column]) {
- counter++;
- }
- else {
- counter = 1;
- }
- if (counter >= 4) {
- return board[row][column];
- }
- }
- }
- return ' ';
- //When we pass an empty char to the final method, it will take that to mean a draw
- }
- public static char winConCheckDiagonals(char[][] board) {
- //Setup to ensure there are no bugs
- int counter = 0;
- int column = 0;
- int row = 0;
- //Right to left diagonals
- for (column = 0; column < 7; column++) {
- for (row = 1; row < 7; row++) {
- if (column + row >= 7) {
- break;
- }
- if (board[row][column+row] != ' ' && board[row-1][column + row - 1] == board[row][column+row]) {
- counter++;
- }
- else {
- counter = 1;
- }
- if (counter >= 4) {
- return board[row][column+row];
- }
- }
- }
- counter = 0;
- column = 0;
- row = 0;
- //Left to right
- for (row = 0; row < 7; row++) {
- for (column = 1; column < 7; column++) {
- if (column + row >= 7) {
- break;
- }
- if (board[row + column][column] != ' ' && board[row+column - 1][column - 1] == board[row + column][column]) {
- counter++;
- }
- else {
- counter = 1;
- }
- if (counter >= 4) {
- return board[row + column][column];
- }
- }
- }
- counter = 0;
- column = 0;
- row = 0;
- //Top right to bottom left
- for (column = 0; column < 7; column++) {
- for (row = 1; row < 7; row++) {
- if (column - row < 0) {
- break;
- }
- if (board[row][column-row] != ' ' && board[row - 1][column - row + 1] == board[row][column-row]) {
- counter++;
- }
- else {
- counter = 1;
- }
- if (counter >= 4) {
- return board[row][column-row];
- }
- }
- }
- counter = 0;
- column = 0;
- row = 0;
- //Left down diagonals
- for (row = 0; row < 7; row++) {
- for (column = 5; column >= 0; --column) {
- if (column - row < 0) {
- break;
- }
- if (board[column - row][column] != ' ' && board[column - row - 1][column + 1] == board[column - row][column]) {
- counter++;
- }
- else {
- counter = 1;
- }
- if (counter >= 4) {
- return board[column - row][column];
- }
- }
- }
- return ' ';
- //When we pass an empty char to the final method, it will take that to mean a draw
- }
- public static char winnerMasterMethod(char[][] board) {
- /**
- * Within this method I call 3 other methods established earlier
- * Each one checks a different kind of win condition
- * I check diagonals first because this is the most likely win type
- * The verts and horizontals
- *
- * We discussed that we can place brackets anywhere we choose in code
- * Brackets are used to help distinguish different win checks
- *
- */
- {
- char winner = winConCheckDiagonals(board);
- if (winner != ' ') return winner;
- }
- {
- char winner = winConCheckRows(board);
- if (winner != ' ') return winner;
- }
- {
- char winner = winConCheckColumns(board);
- if (winner != ' ') return winner;
- }
- //If all squares are filled, then we're done here (Draw)
- for (int i = 0; i < board.length; ++i)
- for (int j = 0; j < board[i].length; ++j)
- if (board[i][j] == ' ') return ' ';
- return 'D';
- }
- public static void resetBoard(char [][] board) {
- //Reset the board with pounds (#)
- for (int i = 0; i < 7; ++i) {
- for (int j = 0; j < 7; ++j) {
- board[i][j] = ' ';
- }
- }
- }
- public static void printBoard(char[][] board) {
- //Print out board systematically with formatting
- for (int row = 0; row < 7; row++) {
- System.out.print("| ");
- for (int col = 0; col < 7; ++col)
- System.out.print(board[row][col] + "| ");
- System.out.println();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement