Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ristinolla;
- import java.util.ArrayList;
- import javafx.application.Application;
- import javafx.stage.Stage;
- import javafx.scene.Scene;
- import javafx.scene.layout.BorderPane;
- import javafx.scene.layout.GridPane;
- import javafx.scene.control.Label;
- import javafx.scene.control.Button;
- import java.util.HashMap;
- import javafx.scene.text.Font;
- import javafx.geometry.Insets;
- public class RistinollaSovellus extends Application {
- @Override
- public void start(Stage ikkuna) {
- BorderPane asettelu = new BorderPane();
- GridPane ruudukko = new GridPane();
- String vuoro = "X";
- String[] taulukko = new String[6];
- taulukko[0] = "X";
- taulukko[1] = "0";
- taulukko[2] = "X";
- taulukko[3] = "0";
- taulukko[4] = "X";
- taulukko[5] = "0";
- int pis = 0;
- ArrayList<Risti> lista = new ArrayList<>();
- lista.add(new Risti(vuoro));
- String[][] tau = new String[20][20];
- Label tekstikentta = new Label("Vuoro: " + vuoro);
- tekstikentta.setFont(Font.font("Monospaced", 40));
- asettelu.setTop(tekstikentta);
- asettelu.setCenter(ruudukko);
- ruudukko.setHgap(10);
- ruudukko.setVgap(10);
- ruudukko.setPadding(new Insets(10));
- Button[][] napit = new Button[3][3];
- for (int y = 0; y < 3; y++) {
- for (int x = 0; x < 3; x++) {
- napit[y][x] = new Button(" ");
- napit[y][x].setFont(Font.font("Monospaced", 40));
- ruudukko.add(napit[y][x], x, y);
- }
- }
- for (int y = 0; y < 3; y++) {
- for (int x = 0; x < 3; x++) {
- int i = y;
- int j = x;
- int kak = pis + 1;
- napit[y][x].setOnAction((event) -> {
- if (lista.size() == 8) {
- tekstikentta.setText("Loppu");
- }
- if (tau[i][j] == null) {
- if (lista.size() % 2 == 0) {
- napit[i][j].setText("O");
- lista.add(new Risti(taulukko[kak]));
- tekstikentta.setText("Vuoro: X");
- tau[i][j] = "kak";
- if (lista.size() == 10) {
- tekstikentta.setText("Loppu!");
- }
- } else {
- napit[i][j].setText("X");
- lista.add(new Risti(taulukko[kak]));
- tekstikentta.setText("Vuoro: O");
- tau[i][j] = "kak";
- if (lista.size() == 10) {
- tekstikentta.setText("Loppu!");
- }
- }
- }
- });
- }
- }
- Scene nakyma = new Scene(asettelu);
- ikkuna.setScene(nakyma);
- ikkuna.show();
- }
- public static void main(String[] args) {
- launch(RistinollaSovellus.class);
- }
- }
- ################################
- Luokka vaihtuu
- ##################################
- package ristinolla;
- public class Risti {
- private String voro;
- public Risti(String voro) {
- this.voro = voro;
- }
- public void vaihda() {
- this.voro = "0";
- }
- public String getVoro() {
- return this.voro;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement