Guest User

Untitled

a guest
Jul 19th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.04 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.AdjustmentListener;
  3. import java.util.Vector;
  4. import javax.swing.*;
  5.  
  6.  
  7.  
  8. class GameBeta extends JFrame{
  9. static int field[][]=new int[3][3];
  10. static Vector<JButton> bf=new Vector<JButton>();
  11. static boolean turn;
  12. static int gameStatus; //0 - on game, 1 - 1st player win, 2- 2nd player win, 3d- draw
  13.  
  14. private void makeTurn(int x, int y){
  15. if (gameStatus==0){
  16. if (field[y][x]==0){
  17. if (turn==true){
  18. field[y][x]=1;
  19. }else{
  20. field[y][x]=2;
  21. }
  22. }
  23. }
  24. }
  25.  
  26.  
  27. public GameBeta(){
  28. getContentPane().setLayout(new GridLayout(4,3, 10, 10));
  29. for(int k=0; k<9; k++) {
  30. bf.add(new JButton(""));
  31. getContentPane().add(bf.get(k));
  32. }
  33. bf.add(new JButton(""));
  34. getContentPane().add(bf.get(9));
  35. getContentPane().add(new JButton("Заново"));
  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.  
  44. ActionListener al = new ActionListener() {
  45. public void actionPerformed(ActionEvent e){
  46. String name =
  47. ((JButton)e.getSource()).getText();
  48. txt.setText(name);
  49. }
  50. };
  51. bf.get(9).addActionListener(al);
  52. }
  53.  
  54. private static int calcGameStatus(){
  55. boolean b=false;
  56. for (int i=0; i<3; i++){
  57. if ( (field[i][1]!=0)&&(field[i][0]==field[i][1])&&(field[i][1]==field[i][2]) ){
  58. gameStatus=field[i][1];
  59. return gameStatus;
  60. }
  61. if ( (field[1][i]!=0)&&(field[0][i]==field[1][i])&&(field[1][i]==field[2][i]) ){
  62. gameStatus=field[1][i];
  63. return gameStatus;
  64. }
  65. }
  66. if ( (field[1][1]!=0) ){
  67. if ( ((field[0][0]==field[1][1])&&(field[1][1]==field[2][2])) || ((field[2][0]==field[1][1])&&(field[0][2]==field[2][2])) ){
  68. gameStatus=field[1][1];
  69. return gameStatus;
  70. }
  71. }
  72.  
  73. return 0;
  74. }
  75.  
  76. public static void main(String[] args){
  77. GameBeta flt = new GameBeta();
  78. flt.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  79. flt.setVisible(true);
  80. }
  81. }
Add Comment
Please, Sign In to add comment