Advertisement
Guest User

NistiRolla

a guest
Apr 21st, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.53 KB | None | 0 0
  1. package ristinolla;
  2.  
  3. import java.util.ArrayList;
  4. import javafx.application.Application;
  5. import javafx.stage.Stage;
  6. import javafx.scene.Scene;
  7. import javafx.scene.layout.BorderPane;
  8. import javafx.scene.layout.GridPane;
  9. import javafx.scene.control.Label;
  10. import javafx.scene.control.Button;
  11. import java.util.HashMap;
  12. import javafx.scene.text.Font;
  13. import javafx.geometry.Insets;
  14.  
  15. public class RistinollaSovellus extends Application {
  16.  
  17. @Override
  18. public void start(Stage ikkuna) {
  19. BorderPane asettelu = new BorderPane();
  20. GridPane ruudukko = new GridPane();
  21. String vuoro = "X";
  22. String[] taulukko = new String[6];
  23. taulukko[0] = "X";
  24. taulukko[1] = "0";
  25. taulukko[2] = "X";
  26. taulukko[3] = "0";
  27. taulukko[4] = "X";
  28. taulukko[5] = "0";
  29. int pis = 0;
  30. ArrayList<Risti> lista = new ArrayList<>();
  31. lista.add(new Risti(vuoro));
  32. String[][] tau = new String[20][20];
  33.  
  34. Label tekstikentta = new Label("Vuoro: " + vuoro);
  35. tekstikentta.setFont(Font.font("Monospaced", 40));
  36.  
  37. asettelu.setTop(tekstikentta);
  38. asettelu.setCenter(ruudukko);
  39. ruudukko.setHgap(10);
  40. ruudukko.setVgap(10);
  41. ruudukko.setPadding(new Insets(10));
  42.  
  43. Button[][] napit = new Button[3][3];
  44. for (int y = 0; y < 3; y++) {
  45. for (int x = 0; x < 3; x++) {
  46. napit[y][x] = new Button(" ");
  47. napit[y][x].setFont(Font.font("Monospaced", 40));
  48.  
  49. ruudukko.add(napit[y][x], x, y);
  50.  
  51. }
  52. }
  53.  
  54. for (int y = 0; y < 3; y++) {
  55. for (int x = 0; x < 3; x++) {
  56. int i = y;
  57. int j = x;
  58. int kak = pis + 1;
  59. napit[y][x].setOnAction((event) -> {
  60. if (lista.size() == 8) {
  61. tekstikentta.setText("Loppu");
  62.  
  63. }
  64. if (tau[i][j] == null) {
  65.  
  66. if (lista.size() % 2 == 0) {
  67. napit[i][j].setText("O");
  68. lista.add(new Risti(taulukko[kak]));
  69. tekstikentta.setText("Vuoro: X");
  70. tau[i][j] = "kak";
  71. if (lista.size() == 10) {
  72. tekstikentta.setText("Loppu!");
  73.  
  74. }
  75.  
  76. } else {
  77.  
  78. napit[i][j].setText("X");
  79. lista.add(new Risti(taulukko[kak]));
  80. tekstikentta.setText("Vuoro: O");
  81. tau[i][j] = "kak";
  82. if (lista.size() == 10) {
  83. tekstikentta.setText("Loppu!");
  84.  
  85. }
  86. }
  87. }
  88.  
  89. });
  90.  
  91. }
  92.  
  93. }
  94. Scene nakyma = new Scene(asettelu);
  95.  
  96. ikkuna.setScene(nakyma);
  97. ikkuna.show();
  98. }
  99.  
  100. public static void main(String[] args) {
  101. launch(RistinollaSovellus.class);
  102. }
  103.  
  104. }
  105.  
  106.  
  107.  
  108.  
  109. ################################
  110. Luokka vaihtuu
  111. ##################################
  112.  
  113.  
  114.  
  115.  
  116.  
  117. package ristinolla;
  118.  
  119.  
  120. public class Risti {
  121.  
  122. private String voro;
  123.  
  124. public Risti(String voro) {
  125. this.voro = voro;
  126. }
  127.  
  128. public void vaihda() {
  129. this.voro = "0";
  130. }
  131.  
  132. public String getVoro() {
  133. return this.voro;
  134. }
  135.  
  136.  
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement