Advertisement
brilliant_moves

KnightProject.java

Jun 1st, 2015
487
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 2.30 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class KnightProject {
  4.  
  5.     /**
  6.     *   Program:    KnightProject.java
  7.     *   Purpose:    Knight move problem for Yahoo! Answers
  8.     *   Creator:    Chris Clarke
  9.     *   Ceated:     01.06.2015
  10.     */
  11.  
  12.     /*
  13.  
  14.     Print out a board with the size entered by user and the knight starting position entered by user.
  15.     Then move the knight 3 steps from it's starting position.
  16.     The knight's move should be labelled as 0(starting position), 1, 2, etc.
  17.  
  18.     */
  19.  
  20.     public static int size;
  21.     public static int currRow, currCol;
  22.     public static int moveCount = 0;
  23.     public static int[][] board;
  24.  
  25.     public static boolean canGo(int r, int c) {
  26.         if (r>=1 && r<=size && c>=1 && c<=size)
  27.             return true;
  28.  
  29.         return false;
  30.     }
  31.  
  32.     public static void makeMove(int r, int c) {
  33.         board[r][c] = ++moveCount;
  34.         currRow = r;
  35.         currCol = c;
  36.     }
  37.  
  38.     public static void displayBoard() {
  39.         for (int r=1; r<=size; r++) {
  40.             for (int c=1; c<=size; c++) {
  41.                 if (board[r][c] == -1) {
  42.                     System.out.print(".");
  43.                 } else {
  44.                     System.out.print(board[r][c]);
  45.                 } // if
  46.             } // for c
  47.             System.out.println();
  48.         } // for r
  49.         System.out.println();
  50.     }
  51.  
  52.     public static void chooseMove(int r, int c) {
  53.     // there are up to 8 possible knight moves
  54.         if (canGo(r-2, c-1)) makeMove(r-2, c-1);    // 1
  55.         else if (canGo(r-2,c+1)) makeMove(r-2, c+1);    // 2
  56.         else if (canGo(r-1,c+2)) makeMove(r-1, c+2);    // 3
  57.         else if (canGo(r+1,c+2)) makeMove(r+1,c+2); // 4
  58.         else if (canGo(r+2,c+1)) makeMove(r+2,c+1); // 5
  59.         else if (canGo(r+2,c-1)) makeMove(r+2,c-1); // 6
  60.         else if (canGo(r+1,c-2)) makeMove(r+1,c-2); // 7
  61.         else if (canGo(r-1,c-2)) makeMove(r-1,c-2); // 8
  62.     }
  63.  
  64.     public static void main(String[] args) {
  65.         Scanner scan = new Scanner(System.in);
  66.         System.out.print("What size board would you like? ");
  67.         size = scan.nextInt();
  68.         board = new int[size+1][size+1];
  69.         for (int r=1; r<=size; r++) {
  70.             for (int c=1; c<=size; c++) {
  71.                 board[r][c] = -1;
  72.             } // for c
  73.         } // for r
  74.  
  75.         System.out.print("Place knight on which row (1-"+size+")? ");
  76.         currRow = scan.nextInt();
  77.         System.out.print("Place knight on which column (1-"+size+")? ");
  78.         currCol = scan.nextInt();
  79.  
  80.         board[currRow][currCol] = 0;
  81.         for (int i=0; i<3; i++) {
  82.             chooseMove(currRow, currCol);
  83.             displayBoard();
  84.         } // for
  85.     } // main()
  86.  
  87. } // class KnightProject
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement