Skip to content

Instantly share code, notes, and snippets.

@Eldres
Last active August 29, 2015 13:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Eldres/57b13310bee98f33b722 to your computer and use it in GitHub Desktop.
Save Eldres/57b13310bee98f33b722 to your computer and use it in GitHub Desktop.
CSC 210 HW8_Chess Board
package hw8_chessboard;
import java.util.Scanner;
/**
*
* @author Josh
*/
public class HW8_ChessBoard {
public static void main(String[] args) {
String board[][] = new String[8][8];
String choice;
String selection = "";
String playAgain;
boolean isValid = false;
int currentLocX[] = null;
int currentLocY[] = null;
int targetLocX[] = null;
int targetLocY[] = null;
Scanner input = new Scanner(System.in);
System.out.println("White squares are represented by a '.', while black squares are shown with a '*'.");
/* step 2
prompts the user to choose the color they want to play as, black or white
*/
System.out.print("Please select which color you would like to play as. W or B: ");
do {
choice = input.next();
if ((choice.equals("W") || (choice.equals("w")))) {
isValid = true;
break;
} else if ((choice.equals("B")) || (choice.equals("b"))) {
isValid = true;
break;
} else {
System.out.println("Not a valid choice, try again.");
isValid = false;
}
} while (isValid = true);
if ((choice.equals("W") || (choice.equals("w")))) {
selection = "White";
} else if ((choice.equals("B")) || (choice.equals("b"))) {
selection = "Black";
}
do { //part of step 7
/* step 1
initializes chess board
*/
System.out.println("You are currently playing as " + selection + ".");
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[i].length; j++) {
if ((i + j) % 2 == 0) {
System.out.print("*");
} else {
System.out.print(".");
}
}
System.out.print("\n");
}
/* step 4
gets the current location and the desired location from the user
*/
System.out.print("Enter your starting row: ");
for (int i = 0; i < currentLocX.length; i++) {
currentLocX[i] = input.nextInt();
}
System.out.print("Enter your starting column: ");
for (int i = 0; i < currentLocY.length; i++) {
currentLocY[i] = input.nextInt();
}
System.out.print("Enter your desired row: ");
for(int i = 0; i < targetLocX.length; i++)
{
targetLocX[i] = input.nextInt();
}
System.out.print("Enter your desired column: ");
for(int i = 0; i < targetLocY.length;i++)
{
targetLocY[i] = input.nextInt();
}
/* step 5a
checks whether there is a piece at the current location
*/
/* step 5b
check whether the target location is empty
*/
/* step 5c
if valid moves then inform user of such, and display the updated board
*/
/* step 6
switches the color of user, then asks the user to make a move for opposite color
*/
/* step 7
prompts the user to play again
*/
System.out.print("would you like to play again?(Y or N): ");
playAgain = input.next();
} while ((playAgain.equals("Y")) || (playAgain.equals("y")));
}// end of program
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment