Advertisement
drunkspinda02

trabalho da v3

Mar 18th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.16 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Jogo do galo</title>
  5. <style type="text/css">
  6.  
  7. body{
  8. background-color: blue;
  9. color: white;
  10. }
  11.  
  12. table,td{
  13. border: 1px solid white;
  14. margin: auto;
  15. border-collapse: collapse;
  16. background-color: green;
  17. text-align: center;
  18.  
  19. }
  20.  
  21. td{
  22. width: 100px;
  23. height: 100px;
  24.  
  25. }
  26.  
  27. table{
  28. width: 300px;
  29. height: 300px;
  30. }
  31.  
  32. p{
  33. text-align: center;
  34. }
  35.  
  36. input{
  37. border-radius: 15px;
  38. }
  39.  
  40. input[type=button]{
  41. width: 150px;
  42. height: 27px;
  43. border-color: yellow;
  44. }
  45.  
  46. input[type=text]{
  47. text-align: center;
  48. border-color: yellow;
  49. }
  50.  
  51. .off{
  52. background-color: brown;
  53. }
  54.  
  55. .on{
  56. background-color: green;
  57. }
  58.  
  59. </style>
  60. </head>
  61. <body>
  62. <h1 style="text-align: center; text-shadow: 2px 2px grey;">Jogo do Galo</h1>
  63. <h2 style="text-align: center; text-shadow: 2px 2px grey;">Versão Definitiva</h2>
  64. <table>
  65. <tr>
  66. <td class="off" id="a1" onclick="marca('a1');">&nbsp;</td>
  67. <td class="off" id="a2" onclick="marca('a2');">&nbsp;</td>
  68. <td class="off" id="a3" onclick="marca('a3');">&nbsp;</td>
  69. </tr>
  70. <tr>
  71. <td class="off" id="a4" onclick="marca('a4');">&nbsp;</td>
  72. <td class="off" id="a5" onclick="marca('a5');">&nbsp;</td>
  73. <td class="off" id="a6" onclick="marca('a6');">&nbsp;</td>
  74. </tr>
  75. <tr>
  76. <td class="off" id="a7" onclick="marca('a7');">&nbsp;</td>
  77. <td class="off" id="a8" onclick="marca('a8');">&nbsp;</td>
  78. <td class="off" id="a9" onclick="marca('a9');">&nbsp;</td>
  79. </tr>
  80. </table>
  81. <p><select id="modo">
  82. <option value="1">Contra o jogador</option>
  83. <option value="2">Contra o CPU</option>
  84. </select></p>
  85. <p><input type="button" name="start" value="Start" id="start" onclick="start()"></p>
  86. <p><input type="button" name="reset" value="Reset" id="reset" onclick="limpa()"></p>
  87. <p>Jogador<br><input type="text" name="jg1" id="jg1" value="0"></p>
  88. <p>CPU<br><input type="text" name="jg2" id="jg2" value="0"></p>
  89. <p>Empate<br><input type="text" name="emp" id="emp" value="0"></p>
  90.  
  91. <script type="text/javascript">
  92. var modojogo=document.getElementById('modo').value;
  93. var j=0;
  94. var i,b,c,j,w;
  95. var win=0;
  96. var jogada=0;
  97. var k =0;
  98. var linhascpu;
  99. var linhasplayer;
  100. var board = [0, 0, 0, 0, 0, 0, 0, 0, 0];
  101.  
  102. function start(){
  103. for(i=1;i<=9;i++){
  104. if(document.getElementById('a' +i).className= "off"){
  105. document.getElementById('a' +i).className= "on"
  106. }
  107. }
  108. }
  109.  
  110. function marca(x){
  111. if (win==0 && k==0) {
  112. if(document.getElementById(x).innerHTML=='&nbsp;' && document.getElementById(x).className== "on"){
  113. document.getElementById(x).innerHTML='X';
  114. for (i=1;i<=9;i++){
  115. if(document.getElementById('a' +i).innerHTML=='X'){
  116. board[i-1]=1;
  117. }
  118. }
  119. jogada++;
  120. k=1;
  121. verifica();
  122. }
  123.  
  124. else{
  125. alert('Não pode clickar nesta casa.')
  126. }
  127.  
  128.  
  129.  
  130.  
  131.  
  132. if (jogada<9 && win==0 && k==1 /*&& modojogo==2*/) {
  133.  
  134. if (jogada==1 && k==1) {
  135. if (board[4]==1) {
  136. board[6]=2;
  137. console.log(board);
  138. jogada++;
  139. document.getElementById('a7').innerHTML='O';
  140. k=0;
  141. verifica();
  142. }
  143. else{
  144. board[4]=2;
  145. console.log(board);
  146. jogada++;
  147. document.getElementById('a5').innerHTML='O';
  148. k=0;
  149. verifica();
  150. }
  151. }
  152.  
  153. if (jogada==3 && k==1) {
  154. if (board[2]==1 && board[6]==1 || board[0]==1 && board[8]==1) {
  155. board[7]=2;
  156. console.log(board);
  157. jogada++;
  158. document.getElementById('a8').innerHTML='O';
  159. k=0;
  160. verifica();
  161. }
  162.  
  163. }
  164.  
  165.  
  166. if (k==1) {
  167. for(i=0,b=3,c=6;i<=3;i++,b++,c++){
  168. if(board[i]==board[b] && board[i]!=0 || board[i]==board[c] && board[i]!=0 || board[b]==board[c] && board[b]!=0){
  169. if (board[i]==0) {
  170. board[i]=2;
  171. console.log(board);
  172. jogada++;
  173. document.getElementById('a'+(i+1)).innerHTML='O';
  174. k=0;
  175. verifica();
  176. }
  177. else if (board[b]==0) {
  178. board[b]=2;
  179. console.log(board);
  180. jogada++;
  181. document.getElementById('a'+(b+1)).innerHTML='O';
  182. k=0;
  183. verifica();
  184. }
  185. else if (board[c]==0) {
  186. board[c]=2;
  187. console.log(board);
  188. jogada++;
  189. document.getElementById('a'+(c+1)).innerHTML='O';
  190. k=0;
  191. verifica();
  192. }
  193. }
  194. }
  195. }
  196.  
  197. if (k==1) {
  198. for(i=0,b=1,c=2;i<=6;i=i+3,b=b+3,c=c+3){
  199. if(board[i]==board[b] && board[i]!=0 || board[i]==board[c] && board[i]!=0 || board[b]==board[c] && board[b]!=0){
  200. if (board[i]==0) {
  201. board[i]=2;
  202. console.log(board);
  203. jogada++;
  204. document.getElementById('a'+(i+1)).innerHTML='O';
  205. k=0;
  206. verifica();
  207. }
  208. else if (board[b]==0) {
  209. board[b]=2;
  210. console.log(board);
  211. jogada++;
  212. document.getElementById('a'+(b+1)).innerHTML='O';
  213. k=0;
  214. verifica();
  215. }
  216. else if (board[c]==0) {
  217. board[c]=2;
  218. console.log(board);
  219. jogada++;
  220. document.getElementById('a'+(c+1)).innerHTML='O';
  221. k=0;
  222. verifica();
  223. }
  224. }
  225. }
  226. }
  227.  
  228. if (k==1) {
  229. if (board[0]==board[4] && board[0]!=0 || board[0]==board[8] && board[0]!=0 || board[4]==board[8] && board[4]!=0){
  230. if (board[0]==0) {
  231. board[0]=2;
  232. console.log(board);
  233. jogada++;
  234. document.getElementById('a1').innerHTML='O';
  235. k=0;
  236. verifica();
  237. }
  238. else if (board[4]==0) {
  239. board[4]=2;
  240. console.log(board);
  241. jogada++;
  242. document.getElementById('a5').innerHTML='O';
  243. k=0;
  244. verifica();
  245. }
  246. else if (board[8]==0) {
  247. board[8]=2;
  248. console.log(board);
  249. jogada++;
  250. document.getElementById('a9').innerHTML='O';
  251. k=0;
  252. verifica();
  253. }
  254. }
  255. }
  256.  
  257. if (k==1) {
  258. if (board[2]==board[4] && board[2]!=0 || board[2]==board[6] && board[2]!=0 || board[4]==board[6] && board[4]!=0){
  259. if (board[2]==0) {
  260. board[2]=2;
  261. console.log(board);
  262. jogada++;
  263. document.getElementById('a3').innerHTML='O';
  264. k=0;
  265. verifica();
  266. }
  267. else if (board[4]==0) {
  268. board[4]=2;
  269. console.log(board);
  270. jogada++;
  271. document.getElementById('a5').innerHTML='O';
  272. k=0;
  273. verifica();
  274. }
  275. else if (board[6]==0) {
  276. board[6]=2;
  277. console.log(board);
  278. jogada++;
  279. document.getElementById('a7').innerHTML='O';
  280. k=0;
  281. verifica();
  282. }
  283. }
  284. }
  285.  
  286. if(k==1){
  287. do{
  288. c=Math.floor((Math.random()*9)+1);
  289. }while (document.getElementById('a' + c ).innerHTML!='&nbsp;' && document.getElementById(x).className== "on");
  290. board[c-1]=2;
  291. console.log(board);
  292. jogada++;
  293. document.getElementById('a'+c).innerHTML='O';
  294. k=0;
  295. verifica();
  296. }
  297.  
  298.  
  299. }
  300.  
  301. }
  302. }
  303.  
  304. /*if (jogada<9 && win==0 && k==1 && modojogo==1){
  305. if(document.getElementById(x).innerHTML=='&nbsp;' && document.getElementById(x).className== "on"){
  306. document.getElementById(x).innerHTML='O';
  307. for (i=1;i<=9;i++){
  308. if(document.getElementById('a' +i).innerHTML=='O'){
  309. board[i-1]=1;
  310. }
  311. }
  312. jogada++;
  313. k=0;
  314. verifica();
  315. }
  316.  
  317. else{
  318. alert('Não pode clickar nesta casa.')
  319. }
  320. }*/
  321.  
  322.  
  323. function verifica(){
  324.  
  325. for(i=1,b=4,c=7;i<=3;i++,b++,c++){
  326. if(win==0){
  327. if (document.getElementById('a' + i).innerHTML==document.getElementById('a' + b).innerHTML && document.getElementById('a' + i).innerHTML== document.getElementById('a' + c).innerHTML && document.getElementById('a' + i).innerHTML!='&nbsp;') {
  328. if(document.getElementById('a'+i).innerHTML=='X'){
  329. alert('Jogador venceu!');
  330. document.getElementById('jg1').value=parseInt(document.getElementById('jg1').value) + 1;
  331. win=1;
  332. board = [0, 0, 0, 0, 0, 0, 0, 0, 0];
  333.  
  334. }
  335. else
  336. if(document.getElementById('a'+i).innerHTML=='O'){
  337. alert('CPU venceu!');
  338. document.getElementById('jg2').value=parseInt(document.getElementById('jg2').value) + 1;
  339. win=1;
  340. board = [0, 0, 0, 0, 0, 0, 0, 0, 0];
  341. }
  342. }
  343. }
  344. }
  345.  
  346. for(i=1,b=2,c=3;i<=7;i=i+3,b=b+3,c=c+3){
  347. if(win==0){
  348. if (document.getElementById('a' + i).innerHTML==document.getElementById('a' + b).innerHTML && document.getElementById('a' + i).innerHTML== document.getElementById('a' + c).innerHTML && document.getElementById('a' + i).innerHTML!='&nbsp;') {
  349. if(document.getElementById('a'+i).innerHTML=='X'){
  350. alert('Jogador venceu!');
  351. document.getElementById('jg1').value=parseInt(document.getElementById('jg1').value) + 1;
  352. win=1;
  353. board = [0, 0, 0, 0, 0, 0, 0, 0, 0];
  354.  
  355. }
  356. else
  357. if(document.getElementById('a'+i).innerHTML=='O'){
  358. alert('CPU venceu!');
  359. document.getElementById('jg2').value=parseInt(document.getElementById('jg2').value) + 1;
  360. win=1;
  361. board = [0, 0, 0, 0, 0, 0, 0, 0, 0];
  362. }
  363. }
  364.  
  365. }
  366. }
  367.  
  368. if(win==0){
  369. if(document.getElementById('a1').innerHTML== document.getElementById('a5').innerHTML && document.getElementById('a1').innerHTML==document.getElementById('a9').innerHTML && document.getElementById('a1').innerHTML!='&nbsp;'){
  370. if(document.getElementById('a1').innerHTML=='X' && document.getElementById('a5').innerHTML=='X' && document.getElementById('a9').innerHTML=='X' ){
  371. alert('Jogador venceu!');
  372. document.getElementById('jg1').value=parseInt(document.getElementById('jg1').value) + 1;
  373. win=1;
  374. board = [0, 0, 0, 0, 0, 0, 0, 0, 0];
  375.  
  376. }
  377. else
  378. if(document.getElementById('a1').innerHTML=='O' && document.getElementById('a5').innerHTML=='O' && document.getElementById('a9').innerHTML=='O' ){
  379. alert('CPU venceu!');
  380. document.getElementById('jg2').value=parseInt(document.getElementById('jg2').value) + 1;
  381. win=1;
  382. board = [0, 0, 0, 0, 0, 0, 0, 0, 0];
  383. }
  384.  
  385. }
  386. }
  387.  
  388. if(win==0){
  389. if(document.getElementById('a3').innerHTML== document.getElementById('a5').innerHTML && document.getElementById('a3').innerHTML==document.getElementById('a7').innerHTML && document.getElementById('a3').innerHTML!='&nbsp;'){
  390. if(document.getElementById('a3').innerHTML=='X' && document.getElementById('a5').innerHTML=='X' && document.getElementById('a7').innerHTML=='X' ){
  391. alert('Jogador venceu!');
  392. document.getElementById('jg1').value=parseInt(document.getElementById('jg1').value) + 1;
  393. win=1;
  394. board = [0, 0, 0, 0, 0, 0, 0, 0, 0];
  395.  
  396. }
  397. else
  398. if(document.getElementById('a3').innerHTML=='O' && document.getElementById('a5').innerHTML=='O' && document.getElementById('a7').innerHTML=='O' ){
  399. alert('CPU venceu!');
  400. document.getElementById('jg2').value=parseInt(document.getElementById('jg2').value) + 1;
  401. win=1;
  402. board = [0, 0, 0, 0, 0, 0, 0, 0, 0];
  403. }
  404. }
  405. }
  406.  
  407. if (win==0 && jogada==9) {
  408. alert('Empate.');
  409. document.getElementById('emp').value=parseInt(document.getElementById('emp').value) + 1;
  410. board = [0, 0, 0, 0, 0, 0, 0, 0, 0];
  411.  
  412.  
  413. }
  414. }
  415. function limpa(){
  416. for(i=1;i<=9;i++){
  417. document.getElementById('a' + i).innerHTML='&nbsp;';
  418. }
  419. jogada=0;
  420. win=0;
  421. j=0;
  422. k=0;
  423. board = [0, 0, 0, 0, 0, 0, 0, 0, 0];
  424. }
  425.  
  426.  
  427. </script>
  428. </body>
  429. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement