Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <head>
- <title>Jogo do galo</title>
- <style type="text/css">
- body{
- background-color: blue;
- color: white;
- }
- table,td{
- border: 1px solid white;
- margin: auto;
- border-collapse: collapse;
- background-color: green;
- text-align: center;
- }
- td{
- width: 100px;
- height: 100px;
- }
- table{
- width: 300px;
- height: 300px;
- }
- p{
- text-align: center;
- }
- input{
- border-radius: 15px;
- }
- input[type=button]{
- width: 150px;
- height: 27px;
- border-color: yellow;
- }
- input[type=text]{
- text-align: center;
- border-color: yellow;
- }
- .off{
- background-color: brown;
- }
- .on{
- background-color: green;
- }
- </style>
- </head>
- <body>
- <h1 style="text-align: center; text-shadow: 2px 2px grey;">Jogo do Galo</h1>
- <h2 style="text-align: center; text-shadow: 2px 2px grey;">Versão Definitiva</h2>
- <table>
- <tr>
- <td class="off" id="a1" onclick="marca('a1');"> </td>
- <td class="off" id="a2" onclick="marca('a2');"> </td>
- <td class="off" id="a3" onclick="marca('a3');"> </td>
- </tr>
- <tr>
- <td class="off" id="a4" onclick="marca('a4');"> </td>
- <td class="off" id="a5" onclick="marca('a5');"> </td>
- <td class="off" id="a6" onclick="marca('a6');"> </td>
- </tr>
- <tr>
- <td class="off" id="a7" onclick="marca('a7');"> </td>
- <td class="off" id="a8" onclick="marca('a8');"> </td>
- <td class="off" id="a9" onclick="marca('a9');"> </td>
- </tr>
- </table>
- <p><select id="modo">
- <option value="1">Contra o jogador</option>
- <option value="2">Contra o CPU</option>
- </select></p>
- <p><input type="button" name="start" value="Start" id="start" onclick="start()"></p>
- <p><input type="button" name="reset" value="Reset" id="reset" onclick="limpa()"></p>
- <p>Jogador<br><input type="text" name="jg1" id="jg1" value="0"></p>
- <p>CPU<br><input type="text" name="jg2" id="jg2" value="0"></p>
- <p>Empate<br><input type="text" name="emp" id="emp" value="0"></p>
- <script type="text/javascript">
- var modojogo=document.getElementById('modo').value;
- var j=0;
- var i,b,c,j,w;
- var win=0;
- var jogada=0;
- var k =0;
- var linhascpu;
- var linhasplayer;
- var board = [0, 0, 0, 0, 0, 0, 0, 0, 0];
- function start(){
- for(i=1;i<=9;i++){
- if(document.getElementById('a' +i).className= "off"){
- document.getElementById('a' +i).className= "on"
- }
- }
- }
- function marca(x){
- if (win==0 && k==0) {
- if(document.getElementById(x).innerHTML==' ' && document.getElementById(x).className== "on"){
- document.getElementById(x).innerHTML='X';
- for (i=1;i<=9;i++){
- if(document.getElementById('a' +i).innerHTML=='X'){
- board[i-1]=1;
- }
- }
- jogada++;
- k=1;
- verifica();
- }
- else{
- alert('Não pode clickar nesta casa.')
- }
- if (jogada<9 && win==0 && k==1 /*&& modojogo==2*/) {
- if (jogada==1 && k==1) {
- if (board[4]==1) {
- board[6]=2;
- console.log(board);
- jogada++;
- document.getElementById('a7').innerHTML='O';
- k=0;
- verifica();
- }
- else{
- board[4]=2;
- console.log(board);
- jogada++;
- document.getElementById('a5').innerHTML='O';
- k=0;
- verifica();
- }
- }
- if (jogada==3 && k==1) {
- if (board[2]==1 && board[6]==1 || board[0]==1 && board[8]==1) {
- board[7]=2;
- console.log(board);
- jogada++;
- document.getElementById('a8').innerHTML='O';
- k=0;
- verifica();
- }
- }
- if (k==1) {
- for(i=0,b=3,c=6;i<=3;i++,b++,c++){
- if(board[i]==board[b] && board[i]!=0 || board[i]==board[c] && board[i]!=0 || board[b]==board[c] && board[b]!=0){
- if (board[i]==0) {
- board[i]=2;
- console.log(board);
- jogada++;
- document.getElementById('a'+(i+1)).innerHTML='O';
- k=0;
- verifica();
- }
- else if (board[b]==0) {
- board[b]=2;
- console.log(board);
- jogada++;
- document.getElementById('a'+(b+1)).innerHTML='O';
- k=0;
- verifica();
- }
- else if (board[c]==0) {
- board[c]=2;
- console.log(board);
- jogada++;
- document.getElementById('a'+(c+1)).innerHTML='O';
- k=0;
- verifica();
- }
- }
- }
- }
- if (k==1) {
- for(i=0,b=1,c=2;i<=6;i=i+3,b=b+3,c=c+3){
- if(board[i]==board[b] && board[i]!=0 || board[i]==board[c] && board[i]!=0 || board[b]==board[c] && board[b]!=0){
- if (board[i]==0) {
- board[i]=2;
- console.log(board);
- jogada++;
- document.getElementById('a'+(i+1)).innerHTML='O';
- k=0;
- verifica();
- }
- else if (board[b]==0) {
- board[b]=2;
- console.log(board);
- jogada++;
- document.getElementById('a'+(b+1)).innerHTML='O';
- k=0;
- verifica();
- }
- else if (board[c]==0) {
- board[c]=2;
- console.log(board);
- jogada++;
- document.getElementById('a'+(c+1)).innerHTML='O';
- k=0;
- verifica();
- }
- }
- }
- }
- if (k==1) {
- if (board[0]==board[4] && board[0]!=0 || board[0]==board[8] && board[0]!=0 || board[4]==board[8] && board[4]!=0){
- if (board[0]==0) {
- board[0]=2;
- console.log(board);
- jogada++;
- document.getElementById('a1').innerHTML='O';
- k=0;
- verifica();
- }
- else if (board[4]==0) {
- board[4]=2;
- console.log(board);
- jogada++;
- document.getElementById('a5').innerHTML='O';
- k=0;
- verifica();
- }
- else if (board[8]==0) {
- board[8]=2;
- console.log(board);
- jogada++;
- document.getElementById('a9').innerHTML='O';
- k=0;
- verifica();
- }
- }
- }
- if (k==1) {
- if (board[2]==board[4] && board[2]!=0 || board[2]==board[6] && board[2]!=0 || board[4]==board[6] && board[4]!=0){
- if (board[2]==0) {
- board[2]=2;
- console.log(board);
- jogada++;
- document.getElementById('a3').innerHTML='O';
- k=0;
- verifica();
- }
- else if (board[4]==0) {
- board[4]=2;
- console.log(board);
- jogada++;
- document.getElementById('a5').innerHTML='O';
- k=0;
- verifica();
- }
- else if (board[6]==0) {
- board[6]=2;
- console.log(board);
- jogada++;
- document.getElementById('a7').innerHTML='O';
- k=0;
- verifica();
- }
- }
- }
- if(k==1){
- do{
- c=Math.floor((Math.random()*9)+1);
- }while (document.getElementById('a' + c ).innerHTML!=' ' && document.getElementById(x).className== "on");
- board[c-1]=2;
- console.log(board);
- jogada++;
- document.getElementById('a'+c).innerHTML='O';
- k=0;
- verifica();
- }
- }
- }
- }
- /*if (jogada<9 && win==0 && k==1 && modojogo==1){
- if(document.getElementById(x).innerHTML==' ' && document.getElementById(x).className== "on"){
- document.getElementById(x).innerHTML='O';
- for (i=1;i<=9;i++){
- if(document.getElementById('a' +i).innerHTML=='O'){
- board[i-1]=1;
- }
- }
- jogada++;
- k=0;
- verifica();
- }
- else{
- alert('Não pode clickar nesta casa.')
- }
- }*/
- function verifica(){
- for(i=1,b=4,c=7;i<=3;i++,b++,c++){
- if(win==0){
- 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!=' ') {
- if(document.getElementById('a'+i).innerHTML=='X'){
- alert('Jogador venceu!');
- document.getElementById('jg1').value=parseInt(document.getElementById('jg1').value) + 1;
- win=1;
- board = [0, 0, 0, 0, 0, 0, 0, 0, 0];
- }
- else
- if(document.getElementById('a'+i).innerHTML=='O'){
- alert('CPU venceu!');
- document.getElementById('jg2').value=parseInt(document.getElementById('jg2').value) + 1;
- win=1;
- board = [0, 0, 0, 0, 0, 0, 0, 0, 0];
- }
- }
- }
- }
- for(i=1,b=2,c=3;i<=7;i=i+3,b=b+3,c=c+3){
- if(win==0){
- 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!=' ') {
- if(document.getElementById('a'+i).innerHTML=='X'){
- alert('Jogador venceu!');
- document.getElementById('jg1').value=parseInt(document.getElementById('jg1').value) + 1;
- win=1;
- board = [0, 0, 0, 0, 0, 0, 0, 0, 0];
- }
- else
- if(document.getElementById('a'+i).innerHTML=='O'){
- alert('CPU venceu!');
- document.getElementById('jg2').value=parseInt(document.getElementById('jg2').value) + 1;
- win=1;
- board = [0, 0, 0, 0, 0, 0, 0, 0, 0];
- }
- }
- }
- }
- if(win==0){
- if(document.getElementById('a1').innerHTML== document.getElementById('a5').innerHTML && document.getElementById('a1').innerHTML==document.getElementById('a9').innerHTML && document.getElementById('a1').innerHTML!=' '){
- if(document.getElementById('a1').innerHTML=='X' && document.getElementById('a5').innerHTML=='X' && document.getElementById('a9').innerHTML=='X' ){
- alert('Jogador venceu!');
- document.getElementById('jg1').value=parseInt(document.getElementById('jg1').value) + 1;
- win=1;
- board = [0, 0, 0, 0, 0, 0, 0, 0, 0];
- }
- else
- if(document.getElementById('a1').innerHTML=='O' && document.getElementById('a5').innerHTML=='O' && document.getElementById('a9').innerHTML=='O' ){
- alert('CPU venceu!');
- document.getElementById('jg2').value=parseInt(document.getElementById('jg2').value) + 1;
- win=1;
- board = [0, 0, 0, 0, 0, 0, 0, 0, 0];
- }
- }
- }
- if(win==0){
- if(document.getElementById('a3').innerHTML== document.getElementById('a5').innerHTML && document.getElementById('a3').innerHTML==document.getElementById('a7').innerHTML && document.getElementById('a3').innerHTML!=' '){
- if(document.getElementById('a3').innerHTML=='X' && document.getElementById('a5').innerHTML=='X' && document.getElementById('a7').innerHTML=='X' ){
- alert('Jogador venceu!');
- document.getElementById('jg1').value=parseInt(document.getElementById('jg1').value) + 1;
- win=1;
- board = [0, 0, 0, 0, 0, 0, 0, 0, 0];
- }
- else
- if(document.getElementById('a3').innerHTML=='O' && document.getElementById('a5').innerHTML=='O' && document.getElementById('a7').innerHTML=='O' ){
- alert('CPU venceu!');
- document.getElementById('jg2').value=parseInt(document.getElementById('jg2').value) + 1;
- win=1;
- board = [0, 0, 0, 0, 0, 0, 0, 0, 0];
- }
- }
- }
- if (win==0 && jogada==9) {
- alert('Empate.');
- document.getElementById('emp').value=parseInt(document.getElementById('emp').value) + 1;
- board = [0, 0, 0, 0, 0, 0, 0, 0, 0];
- }
- }
- function limpa(){
- for(i=1;i<=9;i++){
- document.getElementById('a' + i).innerHTML=' ';
- }
- jogada=0;
- win=0;
- j=0;
- k=0;
- board = [0, 0, 0, 0, 0, 0, 0, 0, 0];
- }
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement