Tic Tac Toe Winner
I am having problems with the win code. I tried for a very long time and I have no idea what the problem is. Ive tried debugging, but I got nothing out of it. (Sorry for the Swedish comments)
import java.util.Scanner;
public class Tictactoe {
static char[][] MakeMove (char[][] spelplan, char spelare, int rad, int kolumn){
spelplan[rad][kolumn]=spelare;
System.out.println(spelplan[rad][kolumn]);
return spelplan;
}
static boolean CheckMove (char[][] spelplan, int x, int y){
if (spelplan[x][y] != ' ')
return false;
else return true;
}
static void SkrivUtSpelplan(char[][] spelplan){
System.out.println("-------");
System.out.println("|"+spelplan[1][1] + "|" + spelplan[1][2] + "|" +spelplan[1][3] + "|");
System.out.println("|-+-+-|");
System.out.println("|"+spelplan[2][1] + "|" + spelplan[2][2] + "|" +spelplan[2][3] + "|");
System.out.println("|-+-+-|");
System.out.println("|"+spelplan[3][1] + "|" + spelplan[3][2] + "|" +spelplan[3][3] + "|");
System.out.println("-------");
}
Here is a piece of code that checks the winner
public static boolean KollaVinst(char[][] spelplan) {
return isHorizontalSolved(spelplan) || isVerticalSolved(spelplan) || isDiagonalSolved(spelplan);
}
Horizontally
//Kollar om horisontella Àr löst
public static boolean isHorizontalSolved(char[][] spelplan) {
for (int y = 0; y < spelplan.length; ++y) {
//För varje rad kolla om varje kolumn Àr fylld
boolean solved = true;
char first = spelplan[0][y];
for (int x = 0; x < spelplan[y].length; ++x) {
if (spelplan[x][y] == ' ' || first != spelplan[x][y]) {
// Om en kolumn inte Àr fylld sÄ Àr raden inte klar
// Om en kolumn i raden Àr fylld med olika tecken sÄ Àr den inte klar
solved = false;
}
if (solved == true) {
return true;
}
}
}
return false;
}
horizontal ends
vertical
//Kollar om vertikala Àr löst
public static boolean isVerticalSolved(char[][] spelplan) {
for (int x = 0; x < spelplan.length; ++x) {
boolean solved = true;
char first = spelplan[x][0];
for (int y = 0; y < spelplan[x].length; ++y){
if (spelplan[x][y] == ' ' || first != spelplan[x][y]){
solved = false;
}
}
if (solved == true){
return true;
}
}
return false;
}
vertically.
diagonal from left to right
// Kollar om digonalen Àr löst
public static boolean isDiagonalSolved(char[][] spelplan) {
// Kollar vÀnster till höger
char first = spelplan[0][0];
boolean solved = true;
for (int y = 0, x = 0; y < spelplan.length && x < spelplan[y].length; ++y, ++x) {
if (spelplan[x][y] == ' ' || first != spelplan[x][y]) {
//Om en plats Àr tom eller om det Àr olika tecken sÄ Àr den inte klar
solved = false;
}
}
if (solved) {
return true;
}
diagonally from left to right
diagonal from right to left
//Kollar höger till vÀnster
int topRightX = spelplan[0].length - 1;
solved = true;
first = spelplan[0][topRightX];
for (int y = 0, x = topRightX; y < spelplan.length && x >= 0; ++y, --x) {
if (spelplan[x][y] == ' ' || first != spelplan[x][y]) {
//Om en plats Àr tom eller om det Àr olika tecken sÄ Àr den inte klar
solved = false;
}
}
return solved;
}
This is where the winner code ends.
public static void main(String[] args) {
char spelplan[][] = new char [4][4];
char spelare;
int rad=3, kolumn=3, i=0;
for(int x=1; x<4; x++){
for (int y=1; y<4; y++){
spelplan[x][y]=' ';
}
}
System.out.println("-------");
System.out.println("| | | |");
System.out.println("|-+-+-|");
System.out.println("| | | |");
System.out.println("|-+-+-|");
System.out.println("| | | |");
System.out.println("-------");
for (i=0; i<=9; i++){
if (KollaVinst(spelplan) == false){
break;
}
else
CheckMove(spelplan, rad, kolumn);
for (i=0; i<9; i++){
if (i%2==0){
spelare='X';
}
else
spelare='O';
System.out.println("Spelare 1 skriv vilken rad: 1-3");
int x = new Scanner(System.in).nextInt();
System.out.println("Spelare 1 skriv vilken kolumn: 1-3");
int y = new Scanner(System.in).nextInt();
if (CheckMove(spelplan, x, y) == true){
MakeMove(spelplan, spelare, x, y);
}
System.out.println(" ");
SkrivUtSpelplan(spelplan);
}
}
}
}
source to share
Two things (generally speaking):
1) change your main class - you are checking the winner before the first move and after the last move ... So the game should look like this:
for (i = 0; i < 9; i++) {
if (KollaVinst(spelplan)) {
break;
} else {
CheckMove(spelplan, rad, kolumn);
}
if (i % 2 == 0) {
spelare = 'X';
} else {
spelare = 'O';
}
System.out.println("Spelare 1 skriv vilken rad: 1-3");
int x = new Scanner(System.in).nextInt();
System.out.println("Spelare 1 skriv vilken kolumn: 1-3");
int y = new Scanner(System.in).nextInt();
if (CheckMove(spelplan, x, y) == true) {
MakeMove(spelplan, spelare, x, y);
}
System.out.println(" ");
SkrivUtSpelplan(spelplan);
}
2) Checking the winner - I've updated your features (see below). You don't need to iterate over everything (especially files [0] [x]), because outside of the field you are checking, there are only two other fields;) So in horizontal and vertical validation, one for is enough (moreover, you can check both of these possibilities in one for). And to check the diagonal, since it is not necessary - these are only two opportunities to win the game this way.
public static boolean isHorizontalSolved(char[][] spelplan) {
boolean solved = false;
for (int y = 1; y < spelplan.length; y++) {
if ((spelplan[y][1] == spelplan[y][2]) && (spelplan[y][1] == spelplan[y][3]) && (spelplan[y][1] != ' ')) {
solved = true;
}
}
return solved;
}
public static boolean isVerticalSolved(char[][] spelplan) {
boolean solved = false;
for (int y = 1; y < spelplan.length; y++) {
if ((spelplan[1][y] == spelplan[2][y]) && (spelplan[1][y] == spelplan[3][y]) && (spelplan[1][y] != ' ')) {
solved = true;
}
}
return solved;
}
public static boolean isDiagonalSolved(char[][] spelplan) {
boolean solved = false;
if ((spelplan[1][1] == spelplan[2][2]) && (spelplan[1][1] == spelplan[3][3]) && (spelplan[1][1] != ' ')) {
solved = true;
}
if ((spelplan[1][3] == spelplan[2][2]) && (spelplan[1][3] == spelplan[3][1]) && (spelplan[1][3] != ' ')) {
solved = true;
}
return solved;
}
source to share
In your isHorizontalSolved, you have an error. A loop locking bracket must be placed in front of the following if:
//Kollar om horisontella Àr löst
public static boolean isHorizontalSolved(char[][] spelplan) {
for (int y = 0; y < spelplan.length; ++y) {
//För varje rad kolla om varje kolumn Àr fylld
boolean solved = true;
char first = spelplan[0][y];
for (int x = 0; x < spelplan[y].length; ++x) {
if (spelplan[x][y] == ' ' || first != spelplan[x][y]) {
// Om en kolumn inte Àr fylld sÄ Àr raden inte klar
// Om en kolumn i raden Àr fylld med olika tecken sÄ Àr den inte klar
solved = false;
}
}
if (solved == true) {
return true;
}
}
return false;
}
As you did, the loop will end with true on the first iteration.
source to share