Guest User

Untitled

a guest
Jul 19th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.77 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.*;//.ActionListener;
  3. import java.awt.event.AdjustmentListener;
  4. import java.util.Vector;
  5. import javax.swing.*;
  6.  
  7.  
  8.  
  9. class GameBeta extends JFrame{
  10. static int field[][]=new int[3][3];
  11. static Vector<JButton> bf=new Vector<JButton>();
  12. static boolean turn;
  13. static int gameStatus; //0 - on game, 1 - 1st player win, 2- 2nd player win, 3d- draw
  14.  
  15. private void makeTurn(int x, int y){
  16. if (gameStatus==0){
  17. if (field[y][x]==0){
  18. if (turn==true){
  19. field[y][x]=1;
  20. }else{
  21. field[y][x]=2;
  22. }
  23. }
  24. }
  25. }
  26.  
  27.  
  28. public GameBeta(){
  29. getContentPane().setLayout(new GridLayout(4,3, 10, 10));
  30. for(int k=0; k<9; k++) {
  31. bf.add(new JButton(""));
  32. getContentPane().add(bf.get(k));
  33. }
  34. bf.add(new JButton("restart"));
  35. getContentPane().add(bf.get(9));
  36. setBounds(100,100,300,300);
  37. for (int i=0; i<3; i++){
  38. for (int j=0; j<3; j++)
  39. field[j][i]=0;
  40. }
  41. gameStatus=0;
  42. turn=true;
  43. final JLabel gs=new JLabel("");
  44. getContentPane().add(gs);
  45.  
  46. bf.get(9).addActionListener(new ActionListener(){
  47. public void actionPerformed(ActionEvent e){
  48. for (int i=0; i<3; i++){
  49. for (int j=0; j<3; j++)
  50. field[j][i]=0;
  51. }
  52. for (int i=0; i<9; i++)
  53. bf.get(i).setText("");
  54. gs.setText("");
  55. gameStatus=0;
  56. turn=true;
  57. }
  58. });
  59. for (int i=0; i<9; i++){
  60. final int k=i;
  61. bf.get(i).addActionListener(new ActionListener(){
  62. public void actionPerformed(ActionEvent e){
  63. if (gameStatus==0){
  64. if (turn){
  65. field[k/3][k%3]=1;
  66. bf.get(k).setText("x");
  67. }else{
  68. field[k/3][k%3]=2;
  69. bf.get(k).setText("0");
  70. }
  71. boolean temp=!turn;
  72. turn=temp;
  73. calcGameStatus();
  74. if (gameStatus==1)
  75. gs.setText("1st player win");
  76. if (gameStatus==2)
  77. gs.setText("2st player win");
  78. if (gameStatus==3)
  79. gs.setText("draw");
  80. }
  81. }
  82. });
  83. }
  84. }
  85.  
  86. private static int calcGameStatus(){
  87. boolean b=false;
  88. for (int i=0; i<3; i++){
  89. if ( (field[i][1]!=0)&&(field[i][0]==field[i][1])&&(field[i][1]==field[i][2]) ){
  90. gameStatus=field[i][1];
  91. return gameStatus;
  92. }
  93. if ( (field[1][i]!=0)&&(field[0][i]==field[1][i])&&(field[1][i]==field[2][i]) ){
  94. gameStatus=field[1][i];
  95. return gameStatus;
  96. }
  97. }
  98. if ( (field[1][1]!=0) ){
  99. if ( ((field[0][0]==field[1][1])&&(field[1][1]==field[2][2])) || ((field[2][0]==field[1][1])&&(field[0][2]==field[1][1])) ){
  100. gameStatus=field[1][1];
  101. return gameStatus;
  102. }
  103. }
  104.  
  105. return 0;
  106. }
  107.  
  108. public static void main(String[] args){
  109. GameBeta flt = new GameBeta();
  110. flt.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  111. flt.setVisible(true);
  112. }
  113. }
Add Comment
Please, Sign In to add comment