Estou desenvolvendo um jogo da velha e nao sei oq deu errado, nao consigo preencher a matriz com os valores, alguem poderia me ajudar.
Até então o meu codigo ficou assim:
package Atividade20;
import java.util.Scanner;
public class JogoDaVelha {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String[][] matriz = new String[3][3];
int l,c,cont,po;
boolean R;
cont = 1;
String simb = "X";
for (int l1 = 0; l1 < matriz.length; l1++) {
for (int c1 = 0; c1 < matriz.length; c1++) {
matriz[l1][c1] = Integer.toString(cont);
cont++;
}
}
mostrarVelha();
do {
do {
System.out.println("Vai jogar [" + simb + "] em qual posição? ");
po = sc.nextInt();
R = Jogar(simb,po, matriz);
if (R==false) {
System.out.println("Jogada invalida");
}
} while (R==true);
mudaJogador();
mostrarVelha();
} while (R = true);
System.out.println("Jogo finalizado");
}
private static String[][] mostrarVelha(){
String[][] matriz = new String[3][3];
System.out.println("+-----+-----+-----+");
for (int l = 0; l < matriz.length; l++) {
for (int c = 0; c < matriz.length; c++) {
System.out.print("| " + matriz[l][c]);
}
System.out.print("|");
System.out.println();
System.out.println("+-----+-----+-----+");
}
return matriz;
}
private static void mudaJogador(){
String simb = "X";
if (simb == "x") {
simb = "O";
} else {
simb = "X";
}
}
private static boolean Jogar(String s, int p, String[][] matriz){
boolean mudou = false;
for (int l = 0; l < matriz.length; l++) {
for (int c = 0; c < matriz.length; c++) {
if (matriz[l][c] == Integer.toString(p)){
matriz[l][c] = s;
mudou = true;
}
}
}
return mudou;
}
private static boolean terminouVelha(String[][] matriz){
boolean terminou = false;
int ocorr;
for (int l = 0; l < matriz.length; l++) {
if ((matriz[l][1]==matriz[l][2]) && (matriz[l][2]==matriz[l][3])) {
terminou = true;
}
}
for (int c = 0; c < matriz.length; c++) {
if ((matriz[1][c] == matriz[2][c]) && (matriz[2][c]==matriz[3][c])) {
terminou = true;
}
}
if ((matriz[1][1]== matriz[2][2]) && (matriz[2][2]==matriz[3][3])) {
terminou = true;
}
if ((matriz[1][3]==matriz[2][2]) && (matriz[2][2]==matriz[3][1])) {
terminou = true;
}
ocorr = 0;
for (int l = 0; l < matriz.length; l++) {
for (int c = 0; c < matriz.length; c++) {
if ((matriz[l][c]!= "X") && (matriz[l][c]!= "O")) {
ocorr++;
}
}
}
if (ocorr == 0) {
terminou = true;
}
return terminou;
}
}