Last active
December 21, 2015 06:18
-
-
Save IuryAlves/6262831 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.applet.Applet; | |
import java.awt.BorderLayout; | |
import java.awt.Button; | |
import java.awt.Event; | |
import java.awt.GridLayout; | |
import java.awt.Label; | |
import java.awt.Panel; | |
public class Jogo extends Applet{ | |
private static final long serialVersionUID = 1L; | |
private int contX; | |
private int contO; | |
Label display; | |
Panel bottom; | |
Panel num_panel; | |
public int vez = 0; // se vez == 0: bolinha joga, caso contrario X joga | |
Button number[][] = new Button[3][3]; | |
Verificacoes v = new Verificacoes(); | |
public boolean action(Event evt,Object obj){ | |
for (int i = 0; i < 3; i++){ | |
for(int j = 0; j < 3; j++){ | |
if (evt.target == number[i][j]){ | |
if (number[i][j].getLabel() == " ") | |
if (vez == 0){ | |
number[i][j].setLabel("O"); | |
display.setText("Vez do jogador X"); | |
vez = 1; | |
} | |
else if (vez == 1){ | |
number[i][j].setLabel("X"); | |
display.setText("Vez do jogador bolinha"); | |
vez = 0; | |
} | |
} | |
} | |
} | |
if (v.varrerHorizontal(number,contX,contO) == 1 )display.setText("Jogador X ganhou"); | |
if(v.varrerHorizontal(number,contX,contO) == 0) display.setText("Jogador bolinha ganhou"); | |
if (v.varrerVertical(number,contX,contO) == 1 )display.setText("Jogador X ganhou"); | |
if(v.varrerVertical(number,contX,contO) == 0) display.setText("Jogador bolinha ganhou"); | |
if (v.varrerDiagonal1(number,contX,contO) == 1 )display.setText("Jogador X ganhou"); | |
if(v.varrerDiagonal1(number,contX,contO) == 0) display.setText("Jogador bolinha ganhou"); | |
if (v.varrerDiagonal2(number,contX,contO) == 1 )display.setText("Jogador X ganhou"); | |
if(v.varrerDiagonal2(number,contX,contO) == 0) display.setText("Jogador bolinha ganhou"); | |
return true; | |
} | |
public void init() { | |
setLayout(new BorderLayout()); | |
display = new Label("Jogador bolinha começa", Label.LEFT); | |
add("North", display); | |
bottom = new Panel(); | |
bottom.setLayout(new BorderLayout()); | |
num_panel = new Panel(); | |
num_panel.setLayout(new GridLayout(4,3)); | |
for (int i = 0; i < 3; i++) { | |
for (int j = 0; j < 3; j++){ | |
number[i][j] = new Button(" "); | |
num_panel.add(number[i][j]); | |
} | |
} | |
bottom.add("Center", num_panel); | |
add("Center",bottom); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.Button; | |
public class Verificacoes { | |
/* classe que verifica se algum jogador venceu ou se ninguem ganhou */ | |
// retorna um se jogador X ganhou e zero se jogador bolinha ganhou | |
public int varrerHorizontal(Button b[][],int contX,int contO){ | |
contX = 0; | |
contO = 0; | |
for (int i = 0; i < 3; i++){ | |
contX = 0; | |
contO = 0; | |
for (int j = 0; j < 3; j++){ | |
if (b[i][j].getLabel() == "X" ){ | |
contX++; | |
} | |
else if (b[i][j].getLabel() == "O"){ | |
contO++; | |
} | |
if (contX == 3) return 1; | |
else if (contO == 3) return 0; | |
} | |
} | |
contX = 0;contO = 0; | |
return -1; | |
} | |
public int varrerVertical(Button b[][],int contX,int contO){ | |
for (int i = 0; i < 3; i++){ | |
contX = 0; | |
contO = 0; | |
for(int j = 0; j < 3; j++){ | |
if (b[j][i].getLabel() == "X") contX++; | |
else if (b[j][i].getLabel() == "O") contO++; | |
if (contX == 3) return 1; | |
else if (contO == 3) return 0; | |
} | |
} | |
return -1; | |
} | |
public int varrerDiagonal1(Button b[][],int contX, int contO){ | |
contX = 0; | |
contO = 0; | |
int j = -1; | |
for(int i = 0; i < 3;i++){ | |
j++; | |
if (b[i][j].getLabel() == "X") contX++; | |
else if (b[i][j].getLabel() == "O") contO++; | |
if (contX == 3) return 1; | |
else if (contO == 3) return 0; | |
} | |
return -1; | |
} | |
public int varrerDiagonal2(Button b[][], int contX, int contO){ | |
contX = 0; | |
contO = 0; | |
int j = -1; | |
for(int i = 2; i >=0; i--){ | |
j++; | |
if (b[i][j].getLabel() == "X") contX++; | |
else if(b[i][j].getLabel() == "O") contO++; | |
if (contX == 3) return 1; | |
else if (contO == 3) return 0; | |
} | |
return -1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment