Question: Gomoku...can someone take a look at my Gomoku code and see what I've done wrong? Instead of going through both player's turns it says invalid
Gomoku...can someone take a look at my Gomoku code and see what I've done wrong? Instead of going through both player's turns it says invalid move and gives player one a turn again. I've pasted in my code below. Thanks so much.
import java.util.Scanner;
public class Gomoku { public static void main (String[] args) { Scanner input = new Scanner(System.in);
char[][] map = new char [19][19];
int row = 0; int column = 0;
//fill game with dots for (int i = 0; i < map.length; i++) { for (int j = 0; j < map[i].length; j++) { map[i][j] = '.'; } }
printMap(map);
char player1Choice = 'X'; char player2Choice = 'O';
while (true) {
System.out.println("Player 1's turn!"); userTurn(map, player1Choice);
if (isValidMove(map, row, column) == false) { System.out.println("Invalid move! Try again!"); userTurn(map, player1Choice); } if (isBoardFull(map) == true) { System.out.println("Board is full. Tied game."); break; }
if (hasPlayerWon(map, player1Choice) == true) { System.out.println("Player 1 Wins!"); break; }
System.out.println("Player 2's turn!: "); userTurn(map, player2Choice);
System.out.println(isValidMove(map, row, column));
if (isValidMove(map, row, column) == false) { System.out.println("Invalid move! Try again!"); userTurn(map, player1Choice); } if (isBoardFull(map) == true) { System.out.println("Board is full. Tied game."); break; } if (hasPlayerWon(map, player2Choice) == true) { System.out.println("Player 2 Wins!"); break; }
} }
public static void printMap (char[][] map) { for (int i = 0; i < map.length; i++) { for (int j = 0; j < map[i].length; j++) { System.out.printf("%2c", map[i][j]); } System.out.println(); } }
public static void userTurn (char[][] map, char playerChoice) { Scanner input = new Scanner(System.in);
System.out.print("Enter row: "); int row = input.nextInt();
System.out.print("Enter column: "); int column = input.nextInt();
int place [][] = new int [row][column];
map [row][column] = playerChoice; printMap(map); }
public static boolean isValidMove (char[][] map, int row, int column) { //System.out.println ("n is valid move"); if (row < 0 || row > 18 || column < 0 || column > 18 || map[row][column]=='O' || map[row][column]=='X') { return false; } else { return true; } }
public static boolean isBoardFull (char[][] map) { int openSpots = 0; for (int i = 0; i < map.length; i++) { for (int j = 0; j < map.length; j++) { if (!(map[i][j]=='.')) openSpots++; } } if (openSpots == 361) { return true; } return false; }
public static boolean hasPlayerWon(char[][] map, int player) { if (isHorizontalWin(map, player) == true || isVerticalWin(map, player) == true || isDiagonalWin(map, player) == true) { return true; } return false; }
public static boolean isHorizontalWin(char[][] map, int player) { int count = 0;
int r; int c;
for (int i = 0; i < map.length; i++) { for (int j = 0; j < map.length; j++) { if (map[i][j]==(player)) { r = i; c = j; while (r >= 0 && r <= 18 && c >= 0 && c <= 18 && map[r][c] == player) { count++; r += 0; c += 1; } } } } if (count == 5) { return true; } return false; }
public static boolean isVerticalWin(char[][] map, int player) { int count = 0;
int r; int c;
for (int i = 0; i < map.length; i++) { for (int j = 0; j < map.length; j++) { if (map[i][j]==(player)) { r = i; c = j; while (r >= 0 && r <= 18 && c >= 0 && c <= 18 && map[r][c] == player) { count++; r += 1; c += 0; } } } } if (count == 5) { return true; } return false; }
public static boolean isDiagonalWin(char[][] map, int player) { int count = 0; int r; int c;
for (int i = 0; i < map.length; i++) { for (int j = 0; j < map.length; j++) { if (map[i][j]==(player)) { r = i; c = j; while (r >= 0 && r <= 18 && c >= 0 && c <= 18 && map[r][c] == player) { count++; r += 1; c += 1; } } } } if (count == 5) { return true; } return false; }
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
