Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

hw8 place queens

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Labib
 */
import java.util.Scanner;
public class HW8_PlaceQueens {

    
    public static void main(String[] args) {
        Scanner kb = new Scanner(System.in);
        String option = "start";
        while (!option.equals("q"))
        {
            System.out.println("q - quit");
            System.out.println("r - reset");
            System.out.println("m - make");
            System.out.println("a - add queen");
            System.out.println("d - delete queen");
            System.out.println("c - count queen");
            System.out.println("p - print board\n");
            System.out.print("Enter option: ");
            option = kb.next().toLowerCase();
            int[][] userboard = null;
            if (option.equals("r"))
            {
                System.out.print("Enter board size ");
                int boardSize = kb.nextInt();
                PrintBoard(MakeBoard(boardSize));
            }
            if (option.equals("m"))
            {
                System.out.print("Enter board size ");
                int boardSize = kb.nextInt();
                userboard = MakeBoard(boardSize);
                PrintBoard(userboard);
            }
            else if (option.equals("a"))
            {
                System.out.print("Enter row and column (1-N): ");
                int userRow = kb.nextInt();
                int userCol = kb.nextInt();
                userboard = AddQueen(userRow, userCol, userboard);
                PrintBoard(userboard);
            }
        }
        
        
    }
    
    public static int[][] MakeBoard(int size)
    {
        int[][] res = new int[size][size];
        return res;
    }
    
    public static int[][] AddQueen(int x, int y, int[][] board)
    {
        int xPos = x - 1;
        int yPos = y - 1;
        board[xPos][yPos] = 1;  
        int row, col;
        for (row = 0; row < board.length; row++) {
            for (col = 0; col < board[row].length; col++) {
                if (row == xPos)
                {
                    board[row][col] = 2;
                }
                if (col == yPos)
                {
                    board[row][col] = 2;
                }
            }       
        }
        board[xPos][yPos] = 1;
        return board;
    }
    
    public static void PrintBoard(int[][] table) {
        int row, col;
        for ( int i = 0; i <= table.length; i ++)
        {
            System.out.printf("%3d|", i);
        }
        System.out.println();
        System.out.println(DottedLine(table.length));
        for (row = 0; row < table.length; row++) {
            System.out.printf("%3d|", row + 1);
            for (col = 0; col < table[row].length; col++) {
                
                if (table[row][col] == 0)
                {
                    System.out.print("   |");
                }
                else if (table[row][col] == 1)
                {
                    System.out.print(" Q |");
                }
                else 
                {
                    System.out.print(" * |");
                }
            }
            System.out.println();
            System.out.println(DottedLine(table[row].length));
            
        }
    }
    
    public static String DottedLine(int n)
    {
        String res = "";
        for (int i = 0; i <= n; i++ )
        {
            res += "----";
        }
        return res;    
    }
            
    
}

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.