Advertisement
strikero

finished

Nov 7th, 2017
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.92 KB | None | 0 0
  1. package magicsquare;
  2.  
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5. import java.awt.event.MouseEvent;
  6. import java.util.Scanner;
  7.  
  8. import javax.swing.*;
  9. public class Magicsquare {
  10. public static void main(String[] args) {
  11. gui g = new gui();
  12. JFrame f = new JFrame("Magic Square");
  13. JLabel label = new JLabel("");
  14. JLabel label2 = new JLabel("");
  15. JButton setbutton = new JButton("Set");
  16. JTextField button00 = new JTextField("");
  17. JTextField button01 = new JTextField("");
  18. JTextField button02 = new JTextField("");
  19. JTextField button10 = new JTextField("");
  20. JTextField button11 = new JTextField("");
  21. JTextField button12 = new JTextField("");
  22. JTextField button20 = new JTextField("");
  23. JTextField button21 = new JTextField("");
  24. JTextField button22 = new JTextField("");
  25. setbutton.setBounds(200,400,100,50);
  26. button00.setBounds(100, 100, 50, 50);
  27. label.setBounds(400,100,150,50);
  28. label2.setBounds(400,200,150,50);
  29. button01.setBounds(200,100,50,50);
  30. button02.setBounds(300,100,50,50);
  31. button10.setBounds(100,200,50,50);
  32. button11.setBounds(200,200,50,50);
  33. button12.setBounds(300,200,50,50);
  34. button20.setBounds(100, 300, 50, 50);
  35. button21.setBounds(200, 300, 50, 50);
  36. button22.setBounds(300, 300, 50, 50);
  37. label.setVisible(true);
  38. label2.setVisible(true);
  39. f.add(label);
  40. f.add(label2);
  41. f.add(setbutton);
  42. f.add(button00);
  43. f.add(button01);
  44. f.add(button02);
  45. f.add(button20);
  46. f.add(button21);
  47. f.add(button22);
  48. f.add(button10);
  49. f.add(button11);
  50. f.add(button12);
  51.  
  52.  
  53. f.setSize(650,550);
  54. f.setLayout(null);
  55. f.setVisible(true);
  56.  
  57.  
  58.  
  59.  
  60. setbutton.addActionListener(new ActionListener() {
  61. @Override public void actionPerformed(ActionEvent e) {
  62. String bb00 = "1";
  63. String bb01 = "2";
  64. String bb02 = "3";
  65. String bb10 = "4";
  66. String bb11 = "5";
  67. String bb12 = "6";
  68. String bb20 = "7";
  69. String bb21 = "8";
  70. String bb22 = "9";
  71. bb00 = button00.getText();
  72. bb01 = button01.getText();
  73. bb02 = button02.getText();
  74. bb10 = button10.getText();
  75. bb11 = button11.getText();
  76. bb12 = button12.getText();
  77. bb20 = button20.getText();
  78. bb21 = button21.getText();
  79. bb22 = button22.getText();
  80. int b00 = Integer.parseInt(bb00);
  81. int b01 = Integer.parseInt(bb01);
  82. int b02 = Integer.parseInt(bb02);
  83. int b10 = Integer.parseInt(bb10);
  84. int b11 = Integer.parseInt(bb11);
  85. int b12 = Integer.parseInt(bb12);
  86. int b20 = Integer.parseInt(bb20);
  87. int b21 = Integer.parseInt(bb21);
  88. int b22 = Integer.parseInt(bb22);
  89. g.setsquare(0, 0, b00);
  90. g.setsquare(0, 1, b01);
  91. g.setsquare(0, 2, b02);
  92. g.setsquare(1, 0, b10);
  93. g.setsquare(1, 1, b11);
  94. g.setsquare(1, 2, b12);
  95. g.setsquare(2, 0, b20);
  96. g.setsquare(2, 1, b21);
  97. g.setsquare(2, 2, b22);
  98. int check,check2;
  99. check = g.checksquare();
  100. if(check == 1){
  101. label.setText("It is a magic Square");
  102. }
  103. else{
  104. label.setText("It is not a magic Square");
  105. }
  106. check2 = g.checkrepeat();
  107. if(check2==1){
  108. label2.setText("There was a number repeated");
  109. }
  110. }
  111. });
  112. }
  113.  
  114.  
  115. }
  116.  
  117. class gui{
  118. int multivalue[][] = {{1,2,3},{4,5,6},{7,8,9}};
  119. Magicsquare m = new Magicsquare();
  120. int interval = 0;
  121. public void setsquare(int row, int col, int value){
  122. multivalue[row][col] = value;
  123. interval ++;
  124. }
  125.  
  126. public int checksquare(){
  127. int k;
  128. int j;
  129. int i = 0;
  130. int sum = 15;
  131. int sum1 = 15;
  132. int sum2 = 0;
  133. int sum3 = 0;
  134. int checkk = 0;
  135. int fail = 0;
  136. int success = 0;
  137. boolean checking = false;
  138. for(k = 0; k<3;k++){
  139. if(sum!=15){
  140. break;
  141. }
  142. sum = 0;
  143. for(j=0;j<3;j++){
  144. sum = sum+multivalue[k][j];
  145. }
  146. }
  147. for(k = 0; k<3;k++){
  148. if(sum1!=15){
  149. break;
  150. }
  151. sum1 = 0;
  152. for(j=0;j<3;j++){
  153. sum1 = sum1+multivalue[j][k];
  154. }
  155. }
  156. for(k=0; k<3;k++){
  157. sum2 = sum2+multivalue[k][k];
  158. }
  159. for(j=2; j>=0;j--){
  160. sum3 = sum3+multivalue[j][j];
  161. }
  162. if(sum == 15 && sum1 == 15 && sum2 == 15 && sum3 == 15){
  163. checking = true;
  164. System.out.println("won");
  165. }
  166. if(checking==true){
  167. return 1;
  168. }
  169. else{
  170. return 2;
  171. }
  172. }
  173. public int checkrepeat(){
  174. for(int i = 0; i < 3; i++){
  175. for(int x = 0 ; x< 3; x++){
  176. for(int y = 0 ; y<3; y++){
  177. for(int z = 0; z<3;z++){
  178. if(multivalue[i][x] == multivalue[y][z]){
  179. if(i==y && x == z){
  180. System.out.println("do nothing");
  181. }
  182. else{
  183. return 1;
  184. }
  185. }
  186.  
  187. }
  188. }
  189. }
  190. }
  191. return 0;
  192. }
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement