Last active
August 4, 2019 01:03
-
-
Save wagfim/fdbdfcaba9a1263a0a8f2b6b4d41fae0 to your computer and use it in GitHub Desktop.
# Jogo da Velha com modos de 2 jogadores e 1 jogador (vs computador)
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.util.Random; | |
import java.util.Scanner; | |
/** | |
* @author Wagner Bonfim | |
*/ | |
public class Questao6 { | |
public static void main(String[] args) { | |
String[][] jogoDaVelha = criaMatriz(); | |
Scanner leitor = new Scanner(System.in); | |
System.out.println("*************************"); | |
System.out.println("* JOGO DA VELHA *"); | |
System.out.println("*************************"); | |
System.out.println("Utilize 1,2 ou 3 para escolher linhas e colunas"); | |
System.out.println(); | |
System.out.println("Escolha o modo de jogo:"); | |
System.out.println("1. Um Jogador"); | |
System.out.println("2. Dois Jogadores"); | |
System.out.print("> "); | |
int opcao = leitor.nextInt(); | |
switch (opcao) { | |
case 1: | |
modoHumanoVsComputador(jogoDaVelha); | |
break; | |
case 2: | |
modoHumanoVsHumano(jogoDaVelha); | |
break; | |
default: | |
System.out.println("Opção Inválida"); | |
} | |
} | |
public static void modoHumanoVsHumano(String[][] jogoDaVelha) { | |
desenhaMatriz(jogoDaVelha); | |
for (int i = 0; i < 9; i++) { | |
if (i % 2 == 0) { | |
jogoDaVelha = executarJogada(jogoDaVelha, "Player1", i); | |
} else { | |
jogoDaVelha = executarJogada(jogoDaVelha, "Player2", i); | |
} | |
System.out.println(); | |
desenhaMatriz(jogoDaVelha); //exibe a jogada feita | |
if (isJogoCompleto(jogoDaVelha)) { //se true ganha quem fez a ultima jogada | |
if (i % 2 == 0) { | |
System.out.println("Player1 ganhou!"); | |
break; | |
} else { | |
System.out.println("Player2 ganhou!"); | |
break; | |
} | |
} else if (i == 8) { | |
System.out.println("Fim de jogo, ninguém ganhou :-("); | |
} | |
} | |
} | |
public static void modoHumanoVsComputador(String[][] jogoDaVelha) { | |
desenhaMatriz(jogoDaVelha); | |
for (int i = 0; i < 9; i++) { | |
if (i % 2 == 0) { //player1 | |
jogoDaVelha = executarJogada(jogoDaVelha, "Player1", i); | |
} else { //computador | |
jogoDaVelha = executarJogada(jogoDaVelha, "Computador", i); | |
} | |
System.out.println(); | |
desenhaMatriz(jogoDaVelha); //exibe a jogada feita | |
if (isJogoCompleto(jogoDaVelha)) { //se true ganha quem fez a ultima jogada | |
if (i % 2 == 0) { | |
System.out.println("Player1 ganhou!"); | |
break; | |
} else { | |
System.out.println("Computador ganhou!"); | |
break; | |
} | |
} else if (i == 8) { | |
System.out.println("Fim de jogo, ninguém ganhou :-("); | |
} | |
} | |
} | |
public static String[][] executarJogada(String[][] jogoDaVelha, String nomePlayer, int numeroDaJogada) { | |
Scanner leitor = new Scanner(System.in); | |
int linha; | |
int coluna; | |
if (nomePlayer.equals("Computador")) { //a função muda o tipo de jogo de acordo com o nome do jogador | |
Random random = new Random(); | |
boolean repetirJogada = true; //se a posição estiver ocupada, continua tentando | |
System.out.print("\nVez do "+nomePlayer); | |
do { | |
linha = random.nextInt(3); | |
coluna = random.nextInt(3); | |
boolean posicaoValida = posicaoOcupada(jogoDaVelha, linha, coluna); //verifica se a posição já não está ocupada | |
if (!posicaoValida) { //se a posição retornar false (livre) execute a jogada | |
jogoDaVelha[linha][coluna] = "O"; | |
repetirJogada = false; | |
} | |
} while (repetirJogada); | |
} else { //modo dois jogadores | |
System.out.println("\nVez do " + nomePlayer); | |
System.out.print("Informe linha: "); | |
linha = leitor.nextInt() - 1; | |
System.out.print("Informe coluna: "); | |
coluna = leitor.nextInt() - 1; | |
} | |
if (numeroDaJogada % 2 == 0) { | |
jogoDaVelha[linha][coluna] = "X"; | |
} else { | |
jogoDaVelha[linha][coluna] = "O"; | |
} | |
return jogoDaVelha; | |
} | |
public static boolean posicaoOcupada(String[][] matriz, int linha, int coluna) { | |
return matriz[linha][coluna].equals("X") || matriz[linha][coluna].equals("O"); | |
} | |
public static String[][] criaMatriz() { | |
String[][] matriz = new String[3][3]; | |
for (int i = 0; i < 3; i++) { | |
for (int j = 0; j < 3; j++) { | |
matriz[i][j] = " "; | |
} | |
} | |
return matriz; | |
} | |
public static void desenhaMatriz(String[][] matriz) { | |
for (int i = 0; i < 3; i++) { | |
for (int j = 0; j < 3; j++) { | |
if (j != 2) { | |
System.out.print(matriz[i][j] + " | "); | |
} else { | |
System.out.print(matriz[i][j]); | |
} | |
} | |
System.out.println(); | |
} | |
} | |
public static boolean isJogoCompleto(String[][] matriz) { | |
boolean linha0 = checarLinhaCompleta(extrairLinha(matriz, 0)); | |
boolean linha1 = checarLinhaCompleta(extrairLinha(matriz, 1)); | |
boolean linha2 = checarLinhaCompleta(extrairLinha(matriz, 2)); | |
boolean coluna0 = checarLinhaCompleta(extrairColuna(matriz, 0)); | |
boolean coluna1 = checarLinhaCompleta(extrairColuna(matriz, 1)); | |
boolean coluna2 = checarLinhaCompleta(extrairColuna(matriz, 2)); | |
boolean diagonalPrimaria = checarLinhaCompleta(extrairDiagonalPrincipal(matriz)); | |
boolean diagonalSecundaria = checarLinhaCompleta(extrairDiagonalSecundaria(matriz)); | |
return linha0 | linha1 | linha2 | coluna0 | coluna1 | coluna2 | diagonalPrimaria | diagonalSecundaria; | |
} | |
public static String[] extrairLinha(String[][] matriz, int numeroLinha) { | |
String[] linha = new String[3]; | |
for (int i = 0; i < 3; i++) { | |
linha[i] = matriz[numeroLinha][i]; | |
} | |
return linha; | |
} | |
public static String[] extrairColuna(String[][] matriz, int numeroColuna) { | |
String[] linha = new String[3]; | |
for (int i = 0; i < 3; i++) { | |
linha[i] = matriz[i][numeroColuna]; | |
} | |
return linha; | |
} | |
public static String[] extrairDiagonalPrincipal(String[][] matriz) { | |
String[] array = new String[matriz.length]; | |
for (int i = 0; i < matriz.length; i++) { | |
for (int j = 0; j < matriz[i].length; j++) { | |
if (i == j) { | |
array[i] = matriz[i][j]; | |
} | |
} | |
} | |
return array; | |
} | |
public static String[] extrairDiagonalSecundaria(String[][] matriz) { | |
String[] array = new String[matriz.length]; | |
for (int i = 0; i < matriz.length; i++) { | |
for (int j = 0; j < matriz[i].length; j++) { | |
if (i + j == matriz[i].length - 1) { | |
array[i] = matriz[i][j]; | |
} | |
} | |
} | |
return array; | |
} | |
public static boolean checarLinhaCompleta(String[] linha) { | |
int sequencia = 1; | |
for (int i = 0; i < 1; i++) { | |
for (int j = i + 1; j < linha.length; j++) { | |
if (linha[i].equals(" ") || linha[j].equals(" ")) { | |
return false; | |
} else { | |
if (linha[i].equals(linha[j])) { | |
sequencia++; | |
} else { | |
break; | |
} | |
if (sequencia == 3) { | |
return true; | |
} | |
} | |
} | |
} | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment