fork(1) download
  1. import java.applet.*;
  2. import java.awt.*;
  3. import java.util.Scanner;
  4.  
  5. //<applet code="tester" height=400 width=400></applet>
  6.  
  7. /*
  8. I have made a TIC TAC TOE game using Applet.
  9. I have a problem in this when I scan the position of the
  10. element on the console everything vanishes from the screen.
  11. I have tried using a array instead but it doesnt give
  12. real time feeling of the game.My scanning is in form of
  13. integers like 13 for 1st row and 3rd column */
  14.  
  15.  
  16. class tic extends Applet {
  17. static int count=0;
  18. static int str[][]=new int[3][3];
  19. int l;
  20. static int k;
  21. static int value=-1;
  22. static Scanner sc=new Scanner(System.in);
  23. public void paint(Graphics g){
  24. try{
  25.  
  26.  
  27. //To draw rectangles on the Applet
  28.  
  29. g.drawRect(0,0,20,20);
  30. g.drawRect(0,20,20,20);
  31. g.drawRect(0,40,20,20);
  32. g.drawRect(20,0,20,20);
  33. g.drawRect(20,20,20,20);
  34. g.drawRect(20,40,20,20);
  35. g.drawRect(40,0,20,20);
  36. g.drawRect(40,20,20,20);
  37. g.drawRect(40,40,20,20);
  38. for(int i=0;i<=count;i++)
  39. {
  40.  
  41. // Main problem lies here which is for scanning the input
  42. l=ret();
  43.  
  44.  
  45. if(i%2==0)
  46. g.drawString("X",(((2*(l%10)-1)-1)*10)+7,(((2*(l/10)-1)-1)*10)+14);
  47. else
  48. g.drawString("O",(((2*(l%10)-1)-1)*10)+6,(((2*(l/10)-1)-1)*10)+14);
  49.  
  50. //I have made this formula to print X and O at the centre of the rectangle
  51.  
  52. System.out.println(l);
  53. if(i==count)
  54. {
  55. if(count%2==0)
  56. store(l,1);
  57. else
  58. store(l,2);
  59. }
  60. value++;
  61. }
  62. value=-1;
  63. Thread.sleep(500);
  64. if(count>=5)
  65. {
  66. int l=check();
  67. /* if(count==8){
  68. for(int i=0;i<3;i++)
  69. {
  70. for(int j=0;j<3;j++)
  71. System.out.print(str[i][j]);
  72. System.out.println();
  73. }
  74. System.out.println(l);
  75. }*/
  76. if(count==8 && l==0)
  77. g.drawString("Draw",65 ,65);
  78. else
  79. {
  80. if(l==1)
  81. {
  82. if(count%2==0)
  83. g.drawString("Player 1 win",60 ,60);
  84. else
  85. g.drawString("Player 2 win",65 ,65);
  86. }
  87. else{
  88. count++;
  89. repaint();
  90. }
  91. }
  92. }
  93. else
  94. {
  95. count++;
  96. repaint();
  97. }
  98. }
  99. e.printStackTrace();
  100. }
  101. System.out.println("array pbm");
  102. }
  103.  
  104. }
  105.  
  106.  
  107. //For scanning the input
  108. public static int ret(){
  109. value++;
  110. int mat[]=new int[9];
  111. if(value==0)
  112. k=scan();
  113. mat[count]=k;
  114. return mat[value];
  115.  
  116.  
  117. }
  118.  
  119. //For scanning the input
  120. public static int scan(){
  121. int w=sc.nextInt();
  122. return w;
  123. }
  124.  
  125. //For storing value in array
  126. public static void store(int l,int n){
  127. str[(l/10)-1][(l%10)-1]=n;
  128. }
  129.  
  130. // For checking the conditions of wining
  131. public static int check(){
  132. if(str[0][0]==str[0][1] && str[0][1]==str[0][2] && str[0][0]!=0)
  133. return 1;
  134. else if(str[1][0]==str[1][1] && str[1][1]==str[1][2] && str[1][0]!=0)
  135. return 1;
  136. else if(str[2][0]==str[2][1] && str[2][1]==str[2][2] && str[2][0]!=0)
  137. return 1;
  138. else if(str[0][0]==str[1][0] && str[1][0]==str[2][0] && str[1][0]!=0)
  139. return 1;
  140. else if(str[0][1]==str[1][1] && str[1][1]==str[2][1] && str[0][1]!=0)
  141. return 1;
  142. else if(str[0][2]==str[1][2] && str[1][2]==str[2][2] && str[0][2]!=0)
  143. return 1;
  144. else if(str[0][0]==str[1][1] && str[1][1]==str[2][2] && str[0][0]!=0)
  145. return 1;
  146. else if(str[2][0]==str[1][1] && str[1][1]==str[0][2] && str[2][0]!=0)
  147. return 1;
  148. else
  149. return 0;
  150.  
  151. }
  152.  
  153. }
  154.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
13
12
11
21
22
23
compilation info
spoj: The program compiled successfully, but main class was not found.
      Main class should contain method: public static void main (String[] args).
stdout
Standard output is empty