Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class board {
- String[][] board= new String[8][8];
- String[][] whiteLocation = new String[8][8];
- String[][] blackLocation = new String[8][8]; //Have first hold row, second holds column
- Scanner input = new Scanner(System.in);
- boolean valid;
- String lastColourLanded;
- public board(){
- setup();
- lastColourLanded = null;
- whiteLocation[7][0]="OR ";
- whiteLocation[7][1]="RE ";
- whiteLocation[7][2]="GR ";
- whiteLocation[7][3]="PI ";
- whiteLocation[7][4]="YE ";
- whiteLocation[7][5]="BL ";
- whiteLocation[7][6]="PU ";
- whiteLocation[7][7]="BR ";
- blackLocation[0][0]="BR ";
- blackLocation[0][1]="PU ";
- blackLocation[0][2]="BL ";
- blackLocation[0][3]="YE ";
- blackLocation[0][4]="PI ";
- blackLocation[0][5]="GR ";
- blackLocation[0][6]="RE ";
- blackLocation[0][7]="OR ";
- }
- public void BoardLayout(){
- board[0][0]="BROWN"; board[0][1]="PURPL"; board[0][2]= "BLUE "; board[0][3]="YELLO"; board[0][4]="PINK "; board[0][5]= "GREEN"; board[0][6]= "RED "; board[0][7]="ORANG";
- board[1][0]="GREEN"; board[1][1]="BROWN"; board[1][2]= "YELLO"; board[1][3]="RED "; board[1][4]="PURPL"; board[1][5]= "PINK "; board[1][6]= "ORANG"; board[1][7]="BLUE ";
- board[2][0]="RED "; board[2][1]="YELLO"; board[2][2]= "BROWN"; board[2][3]="GREEN"; board[2][4]="BLUE "; board[2][5]= "ORANG"; board[2][6]= "PINK "; board[2][7]="PURPL";
- board[3][0]="YELLO"; board[3][1]="BLUE "; board[3][2]= "PURPL"; board[3][3]="BROWN"; board[3][4]="ORANG"; board[3][5]= "RED "; board[3][6]= "GREEN"; board[3][7]="PINK ";
- board[4][0]="PINK "; board[4][1]="GREEN"; board[4][2]= "RED "; board[4][3]="ORANG"; board[4][4]="BROWN"; board[4][5]= "PURPL"; board[4][6]= "BLUE "; board[4][7]="YELLO";
- board[5][0]="PURPL"; board[5][1]="PINK "; board[5][2]= "ORANG"; board[5][3]="BLUE "; board[5][4]="GREEN"; board[5][5]= "BROWN"; board[5][6]= "YELLO"; board[5][7]="RED ";
- board[6][0]="BLUE "; board[6][1]="ORANG"; board[6][2]= "PINK "; board[6][3]="PURPL"; board[6][4]="RED "; board[6][5]= "YELLO"; board[6][6]= "BROWN"; board[6][7]="GREEN";
- board[7][0]="ORANG"; board[7][1]="RED "; board[7][2]= "GREEN"; board[7][3]="PINK "; board[7][4]="YELLO"; board[7][5]= "BLUE "; board[7][6]= "PURPL"; board[7][7]="BROWN";
- }
- public String[][] getBlackLoc(){return blackLocation;}
- public String[][] getWhiteLoc(){return whiteLocation;}
- public void setup(){
- BoardLayout();
- }
- public void forceUndo(String[][] lastWhiteLocation,String[][] lastBlackLocation,String lastColour){
- // Forces the gameboard to update and undo the last moves alongside bypassing the verification
- undoBlack(lastBlackLocation);
- undoWhite(lastWhiteLocation);
- undoColourLandedOn(lastColour);
- }
- public String[][] undoBlack(String[][] lastBlackLocation){
- for (int x=0;x<8;x++){
- for(int y=0;y<8;y++){
- blackLocation[x][y]=lastBlackLocation[x][y];
- }
- }
- return blackLocation;
- }
- public String[][] undoWhite(String[][] lastWhiteLocation){
- for (int x=0;x<8;x++){
- for(int y=0;y<8;y++){
- whiteLocation[x][y]=lastWhiteLocation[x][y];
- }
- }
- return whiteLocation;
- }
- public String undoColourLandedOn(String lastColour){
- lastColourLanded=lastColour;
- return lastColourLanded;
- }
- public void prntboard(){
- System.out.println(); // prints out the board row by row, replaces board squares with a tower token if one is present
- for (int p= 0;p<8;p++)
- System.out.print(" "+ p);
- System.out.println();
- for (int x=0 ;x<8;x++){
- System.out.println();
- System.out.println();
- System.out.print(x +" - |");
- for (int y=0;y<8;y++){
- if(whiteLocation[x][y]!=null){
- System.out.print("O-"+whiteLocation[x][y]+"|");}
- else if(blackLocation[x][y]!=null){
- System.out.print("X-"+blackLocation[x][y] +"|");}
- else{
- System.out.print(board[x][y]+" |");
- }
- }
- }
- }
- public String[][] getControlInput(String row1, String row2, String column1, String column2,int contPlayer){
- int startRow = Integer.parseInt(row1); // converts the row and column strings given to the class into ints to be used as array indices
- int startCol = Integer.parseInt(column1);
- int destRow= Integer.parseInt(row2);
- int destCol= Integer.parseInt(column2);
- //lastColour(startRow,startCol,destRow,destCol,contPlayer);
- update(startRow,startCol,destRow,destCol,contPlayer,whiteLocation,blackLocation); //calls update to update the gameboard
- if (contPlayer == 1){return whiteLocation; }
- if (contPlayer ==2){return blackLocation;}
- return whiteLocation;
- }
- public String returnLastColour(){
- return lastColourLanded;
- }
- /* public String lastColour(int startRow,int startCol,int destRow,int destCol,int contPlayer){
- if(lastColourLanded==null && checkValid(startRow, startCol,destRow,destCol,contPlayer) ){
- return lastColourLanded;
- }/*else{
- if(checkValid(startRow, startCol,destRow,destCol,contPlayer)){lastColourLanded=board[destRow][destCol]; }
- }
- return lastColourLanded;
- }
- */
- public String updateLastColour(int destRow,int destCol){
- lastColourLanded=board[destRow][destCol];
- return lastColourLanded;
- }
- public String[][] update(int startRow,int startCol,int destRow,int destCol,int contPlayer,String[][] whiteLocation,String[][] blackLocation){
- if(contPlayer==1){ //if its the white player's move
- if(whiteLocation[startRow][startCol]==null){
- System.out.println("You have selected the wrong token or not a valid location you are white player (o)");
- valid=false;}
- if(whiteLocation[startRow][startCol]!=null){
- checkValid(startRow, startCol,destRow,destCol,contPlayer); // checks if move is valid
- }
- if (!valid){
- System.out.println();
- prntboard();
- System.out.println();
- System.out.println("Invalid Move");
- System.out.println("Remember you must move the " + lastColourLanded +"Coloured token." );
- System.out.println("Select token to move to perform a valid move, (Row then column, with no space) or e to exit: ");
- String selected = input.next();
- if (selected.equals("e")){
- System.exit(0);
- }else{
- String[] parts = {selected.substring(0,1),selected.substring(1) };
- String row1 = parts[0];
- String column1 = parts[1];
- System.out.println("Select space to move to or e to exit: ");
- String dest = input.next();
- if (dest.equals("e")){
- System.exit(0);
- }
- String[] destParts= {dest.substring(0,1),dest.substring(1) };
- String row2 = destParts[0];
- String column2 = destParts[1];
- getControlInput(row1,row2,column1,column2,contPlayer);
- System.out.println();
- contPlayer=2;
- }
- }
- if (valid){
- String temp = whiteLocation[startRow][startCol];
- whiteLocation[destRow][destCol]= temp;
- whiteLocation[startRow][startCol]=null;
- System.out.println();
- updateLastColour(destRow,destCol);
- if(destRow==0){System.out.println("GAME OVER WHITE WINS!");System.exit(0);}
- prntboard();
- }
- return whiteLocation;
- }
- if(contPlayer==2){
- checkValid(startRow, startCol,destRow,destCol,contPlayer);
- if(blackLocation[startRow][startCol]==null){
- System.out.println("You have selected the wrong token or not a valid location you are black player (x)");
- valid=false;}
- if (!valid){
- System.out.println();
- prntboard();
- System.out.println();
- System.out.println("Invalid Move");
- System.out.println("Select token to move to make a valid move, (Row then column, with no space) or e to exit: ");
- String selected = input.next();
- if (selected.equals("e")){
- System.exit(0);
- }else{
- String[] parts = {selected.substring(0,1),selected.substring(1) };
- String row1 = parts[0];
- String column1 = parts[1];
- System.out.println("Select space to move to or e to exit: ");
- String dest = input.next();
- if (dest.equals("e")){
- System.exit(0);
- }
- String[] destParts= {dest.substring(0,1),dest.substring(1) };
- String row2 = destParts[0];
- String column2 = destParts[1];
- getControlInput(row1,row2,column1,column2,contPlayer);
- }}
- if (valid){
- //System.out.println(valid);
- String temp = blackLocation[startRow][startCol];
- blackLocation[destRow][destCol]= temp;
- blackLocation[startRow][startCol]=null;
- updateLastColour(destRow,destCol);
- if(destRow==7){System.out.println("GAME OVER BLACK WINS!");System.exit(0);}
- prntboard();
- return blackLocation;
- }
- }
- return whiteLocation;
- }
- public boolean checkValid(int startRow,int startCol,int destRow,int destCol,int controller){
- valid=true;
- /*
- if(controller ==1){
- if(!lastColourLanded.contains(whiteLocation[startRow][startCol])){
- valid=false;
- return valid;
- }
- if(lastColourLanded.contains(whiteLocation[startRow][startCol])){
- lastColourLanded=board[destRow][destCol];
- valid=true;
- return valid;
- }
- }
- if(controller ==2){
- if(!lastColourLanded.contains(blackLocation[startRow][startCol])){
- valid=false;
- return valid;
- }
- if(lastColourLanded.contains(blackLocation[startRow][startCol])){
- lastColourLanded=board[destRow][destCol];
- valid=true;
- return valid;
- }
- }
- */
- if(controller == 1){ //Checks if white player is moving
- String tokenColour=whiteLocation[startRow][startCol].substring(0,2);
- if(lastColourLanded==null){valid=true;}
- else{
- if(!lastColourLanded.contains(tokenColour)&& lastColourLanded!=null){
- valid=false;
- return valid;
- }
- }
- if(startRow<destRow){
- valid=false;
- return valid;}
- int temp = Math.abs(destRow-startRow);
- int temp2 = Math.abs(destCol-startCol); //checks its not moving backwards and only moves forward or diagonally
- /*int lowestVal;
- int highestVal;
- if(startCol>=destCol){lowestVal=destCol; highestVal=startCol;}
- else{ lowestVal=startCol; highestVal = destCol;}
- */
- if ( startCol!=destCol && temp != temp2){
- valid=false;
- return valid;}
- if(startCol>destCol){ //checks diagonally left
- for (int x=startRow-1;x<=destRow;x--){
- int y=startCol-1;
- if(whiteLocation[x][y]!=null || blackLocation[x][y]!=null){
- valid=false;
- return valid;}
- y--;
- }
- }
- if(startCol<destCol){
- for (int x=startRow-1;x<=destRow;x--){
- for (int y=startCol+1;y<=destCol;y++){
- if(whiteLocation[x][y]!=null || blackLocation[x][y]!=null){
- valid=false;
- return valid;}
- }
- }
- }
- // the above checks that there is no towers in the path.
- if(startCol==destCol){
- for(int x=startRow-1;x<=destRow;x--){
- if(whiteLocation[x][startCol]!=null || blackLocation[x][startCol]!=null){
- valid=false;
- return valid;}
- }
- }
- if(blackLocation[destRow][destCol]!=null || whiteLocation[destRow][destCol]!=null){
- valid=false;
- return valid;
- }
- if(startRow == destRow && startCol == destCol){
- valid=false;
- return valid;}
- }
- if(controller==2){
- String tokenColour="";
- if(blackLocation[startRow][startCol]!=null){
- tokenColour=blackLocation[startRow][startCol].substring(0,2);}
- if(lastColourLanded==null){valid=true;}
- else{
- boolean contains =lastColourLanded.contains(tokenColour);
- if(!contains&& lastColourLanded!=null){
- valid=false;
- return valid;
- } }
- if(startRow>destRow){valid=false; return valid;}
- int temp = Math.abs(destRow-startRow);
- int temp2 = Math.abs(destCol-startCol);
- /* int lowestVal;
- int highestVal;
- if(startCol>destCol){lowestVal=destCol; highestVal=startCol;}
- else{ lowestVal=startCol; highestVal = destCol;}
- for (int x=startRow+1;x<=destRow;x++){
- for (int y=lowestVal;y<=highestVal;y++){
- if(whiteLocation[x][y]!=null && blackLocation[x][y]!=null){
- valid=false;
- return valid;}
- }
- } */
- if(startCol!=destCol && temp != temp2){valid=false; return valid;}
- if(startRow == destRow && startCol == destCol){
- valid=false;
- return valid;}
- if(blackLocation[destRow][destCol]!=null || whiteLocation[destRow][destCol]!=null){
- valid=false;
- return valid;
- }
- if(startCol<destCol){ //checks diagonally right for things in path
- for (int x=startRow+1;x<=destRow;x++){
- int y=startCol+1;
- if(whiteLocation[x][y]!=null || blackLocation[x][y]!=null){
- valid=false;
- return valid;}
- y++;
- }
- }
- if(startCol>destCol){ //checks diag left
- for (int x=startRow+1;x<=destRow;x++){
- int y=startCol-1;
- if(whiteLocation[x][y]!=null || blackLocation[x][y]!=null){
- valid=false;
- return valid;}
- y--;
- }
- }
- if(startCol==destCol){
- for(int x=startRow+1;x<=destRow;x++){
- if(whiteLocation[x][startCol]!=null || blackLocation[x][startCol]!=null){
- valid=false;
- return valid;}
- }
- }
- }
- updateLastColour(destRow,destCol);
- valid =true;
- return valid;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement