my code stock.com

Class Rainha

Snippet options

Download: Download snippet as eight-queens.java.
Copy snippet: For this you need a free my code stock.com account.
Embed code : You will find the embed code for this snippet at the end of the page, if you want to embed it into a website or a blog!

package Aula;
/* 
 This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

http://www.gnu.org/licenses/gpl.html
*/
public class Rainha {
    private int linha;
    private int coluna;
    private Rainha vizinha;
    private int maxl = 8;
    
    public Rainha(int c, Rainha q) {
        coluna = c;
        linha = 3;
        vizinha = q;
    }

    public boolean posicaoValida() {
    	
        while (vizinha != null && vizinha.posicaoValida(linha, coluna)) {
            if (!posicionar()) {
                return false;
            }
        }
        return true;
        
    }

    public boolean posicionar() {
    	
        if (linha < maxl) {
            linha=linha +1;
            return posicaoValida();
        }
        
        if (vizinha != null) {
        	
            if (!vizinha.posicionar()) {
                return false;
            }
            
            if (!vizinha.posicaoValida()) {
                return false;
            }
            
        } else {
        	
            return false;
        }
        
        linha = 1;
        return posicaoValida();
    }
    
    private boolean posicaoValida(int l, int c) {
        int col = c - coluna;
        if ((linha == l) || (linha + col == l) || (linha - col == l)) {
            return true;
        }
        if (vizinha != null) {
            return vizinha.posicaoValida(l, c);
        }
        return false;
    }
    
    public void imprime() {
        if (vizinha != null) {
            vizinha.imprime();
        }
        System.out.println(coluna+1+" "+linha);
    }
}

Create a free my code stock.com account now.

my code stok.com is a free service, which allows you to save and manage code snippes of any kind and programming language. We provide many advantages for your daily work with code-snippets, also for your teamwork. Give it a try!

Find out more and register now

You can customize the height of iFrame-Codes as needed! You can find more infos in our API Reference for iframe Embeds.