Skip to content

Instantly share code, notes, and snippets.

@AratioD
Last active September 17, 2016 10:52
Show Gist options
  • Save AratioD/81f31ad7acb6bbd99079f3869e54149d to your computer and use it in GitHub Desktop.
Save AratioD/81f31ad7acb6bbd99079f3869e54149d to your computer and use it in GitHub Desktop.
import java.util.*;
public class Taikaneliotehdas {
private int x;
private int y;
private Taikanelio nelio;
public Taikanelio luoTaikanelio(int koko) {
Taikanelio nelio = new Taikanelio(koko);
this.nelio = nelio;
// Create a random number generator
Random random = new Random();
this.x = 0;
this.y = 0;
if (koko % 2 == 1) {
for (int i = 0; i < nelio.getLeveys() * nelio.getKorkeus(); i++) {
int randomNumber = random.nextInt(100 - 1) + 1;
System.out.println(randomNumber);
// nelio.asetaArvo(0, 0, 1);
// nelio.asetaArvo(2, 2, 2);
System.out.println("i" + i);
if (i == 0) {
System.out.println("sdsd");
x = nelio.getLeveys() / 2;
y = i;
nelio.asetaArvo(x, y, 123);
System.out.println(nelio.toString());
}
if (i > 1) {
x++;
y++;
if (nelio.annaArvo(x, y) == 0) {
nelio.asetaArvo(x, y, randomNumber);
// break;
}
if (nelio.annaArvo(x, y) == -1 && x > y) {
x = 0;
nelio.asetaArvo(x, y, randomNumber);
//break;
}
if (nelio.annaArvo(x, y) == -1 && y > x) {
y = 0;
nelio.asetaArvo(x, y, randomNumber);
//break;
}
}
}
}
return nelio;
}
public boolean isTheSquareSlotReserved() {
if (nelio.annaArvo(x, y) > 0 && y <= nelio.getKorkeus() && x <= nelio.getLeveys()) {
return true;
}
return false;
}
}
******************************************************************************************************************************************************************************************************************************************************
Main
public class Main {
public static void main(String[] args) {
// Testaa Taikanelio-luokkaasi täällä
Taikaneliotehdas tt = new Taikaneliotehdas();
System.out.println(tt.luoTaikanelio(3));
}
}
*********************************************************************************************************************************************************************************************************
Luokka Taikanelio.java
import java.util.ArrayList;
import java.util.Collections;
import java.util.*;
public class Taikanelio {
private int[][] nelio;
// valmis konstruktori
public Taikanelio(int koko) {
if (koko < 2) {
koko = 2;
}
this.nelio = new int[koko][koko];
}
// toteuta nämä kolme metodia
public ArrayList<Integer> rivienSummat() {
ArrayList<Integer> sumArray = new ArrayList<>();
int sum = 0;
for (int row = 0; row < this.nelio.length; row++) {
for (int col = 0; col < this.nelio.length; col++) {
sum += this.nelio[row][col];
}
sumArray.add(sum);
sum = 0;
}
return sumArray;
}
public ArrayList<Integer> sarakkeidenSummat() {
ArrayList<Integer> sumArray = new ArrayList<>();
int sum = 0;
for (int row = 0; row < this.nelio.length; row++) {
for (int col = 0; col < this.nelio.length; col++) {
sum += this.nelio[col][row];
}
sumArray.add(sum);
sum = 0;
}
return sumArray;
}
public ArrayList<Integer> lavistajienSummat() {
ArrayList<Integer> sumArray = new ArrayList<>();
int sumRight = 0;
int sumLeft = 0;
for (int row = 0; row < this.nelio.length; row++) {
sumRight += this.nelio[row][row];
}
sumArray.add(sumRight);
int counter = 0;
for (int col = this.nelio.length - 1; col > -1; col--) {
sumLeft += this.nelio[counter][col];
counter++;
}
sumArray.add(sumLeft);
return sumArray;
}
// valmiit apumetodit -- älä koske näihin
public boolean onTaikanelio() {
return summatSamat() && kaikkiNumerotEri();
}
public ArrayList<Integer> annaKaikkiNumerot() {
ArrayList<Integer> numerot = new ArrayList<>();
for (int y = 0; y < nelio.length; y++) {
for (int x = 0; x < nelio[y].length; x++) {
numerot.add(nelio[y][x]);
}
}
return numerot;
}
public boolean kaikkiNumerotEri() {
ArrayList<Integer> numerot = annaKaikkiNumerot();
Collections.sort(numerot);
for (int i = 1; i < numerot.size(); i++) {
if (numerot.get(i - 1) == numerot.get(i)) {
return false;
}
}
return true;
}
public boolean summatSamat() {
ArrayList<Integer> summat = new ArrayList<>();
summat.addAll(rivienSummat());
summat.addAll(sarakkeidenSummat());
summat.addAll(lavistajienSummat());
if (summat.size() < 3) {
return false;
}
for (int i = 1; i < summat.size(); i++) {
if (summat.get(i - 1) != summat.get(i)) {
return false;
}
}
return true;
}
public int annaArvo(int x, int y) {
if (x < 0 || y < 0 || x >= getLeveys() || y >= getKorkeus()) {
return - 1;
}
return this.nelio[y][x];
}
public void asetaArvo(int x, int y, int arvo) {
if (x < 0 || y < 0 || x >= getLeveys() || y >= getKorkeus()) {
return;
}
this.nelio[y][x] = arvo;
}
public int getLeveys() {
return this.nelio.length;
}
public int getKorkeus() {
return this.nelio.length;
}
@Override
public String toString() {
String palautus = "";
for (int y = 0; y < nelio.length; y++) {
for (int x = 0; x < nelio[y].length; x++) {
palautus += nelio[y][x] + "\t";
}
palautus += "\n";
}
return palautus;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment