Question: Stuck on Java Gomoku again. I am trying to write a java program to create a 19x19 board and then alternate between players until one

Stuck on Java Gomoku again.

I am trying to write a java program to create a 19x19 board and then alternate between players until one has five in a row. I got all the methods but then got all tangled up trying to figure out where to call them. Can someone please take a look at this and help me figure out what I've done wrong? 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];

//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, col) == false) { System.out.println("Invalid move! Try again!"); } if (isBoardFull(map) == true) { System.out.println("Board is full. Tied game."); break; }

else continue;

System.out.println("Player 2's turn!: "); userTurn(map, player2Choice);

if (isValidMove(map, row, col) == false) { System.out.println("Invalid move! Try again!"); } else continue; } }

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) { if (row < 0 || row > 18 || column < 0 || column > 18 || map[row][column].equals("O") || map[row][column].equals("X")) { return false; } 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].equals(".")) 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].equals(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].equals(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 isDiagoanlWin(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].equals(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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!